Class: SynthBlocks::Fx::Eq

Inherits:
Object
  • Object
show all
Defined in:
lib/synth_blocks/fx/eq.rb

Overview

Simple 3 band Equalizer with variable shelving frequencies

Source: www.musicdsp.org/en/latest/Filters/236-3-band-equaliser.html

Constant Summary collapse

VSA =

very small amount (Denormal fix) :nodoc:

1.0 / 4294967295.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sample_rate = 44100, lowfreq: 880, highfreq: 5000) ⇒ Eq

Create new Equalizer instance

lowfreq and highfreq are the shelving frequencies in Hz



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synth_blocks/fx/eq.rb', line 16

def initialize(sample_rate=44100, lowfreq: 880, highfreq: 5000)

  # Poles Lowpass
  @f1p0 = 0.0
  @f1p1 = 0.0
  @f1p2 = 0.0
  @f1p3 = 0.0

  # Poles Highpass
  @f2p0 = 0.0
  @f2p1 = 0.0
  @f2p2 = 0.0
  @f2p3 = 0.0

  # Sample history buffer

  @sdm1 = 0.0 # Sample data minus 1
  @sdm2 = 0.0 #                   2
  @sdm3 = 0.0 #                   3

  # Gain Controls
  # Set Low/Mid/High gains to unity

  @low_gain  = 1.0       # low  gain
  @mid_gain  = 1.0       # mid  gain
  @high_gain = 1.0       # high gain

  # Calculate filter cutoff frequencies

  @lf = 2 * Math.sin(Math::PI * (lowfreq.to_f / sample_rate.to_f))
  @hf = 2 * Math.sin(Math::PI * (highfreq.to_f / sample_rate.to_f))
end

Instance Attribute Details

#high_gainObject

:nodoc: Low Gain, Mid Gain, High Gain



10
11
12
# File 'lib/synth_blocks/fx/eq.rb', line 10

def high_gain
  @high_gain
end

#low_gainObject

:nodoc: Low Gain, Mid Gain, High Gain



10
11
12
# File 'lib/synth_blocks/fx/eq.rb', line 10

def low_gain
  @low_gain
end

#mid_gainObject

:nodoc: Low Gain, Mid Gain, High Gain



10
11
12
# File 'lib/synth_blocks/fx/eq.rb', line 10

def mid_gain
  @mid_gain
end

Instance Method Details

#run(sample) ⇒ Object

run equalizer



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/synth_blocks/fx/eq.rb', line 51

def run(sample)
  # Filter #1 (lowpass)

  @f1p0  += (@lf * (sample - @f1p0)) + VSA;
  @f1p1  += (@lf * (@f1p0 - @f1p1));
  @f1p2  += (@lf * (@f1p1 - @f1p2));
  @f1p3  += (@lf * (@f1p2 - @f1p3));

  l = @f1p3;

  # Filter #2 (highpass)

  @f2p0  += (@hf * (sample   - @f2p0)) + VSA;
  @f2p1  += (@hf * (@f2p0 - @f2p1));
  @f2p2  += (@hf * (@f2p1 - @f2p2));
  @f2p3  += (@hf * (@f2p2 - @f2p3));

  h = @sdm3 - @f2p3;

  # Calculate midrange (signal - (low + high))

  m = @sdm3 - (h + l);

  # Scale, Combine and store

  l *= @low_gain
  m *= @mid_gain
  h *= @high_gain

  # Shuffle history buffer

  @sdm3   = @sdm2;
  @sdm2   = @sdm1;
  @sdm1   = sample;

  # Return result

  l + m + h
end