Class: PicoTune::WaveSample
- Inherits:
-
Object
- Object
- PicoTune::WaveSample
- Defined in:
- lib/picotune.rb
Instance Method Summary collapse
-
#initialize(tone, samples_per_wave) ⇒ WaveSample
constructor
A new instance of WaveSample.
- #noise(index) ⇒ Object
- #sample(index) ⇒ Object
- #saw(index) ⇒ Object
- #sine(index) ⇒ Object
- #square(index) ⇒ Object
- #triangle(index) ⇒ Object
Constructor Details
#initialize(tone, samples_per_wave) ⇒ WaveSample
79 80 81 82 |
# File 'lib/picotune.rb', line 79 def initialize tone, samples_per_wave @tone = tone @samples_per_wave = samples_per_wave end |
Instance Method Details
#noise(index) ⇒ Object
118 119 120 121 122 |
# File 'lib/picotune.rb', line 118 def noise index value = sine index rand = Random.rand - 0.5 value * rand end |
#sample(index) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/picotune.rb', line 84 def sample index value = case @tone when 'square' square index when 'sine' sine index when 'triangle' triangle index when 'noise' noise index when 'saw' saw index else sine index end PicoTune::Sample.new value, value end |
#saw(index) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/picotune.rb', line 107 def saw index interval = @samples_per_wave / 2 half_interval = interval / 2 percent = ((index + half_interval) % interval) / interval.to_f ((0.6 * percent) - 0.3) end |
#sine(index) ⇒ Object
103 104 105 |
# File 'lib/picotune.rb', line 103 def sine index Math.sin(index / (@samples_per_wave / (Math::PI * 2))) end |
#square(index) ⇒ Object
114 115 116 |
# File 'lib/picotune.rb', line 114 def square index (index <= @samples_per_wave / 2 ? 1.0 : -1.0) end |
#triangle(index) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/picotune.rb', line 124 def triangle index half = @samples_per_wave / 2 quarter = @samples_per_wave / 4 ramp = 1.0 / quarter if index <= half if index <= quarter index * ramp else (half - index) * ramp end else if index <= half + quarter -((index - half) * ramp) else -((@samples_per_wave - index) * ramp) end end end |