Class: HeadMusic::PitchClass
- Inherits:
-
Object
- Object
- HeadMusic::PitchClass
- Includes:
- Comparable
- Defined in:
- lib/head_music/pitch_class.rb
Overview
A pitch class is a set of pitches separated by octaves.
Constant Summary collapse
- SHARP_SPELLINGS =
%w[C C♯ D D♯ E F F♯ G G♯ A A♯ B].freeze
- FLAT_SPELLINGS =
%w[C D♭ D E♭ E F G♭ G A♭ A B♭ B].freeze
- FLATTER_SPELLINGS =
%w[C D♭ D E♭ F♭ F G♭ G A♭ A B♭ C♭].freeze
- INTEGER_NOTATION =
%w[0 1 2 3 4 5 6 7 8 9 t e].freeze
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#spelling ⇒ Object
readonly
Returns the value of attribute spelling.
Class Method Summary collapse
- .get(identifier) ⇒ Object (also: [])
Instance Method Summary collapse
-
#+(other) ⇒ Object
Pass in the number of semitones.
-
#-(other) ⇒ Object
Pass in the number of semitones.
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #enharmonic?)
- #black_key? ⇒ Boolean
- #flat_spelling ⇒ Object
- #flatter_spelling ⇒ Object
-
#initialize(pitch_class_or_midi_number) ⇒ PitchClass
constructor
A new instance of PitchClass.
- #intervals_to(other) ⇒ Object
- #sharp_spelling ⇒ Object
- #smallest_interval_to(other) ⇒ Object
- #smart_spelling(max_sharps_in_major_key_signature: 6) ⇒ Object
- #to_i ⇒ Object
- #to_integer_notation ⇒ Object
- #white_key? ⇒ Boolean
Constructor Details
#initialize(pitch_class_or_midi_number) ⇒ PitchClass
Returns a new instance of PitchClass.
28 29 30 |
# File 'lib/head_music/pitch_class.rb', line 28 def initialize(pitch_class_or_midi_number) @number = pitch_class_or_midi_number.to_i % 12 end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
7 8 9 |
# File 'lib/head_music/pitch_class.rb', line 7 def number @number end |
#spelling ⇒ Object (readonly)
Returns the value of attribute spelling.
7 8 9 |
# File 'lib/head_music/pitch_class.rb', line 7 def spelling @spelling end |
Class Method Details
.get(identifier) ⇒ Object Also known as: []
14 15 16 17 18 19 20 21 22 |
# File 'lib/head_music/pitch_class.rb', line 14 def self.get(identifier) @pitch_classes ||= {} if HeadMusic::Spelling.matching_string(identifier) spelling = HeadMusic::Spelling.get(identifier) number = spelling.pitch_class.to_i end number ||= identifier.to_i % 12 @pitch_classes[number] ||= new(number) end |
Instance Method Details
#+(other) ⇒ Object
Pass in the number of semitones
63 64 65 |
# File 'lib/head_music/pitch_class.rb', line 63 def +(other) HeadMusic::PitchClass.get(to_i + other.to_i) end |
#-(other) ⇒ Object
Pass in the number of semitones
68 69 70 |
# File 'lib/head_music/pitch_class.rb', line 68 def -(other) HeadMusic::PitchClass.get(to_i - other.to_i) end |
#<=>(other) ⇒ Object
77 78 79 |
# File 'lib/head_music/pitch_class.rb', line 77 def <=>(other) to_i <=> other.to_i end |
#==(other) ⇒ Object Also known as: enharmonic?
72 73 74 |
# File 'lib/head_music/pitch_class.rb', line 72 def ==(other) to_i == other.to_i end |
#black_key? ⇒ Boolean
95 96 97 |
# File 'lib/head_music/pitch_class.rb', line 95 def black_key? !white_key? end |
#flat_spelling ⇒ Object
44 45 46 |
# File 'lib/head_music/pitch_class.rb', line 44 def flat_spelling FLAT_SPELLINGS[number] end |
#flatter_spelling ⇒ Object
48 49 50 |
# File 'lib/head_music/pitch_class.rb', line 48 def flatter_spelling FLATTER_SPELLINGS[number] end |
#intervals_to(other) ⇒ Object
81 82 83 84 85 |
# File 'lib/head_music/pitch_class.rb', line 81 def intervals_to(other) delta = other.to_i - to_i inverse = delta.positive? ? delta - 12 : delta + 12 [delta, inverse].sort_by(&:abs).map { |interval| HeadMusic::ChromaticInterval.get(interval) } end |
#sharp_spelling ⇒ Object
40 41 42 |
# File 'lib/head_music/pitch_class.rb', line 40 def sharp_spelling SHARP_SPELLINGS[number] end |
#smallest_interval_to(other) ⇒ Object
87 88 89 |
# File 'lib/head_music/pitch_class.rb', line 87 def smallest_interval_to(other) intervals_to(other).first end |
#smart_spelling(max_sharps_in_major_key_signature: 6) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/head_music/pitch_class.rb', line 52 def smart_spelling(max_sharps_in_major_key_signature: 6) sharp_key = HeadMusic::KeySignature.get(sharp_spelling) return HeadMusic::Spelling.get(sharp_spelling) if sharp_key.num_sharps <= max_sharps_in_major_key_signature flat_key = HeadMusic::KeySignature.get(flat_spelling) return HeadMusic::Spelling.get(flat_spelling) if flat_key.num_sharps <= max_sharps_in_major_key_signature HeadMusic::Spelling.get(flatter_spelling) end |
#to_i ⇒ Object
32 33 34 |
# File 'lib/head_music/pitch_class.rb', line 32 def to_i number end |
#to_integer_notation ⇒ Object
36 37 38 |
# File 'lib/head_music/pitch_class.rb', line 36 def to_integer_notation INTEGER_NOTATION[number] end |
#white_key? ⇒ Boolean
91 92 93 |
# File 'lib/head_music/pitch_class.rb', line 91 def white_key? [0, 2, 4, 5, 7, 9, 11].include?(number) end |