Class: Synthesizer::Modulation::Lfo

Inherits:
Object
  • Object
show all
Defined in:
lib/synthesizer/modulation/lfo.rb

Instance Method Summary collapse

Constructor Details

#initialize(shape: Shape::Sine, delay: 0.0, attack: 0.0, attack_curve: Curve::Straight, phase: 0.0, rate: 0.3) ⇒ Lfo

Returns a new instance of Lfo.

Parameters:

  • shape (Synthesizer::Shape) (defaults to: Shape::Sine)
  • delay (AudioStream::Rate | Float) (defaults to: 0.0)

    delay sec (0.0~)

  • attack (AudioStream::Rate | Float) (defaults to: 0.0)

    attack sec (0.0~)

  • attack_curve (Synthesizer::Curve) (defaults to: Curve::Straight)
  • phase (Float) (defaults to: 0.0)

    phase percent (0.0~1.0)

  • rate (AudioStream::Rate | Float) (defaults to: 0.3)

    wave freq (0.0~)



11
12
13
14
15
16
17
18
# File 'lib/synthesizer/modulation/lfo.rb', line 11

def initialize(shape: Shape::Sine, delay: 0.0, attack: 0.0, attack_curve: Curve::Straight, phase: 0.0, rate: 0.3)
  @shape = shape
  @delay = AudioStream::Rate.sec(delay)
  @attack = AudioStream::Rate.sec(attack)
  @attack_curve = attack_curve
  @phase = phase.to_f
  @rate = AudioStream::Rate.sec(rate)
end

Instance Method Details

#amp_generator(note_perform, samplecount, depth, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/synthesizer/modulation/lfo.rb', line 48

def amp_generator(note_perform, samplecount, depth, &block)
  bottom = 1.0 - depth
  gen = generator(note_perform, samplecount)

  -> {
    val = (gen.next + 1) / 2
    val * depth + bottom
  }
end

#balance_generator(note_perform, samplecount, depth, &block) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/synthesizer/modulation/lfo.rb', line 58

def balance_generator(note_perform, samplecount, depth, &block)
  gen = generator(note_perform, samplecount)

  -> {
    gen.next * depth
  }
end

#generator(note_perform, samplecount, &block) ⇒ Object



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
# File 'lib/synthesizer/modulation/lfo.rb', line 20

def generator(note_perform, samplecount, &block)
  soundinfo = note_perform.synth.soundinfo
  hz = @rate.freq(soundinfo)

  Enumerator.new do |yld|
    pos = ShapePos.new(soundinfo.samplerate / samplecount, @phase)

    # delay
    (@delay.sample(soundinfo) / samplecount).to_i.times {|i|
      yld << 0.0
    }

    # attack
    attack_len = (@attack.sample(soundinfo) / samplecount).to_i
    attack_len.times {|i|
      x = i.to_f / attack_len
      y = @attack_curve[x]
      yld << @shape[pos.next(hz, 0.0, 0.0)] * y
    }

    # sustain
    loop {
      val = @shape[pos.next(hz, 0.0, 0.0)]
      yld << val
    }
  end.each(&block)
end