Class: SPCore::EnvelopeDetector
- Inherits:
-
Object
- Object
- SPCore::EnvelopeDetector
- Includes:
- Hashmake::HashMakeable
- Defined in:
- lib/spcore/util/envelope_detector.rb
Overview
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
-
#attack_time ⇒ Object
Returns the value of attribute attack_time.
-
#envelope ⇒ Object
readonly
Returns the value of attribute envelope.
-
#release_time ⇒ Object
Returns the value of attribute release_time.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
Instance Method Summary collapse
-
#initialize(args) ⇒ EnvelopeDetector
constructor
A new instance of EnvelopeDetector.
-
#process_sample(sample) ⇒ Object
Process a sample, returning the updated envelope.
Constructor Details
#initialize(args) ⇒ EnvelopeDetector
A new instance of EnvelopeDetector. The envelope is initialized to zero.
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_time ⇒ Object
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 |
#envelope ⇒ Object (readonly)
Returns the value of attribute envelope.
15 16 17 |
# File 'lib/spcore/util/envelope_detector.rb', line 15 def envelope @envelope end |
#release_time ⇒ Object
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_rate ⇒ Object (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 |