Class: SynthBlocks::Synth::Polysynth

Inherits:
Core::Sound show all
Defined in:
lib/synth_blocks/synth/polysynth.rb

Overview

A simple polyphonic synthesizer

OSC > Filter > Amp

Instance Attribute Summary

Attributes inherited from Core::Sound

#mode

Instance Method Summary collapse

Methods inherited from Core::Sound

#active_events, #duration, #get, #set, #start, #stop

Constructor Details

#initialize(sfreq, preset = {}) ⇒ Polysynth

Parameters

  • amp_attack, _decay, _sustain, _release - Amp Envelope params

  • flt_attack, _decay, _sustain, _release - Filter Envelope params

  • flt_envmod - filter envelope modulation amount in Hz

  • flt_frequency, flt_Q - filter params

  • osc_waveform - waveform to generate (see Oscillator class)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/synth_blocks/synth/polysynth.rb', line 40

def initialize(sfreq, preset = {})
  @preset = {
    osc_waveform: :sawtooth,
    amp_env_attack: 0.2,
    amp_env_decay: 0.2,
    amp_env_sustain: 0.8,
    amp_env_release: 0.5,
    flt_env_attack: 0.5,
    flt_env_decay: 0.7,
    flt_env_sustain: 0.4,
    flt_env_release: 0.5,
    flt_frequency: 1000,
    flt_envmod: 2000,
    flt_Q: 3
  }.merge(preset)
  super(sfreq, mode: :polyphonic)
  @active_voices = {}
end

Instance Method Details

#live_paramsObject

:nodoc:



59
60
61
# File 'lib/synth_blocks/synth/polysynth.rb', line 59

def live_params # :nodoc:
  [:flt_frequency, :flt_envmod]
end

#release(t) ⇒ Object

:nodoc:



63
64
65
# File 'lib/synth_blocks/synth/polysynth.rb', line 63

def release(t) # :nodoc:
  get(:flt_env_release, t)
end

#run(offset) ⇒ Object

run sound generator



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/synth_blocks/synth/polysynth.rb', line 69

def run(offset)
  t = time(offset)
  events = active_events(t)
  voice_results = []
  events.each do |note, event|
    local_started = t - event[:started]
    next if local_started < 0
    local_stopped = event[:stopped] && event[:stopped] - event[:started]
    note_key = "#{note}:#{event[:started]}"
    if @active_voices[note_key].nil?
      @active_voices[note_key] = PolyVoice.new(@sampling_frequency, self, @preset)
    end
    if @active_voices[note_key]
      voice_results << @active_voices[note_key].run(local_started, local_stopped, frequency(note), event[:velocity])
    end
  end
  0.3 * voice_results.inject(0) {|sum, result| sum + result}
end