Class: SPCore::Gain

Inherits:
Object
  • Object
show all
Defined in:
lib/spcore/util/gain.rb

Overview

Provide utility functions to convert between a linear and decibel (logarithm) unit.

Constant Summary collapse

MAX_DB_ABS =
6000.0

Class Method Summary collapse

Class Method Details

.db_to_linear(db) ⇒ Object

Convert a decibel value to a linear value.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/spcore/util/gain.rb', line 9

def self.db_to_linear db
  db_abs = db.abs
  raise ArgumentError, "decibel value #{db} is more than allowed maximum #{MAX_DB_ABS}" if db_abs > MAX_DB_ABS
  raise ArgumentError, "decibel value #{db} is less than allowed minimum #{-MAX_DB_ABS}" if db_abs < -MAX_DB_ABS
  return 10.0**(db / 20.0)
end

.linear_to_db(linear) ⇒ Object

Convert a linear value to a decibel value.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/spcore/util/gain.rb', line 17

def self.linear_to_db linear
  raise ArgumentError, "linear value #{linear} is less than 0.0" if linear < 0.0
  if linear == 0.0
    return -MAX_DB_ABS
  else
    return 20.0 * Math::log10(linear)
  end
end