Class: SPCore::EnvelopeDetector

Inherits:
Object
  • Object
show all
Includes:
Hashmake::HashMakeable
Defined in:
lib/spcore/util/envelope_detector.rb

Overview

Author:

  • James Tunnell

Constant Summary collapse

ARG_SPECS =

Used to process hashed arguments in #initialize.

{
  :sample_rate => arg_spec(:reqd => true, :type => Fixnum, :validator => ->(a){ a > 0.0 } ),
  :attack_time => arg_spec(:reqd => true, :type => Float, :validator => ->(a){ a > 0.0 } ),
  :release_time => arg_spec(:reqd => true, :type => Float, :validator => ->(a){ a > 0.0 } ),
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ EnvelopeDetector

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

Parameters:

  • args (Hash)

    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.



22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#attack_timeObject

Returns the value of attribute attack_time.



15
16
17
# File 'lib/spcore/util/envelope_detector.rb', line 15

def attack_time
  @attack_time
end

#envelopeObject (readonly)

Returns the value of attribute envelope.



15
16
17
# File 'lib/spcore/util/envelope_detector.rb', line 15

def envelope
  @envelope
end

#release_timeObject

Returns the value of attribute release_time.



15
16
17
# File 'lib/spcore/util/envelope_detector.rb', line 15

def release_time
  @release_time
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



15
16
17
# File 'lib/spcore/util/envelope_detector.rb', line 15

def sample_rate
  @sample_rate
end

Instance Method Details

#process_sample(sample) ⇒ Object

Process a sample, returning the updated envelope.



46
47
48
49
50
51
52
53
54
55
56
# 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