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 =

MAX_DB = 72 NUM_DB_HALF_STEPS=146 DB_TO_SCALAR_TABLE = [

1.0,              # 0 dB
1.059253725,      # 0.5
1.122018454,      # 1
1.188502227,      # 1.5
1.258925412,      # 2
1.333521432,      # 2.5
1.412537545,      # 3
1.496235656,      # 3.5
1.584893192,      # 4
1.678804018,      # 4.5
1.77827941,       # 5
1.883649089,      # 5.5
1.995262315,      # 6
2.11348904,       # 6.5
2.238721139,      # 7
2.371373706,      # 7.5
2.511886432,      # 8
2.66072506,       # 8.5
2.818382931,      # 9
2.985382619,      # 9.5
3.16227766,       # 10 dB
3.349654392,      # 10.5
3.548133892,      # 11
3.758374043,      # 11.5
3.981071706,      # 12
4.216965034,      # 12.5
4.466835922,      # 13
4.73151259,       # /13.5
5.011872336,      # 14
5.308844442,      # 14.5
5.623413252,      # 15
5.956621435,      # 15.5
6.309573445,      # 16
6.683439176,      # 16.5
7.079457844,      # 17
7.498942093,      # 17.5
7.943282347,      # 18
8.413951416,      # 18.5
8.912509381,      # 19
9.440608763,      # 19.5
10.0,             # 20 dB
10.59253725,      # 20.5
11.22018454,      # 21
11.88502227,      # 21.5
12.58925412,      # 22
13.33521432,      # 22.5
14.12537545,      # 23
14.96235656,      # 23.5
15.84893192,      # 24
16.78804018,      # 24.5
17.7827941,       # 25
18.83649089,      # 25.5
19.95262315,      # 26
21.1348904,       # 26.5
22.38721139,      # 27
23.71373706,      # 27.5
25.11886432,      # 28
26.6072506,       # 28.5
28.18382931,      # 29
29.85382619,      # 29.5
31.6227766,       # 30 dB
33.49654392,      # 30.5
35.48133892,      # 31
37.58374043,      # 31.5
39.81071706,      # 32
42.16965034,      # 32.5
44.66835922,      # 33
47.3151259,       # 33.5
50.11872336,      # 34
53.08844442,      # 34.5
56.23413252,      # 35
59.56621435,      # 35.5
63.09573445,      # 36
66.83439176,      # 36.5
70.79457844,      # 37
74.98942093,      # 37.5
79.43282347,      # 38
84.13951416,      # 38.5
89.12509381,      # 39
94.40608763,      # 39.5
100.0,            # 40 dB
105.9253725,      # 40.5
112.2018454,      # 41
118.8502227,      # 41.5
125.8925412,      # 42
133.3521432,      # 42.5
141.2537545,      # 43
149.6235656,      # 43.5
158.4893192,      # 44
167.8804018,      # 44.5
177.827941,       # 45
188.3649089,      # 45.5
199.5262315,      # 46
211.348904,       # 46.5
223.8721139,      # 47
237.1373706,      # 47.5
251.1886432,      # 48
266.072506,       # 48.5
281.8382931,      # 49
298.5382619,      # 49.5
316.227766,       # 50 dB
334.9654392,      # 50.5
354.8133892,      # 51
375.8374043,      # 51.5
398.1071706,      # 52
421.6965034,      # 52.5
446.6835922,      # 53
473.151259,       # 53.5
501.1872336,      # 54
530.8844442,      # 54.5
562.3413252,      # 55
595.6621435,      # 55.5
630.9573445,      # 56
668.3439176,      # 56.5
707.9457844,      # 57
749.8942093,      # 57.5
794.3282347,      # 58
841.3951416,      # 58.5
891.2509381,      # 59
944.0608763,      # 59.5
1000.0,           # 60 dB
1059.253725,      # 60.5
1122.018454,      # 61
1188.502227,      # 61.5
1258.925412,      # 62
1333.521432,      # 62.5
1412.537545,      # 63
1496.235656,      # 63.5
1584.893192,      # 64
1678.804018,      # 64.5
1778.27941,       # 65
1883.649089,      # 65.5
1995.262315,      # 66
2113.48904,       # 66.5
2238.721139,      # 67
2371.373706,      # 67.5
2511.886432,      # 68
2660.72506,       # 68.5
2818.382931,      # 69
2985.382619,      # 69.5
3162.27766,       # 70 dB
3349.654392,      # 70.5
3548.133892,      # 71
3758.374043,      # 71.5
3981.071706,      # 72

]

def self.db_to_linear db

raise ArgumentError, "#{db} db is not between -#{MAX_DB} and #{MAX_DB}" unless db.between(-MAX_DB, MAX_DB)
db_half_step = (dB * 2.0).to_i

if(db_half_step >= 0)
  linear = DB_TO_SCALAR_TABLE[db_half_step]
elsif
  linear = DB_TO_SCALAR_TABLE[-db_half_step]
  scalar = 1.0 / scalar
end

return scalar

end

6000.0

Class Method Summary collapse

Class Method Details

.db_to_linear(db) ⇒ Object

Convert a decibel value to a linear value.

Raises:

  • (ArgumentError)


172
173
174
175
176
# File 'lib/spcore/util/gain.rb', line 172

def self.db_to_linear db
  db_abs = db.abs
  raise ArgumentError, "|db| is #{db_abs}, which is greater than the allowed #{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)


179
180
181
182
# File 'lib/spcore/util/gain.rb', line 179

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