lib/head_music/scale.rb in head_music-0.20.0 vs lib/head_music/scale.rb in head_music-0.22.0
- old
+ new
@@ -1,10 +1,10 @@
# frozen_string_literal: true
# A scale contains ordered pitches starting at a tonal center.
class HeadMusic::Scale
- SCALE_REGEX = /^[A-G][#b]?\s+\w+$/
+ SCALE_REGEX = /^[A-G][#b]?\s+\w+$/.freeze
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)
@@ -13,11 +13,11 @@
[root_pitch, scale_type].join(' ').gsub(/#|♯/, 'sharp').gsub(/(\w)[b♭]/, '\\1flat')
)
@scales[hash_key] ||= new(root_pitch, scale_type)
end
- delegate :letter_name_cycle, to: :root_pitch
+ delegate :letter_name_series_ascending, :letter_name_series_descending, to: :root_pitch
attr_reader :root_pitch, :scale_type
def initialize(root_pitch, scale_type)
@root_pitch = HeadMusic::Pitch.get(root_pitch)
@@ -51,10 +51,11 @@
def determine_scale_pitches(direction, octaves)
semitones_from_root = 0
pitches = [root_pitch]
%i[ascending descending].each do |single_direction|
next unless [single_direction, :both].include?(direction)
+
(1..octaves).each do
pitches += octave_scale_pitches(single_direction, semitones_from_root)
semitones_from_root += 12 * direction_sign(single_direction)
end
end
@@ -99,19 +100,22 @@
sharp_letter_for_step(semitones_from_root)
end
def diatonic_letter_for_step(direction, step)
return unless scale_type.diatonic?
- direction == :ascending ? letter_name_cycle[step % 7] : letter_name_cycle[-step % 7]
+
+ direction == :ascending ? letter_name_series_ascending[step % 7] : letter_name_series_descending[step % 7]
end
def child_scale_letter_for_step(semitones_from_root)
return unless scale_type.parent
+
parent_scale_pitch_for(semitones_from_root).letter_name
end
def flat_letter_for_step(semitones_from_root)
return unless root_pitch.flat?
+
HeadMusic::PitchClass::FLAT_SPELLINGS[pitch_class_number(semitones_from_root)]
end
def sharp_letter_for_step(semitones_from_root)
HeadMusic::PitchClass::SHARP_SPELLINGS[pitch_class_number(semitones_from_root)]