Class: Synthesizer::MonoSynth

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oscillators:, filter: nil, amplifier:, glide: AudioStream::Rate.sec(0.1), quality: Quality::LOW, soundinfo:) ⇒ MonoSynth

Returns a new instance of MonoSynth.

Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/synthesizer/mono_synth.rb', line 21

def initialize(oscillators:, filter: nil, amplifier:, glide: AudioStream::Rate.sec(0.1), quality: Quality::LOW, soundinfo:)
  @oscillators = [oscillators].flatten.compact
  @filter = filter
  @amplifier = amplifier

  @quality = quality
  @soundinfo = soundinfo

  @processor = Processor.create(quality)
  @note_nums = []
  @perform = nil
  @glide = Modulation::Glide.new(time: glide)
  @pitch_bend = 0.0
end

Instance Attribute Details

#amplifierObject (readonly)

Returns the value of attribute amplifier.



6
7
8
# File 'lib/synthesizer/mono_synth.rb', line 6

def amplifier
  @amplifier
end

#filterObject (readonly)

Returns the value of attribute filter.



5
6
7
# File 'lib/synthesizer/mono_synth.rb', line 5

def filter
  @filter
end

#glideObject (readonly)

Returns the value of attribute glide.



12
13
14
# File 'lib/synthesizer/mono_synth.rb', line 12

def glide
  @glide
end

#oscillatorsObject (readonly)

Returns the value of attribute oscillators.



4
5
6
# File 'lib/synthesizer/mono_synth.rb', line 4

def oscillators
  @oscillators
end

#pitch_bendObject

Returns the value of attribute pitch_bend.



13
14
15
# File 'lib/synthesizer/mono_synth.rb', line 13

def pitch_bend
  @pitch_bend
end

#processorObject (readonly)

Returns the value of attribute processor.



7
8
9
# File 'lib/synthesizer/mono_synth.rb', line 7

def processor
  @processor
end

#qualityObject (readonly)

Returns the value of attribute quality.



9
10
11
# File 'lib/synthesizer/mono_synth.rb', line 9

def quality
  @quality
end

#soundinfoObject (readonly)

Returns the value of attribute soundinfo.



10
11
12
# File 'lib/synthesizer/mono_synth.rb', line 10

def soundinfo
  @soundinfo
end

Instance Method Details

#nextObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/synthesizer/mono_synth.rb', line 36

def next
  buf = nil
  if @perform
    buf = @perform.next

    # delete released note perform
    if @perform.released?
      @perform = nil
    end
  end
  buf || AudioStream::Buffer.create(@soundinfo.window_size, @soundinfo.channels)
end

#note_off(note) ⇒ Object

Parameters:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/synthesizer/mono_synth.rb', line 67

def note_off(note)
  # Note Off
  @note_nums.delete_if {|note_num| note_num==note.num}

  if @perform
    if @note_nums.length==0
      # Note Off
      @perform.note_off!
    else
      # Glide
      @glide.target = @note_nums.last
    end
  end
end

#note_on(note, velocity: 1.0) ⇒ Object

Parameters:

  • note (Synthesizer::Note)
  • velocity (Float) (defaults to: 1.0)

    volume percent (0.0~1.0)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/synthesizer/mono_synth.rb', line 51

def note_on(note, velocity: 1.0)
  # Note Off
  note_off(note)

  if @perform && @perform.note_on?
    # Glide
    @glide.target = note.num
  else
    # Note On
    @perform = NotePerform.new(self, note, velocity)
    @glide.base = note.num
  end
  @note_nums << note.num        
end