Class: HeadMusic::Rudiment::Scale
- Inherits:
-
Object
- Object
- HeadMusic::Rudiment::Scale
- Defined in:
- lib/head_music/rudiment/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_classes ⇒ Object
- #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
#initialize(root_pitch, scale_type) ⇒ Scale
Returns a new instance of Scale.
23 24 25 26 |
# File 'lib/head_music/rudiment/scale.rb', line 23 def initialize(root_pitch, scale_type) @root_pitch = HeadMusic::Rudiment::Pitch.get(root_pitch) @scale_type = HeadMusic::Rudiment::ScaleType.get(scale_type) end |
Instance Attribute Details
#root_pitch ⇒ Object (readonly)
Returns the value of attribute root_pitch.
21 22 23 |
# File 'lib/head_music/rudiment/scale.rb', line 21 def root_pitch @root_pitch end |
#scale_type ⇒ Object (readonly)
Returns the value of attribute scale_type.
21 22 23 |
# File 'lib/head_music/rudiment/scale.rb', line 21 def scale_type @scale_type end |
Class Method Details
.get(root_pitch, scale_type = nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/head_music/rudiment/scale.rb', line 8 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::Rudiment::Pitch.get(root_pitch) scale_type = HeadMusic::Rudiment::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
50 51 52 |
# File 'lib/head_music/rudiment/scale.rb', line 50 def degree(degree_number) pitches[degree_number - 1] end |
#pitch_classes ⇒ Object
34 35 36 |
# File 'lib/head_music/rudiment/scale.rb', line 34 def pitch_classes pitches.map(&:pitch_class).uniq end |
#pitch_names(direction: :ascending, octaves: 1) ⇒ Object
42 43 44 |
# File 'lib/head_music/rudiment/scale.rb', line 42 def pitch_names(direction: :ascending, octaves: 1) pitches(direction: direction, octaves: octaves).map(&:name) end |
#pitches(direction: :ascending, octaves: 1) ⇒ Object
28 29 30 31 32 |
# File 'lib/head_music/rudiment/scale.rb', line 28 def pitches(direction: :ascending, octaves: 1) @pitches ||= {} @pitches[direction] ||= {} @pitches[direction][octaves] ||= determine_scale_pitches(direction, octaves) end |
#root_pitch_number ⇒ Object
46 47 48 |
# File 'lib/head_music/rudiment/scale.rb', line 46 def root_pitch_number @root_pitch_number ||= root_pitch.number end |
#spellings(direction: :ascending, octaves: 1) ⇒ Object
38 39 40 |
# File 'lib/head_music/rudiment/scale.rb', line 38 def spellings(direction: :ascending, octaves: 1) pitches(direction: direction, octaves: octaves).map(&:spelling).map(&:to_s) end |