Class: Deftones::Instrument::MonoSynth

Inherits:
Core::Instrument show all
Defined in:
lib/deftones/instrument/mono_synth.rb

Instance Attribute Summary collapse

Attributes inherited from Core::Instrument

#mute, #output, #volume

Attributes inherited from Core::AudioNode

#context, #input

Instance Method Summary collapse

Methods inherited from Core::Instrument

#apply_volume!, #dispose, #get, #input, #mute?, #releaseAll, #release_all, #render, #render_block, #set, #triggerAttack, #triggerAttackRelease, #triggerRelease

Methods inherited from Core::AudioNode

#>>, #attach_destination, #attach_source, #block_time, #chain, #channel_count, #channel_count_mode, #channel_interpretation, #connect, #connected?, #default_input_channels, #default_output_channels, #destination_for_connection, #detach_all_destinations, #detach_destination, #detach_source, #disconnect, #dispose, #disposed?, #fan, #get, #immediate, #input_for_index, #inputs, #mix_source_blocks, #multichannel_process?, #name, #normalize_connection_index, #normalize_output_block, #now, #number_of_inputs, #number_of_outputs, #output, #output_for_connection, #output_for_index, #outputs, #process, #raise_connection_index_error!, #reaches_node?, #render, #render_block, #sample_time, #set, #to_destination, #to_frequency, #to_master, #to_midi, #to_output, #to_s, #to_seconds, #to_ticks, #uses_legacy_render_for_block?, #validate_connectable!, #validate_connection_index!

Constructor Details

#initialize(type: :sawtooth, filter_type: :lowpass, filter_frequency: 1_200.0, filter_q: 0.8, filter_octaves: 2.0, attack: 0.01, decay: 0.1, sustain: 0.4, release: 0.5, context: Deftones.context, &block) ⇒ MonoSynth

Returns a new instance of MonoSynth.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deftones/instrument/mono_synth.rb', line 8

def initialize(type: :sawtooth, filter_type: :lowpass, filter_frequency: 1_200.0, filter_q: 0.8,
               filter_octaves: 2.0, attack: 0.01, decay: 0.1, sustain: 0.4, release: 0.5,
               context: Deftones.context, &block)
  super(context: context)
  @oscillator = Source::OmniOscillator.new(type: type, context: context)
  @filter = Component::Filter.new(
    type: filter_type,
    frequency: filter_frequency,
    q: filter_q,
    context: context
  )
  @envelope = Component::AmplitudeEnvelope.new(
    attack: attack,
    decay: decay,
    sustain: sustain,
    release: release,
    context: context
  )
  @filter_envelope = Component::FrequencyEnvelope.new(
    base_frequency: filter_frequency,
    octaves: filter_octaves,
    attack: attack,
    decay: decay,
    sustain: sustain,
    release: release,
    context: context
  )
  @oscillator.start(0.0)
  @oscillator >> @filter >> @envelope >> @output
  block&.call(self)
end

Instance Attribute Details

#envelopeObject (readonly)

Returns the value of attribute envelope.



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

def envelope
  @envelope
end

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#filter_envelopeObject (readonly)

Returns the value of attribute filter_envelope.



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

def filter_envelope
  @filter_envelope
end

#oscillatorObject (readonly)

Returns the value of attribute oscillator.



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

def oscillator
  @oscillator
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/deftones/instrument/mono_synth.rb', line 67

def active?
  @envelope.active?
end

#play(note, duration: "8n", at: nil, velocity: 1.0) ⇒ Object



40
41
42
# File 'lib/deftones/instrument/mono_synth.rb', line 40

def play(note, duration: "8n", at: nil, velocity: 1.0)
  trigger_attack_release(note, duration, at, velocity)
end

#resolve_time(time) ⇒ Object (private)



81
82
83
84
85
# File 'lib/deftones/instrument/mono_synth.rb', line 81

def resolve_time(time)
  return context.current_time if time.nil?

  Deftones::Music::Time.parse(time)
end

#shape_filter(time, velocity) ⇒ Object (private)



73
74
75
76
77
78
79
# File 'lib/deftones/instrument/mono_synth.rb', line 73

def shape_filter(time, velocity)
  peak = @filter_envelope.base_frequency * (2.0**(@filter_envelope.octaves * velocity))
  sustain_level = @filter_envelope.base_frequency * (1.0 + (@filter_envelope.sustain * velocity))
  @filter.frequency.set_value_at_time(@filter_envelope.base_frequency, time)
  @filter.frequency.linear_ramp_to(peak, @filter_envelope.attack)
  @filter.frequency.linear_ramp_to(sustain_level, @filter_envelope.decay)
end

#trigger_attack(note, time = nil, velocity = 1.0) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/deftones/instrument/mono_synth.rb', line 44

def trigger_attack(note, time = nil, velocity = 1.0)
  scheduled_time = resolve_time(time)
  frequency = Deftones::Music::Note.to_frequency(note)
  @oscillator.frequency.set_value_at_time(frequency, scheduled_time)
  shape_filter(scheduled_time, velocity)
  @envelope.trigger_attack(scheduled_time, velocity)
  self
end

#trigger_attack_release(note, duration, time = nil, velocity = 1.0) ⇒ Object



60
61
62
63
64
65
# File 'lib/deftones/instrument/mono_synth.rb', line 60

def trigger_attack_release(note, duration, time = nil, velocity = 1.0)
  scheduled_time = resolve_time(time)
  trigger_attack(note, scheduled_time, velocity)
  trigger_release(scheduled_time + Deftones::Music::Time.parse(duration))
  self
end

#trigger_release(time = nil) ⇒ Object



53
54
55
56
57
58
# File 'lib/deftones/instrument/mono_synth.rb', line 53

def trigger_release(time = nil)
  scheduled_time = resolve_time(time)
  @filter.frequency.linear_ramp_to(@filter_envelope.base_frequency, @envelope.release)
  @envelope.trigger_release(scheduled_time)
  self
end