class Juicy::Pitch

This class encapsulates all of the pitch mechanics for a given temperament.

Attributes

pitch_standard[R]
temperament[R]
confidence[R]
frequency[R]

Public Class Methods

new(pitch = Pitch.pitch_standard, tune_now = true) click to toggle source
# File lib/juicy/pitch.rb, line 42
def initialize(pitch = Pitch.pitch_standard, tune_now = true)
  
  if pitch.kind_of? Numeric
    @frequency = pitch
    @tuned = false
    tune if tune_now
  else
    raise ArgumentError unless pitch.kind_of? Symbol
    step = PITCHES[pitch.to_sym]
    @frequency = Pitch.pitch_standard*2**(step/12.0)
    @tuned = true
  end
  
end
play(options = {duration: 200}) click to toggle source
# File lib/juicy/pitch.rb, line 79
def self.play(options = {duration: 200})
  binding.pry
  Sound::Out.play_freq(options[:note].pitch.frequency, options[:note].duration)
end

Public Instance Methods

+(interval) click to toggle source
# File lib/juicy/pitch.rb, line 71
def +(interval)
  change_by (interval)
end
-(interval) click to toggle source
# File lib/juicy/pitch.rb, line 75
def -(interval)
  change_by (-interval)
end
<=>(other_pitch) click to toggle source
# File lib/juicy/pitch.rb, line 102
def <=>(other_pitch)
  self.frequency <=> other_pitch.frequency
end
play(options = {duration: 200, octave: 0, volume: 1}) click to toggle source
# File lib/juicy/pitch.rb, line 84
def play(options = {duration: 200, octave: 0, volume: 1})
  options[:duration] ||= 200
  options[:octave] ||= 0
  options[:volume] ||= 1
  device = Sound::Device.new
  data = Sound::Data.new(device.format)
  data.generate_sine_wave(@frequency*2**(options[:octave]), options[:duration], options[:volume])
  device.play data
end
prepare(options = {duration: 200, octave: 0, volume: 1}) click to toggle source
# File lib/juicy/pitch.rb, line 94
def prepare(options = {duration: 200, octave: 0, volume: 1})
  options[:duration] ||= 200
  options[:octave] ||= 0
  options[:volume] ||= 1
  
  return Thread.new{Win32::Sound.play_freq(@frequency*2**(options[:octave]), options[:duration], options[:volume], true)}
end
to_s() click to toggle source
# File lib/juicy/pitch.rb, line 57
def to_s
  "#{@frequency}"
end
tune() click to toggle source
# File lib/juicy/pitch.rb, line 61
def tune
  if out_of_tune
    step = Math.log(@frequency/440.0,2)*12
    @confidence = (1.0-2*(step - step.round).abs)*100.0
    @frequency = Pitch.pitch_standard*2**((step.round)/12.0)
    @tuned = true
  end
  self
end

Private Instance Methods

change_by(interval) click to toggle source
# File lib/juicy/pitch.rb, line 112
def change_by (interval)
  if Pitch.temperament.eql? :equal
    Pitch.new(@frequency*2**(interval/12.0))
  end
end
out_of_tune() click to toggle source
# File lib/juicy/pitch.rb, line 108
def out_of_tune
  !@tuned
end