class Zappa::Processor

Public Instance Methods

amplify(samples, db) click to toggle source
# File lib/zappa/processor.rb, line 11
def amplify(samples, db) # fix order
  mul_samples(samples, db_to_float(db))
end
compress(samples, ratio, threshold) click to toggle source
# File lib/zappa/processor.rb, line 19
def compress(samples, ratio, threshold)
  threshold_value = 32_768 * db_to_float(threshold) # calc this somehow
  samples.each do |f|
    f.map! do |s|
      if s.abs > threshold_value
        s += (threshold_value - s) / ratio if s > 0
        s -= (s + threshold_value) / ratio if s < 0
      end
      s.round
    end
  end
  samples
end
invert(samples) click to toggle source
# File lib/zappa/processor.rb, line 15
def invert(samples)
  mul_samples(samples, -1)
end
normalize(samples, headroom) click to toggle source
# File lib/zappa/processor.rb, line 3
def normalize(samples, headroom)
  fail 'headroom cannot be positive' if headroom > 0.0
  curr_peak = max_sample(samples)
  targ_peak = 32_768 * db_to_float(headroom)  # calculate this constants
  ratio = (targ_peak / curr_peak)
  mul_samples(samples, ratio)
end

Private Instance Methods

clip(value, max = 32_768) click to toggle source
# File lib/zappa/processor.rb, line 43
def clip(value, max = 32_768)
  return max if value > max
  return -max if value < (-max)
  value
end
db_to_float(db) click to toggle source

convert db values to floats

# File lib/zappa/processor.rb, line 58
def db_to_float(db)
  10**(db / 20)
end
max_sample(samples) click to toggle source
# File lib/zappa/processor.rb, line 49
def max_sample(samples)
  curr_max = 0
  samples.each do |f|
    f.each { |s| curr_max = s.abs if s.abs > curr_max }
  end
  curr_max
end
mul_frame(frame, factor) click to toggle source
# File lib/zappa/processor.rb, line 39
def mul_frame(frame, factor)
  frame.map { |s| clip((s * factor).round) }
end
mul_samples(samples, factor) click to toggle source
# File lib/zappa/processor.rb, line 35
def mul_samples(samples, factor)
  samples.map { |f| mul_frame(f, factor) }
end