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.
26 27 28 |
# File 'lib/head_music/pitch_class.rb', line 26 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.
5 6 7 |
# File 'lib/head_music/pitch_class.rb', line 5 def number @number end |
#spelling ⇒ Object (readonly)
Returns the value of attribute spelling.
5 6 7 |
# File 'lib/head_music/pitch_class.rb', line 5 def spelling @spelling end |
Class Method Details
.get(identifier) ⇒ Object Also known as: []
12 13 14 15 16 17 18 19 20 |
# File 'lib/head_music/pitch_class.rb', line 12 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
61 62 63 |
# File 'lib/head_music/pitch_class.rb', line 61 def +(other) HeadMusic::PitchClass.get(to_i + other.to_i) end |
#-(other) ⇒ Object
Pass in the number of semitones
66 67 68 |
# File 'lib/head_music/pitch_class.rb', line 66 def -(other) HeadMusic::PitchClass.get(to_i - other.to_i) end |
#<=>(other) ⇒ Object
75 76 77 |
# File 'lib/head_music/pitch_class.rb', line 75 def <=>(other) to_i <=> other.to_i end |
#==(other) ⇒ Object Also known as: enharmonic?
70 71 72 |
# File 'lib/head_music/pitch_class.rb', line 70 def ==(other) to_i == other.to_i end |
#black_key? ⇒ Boolean
93 94 95 |
# File 'lib/head_music/pitch_class.rb', line 93 def black_key? !white_key? end |
#flat_spelling ⇒ Object
42 43 44 |
# File 'lib/head_music/pitch_class.rb', line 42 def flat_spelling FLAT_SPELLINGS[number] end |
#flatter_spelling ⇒ Object
46 47 48 |
# File 'lib/head_music/pitch_class.rb', line 46 def flatter_spelling FLATTER_SPELLINGS[number] end |
#intervals_to(other) ⇒ Object
79 80 81 82 83 |
# File 'lib/head_music/pitch_class.rb', line 79 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
38 39 40 |
# File 'lib/head_music/pitch_class.rb', line 38 def sharp_spelling SHARP_SPELLINGS[number] end |
#smallest_interval_to(other) ⇒ Object
85 86 87 |
# File 'lib/head_music/pitch_class.rb', line 85 def smallest_interval_to(other) intervals_to(other).first end |
#smart_spelling(max_sharps_in_major_key_signature: 6) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/head_music/pitch_class.rb', line 50 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
30 31 32 |
# File 'lib/head_music/pitch_class.rb', line 30 def to_i number end |
#to_integer_notation ⇒ Object
34 35 36 |
# File 'lib/head_music/pitch_class.rb', line 34 def to_integer_notation INTEGER_NOTATION[number] end |
#white_key? ⇒ Boolean
89 90 91 |
# File 'lib/head_music/pitch_class.rb', line 89 def white_key? [0, 2, 4, 5, 7, 9, 11].include?(number) end |