Class: Coltrane::PitchClass

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



23
24
25
26
27
28
29
30
31
32
# File 'lib/coltrane/pitch_class.rb', line 23

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.



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

def integer
  @integer
end

Class Method Details

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



34
35
36
# File 'lib/coltrane/pitch_class.rb', line 34

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

.allObject



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

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

.sizeObject



102
103
104
# File 'lib/coltrane/pitch_class.rb', line 102

def self.size
  NOTATION.size
end

Instance Method Details

#+(other) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/coltrane/pitch_class.rb', line 70

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



79
80
81
82
83
84
85
86
# File 'lib/coltrane/pitch_class.rb', line 79

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

#<=>(other) ⇒ Object



88
89
90
# File 'lib/coltrane/pitch_class.rb', line 88

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

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



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

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

#accidental?Boolean

Returns:

  • (Boolean)


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

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

#enharmonic?(other) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/coltrane/pitch_class.rb', line 110

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

#flat?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/coltrane/pitch_class.rb', line 63

def flat?
  notation.match? /b/
end

#fundamental_frequencyObject Also known as: frequency



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

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

#pretty_nameObject



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

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

#sharp?Boolean

Returns:

  • (Boolean)


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

def sharp?
  notation.match? /#/
end

#sizeObject



106
107
108
# File 'lib/coltrane/pitch_class.rb', line 106

def size
  self.class.size
end

#true_notationObject Also known as: name, notation, to_s



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

def true_notation
  NOTATION[integer]
end