Sha256: acea71049c6947fe3a1759dd4ede431cdc7ac3319c42fe4d49f7e493ababe438
Contents?: true
Size: 1.93 KB
Versions: 4
Compression:
Stored size: 1.93 KB
Contents
module SPCore # Tracks the envelope of samples as they are passed in one by one. # # @author James Tunnell class EnvelopeDetector include Hashmake::HashMakeable # Used to process hashed arguments in #initialize. ARG_SPECS = { :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 } ), } attr_reader :envelope, :sample_rate, :attack_time, :release_time # 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. def initialize args hash_make EnvelopeDetector::ARG_SPECS, args @g_attack = Math.exp(-1.0 / (sample_rate * attack_time)) @g_release = Math.exp(-1.0 / (sample_rate * release_time)) @envelope = 0.0 end # Set the attack time (in seconds). 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 # Set the release time (in seconds). 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 # Process a sample, returning the updated envelope. 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 end end
Version data entries
4 entries across 4 versions & 1 rubygems