Class: Coltrane::Theory::Pitch

Inherits:
Object
  • Object
show all
Defined in:
lib/coltrane/theory/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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notation_arg = nil, note: nil, octave: nil, notation: nil, frequency: nil) ⇒ Pitch

Returns a new instance of Pitch.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/coltrane/theory/pitch.rb', line 9

def initialize(notation_arg = nil,
               note: nil,
               octave: nil,
               notation: nil,
               frequency: nil)

  @integer = begin
    if (n = notation_arg || notation)
      n.is_a?(Integer) ? n : integer_from_notation(n)
    elsif note && octave
      integer_from_note_and_octave(Note[note], octave)
    elsif frequency
      integer_from_frequency(frequency)
    else
      raise(InvalidArgumentsError)
    end
  end
end

Instance Attribute Details

#integerObject (readonly) Also known as: hash, midi

Returns the value of attribute integer.



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

def integer
  @integer
end

Class Method Details

.[](*args) ⇒ Object



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

def self.[](*args)
  new *args
end

Instance Method Details

#+(other) ⇒ Object



62
63
64
65
66
# File 'lib/coltrane/theory/pitch.rb', line 62

def +(other)
  case other
  when Integer then Pitch[integer + other]
  end
end

#-(other) ⇒ Object



68
69
70
71
72
# File 'lib/coltrane/theory/pitch.rb', line 68

def -(other)
  case other
  when Integer then Pitch[integer - other]
  end
end

#==(other) ⇒ Object Also known as: eql?, eq



51
52
53
# File 'lib/coltrane/theory/pitch.rb', line 51

def ==(other)
  integer == other.integer
end

#frequencyObject



55
56
57
# File 'lib/coltrane/theory/pitch.rb', line 55

def frequency
  pitch_class.frequency.octave(octave)
end

#octaveObject



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

def octave
  (integer / 12) - 1
end

#pitch_classObject



36
37
38
# File 'lib/coltrane/theory/pitch.rb', line 36

def pitch_class
  PitchClass[integer]
end

#scientific_notationObject Also known as: notation, name, to_s



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

def scientific_notation
  "#{pitch_class}#{octave}"
end