Class: SPCore::DelayLine

Inherits:
Object
  • Object
show all
Includes:
Hashmake::HashMakeable
Defined in:
lib/spcore/core/delay_line.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 } ),
  :max_delay_seconds => arg_spec(:reqd => true, :type => Float, :validator => ->(a){ (a > 0.0) } ),
  :delay_seconds => arg_spec(:reqd => false, :type => Float, :default => 0.0, :validator => ->(a){ a >= 0.0 } ),
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DelayLine

A new instance of DelayLine. The circular buffer is filled by pushing an array of zeros.

Parameters:

  • args (Hash)

    Hashed arguments. Valid keys are :sample_rate (reqd), :max_delay_seconds (reqd) and :delay_seconds (not reqd). See ARG_SPECS for more details.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/spcore/core/delay_line.rb', line 22

def initialize args
  hash_make args, DelayLine::ARG_SPECS
  raise ArgumentError, "delay_seconds #{delay_seconds} is greater than max_delay_seconds #{max_delay_seconds}" if @delay_seconds > @max_delay_seconds
  @buffer = CircularBuffer.new((@sample_rate * @max_delay_seconds) + 1, :override_when_full => true)
  @buffer.push_ary Array.new(@buffer.size, 0.0)
  self.delay_seconds=(@delay_seconds)
end

Instance Attribute Details

#delay_samplesObject (readonly)

Returns the value of attribute delay_samples.



8
9
10
# File 'lib/spcore/core/delay_line.rb', line 8

def delay_samples
  @delay_samples
end

#delay_secondsObject

Returns the value of attribute delay_seconds.



8
9
10
# File 'lib/spcore/core/delay_line.rb', line 8

def delay_seconds
  @delay_seconds
end

#max_delay_secondsObject (readonly)

Returns the value of attribute max_delay_seconds.



8
9
10
# File 'lib/spcore/core/delay_line.rb', line 8

def max_delay_seconds
  @max_delay_seconds
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



8
9
10
# File 'lib/spcore/core/delay_line.rb', line 8

def sample_rate
  @sample_rate
end

Instance Method Details

#delayed_sampleObject

Get the sample which is delayed by the number of samples that equates to the set delay in seconds.



45
46
47
# File 'lib/spcore/core/delay_line.rb', line 45

def delayed_sample
  return @buffer.newest(@delay_samples)
end

#push_sample(sample) ⇒ Object

Push a new sample through the circular buffer, overriding the oldest.



39
40
41
# File 'lib/spcore/core/delay_line.rb', line 39

def push_sample sample
  @buffer.push sample
end