2023-10-01_musical-scale-modes-table.html


001| #:name::Musical Scale Modes Table
002| #:link::https://thehardcorecoder.com/2023/10/01/musical-scale-modes-table/
003| #:date::2023-10-01
004| #:code::
005| from sys import stdout
006| 
007| ModeNames = [
008|     'Lydian','Ionian','Mixolydian','Dorian',
009|     'Aeolian','Phrygian','Locrian'
010| ]
011| 
012| ModeIntervals = [
013|     [1, 2, 2, 2, 1, 2, 2, 1],   # Lydian
014|     [1, 2, 2, 1, 2, 2, 2, 1],   # Ionian
015|     [2, 2, 2, 1, 2, 2, 1, 2],   # Mixolydian
016|     [2, 2, 1, 2, 2, 2, 1, 2],   # Dorian
017|     [2, 2, 1, 2, 2, 1, 2, 2],   # Aeolian
018|     [2, 1, 2, 2, 2, 1, 2, 2],   # Phrygian
019|     [2, 1, 2, 2, 1, 2, 2, 2],   # Locrian
020| ]
021| 
022| Flat  = '\u266d'
023| Sharp = '\u266f'
024| Flat2  = '\u266d\u266d'
025| Sharp2 = '\u266f\u266f' #'\U0001D12a'
026| 
027| BaseNotes = [
028|     'A','B'+Flat,'B','C','C'+Sharp,'D',
029|     'E'+Flat,'E','F','F'+Sharp,'G','G'+Sharp
030| ]
031| 
032| NoteTable = {
033|     'A':('A'+Flat2,'A'+Flat,'A','A'+Sharp,'A'+Sharp2),
034|     'B':('B'+Flat2,'B'+Flat,'B','B'+Sharp,'B'+Sharp2),
035|     'C':('C'+Flat2,'C'+Flat,'C','C'+Sharp,'C'+Sharp2),
036|     'D':('D'+Flat2,'D'+Flat,'D','D'+Sharp,'D'+Sharp2),
037|     'E':('E'+Flat2,'E'+Flat,'E','E'+Sharp,'E'+Sharp2),
038|     'F':('F'+Flat2,'F'+Flat,'F','F'+Sharp,'F'+Sharp2),
039|     'G':('G'+Flat2,'G'+Flat,'G','G'+Sharp,'G'+Sharp2),
040| }
041| def Note2Str (t):
042|     base = t[0]
043|     more = 2 + t[1]
044|     return NoteTable[base][more]
045| 
046| def Str2Note (nt):
047|     base = nt[0]
048|     more = nt[1:2]
049|     if more == Sharp: return (base, +1)
050|     if more == Flat: return (base, -1)
051|     return (base, 0)
052| 
053| def NoteAbove (nt):
054|     if nt == 'G': return 'A'
055|     return chr(ord(nt)+1)
056| 
057| def generate_scale (first_note=('C',0), mode_index=0):
058|     mode = ModeIntervals[mode_index]
059|     mode_name = ModeNames[mode_index]
060| 
061|     def next_note (nt, d):
062|         nxnt = NoteAbove(nt[0])
063|         if nt[0] in ['B', 'E']:
064|             flag = nt[1]+(d-1)
065|         else:
066|             flag = nt[1]-(2-d)
067|         return (nxnt, flag)
068| 
069|     scale = [first_note]
070|     for dist in mode[1:7]:
071|         note = next_note(scale[-1], dist)
072|         scale.append(note)
073|     return (mode_name, [Note2Str(n) for n in scale])
074| 
075| def generate_section (first_note=('C',0)):
076|     scales = []
077|     for mode in range(7):
078|         notes = generate_scale(first_note, mode)
079|         scales.append(notes)
080|     return scales
081| 
082| def generate_table ():
083|     table = []
084|     for key in [3,2,1,0,11,10,9,8,7,6,5,4]:
085|         note = Str2Note(BaseNotes[key])
086|         sect = generate_section(note)
087|         table.append(sect)
088|     return table
089| 
090| fmt = '%-11s| %-3s %-3s %-3s %-3s %-3s %-3s %-3s|'
091| 
092| def display_table (table, stream=stdout):
093|     for section in table:
094|         for mode,scale in section:
095|             print(fmt % (mode,*scale), file=stream)
096|         print(file=stream)
097|     print(file=stream)
098| 
099| table = generate_table()
100| display_table(table)
101| 
102| #:code::
103| def generate_scale (first_note=('C',0), mode_index=0):
104|     mode = ModeIntervals[mode_index]
105|     mode_name = ModeNames[mode_index]
106| 
107|     def next_note (nt, d):
108|         nxnt = NoteAbove(nt[0])
109|         if nt[0] in ['B', 'E']:
110|             flag = nt[1]+(d-1)
111|         else:
112|             flag = nt[1]-(2-d)
113|         return (nxnt, flag)
114| 
115|     scale = [first_note]
116|     for dist in mode[1:7]:
117|         note = next_note(scale[-1], dist)
118|         scale.append(note)
119|     return (mode_name, [Note2Str(n) for n in scale])
120| 
121| #:end:code::
122|