Class: AudioStream::AudioInputMetronome

Inherits:
Object
  • Object
show all
Includes:
AudioInput
Defined in:
lib/audio_stream/audio_input_metronome.rb

Instance Method Summary collapse

Constructor Details

#initialize(bpm:, beat: 4, repeat: nil, soundinfo:) ⇒ AudioInputMetronome

Returns a new instance of AudioInputMetronome.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/audio_stream/audio_input_metronome.rb', line 5

def initialize(bpm:, beat: 4, repeat: nil, soundinfo:)
  super()

  @bpm = bpm
  @beat = beat.to_i
  @repeat = repeat

  @synth = Synthesizer::PolySynth.new(
    oscillators: Synthesizer::Oscillator.new(
      source: Synthesizer::OscillatorSource::Sine.instance,
      phase: 0,
    ),
    amplifier: Synthesizer::Amplifier.new(
      volume: Synthesizer::ModulationValue.new(1.0)
        .add(Synthesizer::Modulation::Adsr.new(
          attack: 0.0,
          hold: 0.05,
          decay: 0.0,
          sustain: 0.0,
          release: 0.0
        ), depth: 1.0),
    ),
    soundinfo: soundinfo,
  )

  @soundinfo = soundinfo
end

Instance Method Details

#each(&block) ⇒ Object



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

def each(&block)
  Enumerator.new do |y|
    period = @soundinfo.samplerate.to_f / @soundinfo.window_size * 60.0 / @bpm
    repeat_count = 0.0
    beat_count = 0

    Range.new(0, @repeat).each {|_|
      if repeat_count<1
        if beat_count==0
          @synth.note_on(Synthesizer::Note.new(81))
        else
          @synth.note_on(Synthesizer::Note.new(69))
        end
        beat_count = (beat_count + 1) % @beat
      end
      y << @synth.next
      repeat_count = (repeat_count + 1) % period
    }
  end.each(&block)
end