Class: Tone

Inherits:
Object show all
Defined in:
lib/tone.rb

Overview

a sound to be played.

  • a Chord is made of these.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Tone

args

a Hash containing attributes



16
17
18
19
20
21
22
# File 'lib/tone.rb', line 16

def initialize(args = nil)
  @frames = 0
  @wave = Wave.new
  @amp = Fader.new(0.5,0,MathUtils::GR)
  @freq = Fader.new(220,0,MathUtils::GR)
  init_hash(args)
end

Instance Attribute Details

#ampObject

Fader.final is relative to Fader.start.



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

def amp
  @amp
end

#framesObject

duration of the tone in seconds.



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

def frames
  @frames
end

#freqObject

Fader.final is relative to Fader.start.



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

def freq
  @freq
end

#waveObject

Wave for one single wave in the tone. (Repeated for #frames)



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

def wave
  @wave
end

Instance Method Details

#chord(note, name, element) ⇒ Object

element

Element to save the used tones and notes to.



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

def chord(note, name, element)
  out=Buffer.new
  chrs=Composer.chord_notes note.note, name
  notes_total = chrs.count
  chrs.each_with_index do |v,i|
    ltone = deep_copy
    lamp = 1.0/notes_total # lower vol of each tone
    ltone.amp.start *= lamp # lower vol of each tone
    ltone.amp.final *= lamp # lower range of vol
      # use a reduced amplitude. (by tones in chord).
      # mulitplied by givin amplitude
    realnote = note+v
    ltone.note= realnote # set to right freq
    
    out.push ltone.out(i,notes_total)
    element.add_t ltone
    element.notes.push realnote
  end
  out
end

#detailObject



31
32
33
# File 'lib/tone.rb', line 31

def detail
  wave.detail
end

#fadeObject

make it end with an amlitute of 0 (complete fade out).



132
133
134
135
# File 'lib/tone.rb', line 132

def fade
  amp.final = -amp.start
  self
end

#noteObject

return the note closest to the set frequency



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tone.rb', line 118

def note
  out = Note.new
  fre = freq.start
  linear_frequency = Math.log(fre/220.0,2.0) + 4
  # puts "linear_frequency #{linear_frequency}"
  out.octave= ( linear_frequency ).floor
  cents = 1200.0 * (linear_frequency - out.octave)
  not_wrap = (cents / 99.0)
  # puts "note no wrap #{not_wrap}"
  out.note = not_wrap.floor % 12
  out
end

#note=(note) ⇒ Object

set #freq based off a Note



110
111
112
# File 'lib/tone.rb', line 110

def note=(note)
  freq.start = note.freq
end

#note_end=(note) ⇒ Object



113
114
115
# File 'lib/tone.rb', line 113

def note_end=(note)
  # freq.final = note.freq
end

#outObject

output the WaveData for a full tone (chirp). All sound created flows into this atm.

freq_exp

default 0. 0 means linear. higher means it reachs the frequency at the end of it’s range later.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tone.rb', line 63

def out
  # puts "out amp: #{amp.start}"
  buffer_len = frames
  inc_x = 0.0
  data = WaveData.new
  lfreq = freq.start
  log "tone starting with freq #{lfreq}", 4
  lamp = amp.start
  wave_exp = wave.detail.exp
  freq_exp = freq.exp
  amp_exp = amp.exp
  wave_into=0
  while data.dps.count < buffer_len do
    wave_data = wave.out(lfreq, lamp, wave_into)
    data + wave_data
    inc_x += wave_data.count
    x = (inc_x.to_f / buffer_len)

    #freq
    freq_multiplier = x # when exp is off
    #fade exponentially if on.
    freq_multiplier = x ** (1.0 /((1.0/freq_exp)*x)) if freq_exp
    lfreq = freq.start + freq_multiplier*freq.final

    #amp
    amp_multiplier = x # when exp is off
    #fade exponentially if on.
    amp_multiplier = x ** (1.0 /((1.0/amp_exp)*x)) if amp_exp
    lamp = amp.start + amp_multiplier*amp.final

    #wave
    wave_into = x # when exp is off
    #fade exponentially if on.
    wave_into = x ** (1.0 /((1.0/wave_exp)*x)) if wave_exp

  end
  data.fit_to buffer_len
  data
end

#saturation=(val) ⇒ Object

set saturation for both start and end



25
26
27
# File 'lib/tone.rb', line 25

def saturation= val
  wave.saturation= val
end

#saturationsObject



28
29
30
# File 'lib/tone.rb', line 28

def saturations
  wave.saturations
end

#set_freq_final_no_relative(final) ⇒ Object

setting it absolute



104
105
106
107
# File 'lib/tone.rb', line 104

def set_freq_final_no_relative(final)
  dif=final - freq.start
  freq.final=dif
end