Sha256: 1febf10be1091cea1bc3ece33a75e6b2917449de3dade8aeac95013a4f5b207e

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

module SPCore
class DelayLine
  include Hashmake::HashMakeable
  
  attr_reader :sample_rate, :max_delay_seconds, :delay_seconds, :delay_samples
  
  ARG_SPECS = [
    Hashmake::ArgSpec.new(:reqd => true, :key => :sample_rate, :type => Float, :validator => ->(a){ a > 0.0 } ),
    Hashmake::ArgSpec.new(:reqd => true, :key => :max_delay_seconds, :type => Float, :validator => ->(a){ (a > 0.0) } ),
    Hashmake::ArgSpec.new(:reqd => false, :key => :delay_seconds, :type => Float, :default => 0.0, :validator => ->(a){ a >= 0.0 } ),
  ]
  
  def initialize args
    hash_make DelayLine::ARG_SPECS, args
    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
  
  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
    
    #if @buffer.fill_count < @delay_samples
    #  @buffer.push_ary Array.new(@delay_samples - @buffer.fill_count, 0.0)
    #end
  end
  
  def push_sample sample
    @buffer.push sample
  end
  
  def delayed_sample
    return @buffer.newest(@delay_samples)
  end
end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spcore-0.1.2 lib/spcore/lib/delay_line.rb
spcore-0.1.1 lib/spcore/lib/delay_line.rb
spcore-0.1.0 lib/spcore/lib/delay_line.rb