Class: Juicy::Pitch
Constant Summary collapse
- @@temperament =
:equal
- @@pitch_standard =
440.0
Instance Attribute Summary collapse
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
Class Method Summary collapse
Instance Method Summary collapse
- #+(interval) ⇒ Object
- #-(interval) ⇒ Object
- #<=>(other_pitch) ⇒ Object
-
#initialize(pitch = @@pitch_standard, tune_now = true) ⇒ Pitch
constructor
A new instance of Pitch.
- #play(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object
- #prepare(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object
- #to_s ⇒ Object
- #tune ⇒ Object
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
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
34 35 36 |
# File 'lib/juicy/pitch.rb', line 34 def confidence @confidence end |
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
34 35 36 |
# File 'lib/juicy/pitch.rb', line 34 def frequency @frequency end |
Class Method Details
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( = {duration: 200, octave: 0, volume: 1}) [:duration] ||= 200 [:octave] ||= 0 [:volume] ||= 1 Win32::Sound.play_freq(@frequency*2**([:octave]), [:duration], [: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( = {duration: 200, octave: 0, volume: 1}) [:duration] ||= 200 [:octave] ||= 0 [:volume] ||= 1 return Thread.new{Win32::Sound.play_freq(@frequency*2**([:octave]), [:duration], [:volume], true)} end |
#to_s ⇒ Object
50 51 52 |
# File 'lib/juicy/pitch.rb', line 50 def to_s "#{@frequency}" end |
#tune ⇒ Object
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 |