class SPCore::EnvelopeDetector

Tracks the envelope of samples as they are passed in one by one.

@author James Tunnell

Constants

ARG_SPECS

Used to process hashed arguments in initialize.

Attributes

attack_time[R]
envelope[R]
release_time[R]
sample_rate[R]

Public Class Methods

new(args) click to toggle source

A new instance of EnvelopeDetector. The envelope is initialized to zero.

@param [Hash] args Hashed arguments. Valid keys are :sample_rate (reqd),

:attack_time (in seconds) (reqd) and :release_time
(in seconds) (reqd). See ARG_SPECS for more details.
# File lib/spcore/util/envelope_detector.rb, line 22
def initialize args
  hash_make args, EnvelopeDetector::ARG_SPECS

  @g_attack = Math.exp(-1.0 / (sample_rate * attack_time))
  @g_release = Math.exp(-1.0 / (sample_rate * release_time))
  
  @envelope = 0.0
end

Public Instance Methods

attack_time=(attack_time) click to toggle source

Set the attack time (in seconds).

# File lib/spcore/util/envelope_detector.rb, line 32
def attack_time= attack_time
  raise ArgumentError, "attack_time is <= 0.0" if attack_time <= 0.0
  @g_attack = Math.exp(-1.0 / (sample_rate * attack_time))
  @attack_time = attack_time
end
process_sample(sample) click to toggle source

Process a sample, returning the updated envelope.

# File lib/spcore/util/envelope_detector.rb, line 46
def process_sample sample
  input_abs = sample.abs
    
  if @envelope < input_abs
    @envelope = (@envelope * @g_attack) + ((1.0 - @g_attack) * input_abs)
  else
    @envelope = (@envelope * @g_release) + ((1.0 - @g_release) * input_abs)
  end
  
  return @envelope
end
release_time=(release_time) click to toggle source

Set the release time (in seconds).

# File lib/spcore/util/envelope_detector.rb, line 39
def release_time= release_time
  raise ArgumentError, "release_time is <= 0.0" if release_time <= 0.0
  @g_release = Math.exp(-1.0 / (sample_rate * release_time))
  @release_time = release_time
end