Class: Coltrane::Theory::PitchClass

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/coltrane/theory/pitch_class.rb

Overview

Pitch classes, and by classes here we don’t mean in the sense of a ruby class are all the classes of pitches (frequencies) that are in a whole number of octaves apart.

For example, C1, C2, C3 are all pitches from the C pitch class. Take a look into Notes description if you somehow feel this is confuse and that it could just be called as notes instead.

Direct Known Subclasses

Note

Constant Summary collapse

NOTATION =
%w[C C# D D# E F F# G G# A A# B].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil, frequency: nil) ⇒ PitchClass

Returns a new instance of PitchClass.



28
29
30
31
32
33
34
35
36
37
# File 'lib/coltrane/theory/pitch_class.rb', line 28

def initialize(arg = nil, frequency: nil)
  @integer = case arg
             when String then NOTATION.index(arg)
             when Frequency then frequency_to_integer(Frequency.new(arg))
             when Numeric then (arg % 12)
             when nil then frequency_to_integer(Frequency.new(frequency))
             when PitchClass then arg.integer
             else raise(WrongArgumentsError)
             end
end

Instance Attribute Details

#integerObject (readonly) Also known as: hash

Returns the value of attribute integer.



15
16
17
# File 'lib/coltrane/theory/pitch_class.rb', line 15

def integer
  @integer
end

Class Method Details

.[](arg, frequency: nil) ⇒ Object



39
40
41
# File 'lib/coltrane/theory/pitch_class.rb', line 39

def self.[](arg, frequency: nil)
  new(arg, frequency: nil)
end

.allObject



24
25
26
# File 'lib/coltrane/theory/pitch_class.rb', line 24

def self.all
  NOTATION.map { |n| new(n) }
end

.all_lettersObject



20
21
22
# File 'lib/coltrane/theory/pitch_class.rb', line 20

def self.all_letters
  %w[C D E F G A B]
end

.sizeObject



129
130
131
# File 'lib/coltrane/theory/pitch_class.rb', line 129

def self.size
  NOTATION.size
end

Instance Method Details

#+(other) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/coltrane/theory/pitch_class.rb', line 93

def +(other)
  case other
  when Interval   then self.class[integer + other.semitones]
  when Integer    then self.class[integer + other]
  when PitchClass then self.class[integer + other.integer]
  when Frequency  then self.class.new(frequency: frequency + other)
  end
end

#-(other) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/coltrane/theory/pitch_class.rb', line 102

def -(other)
  case other
  when Interval   then self.class[integer - other.semitones]
  when Integer    then self.class[integer - other]
  when PitchClass then Interval.new(self, other)
  when Frequency  then self.class.new(frequency: frequency - other)
  end
end

#<=>(other) ⇒ Object



115
116
117
# File 'lib/coltrane/theory/pitch_class.rb', line 115

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

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



43
44
45
# File 'lib/coltrane/theory/pitch_class.rb', line 43

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

#accidental?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/coltrane/theory/pitch_class.rb', line 78

def accidental?
  notation.match? /#|b/
end

#ascending_interval_to(other) ⇒ Object Also known as: interval_to



58
59
60
# File 'lib/coltrane/theory/pitch_class.rb', line 58

def ascending_interval_to(other)
  Interval.new(self, (other.is_a?(PitchClass) ? other : Note.new(other)))
end

#descending_interval_to(other) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/coltrane/theory/pitch_class.rb', line 64

def descending_interval_to(other)
  Interval.new(
    self,
    (other.is_a?(PitchClass) ? other : Note.new(other)),
    ascending: false
  )
end

#enharmonic?(other) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'lib/coltrane/theory/pitch_class.rb', line 137

def enharmonic?(other)
  case other
  when String then integer == Note[other].integer
  when Note then integer == other.integer
  end
end

#flat?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/coltrane/theory/pitch_class.rb', line 86

def flat?
  notation.match? /b/
end

#fundamental_frequencyObject Also known as: frequency



119
120
121
122
123
124
125
# File 'lib/coltrane/theory/pitch_class.rb', line 119

def fundamental_frequency
  @fundamental_frequency ||=
    Frequency[
      Theory.base_tuning *
      (2**((integer - Theory::BASE_PITCH_INTEGER.to_f) / 12))
    ]
end

#letterObject



54
55
56
# File 'lib/coltrane/theory/pitch_class.rb', line 54

def letter
  name[0]
end

#pitch_classObject



111
112
113
# File 'lib/coltrane/theory/pitch_class.rb', line 111

def pitch_class
  self
end

#pretty_nameObject



74
75
76
# File 'lib/coltrane/theory/pitch_class.rb', line 74

def pretty_name
  name.tr('b', "\u266D").tr('#', "\u266F")
end

#sharp?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/coltrane/theory/pitch_class.rb', line 82

def sharp?
  notation.match? /#/
end

#sizeObject



133
134
135
# File 'lib/coltrane/theory/pitch_class.rb', line 133

def size
  self.class.size
end

#true_notationObject Also known as: name, notation, to_s



50
51
52
# File 'lib/coltrane/theory/pitch_class.rb', line 50

def true_notation
  NOTATION[integer]
end