Class: Juicy::Pitch
Overview
This class encapsulates all of the pitch mechanics for a given temperament.
Class Attribute Summary collapse
-
.pitch_standard ⇒ Object
readonly
Returns the value of attribute pitch_standard.
-
.temperament ⇒ Object
readonly
Returns the value of attribute temperament.
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.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.pitch_standard, tune_now = true) ⇒ Pitch
Returns a new instance of Pitch.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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 |
Class Attribute Details
.pitch_standard ⇒ Object (readonly)
Returns the value of attribute pitch_standard.
37 38 39 |
# File 'lib/juicy/pitch.rb', line 37 def pitch_standard @pitch_standard end |
.temperament ⇒ Object (readonly)
Returns the value of attribute temperament.
37 38 39 |
# File 'lib/juicy/pitch.rb', line 37 def temperament @temperament end |
Instance Attribute Details
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
40 41 42 |
# File 'lib/juicy/pitch.rb', line 40 def confidence @confidence end |
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
40 41 42 |
# File 'lib/juicy/pitch.rb', line 40 def frequency @frequency end |
Class Method Details
.play(options = {duration: 200}) ⇒ Object
79 80 81 82 |
# File 'lib/juicy/pitch.rb', line 79 def self.play( = {duration: 200}) binding.pry Sound::Out.play_freq([:note].pitch.frequency, [:note].duration) end |
Instance Method Details
#+(interval) ⇒ Object
71 72 73 |
# File 'lib/juicy/pitch.rb', line 71 def +(interval) change_by (interval) end |
#-(interval) ⇒ Object
75 76 77 |
# File 'lib/juicy/pitch.rb', line 75 def -(interval) change_by (-interval) end |
#<=>(other_pitch) ⇒ Object
102 103 104 |
# File 'lib/juicy/pitch.rb', line 102 def <=>(other_pitch) self.frequency <=> other_pitch.frequency end |
#play(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/juicy/pitch.rb', line 84 def play( = {duration: 200, octave: 0, volume: 1}) [:duration] ||= 200 [:octave] ||= 0 [:volume] ||= 1 device = Sound::Device.new data = Sound::Data.new(device.format) data.generate_sine_wave(@frequency*2**([:octave]), [:duration], [:volume]) device.play data end |
#prepare(options = {duration: 200, octave: 0, volume: 1}) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/juicy/pitch.rb', line 94 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
57 58 59 |
# File 'lib/juicy/pitch.rb', line 57 def to_s "#{@frequency}" end |
#tune ⇒ Object
61 62 63 64 65 66 67 68 69 |
# 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 |