Class: Juicy::Pitch

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

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_standardObject (readonly)

Returns the value of attribute pitch_standard.



37
38
39
# File 'lib/juicy/pitch.rb', line 37

def pitch_standard
  @pitch_standard
end

.temperamentObject (readonly)

Returns the value of attribute temperament.



37
38
39
# File 'lib/juicy/pitch.rb', line 37

def temperament
  @temperament
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



40
41
42
# File 'lib/juicy/pitch.rb', line 40

def confidence
  @confidence
end

#frequencyObject (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(options = {duration: 200})
  binding.pry
  Sound::Out.play_freq(options[:note].pitch.frequency, options[: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(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}) ⇒ Object



94
95
96
97
98
99
100
# 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_sObject



57
58
59
# File 'lib/juicy/pitch.rb', line 57

def to_s
  "#{@frequency}"
end

#tuneObject



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