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.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tone.rb', line 122

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



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

def detail
  wave.detail
end

#fadeObject

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



208
209
210
211
# File 'lib/tone.rb', line 208

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

#freq_final(is_relative = true) ⇒ Object



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

def freq_final(is_relative=true)
  return is_relative ? freq.final : freq.start + freq.final
end

#noteObject

return the note closest to the set frequency



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/tone.rb', line 194

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



186
187
188
# File 'lib/tone.rb', line 186

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

#note_end=(note) ⇒ Object



189
190
191
# File 'lib/tone.rb', line 189

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tone.rb', line 145

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

#rand_amp_both(max = 0.8, min = 0.0025) ⇒ Object

random amp.



36
37
38
39
40
41
42
43
44
# File 'lib/tone.rb', line 36

def rand_amp_both(max = 0.8, min = 0.0025)
  upper = max - min
  
  val = rand * upper
  self.amp.start = min + val
  val = rand * val # can't be more
  self.set_amp_final (min + val), false
  self.amp.rand_exp
end

#rand_detail_both(min = 3, max = 512, weight = 2) ⇒ Object

random ammount of detail in a wave. weighted toward lower values weight times.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tone.rb', line 47

def rand_detail_both(min = 3, max = 512, weight=2)
  upper = max - min
  
  val = rand * upper
  weight.times {val *= rand}
  self.wave.detail.start= min + val
  val = rand * upper
  weight.times {val *= rand}
  self.wave.detail.final= min + val
#  puts "start detail #{val}"
#  puts "final detail #{val}"
  self.wave.detail.rand_exp
end

#rand_freq(min = 12, max = 20_000) ⇒ Object

random start frequency in range.



77
78
79
80
81
# File 'lib/tone.rb', line 77

def rand_freq(min = 12, max = 20_000) 
  upper = max - min
  val = min + rand*upper
  self.freq.start = val
end

#rand_freq_both(min = 12, max = 20_000) ⇒ Object

random frequencies in range.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tone.rb', line 84

def rand_freq_both(min = 12, max = 20_000)  
  upper = max - min

  rand_freq min, max
  val = min + rand*upper # final val
  self.set_freq_final val, false
  self.freq.rand_exp
#  puts "freq.start #{freq.start}"
#  puts "freq.f #{freq.final}"
#  puts "freq.x #{freq.exp}"
end

#rand_sat_both(weight = 2, max = 1.0) ⇒ Object

random ammount of saturation, weighted toward lower values weight times.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tone.rb', line 62

def rand_sat_both weight=2, max=1.0
  sat = max
  weight.times {sat *= rand}
  self.saturations.start= sat
  
  sat = max
  weight.times {sat *= rand}
  self.saturations.final= sat
  self.saturations.rand_exp
#  puts "saturations.start #{saturations.start}"
#  puts "saturations.final #{saturations.final}"
#  puts "saturations.x #{saturations.exp}"
end

#saturation=(val) ⇒ Object

set saturation for both start and end



107
108
109
# File 'lib/tone.rb', line 107

def saturation= val
  wave.saturation= val
end

#saturationsObject



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

def saturations
  wave.saturations
end

#set_amp_final(val, is_relative = true) ⇒ Object

is_relative

false for setting it absolute



102
103
104
# File 'lib/tone.rb', line 102

def set_amp_final val, is_relative=true
  self.amp.final = is_relative ? val : val - amp.start
end

#set_freq(val) ⇒ Object

set frequency #freq#start



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

def set_freq (val)
  dif = freq.start - val
  freq.start = val
  set_freq_final freq_final(false) + dif, false
end

#set_freq_final(fq, is_relative = true) ⇒ Object

is_relative

false for setting it absolute



97
98
99
# File 'lib/tone.rb', line 97

def set_freq_final fq, is_relative=true
  self.freq.final = is_relative ? fq : fq - freq.start
end