Class: Wave

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

Overview

a waveform. this is looped to create a Tone.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start = 512, final = 512, exp = 0) ⇒ Wave

Returns a new instance of Wave.



13
14
15
16
17
18
# File 'lib/wave.rb', line 13

def initialize(start=512,final=512,exp=0)
  @detail = Fader.new(start, final, exp)
  @saturations = Fader.new(0,0,0)
  @cache_wave=Array.new(2)
  @old_wave = nil#Wave.new # so false first time with is_eql
end

Instance Attribute Details

#cache_waveObject

hack to dramatically speed it up when on.



11
12
13
# File 'lib/wave.rb', line 11

def cache_wave
  @cache_wave
end

#detailObject

Fader.final is at the end. nil means same as wave start



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

def detail
  @detail
end

#old_waveObject

hack to dramatically speed it up when on.



11
12
13
# File 'lib/wave.rb', line 11

def old_wave
  @old_wave
end

#saturationsObject

the ammount of saturation is higher is more, 0 is none. range: 0 to 1.



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

def saturations
  @saturations
end

Instance Method Details

#is_eql(other) ⇒ Object



73
74
75
# File 'lib/wave.rb', line 73

def is_eql(other)
  vars_eql?(other, ['@detail','@saturations'])
end

#out(freq, amp = 1, wave_into = 1) ⇒ Object

return WaveData of data for a single wave cycle

len

length in frames

amp

amplitude or volume (loudness), 0 to 1. 0 is silent

wave_into

how much of the final wave data is used this time. range: 0 to 1.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wave.rb', line 30

def out(freq, amp = 1, wave_into = 1)
  final=WaveData.new
  start=WaveData.new
  if is_eql old_wave
#    puts "using cahce"
    start = cache_wave[0]
    final = cache_wave[1]
  else
#    puts "not using cahce"
    self.cache_wave=[nil,nil]
    self.old_wave=self.dup
    self.cache_wave[0] =WaveData.new(MathUtils.sinwave(detail.start, saturations.start))
    self.cache_wave[1] =WaveData.new(MathUtils.sinwave(detail.final, saturations.final))
    start = cache_wave[0]
    final = cache_wave[1]
  end

  data = []
  len = Composer.samplerate / freq
  
  len.to_i.times do |i|
    progress=i.to_f/len  # from 0 to 1
    if (detail.start > 0)
      val = start.interpolate progress
      # merging two waveforms
      if (detail.final > 0)
        val2 = final.interpolate progress
        val_range = val2-val
        val = val + val_range*wave_into
      end
    else # normal sign wave
      raise "Error, wave detail isn't > 0"
      # val = Math.sin(2.0*Math::PI*progress)
    end
    # puts "amp #{amp}, val #{val}"
    val *= amp # reduce volume by this
    data[i] = val
  end
  # puts "--> values: #{data.join(', ')}"
  result=WaveData.new(data)
  return result
end

#saturation=(val) ⇒ Object



20
21
22
23
# File 'lib/wave.rb', line 20

def saturation= val
  saturations.start = val
  saturations.final = val
end