Class: Coltrane::Theory::Pitch

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.



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

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.



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

def integer
  @integer
end

Class Method Details

.[](*args) ⇒ Object



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

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

Instance Method Details

#+(other) ⇒ Object



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

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

#-(other) ⇒ Object



73
74
75
76
77
# File 'lib/coltrane/theory/pitch.rb', line 73

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

#<=>(other) ⇒ Object



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

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

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



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

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

#frequencyObject



60
61
62
# File 'lib/coltrane/theory/pitch.rb', line 60

def frequency
  pitch_class.frequency.octave(octave)
end

#octaveObject



45
46
47
# File 'lib/coltrane/theory/pitch.rb', line 45

def octave
  (integer / 12) - 1
end

#pitch_classObject



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

def pitch_class
  PitchClass[integer]
end

#scientific_notationObject Also known as: notation, name, to_s



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

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