Class: HeadMusic::Scale

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_pitch, scale_type) ⇒ Scale

Returns a new instance of Scale.



22
23
24
25
# File 'lib/head_music/scale.rb', line 22

def initialize(root_pitch, scale_type)
  @root_pitch = HeadMusic::Pitch.get(root_pitch)
  @scale_type = HeadMusic::ScaleType.get(scale_type)
end

Instance Attribute Details

#root_pitchObject (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_typeObject (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_numberObject



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