-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
gen.py
executable file
·179 lines (155 loc) · 5.42 KB
/
gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
# Copyright (c) 2019-2023 Ludovic Drolez
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# To build the pack you will need the modified python-mingus library:
# https://github.com/ldrolez/python-mingus.git
# It seems that python-mingus is not maintained and a few important
# chords are missing for some progressions.
import os
import mingus.core.scales as scales
import sys
import re
from src.chords2midi import c2m
from chords import *
# output dir
out = "output"
# Basic keys
keys = [
('C', 'A'), # nothing
#('C#', 'a#') # 7 #
('Db', 'Bb'), # 5 b
('D', 'B'), # 2 #
('Eb', 'C'), # 3 b
('E', 'C#'), # 4 #
('F', 'D'), # 1 b
#('F#', 'd#'), # 6 #
('Gb', 'Eb'), # 6 b
('G', 'E'), # 1 #
('Ab', 'F'), # 4 b
('A', 'F#'), # 3 #
('Bb', 'G'), # 2 b
('B', 'G#'), # 5 #
# ('Cb', 'ab'), # 7 b
]
# Chords in Major and minor scales
deg_maj = ['I', 'ii', 'iii', 'IV', 'V', 'vi', 'vii']
deg_min = ['i', 'ii', 'III', 'iv', 'v', 'VI', 'VII']
# progressions styles
styles = [ '', 'basic4', 'alt4', 'hiphop' ]
# Test mode
if len(sys.argv) > 1 and sys.argv[1] == '--test':
keys = [ ('C', 'A') ]
#styles = [ '' ]
#
# Generate a single chord
#
def gen(dir, key, chords, prefix):
if not os.path.exists(dir):
os.makedirs(dir)
c2m_obj = c2m.Chords2Midi()
c2m_obj.handle([f"{chords}", "-t", "5", "-p", "long", "-d", "4",
"-B", "--key", f"{key}", "-N", f"{prefix} - {chords}", "--output",
f"{dir}/{prefix} - {chords}.mid"])
#
# Generate a chord progression
#
def genprog(dir, key, chords, prefix, style = ''):
c2m_obj = c2m.Chords2Midi()
# two spaces to insert a rest
newchords = re.sub(r' ', ' X ', chords)
args = newchords.split(" ")
if style != '':
args.extend(["-p", style])
dir = dir + "/" + style + " style"
elif re.search(r' X ', newchords):
# if there are rests
args.extend(["-d", "2", "-p", "basic"])
else:
# no rests
args.extend(["-d", "4", "-p", "long"])
args.extend(["-t", "5", "-B",
"--key", f"{key}", "-N", f"{prefix} - {chords}", "--output",
f"{dir}/{prefix} - {chords}.mid"])
if not os.path.exists(dir):
os.makedirs(dir)
c2m_obj.handle(args)
num = 1
# Iterate for each key
for key in keys:
root_maj = key[0]
root_min = key[1]
scale_maj = scales.Major(root_maj).ascending()
scale_min = scales.NaturalMinor(root_min).ascending()
base = f'{out}/{num:02} - {root_maj} Major - {root_min} minor'
# Major triads
i = 0
for n in ['', 'm', 'm', '', '', 'm', 'dim']:
chord = scale_maj[i] + n
gen(f'{base}/1 Triad/Major', root_maj, chord, deg_maj[i])
i = i + 1
# Minor triads
i = 0
for n in ['m', 'dim', '', 'm', 'm', '', '']:
chord = scale_min[i] + n
gen(f'{base}/1 Triad/Minor', root_min, chord, deg_min[i])
i = i + 1
# Major 7th
i = 0
for n in [['M7', 'M9'], ['m7', 'm9'], ['m7', 'm9'],
['M7', 'M9'], ['7', '9'], ['m7', 'm9'],
['m7-5', 'm7b9b5']]:
for c in n:
chord = scale_maj[i] + c
gen(f'{base}/2 7th and 9th/Major', root_maj, chord, deg_maj[i])
i = i + 1
# Minor 7th
i = 0
for n in [['m7', 'm9'], ['m7-5', 'm7b9b5'], ['M7', 'M9'],
['m7', 'm9'], ['m7', 'm9'],
['M7', 'M9'], ['7', '9']]:
for c in n:
chord = scale_min[i] + c
gen(f'{base}/2 7th and 9th/Minor', root_min, chord, deg_min[i])
i = i + 1
# All Other chords
i = 0
for c in [1, 2, 3, 4, 5, 6, 7]:
if c == 1 or c == 4 or c == 5:
chord_types = chord_types_maj
else:
chord_types = chord_types_min
for n in chord_types:
chord = scale_maj[i] + n
gen(f'{base}/3 All chords/', root_maj, chord, deg_maj[i]
+ '-' + deg_min[(i+2) % 7])
i = i + 1
# Major progressions
for style in styles:
for n in prog_maj:
genprog(f'{base}/4 Progression/Major', root_maj, n, root_maj, style)
# Minor progressions
for style in styles:
for n in prog_min:
genprog(f'{base}/4 Progression/Minor', root_min.lower(), n, root_min, style)
# Modal progressions
for style in styles:
for n in prog_modal:
genprog(f'{base}/4 Progression/Modal', root_maj, n, root_maj, style)
# next key
num = num + 1