Class: Synthesizer::Modulation::Adsr

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

Instance Method Summary collapse

Constructor Details

#initialize(attack:, attack_curve: Curve::EaseOut, hold: 0.0, decay:, sustain_curve: Curve::EaseOut, sustain:, release:, release_curve: Curve::EaseOut) ⇒ Adsr

Returns a new instance of Adsr.

Parameters:

  • attack (AudioStream::Rate | Float)

    attack sec (0.0~)

  • attack_curve (Synthesizer::Curve) (defaults to: Curve::EaseOut)
  • hold (AudioStream::Rate | Float) (defaults to: 0.0)

    hold sec (0.0~)

  • decay (AudioStream::Rate | Float)

    decay sec (0.0~)

  • sustain_curve (Synthesizer::Curve) (defaults to: Curve::EaseOut)
  • sustain (Float)

    sustain level (0.0~1.0)

  • release (AudioStream::Rate | Float)

    release sec (0.0~)

  • release_curve (Synthesizer::Curve) (defaults to: Curve::EaseOut)


13
14
15
16
17
18
19
20
21
22
# File 'lib/synthesizer/modulation/adsr.rb', line 13

def initialize(attack:, attack_curve: Curve::EaseOut, hold: 0.0, decay:, sustain_curve: Curve::EaseOut, sustain:, release:, release_curve: Curve::EaseOut)
  @attack = AudioStream::Rate.sec(attack)
  @attack_curve = attack_curve
  @hold = AudioStream::Rate.sec(hold)
  @decay = AudioStream::Rate.sec(decay)
  @sustain_curve = sustain_curve
  @sustain = sustain
  @release = AudioStream::Rate.sec(release)
  @release_curve = release_curve
end

Instance Method Details

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



92
93
94
95
96
97
98
99
# File 'lib/synthesizer/modulation/adsr.rb', line 92

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

  -> {
    gen[] * depth + bottom
  }
end

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



101
102
103
104
105
106
107
# File 'lib/synthesizer/modulation/adsr.rb', line 101

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

  -> {
    gen[] * depth
  }
end

#generator(note_perform, samplecount, release_sustain:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/synthesizer/modulation/adsr.rb', line 75

def generator(note_perform, samplecount, release_sustain:)
  soundinfo = note_perform.synth.soundinfo

  note_on = note_on_envelope(soundinfo, samplecount, sustain: true)
  note_off = note_off_envelope(soundinfo, samplecount, sustain: release_sustain)
  last = 0.0

  -> {
    if note_perform.note_on?
      last = note_on.next
    else
      note_off.next * last
    end
  }
end

#note_off_envelope(soundinfo, samplecount, sustain: false, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/synthesizer/modulation/adsr.rb', line 56

def note_off_envelope(soundinfo, samplecount, sustain: false, &block)
  Enumerator.new do |yld|
    # release
    release_len = (@release.sample(soundinfo) / samplecount).to_i
    release_len.times {|i|
      x = i.to_f / release_len
      y = 1.0 - @release_curve[x]
      yld << y
    }

    # sustain
    if sustain
      loop {
        yld << 0.0
      }
    end
  end.each(&block)
end

#note_on_envelope(soundinfo, samplecount, sustain: false, &block) ⇒ Object



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

def note_on_envelope(soundinfo, samplecount, sustain: false, &block)
  Enumerator.new do |yld|
    # attack
    attack_len = (@attack.sample(soundinfo) / samplecount).to_i
    attack_len.times {|i|
      x = i.to_f / attack_len
      y = @attack_curve[x]
      yld << y
    }

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

    # decay
    decay_len = (@decay.sample(soundinfo) / samplecount).to_i
    decay_len.times {|i|
      x = i.to_f / decay_len
      y = 1.0 - @sustain_curve[x] * (1.0 - @sustain)
      yld << y
    }

    # sustain
    if sustain
      loop {
        yld << @sustain
      }
    end
  end.each(&block)
end

#plot(soundinfo) ⇒ Object



132
133
134
135
# File 'lib/synthesizer/modulation/adsr.rb', line 132

def plot(soundinfo)
  data = plot_data(soundinfo)
  Plotly::Plot.new(data: [data])
end

#plot_data(soundinfo) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/synthesizer/modulation/adsr.rb', line 109

def plot_data(soundinfo)
  samplecount = soundinfo.window_size.to_f
  note_on = note_on_envelope(soundinfo, samplecount, sustain: false)
  note_off = note_off_envelope(soundinfo, samplecount, sustain: false)
  last = 0.0

  xs = []
  ys = []

  note_on.each {|y|
    xs << xs.length
    ys << y
  }

  last = ys.last || 0.0
  note_off.each {|y|
    xs << xs.length
    ys << y * last
  }

  {x: xs, y: ys}
end