class SPCore::DelayLine

Delays samples for a period of time by pushing them through a circular buffer.

@author James Tunnell

Constants

ARG_SPECS

Used to process hashed arguments in initialize.

Attributes

delay_samples[R]
delay_seconds[R]
max_delay_seconds[R]
sample_rate[R]

Public Class Methods

new(args) click to toggle source

A new instance of DelayLine. The circular buffer is filled by pushing an array of zeros. @param [Hash] args Hashed arguments. Valid keys are :sample_rate (reqd),

:max_delay_seconds (reqd) and :delay_seconds (not reqd).
See ARG_SPECS for more details.
# 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

Public Instance Methods

delay_seconds=(delay_seconds) click to toggle source

Set the delay in seconds. Actual delay will vary according because an integer number of delay samples is used.

# File lib/spcore/core/delay_line.rb, line 32
def delay_seconds= delay_seconds
  delay_samples_floor = (@sample_rate * delay_seconds).floor
  @delay_samples = delay_samples_floor.to_i
  @delay_seconds = delay_samples_floor / @sample_rate
end
delayed_sample() click to toggle source

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

# File lib/spcore/core/delay_line.rb, line 45
def delayed_sample
  return @buffer.newest(@delay_samples)
end
push_sample(sample) click to toggle source

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

# File lib/spcore/core/delay_line.rb, line 39
def push_sample sample
  @buffer.push sample
end