Class: MusicTheory::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/lygre/musictheory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch = :c, octave = 1) ⇒ Note

Returns a new instance of Note.



7
8
9
10
11
12
13
14
15
# File 'lib/lygre/musictheory.rb', line 7

def initialize(pitch=:c, octave=1)
  @pitch = pitch
  @pitch_numeric = PITCHES.index @pitch
  @octave = octave

  if @pitch_numeric.nil?
    raise ArgumentError.new("Invalid pitch #{pitch.inspect}")
  end
end

Instance Attribute Details

#octaveObject (readonly)

Returns the value of attribute octave.



17
18
19
# File 'lib/lygre/musictheory.rb', line 17

def octave
  @octave
end

#pitchObject (readonly)

Returns the value of attribute pitch.



17
18
19
# File 'lib/lygre/musictheory.rb', line 17

def pitch
  @pitch
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
# File 'lib/lygre/musictheory.rb', line 35

def ==(other)
  other.pitch == @pitch &&
    other.octave == @octave
end

#diatonic_steps(steps) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lygre/musictheory.rb', line 19

def diatonic_steps(steps)
  base_steps = steps % PITCHES.size
  octaves = steps / PITCHES.size
  new_step = @pitch_numeric + base_steps
  if new_step >= PITCHES.size
    new_step -= PITCHES.size
    octaves += 1
  end
  new_pitch = PITCHES[new_step]
  self.class.new(new_pitch, @octave + octaves)
end

#hashObject



40
41
42
# File 'lib/lygre/musictheory.rb', line 40

def hash
  [@pitch, @octave].hash
end

#valueObject



31
32
33
# File 'lib/lygre/musictheory.rb', line 31

def value
  (@octave + 4) * 12 + VALUES[@pitch_numeric]
end