Class: Coltrane::PitchClass

Inherits:
Object
  • Object
show all
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, frequency: nil) ⇒ PitchClass

Returns a new instance of PitchClass.



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

def initialize(arg, frequency: nil)
  @integer = case arg
             when String then NOTATION.index(arg)
             when Frequency, Float then frequency_to_integer(Frequency.new(arg))
             when Integer then (arg % 12)
             when NilClass then frequency_to_integer(Frequency.new(frequency))
             when PitchClass then arg.integer
             else raise(InvalidArgumentError)
             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



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

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

.allObject



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

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

.sizeObject



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

def self.size
  NOTATION.size
end

Instance Method Details

#+(other) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/coltrane/pitch_class.rb', line 61

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

#-(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 PitchClass[integer - other.semitones]
  when Integer    then PitchClass[integer - other]
  when PitchClass then IntervalClass[frequency / other.frequency]
  when Frequency  then PitchClass.new(frequency: frequency - other)
  end
end

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



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

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

#accidental?Boolean

Returns:

  • (Boolean)


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

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

#enharmonic?(other) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/coltrane/pitch_class.rb', line 97

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

#fundamental_frequencyObject Also known as: frequency



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

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

#pretty_nameObject



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

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

#sizeObject



93
94
95
# File 'lib/coltrane/pitch_class.rb', line 93

def size
  self.class.size
end

#true_notationObject Also known as: name, notation, to_s



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

def true_notation
  NOTATION[integer]
end