Class: Juicy::Pitch

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/juicy/pitch.rb

Constant Summary collapse

@@temperament =
:equal
@@pitch_standard =
440.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch = @@pitch_standard, tune_now = true) ⇒ Pitch

Returns a new instance of Pitch.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/juicy/pitch.rb', line 36

def initialize(pitch = @@pitch_standard, tune_now = true)
  
  if pitch.kind_of? Numeric
    @frequency = pitch
    @tuned = false
    tune if tune_now
  else
    step = PITCHES[pitch.to_sym]
    @frequency = @@pitch_standard*2**(step/12.0)
    @tuned = true
  end
  
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



34
35
36
# File 'lib/juicy/pitch.rb', line 34

def confidence
  @confidence
end

#frequencyObject (readonly)

Returns the value of attribute frequency.



34
35
36
# File 'lib/juicy/pitch.rb', line 34

def frequency
  @frequency
end

Class Method Details

.play(options = {duration: 200}) ⇒ Object



71
72
73
# File 'lib/juicy/pitch.rb', line 71

def self.play(options = {duration: 200})
  Win32::Sound.play_freq(options[:note].pitch.frequency, options[:note].duration)
end

Instance Method Details

#+(interval) ⇒ Object



63
64
65
# File 'lib/juicy/pitch.rb', line 63

def +(interval)
  change_by (interval)
end

#-(interval) ⇒ Object



67
68
69
# File 'lib/juicy/pitch.rb', line 67

def -(interval)
  change_by (-interval)
end

#<=>(other_pitch) ⇒ Object



90
91
92
# File 'lib/juicy/pitch.rb', line 90

def <=>(other_pitch)
  self.frequency <=> other_pitch.frequency
end

#play(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object



75
76
77
78
79
80
# File 'lib/juicy/pitch.rb', line 75

def play(options = {duration: 200, octave: 0, volume: 1})
  options[:duration] ||= 200
  options[:octave] ||= 0
  options[:volume] ||= 1
  Win32::Sound.play_freq(@frequency*2**(options[:octave]), options[:duration], options[:volume])
end

#prepare(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/juicy/pitch.rb', line 82

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_sObject



50
51
52
# File 'lib/juicy/pitch.rb', line 50

def to_s
  "#{@frequency}"
end

#tuneObject



54
55
56
57
58
59
60
61
# File 'lib/juicy/pitch.rb', line 54

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_standard*2**((step.round)/12.0)
    @tuned = true
  end
end