Class: Coltrane::Pitch

Inherits:
Object
  • Object
show all
Defined in:
lib/coltrane/pitch.rb

Overview

It describes a pitch, like E4 or Bb5. It’s like a note, but it has an octave

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch) ⇒ Pitch

Returns a new instance of Pitch.



8
9
10
11
12
13
14
# File 'lib/coltrane/pitch.rb', line 8

def initialize(pitch)
  case pitch
  when String  then @number = number_from_name(pitch)
  when Numeric then @number = pitch
  when Pitch   then @number = pitch.number
  end
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/coltrane/pitch.rb', line 6

def number
  @number
end

Instance Method Details

#+(other) ⇒ Object



33
34
35
# File 'lib/coltrane/pitch.rb', line 33

def +(other)
  Pitch.new(number + (other.is_a?(Pitch) ? other.number : other))
end

#nameObject



21
22
23
# File 'lib/coltrane/pitch.rb', line 21

def name
  "#{note.name}#{octave}"
end

#noteObject



29
30
31
# File 'lib/coltrane/pitch.rb', line 29

def note
  Note[number]
end

#number_from_name(pitch_string) ⇒ Object



16
17
18
19
# File 'lib/coltrane/pitch.rb', line 16

def number_from_name(pitch_string)
  _, note, octaves = pitch_string.match(/(.*)(\d)/).to_a
  Note[note].number + 12 * octaves.to_i
end

#octaveObject



25
26
27
# File 'lib/coltrane/pitch.rb', line 25

def octave
  number / 12
end