Class: AudioStream::Fx::Vocoder

Inherits:
Object
  • Object
show all
Includes:
MultiAudioInputtable
Defined in:
lib/audio_stream/fx/vocoder.rb

Instance Method Summary collapse

Methods included from MultiAudioInputtable

#audio_input_keys, #regist_audio_input

Constructor Details

#initialize(soundinfo, shift: 0, bandwidth: 0.2) ⇒ Vocoder

Returns a new instance of Vocoder.

Parameters:

  • soundinfo (AudioStream::SoundInfo)
  • shift (Float) (defaults to: 0)

    modulator pitch shift. 1.0=1semitone

  • bandwidth (Float) (defaults to: 0.2)

    bandwidth (octave)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/audio_stream/fx/vocoder.rb', line 9

def initialize(soundinfo, shift: 0, bandwidth: 0.2)
  regist_audio_input(:main)
  regist_audio_input(:side)

  band_num = 16
  nyquist = soundinfo.samplerate * 0.5

  @modulator_bpfs = band_num.times.map {|i|
    [i, 6.875 * (2 ** (i*0.5 + 3 - (shift/12.0)))]
  }.select {|i, freq|
    0<freq && freq<nyquist
  }.map {|i, freq|
    [i, BandPassFilter.create(soundinfo, freq: freq, bandwidth: bandwidth)]
  }.to_h

  @carrier_bpfs = band_num.times.map {|i|
    [i, 6.875 * (2 ** (i*0.5 + 3))]
  }.select {|i, freq|
    0<freq && freq<nyquist
  }.map {|i, freq|
    [i, BandPassFilter.create(soundinfo, freq: freq, bandwidth: bandwidth)]
  }.to_h

  @band_keys = @modulator_bpfs.keys & @carrier_bpfs.keys
end

Instance Method Details

#process(inputs) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/audio_stream/fx/vocoder.rb', line 35

def process(inputs)
  carrier = inputs[:main]
  modulator = inputs[:side]

  channels = [carrier.channels, modulator.channels].max
  if channels==2
    carrier = carrier.stereo
    modulator = modulator.stereo
  end

  dsts = channels.times.map {|i|
    @band_keys.map {|key|
      level = @modulator_bpfs[key].process(modulator).streams[i].max
      @carrier_bpfs[key].process(carrier).streams[i] * level
    }.inject(:+)
  }

  Buffer.new(*dsts)
end