class PicoTune::WaveSample

Public Class Methods

new(tone, samples_per_wave) click to toggle source
# File lib/picotune.rb, line 79
def initialize tone, samples_per_wave
  @tone = tone
  @samples_per_wave = samples_per_wave
end

Public Instance Methods

noise(index) click to toggle source
# File lib/picotune.rb, line 118
def noise index
  value = sine index
  rand = Random.rand - 0.5
  value * rand
end
sample(index) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/picotune.rb, line 103
def sine index
  Math.sin(index / (@samples_per_wave / (Math::PI * 2)))
end
square(index) click to toggle source
# File lib/picotune.rb, line 114
def square index
  (index <= @samples_per_wave / 2 ? 1.0 : -1.0)
end
triangle(index) click to toggle source
# 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