Class: HeadMusic::Rudiment::PitchClass

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch_class_or_midi_number) ⇒ PitchClass

Returns a new instance of PitchClass.



29
30
31
# File 'lib/head_music/rudiment/pitch_class.rb', line 29

def initialize(pitch_class_or_midi_number)
  @number = pitch_class_or_midi_number.to_i % 12
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



8
9
10
# File 'lib/head_music/rudiment/pitch_class.rb', line 8

def number
  @number
end

#spellingObject (readonly)

Returns the value of attribute spelling.



8
9
10
# File 'lib/head_music/rudiment/pitch_class.rb', line 8

def spelling
  @spelling
end

Class Method Details

.get(identifier) ⇒ Object Also known as: []



15
16
17
18
19
20
21
22
23
# File 'lib/head_music/rudiment/pitch_class.rb', line 15

def self.get(identifier)
  @pitch_classes ||= {}
  if HeadMusic::Rudiment::Spelling.matching_string(identifier)
    spelling = HeadMusic::Rudiment::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



64
65
66
# File 'lib/head_music/rudiment/pitch_class.rb', line 64

def +(other)
  HeadMusic::Rudiment::PitchClass.get(to_i + other.to_i)
end

#-(other) ⇒ Object

Pass in the number of semitones



69
70
71
# File 'lib/head_music/rudiment/pitch_class.rb', line 69

def -(other)
  HeadMusic::Rudiment::PitchClass.get(to_i - other.to_i)
end

#<=>(other) ⇒ Object



78
79
80
# File 'lib/head_music/rudiment/pitch_class.rb', line 78

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

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



73
74
75
# File 'lib/head_music/rudiment/pitch_class.rb', line 73

def ==(other)
  to_i == other.to_i
end

#black_key?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/head_music/rudiment/pitch_class.rb', line 96

def black_key?
  !white_key?
end

#flat_spellingObject



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

def flat_spelling
  FLAT_SPELLINGS[number]
end

#flatter_spellingObject



49
50
51
# File 'lib/head_music/rudiment/pitch_class.rb', line 49

def flatter_spelling
  FLATTER_SPELLINGS[number]
end

#intervals_to(other) ⇒ Object



82
83
84
85
86
# File 'lib/head_music/rudiment/pitch_class.rb', line 82

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::Rudiment::ChromaticInterval.get(interval) }
end

#sharp_spellingObject



41
42
43
# File 'lib/head_music/rudiment/pitch_class.rb', line 41

def sharp_spelling
  SHARP_SPELLINGS[number]
end

#smallest_interval_to(other) ⇒ Object



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

def smallest_interval_to(other)
  intervals_to(other).first
end

#smart_spelling(max_sharps_in_major_key_signature: 6) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/head_music/rudiment/pitch_class.rb', line 53

def smart_spelling(max_sharps_in_major_key_signature: 6)
  sharp_key = HeadMusic::Rudiment::KeySignature.get(sharp_spelling)
  return HeadMusic::Rudiment::Spelling.get(sharp_spelling) if sharp_key.num_sharps <= max_sharps_in_major_key_signature

  flat_key = HeadMusic::Rudiment::KeySignature.get(flat_spelling)
  return HeadMusic::Rudiment::Spelling.get(flat_spelling) if flat_key.num_sharps <= max_sharps_in_major_key_signature

  HeadMusic::Rudiment::Spelling.get(flatter_spelling)
end

#to_iObject



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

def to_i
  number
end

#to_integer_notationObject



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

def to_integer_notation
  INTEGER_NOTATION[number]
end

#white_key?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/head_music/rudiment/pitch_class.rb', line 92

def white_key?
  [0, 2, 4, 5, 7, 9, 11].include?(number)
end