class SynthBlocks::Fx::Compressor

simple compresor taken from www.musicdsp.org/en/latest/Effects/204-simple-compressor-class-c.html

Public Class Methods

new(srate, attack: 10.0, release: 100.0, ratio: 1.0, threshold: 0.0) click to toggle source

Create compressor instance

attack is the attack time in ms

release is the release time in ms

ratio is the compresor ratio

threshold is the knee threshold

# File lib/synth_blocks/fx/compressor.rb, line 68
def initialize(srate, attack: 10.0, release: 100.0, ratio: 1.0, threshold: 0.0)
  @sample_rate = srate
  @envelope = AttRelEnvelope.new(srate, attack: attack, release: release)
  @env_db = DC_OFFSET
  @ratio = ratio
  @threshold = threshold
end

Public Instance Methods

attack=(attack) click to toggle source

set attack

# File lib/synth_blocks/fx/compressor.rb, line 78
def attack=(attack)
  @envelope.attack = attack
end
release=(release) click to toggle source

set release

# File lib/synth_blocks/fx/compressor.rb, line 84
def release=(release)
  @envelope.release = release
end
run(input) click to toggle source

run compressor

# File lib/synth_blocks/fx/compressor.rb, line 91
def run(input)
  rect = input.abs
  rect += DC_OFFSET
  key_db = lin2db(rect)

  over_db = key_db - @threshold
  over_db = 0.0 if over_db < 0.0

  # attack/release
  over_db += DC_OFFSET
  @env_db = @envelope.run(over_db, @env_db)
  over_db = @env_db - DC_OFFSET

  gr = over_db * @ratio - 1.0
  gr = db2lin(gr)
  input * gr
end

Private Instance Methods

db2lin(db) click to toggle source
# File lib/synth_blocks/fx/compressor.rb, line 115
def db2lin(db)
  return Math.exp( db * DB_2_LOG )
end
lin2db(lin) click to toggle source
# File lib/synth_blocks/fx/compressor.rb, line 111
def lin2db(lin)
  return Math.log( lin ) * LOG_2_DB
end