Class: AudioStream::Fx::Delay

Inherits:
Object
  • Object
show all
Defined in:
lib/audio_stream/fx/delay.rb

Instance Method Summary collapse

Constructor Details

#initialize(soundinfo, time:, level:, feedback:) ⇒ Delay

Returns a new instance of Delay.

Parameters:



8
9
10
11
12
13
14
15
16
17
# File 'lib/audio_stream/fx/delay.rb', line 8

def initialize(soundinfo, time:, level:, feedback:)
  time = Rate.sec(time)
  @level = Decibel.db(level).mag
  @feedback = Decibel.db(feedback).mag

  @delaysample = time.sample(soundinfo).round
  @delaybuf0 = Array.new(@delaysample, 0.0)
  @delaybuf1 = Array.new(@delaysample, 0.0)
  @seek = 0
end

Instance Method Details

#process(input) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/audio_stream/fx/delay.rb', line 19

def process(input)
  window_size = input.window_size
  channels = input.channels

  src0 = input.streams[0]
  src1 = input.streams[1]

  case channels
  when 1
    output = Buffer.create_mono(window_size)
    dst0 = output.streams[0]

    src0.each_with_index {|f, i|
      tmp0 = f + @level * @delaybuf0[@seek]
      @delaybuf0[@seek] = f + @feedback * @delaybuf0[@seek]
      dst0[i] = tmp0
      @seek = (@seek + 1) % @delaysample
    }
    output
  when 2
    output = Buffer.create_stereo(window_size)
    dst0 = output.streams[0]
    dst1 = output.streams[1]

    window_size.times {|i|
      tmp0 = src0[i] + @level * @delaybuf0[@seek]
      tmp1 = src1[i] + @level * @delaybuf1[@seek]

      @delaybuf0[@seek] = src0[i] + @feedback * @delaybuf0[@seek]
      @delaybuf1[@seek] = src1[i] + @feedback * @delaybuf1[@seek]

      dst0[i] = tmp0
      dst1[i] = tmp1

      @seek = (@seek + 1) % @delaysample
    }
    output
  end
end