Class: HeadMusic::Scale
- Inherits:
-
Object
- Object
- HeadMusic::Scale
- Defined in:
- lib/head_music/scale.rb
Overview
A scale contains ordered pitches starting at a tonal center.
Constant Summary collapse
- SCALE_REGEX =
/^[A-G][#b]?\s+\w+$/
Instance Attribute Summary collapse
-
#root_pitch ⇒ Object
readonly
Returns the value of attribute root_pitch.
-
#scale_type ⇒ Object
readonly
Returns the value of attribute scale_type.
Class Method Summary collapse
Instance Method Summary collapse
- #degree(degree_number) ⇒ Object
-
#initialize(root_pitch, scale_type) ⇒ Scale
constructor
A new instance of Scale.
- #pitch_names(direction: :ascending, octaves: 1) ⇒ Object
- #pitches(direction: :ascending, octaves: 1) ⇒ Object
- #root_pitch_number ⇒ Object
- #spellings(direction: :ascending, octaves: 1) ⇒ Object
Constructor Details
Instance Attribute Details
#root_pitch ⇒ Object (readonly)
Returns the value of attribute root_pitch.
20 21 22 |
# File 'lib/head_music/scale.rb', line 20 def root_pitch @root_pitch end |
#scale_type ⇒ Object (readonly)
Returns the value of attribute scale_type.
20 21 22 |
# File 'lib/head_music/scale.rb', line 20 def scale_type @scale_type end |
Class Method Details
.get(root_pitch, scale_type = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/head_music/scale.rb', line 7 def self.get(root_pitch, scale_type = nil) root_pitch, scale_type = root_pitch.split(/\s+/) if root_pitch.is_a?(String) && scale_type =~ SCALE_REGEX root_pitch = HeadMusic::Pitch.get(root_pitch) scale_type = HeadMusic::ScaleType.get(scale_type || :major) @scales ||= {} hash_key = HeadMusic::Utilities::HashKey.for( [root_pitch, scale_type].join(" ").gsub(/#|♯/, "sharp").gsub(/(\w)[b♭]/, '\\1flat') ) @scales[hash_key] ||= new(root_pitch, scale_type) end |
Instance Method Details
#degree(degree_number) ⇒ Object
45 46 47 |
# File 'lib/head_music/scale.rb', line 45 def degree(degree_number) pitches[degree_number - 1] end |
#pitch_names(direction: :ascending, octaves: 1) ⇒ Object
37 38 39 |
# File 'lib/head_music/scale.rb', line 37 def pitch_names(direction: :ascending, octaves: 1) pitches(direction: direction, octaves: octaves).map(&:name) end |
#pitches(direction: :ascending, octaves: 1) ⇒ Object
27 28 29 30 31 |
# File 'lib/head_music/scale.rb', line 27 def pitches(direction: :ascending, octaves: 1) @pitches ||= {} @pitches[direction] ||= {} @pitches[direction][octaves] ||= determine_scale_pitches(direction, octaves) end |
#root_pitch_number ⇒ Object
41 42 43 |
# File 'lib/head_music/scale.rb', line 41 def root_pitch_number @root_pitch_number ||= root_pitch.number end |
#spellings(direction: :ascending, octaves: 1) ⇒ Object
33 34 35 |
# File 'lib/head_music/scale.rb', line 33 def spellings(direction: :ascending, octaves: 1) pitches(direction: direction, octaves: octaves).map(&:spelling).map(&:to_s) end |