Module: MusicUtils::Scales

Included in:
MusicUtils, Interval
Defined in:
lib/music-utils/scales/scales.rb

Overview

Scales module

Constant Summary collapse

DO =

Notes

:do
RE =
:re
MI =
:mi
FA =
:fa
SOL =
:sol
LA =
:la
SI =
:si
DIATONIC_SCALE =
[DO, RE, MI, FA, SOL, LA, SI]
DIATONIC_SCALE_AUX =
[DO, DO, RE, RE, MI, FA, FA, SOL, SOL, LA, LA, SI]
FLAT =

Alterations:

'f'
DFLAT =
FLAT + FLAT
SHARP =
's'
DSHARP =
SHARP + SHARP
DOF =

Altered notes

(DO.to_s + FLAT).to_sym
DOFF =
(DO.to_s + DFLAT).to_sym
DOS =
(DO.to_s + SHARP).to_sym
DOSS =
(DO.to_s + DSHARP).to_sym
REF =
(RE.to_s + FLAT).to_sym
REFF =
(RE.to_s + DFLAT).to_sym
RES =
(RE.to_s + SHARP).to_sym
RESS =
(RE.to_s + DSHARP).to_sym
MIF =
(MI.to_s + FLAT).to_sym
MIFF =
(MI.to_s + DFLAT).to_sym
MIS =
(MI.to_s + SHARP).to_sym
MISS =
(MI.to_s + DSHARP).to_sym
FAF =
(FA.to_s + FLAT).to_sym
FAFF =
(FA.to_s + DFLAT).to_sym
FAS =
(FA.to_s + SHARP).to_sym
FASS =
(FA.to_s + DSHARP).to_sym
SOLF =
(SOL.to_s + FLAT).to_sym
SOLFF =
(SOL.to_s + DFLAT).to_sym
SOLS =
(SOL.to_s + SHARP).to_sym
SOLSS =
(SOL.to_s + DSHARP).to_sym
LAF =
(LA.to_s + FLAT).to_sym
LAFF =
(LA.to_s + DFLAT).to_sym
LAS =
(LA.to_s + SHARP).to_sym
LASS =
(LA.to_s + DSHARP).to_sym
SIF =
(SI.to_s + FLAT).to_sym
SIFF =
(SI.to_s + DFLAT).to_sym
SIS =
(SI.to_s + SHARP).to_sym
SISS =
(SI.to_s + DSHARP).to_sym
CROMATIC_SCALE =
[[SIS, DO, REFF], 
[SISS, DOS, REF], 
[DOSS, RE, MIFF], 
[RES, MIF, FAFF], 
[RESS, MI, FAF], 
[MIS, FA, SOLFF], 
[MISS, FAS, SOLF], 
[FASS, SOL, LAFF], 
[SOLS, LAF], 
[SOLSS, LA, SIFF], 
[LAS, SIF], 
[LASS, SI, DOF]]
MAJ_SCALE =
[2, 2, 1, 2, 2, 2, 1]
NATURAL_MIN_SCALE =
[2, 1, 2, 2, 1, 2, 2]
MELODIC_MIN_SCALE =
[2, 1, 2, 2, 1, 2, 1]
PENTATONIC_MAJ =
[2, 2, 3, 2, 3]
PERF =

Qualities

'P'
MAJ =
'M'
MIN =
'm'
AUG =
'A'
AUGP =
AUG + AUG
DIM =
'd'
DIMP =
DIM + DIM
QUALITIES =

Qualities hash

{
  2 => {0 => DIM, 1 => MIN, 2 => MAJ, 3 => AUG, 4 => AUGP},
  3 => {1 => DIMP, 2 => DIM, 3 => MIN, 4 => MAJ, 5 => AUG, 6 => AUGP},
  4 => {3 => DIMP, 4 => DIM, 5 => PERF, 6 => AUG, 7 => AUGP},
  5 => {5 => DIMP, 6 => DIM, 7 => PERF, 8 => AUG, 9 => AUGP},
  6 => {7 => DIMP, 8 => DIM, 9 => MIN, 10 => MAJ, 11 => AUG, 12 => AUGP},
  7 => {8 => DIMP, 9 => DIM, 10 => MIN, 11 => MAJ, 12 => AUG, 13 => AUGP},
  8 => {10 => DIMP, 11 => DIM, 12 => PERF, 13 => AUG, 14 => AUGP}
}

Class Method Summary collapse

Class Method Details

.cromatic_index(from) ⇒ Object

Returns index of the note in the cromatic scale



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/music-utils/scales/scales.rb', line 142

def Scales.cromatic_index(from)
  i = 0
  find_it = false
  CROMATIC_SCALE.each do |e|
    if e.is_a?(Array)
      e.each do |ee|
        find_it = true if from == ee
      end
    else
      find_it = true if from == e
    end
    break if find_it
    i += 1
  end
  i
end

.diatonic_aux_scale_from(from) ⇒ Object

Create a diatonic scale starting with the “from” note with notes dups



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/music-utils/scales/scales.rb', line 161

def Scales.diatonic_aux_scale_from(from)
  diatonic_scale = []
  length = DIATONIC_SCALE_AUX.length
  i = DIATONIC_SCALE_AUX.index(from)
  c = 0
  while c < length
    diatonic_scale << DIATONIC_SCALE_AUX[i]
    i += 1
    c += 1
    if i > length - 1
      i = 0
    end 
  end
  diatonic_scale
end

.diatonic_scale_from(from) ⇒ Object

Create a diatonic scale starting with the “from” note



137
138
139
# File 'lib/music-utils/scales/scales.rb', line 137

def Scales.diatonic_scale_from(from)
  diatonic_aux_scale_from(from).uniq
end

.scale(from, scale_struct) ⇒ Object

Create scale from a note and scale structure



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/music-utils/scales/scales.rb', line 99

def Scales.scale(from, scale_struct)
  from = from.to_sym
  
  i = cromatic_index(from)
  
  scale = []
  scale << from

  from_note, from_alter = MusicUtils::Note.parse(from)
  
  #diatonic_scale = diatonic_scale_from(from_note)
  diatonic_scale = scale_from(from_note, scale_struct)
  diatonic_scale.delete(from_note)

  length = CROMATIC_SCALE.length

  scale_struct.each do |shift|
    if i + shift > length - 1
      i = (i + shift) - (length)
      shift = 0
    end

    CROMATIC_SCALE[i + shift].each do |e|
      e_note, e_alter = MusicUtils::Note.parse(e)
        
      if diatonic_scale.first == e_note
        scale << e
        diatonic_scale.delete(diatonic_scale.first)
        break
      end 
    end
    i += shift
  end

  scale
end

.scale_from(from_note, scale_struct) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/music-utils/scales/scales.rb', line 177

def Scales.scale_from(from_note, scale_struct)
  i = 0
  c = 0
  scale = []
  scale << from_note
  
  ds = diatonic_aux_scale_from(from_note)
  
  while c < scale_struct.size
    i += scale_struct[c]
    if scale.last != ds[i]
      scale << ds[i]  
    else
      scale << ds[i + 1]
    end
      c += 1
  end
  scale
end