-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmml6.py
More file actions
247 lines (220 loc) · 9.31 KB
/
mmml6.py
File metadata and controls
247 lines (220 loc) · 9.31 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
6th iteration:
insert first few bars partway into the song but better
lead note lengths with markov chain
fix 7 extra lead bars.
"""
import random as r
octaveSign=''
title="Blank"
# Take a tempo between 12 and 15.
tempo=r.randint(12,15)
# Determine the song length depending on the tempo
# It should be about 50 at 12
songLength=int(4.5*tempo) #in bars
song= {"voice1":[],"voice2":[],"voice3":[],"voice4":[],"leadOctaves":[]}
whipeFlag=r.randint(0,1) # either start with only drums, or only without drums, for 16 or so lengths
#v1 is drums/noise
drums=["o4g","o6g","r","o4g","o6g"]
#the rest is just notes
#notes=["a","a+","b","c","c+","d","d+","e","f","f+","g","g+"]
#notes=["c","d","e","f","g","a","b"]
notes=["c","d","f","g","a"]
# --------------------------------------------------------Drums--------------------------------------------------------
for x in range(songLength):
# With L8, it stands to reason there should be 8 total hits.
# Time to implement the three part system again.
result="o2g"+drums[2]+"v45"
# go for the 2-4-2
credit=8
while credit>0:
# Guess a next note's length
nextLength=r.choice([4,8,16])
# If it doesn't fit, guess another
while credit-16/nextLength<0:
# 4 (quarter) isn't going to fit at this point
nextLength=r.choice([8,16])
result+=r.choice(drums)+str(nextLength)
credit-=16/nextLength
result += "v50o2g" + drums[2]
song["voice1"].append(result+' ') # Add the result to the dict.
# --------------------------------------------------------Lead--------------------------------------------------------
# This is a 2D table containing the odds for a markov chain (values need to be cumulative).
markovChain=[[5,60,90,100], # Half note
[10,50,90,100], # Quarter note
[5,70,90,100], # Eighth note
[5,40,90,100]] # Sixteenth note
# In order, these are half (2), quarter (4), eighths and sixteenths.
# If the aim is to reduce the odds of sixteenths, increase the third number.
import math
def markov(noteLength,i):
noteLength=int(math.log(noteLength,2)-1) # Determine the selector through simple base 2 maths.
while True:
c=r.randint(0,100) # Roll that die!
if c<markovChain[noteLength][0] and i<1: # Check those odds!
return 2 # Output that note length!
elif c<markovChain[noteLength][1] and i<2:
return 4
elif c<markovChain[noteLength][2] and i<3:
return 8
elif c<markovChain[noteLength][3]:
return 16
# Time to separate the algorithm for precision strikes.
def algorithm(note):
# If the previous note isn't a rest
if note != "r":
# Maybe make it a rest?
if r.randint(1, 7) == 1:
return "r"
else:
# Or don't, at which point do the magic algorithm.
return int(r.gauss(0,2.5))+notes.index(note)
else:
# The last note was a rest, so just grab a random note.
return r.randint(0,4)#int((r.gauss(0,1.5))+notes.index(song["voice2"][-2][-1]))
leadOctave=4
note=r.choice(notes)
result=''
noteLength=r.choice([2,4,8,16])
for x in range(songLength):
#for y in range(8): #
y=16
while y>0:
i=0
noteLength=markov(noteLength,i)
#while y+int(16/noteLength)>8:
while y-int(16/noteLength)<0:
i+=1
noteLength=markov(noteLength,i) # no half notes, they clearly don't fit.
#y+=int(16/noteLength)
y-=int(16/noteLength)
# Add the result from the loop, which is admittedly a bit of an unusual place.
result+=(octaveSign+note+str(noteLength)) if int(noteLength)!=16 else (octaveSign+note)
# Empty the sign, otherwise it might just go crazy.
octaveSign=''
dnote=algorithm(note) #returns either a rest or an index
if dnote != "r":
if dnote>5 and leadOctave<6: # Going up an octave.
while dnote>=5:
leadOctave+=1
octaveSign+=">"
dnote-=5
elif dnote<0 and leadOctave>3: # Going down an octave.
while dnote<0:
leadOctave-=1
octaveSign+="<"
dnote+=5
else: # It's running out of the octave bounds.
dnote=(dnote+5)%5
note=notes[dnote] # dnote is sanitized now.
else:
note=dnote # what is, how you say, type mismatch?
# Now for the note length, which can be done by looping again.
song["leadOctaves"].append(leadOctave)
# Write down the result into the dictionary.
song["voice2"].append(result+' ')
# Empty the result for recycling.
result=""
"""
# --------------------------------------------------------Harmony------------------------------------------------------
bassOctave=4
for x in range(songLength):
# Select note based on lead track note
dnote=notes.index(song["voice2"][x][-1])-5 if song["voice2"][x][0]!="r" else "r" # Check the lead note at this place and lower it by 5
if dnote!="r":
if dnote>7 and bassOctave!=leadOctave+1: # Going up an octave.
bassOctave+=1
octaveSign+=">"
dnote-=7
elif dnote<0 and bassOctave!=leadOctave-1: # Going down an octave.
bassOctave-=1
octaveSign+="<"
dnote+=7
note=notes[dnote] # dnote is accurate now.
else:
note=dnote # I do not know this type mismatch thing you speak of.
song["voice3"].append(octaveSign+note) # Add the result to the dict.
octaveSign=''
"""
# --------------------------------------------------------Bass--------------------------------------------------------
for y in range(songLength):
# Initialize the procedure.
credit=8
result=''
# Start each bar (as it were) with a random note.
note=r.choice(notes)
# Make it either half or half dotted.
if r.choice([True,False]):
note+='.'
credit-=4
# Add this note for a start
result+=note
while credit>0:
# Guess a next note's length
nextLength=r.choice([4,8,16])
# If it doesn't fit, guess another
while credit-16/nextLength<0:
# 4 (quarter) isn't going to fit at this point
nextLength=r.choice([8,16])
# Add the next note. The next note is up to two notes away, but not itself.
flag=True
while flag:
try:
result+=notes[notes.index(result[0])+r.choice([-1,-2,1,2])]+str(nextLength)
credit-=16/nextLength
flag=False
except IndexError:
# This is pretty crude, all said. It'll eventually reach a note it can use but it's terrible coding.
# Either that or it's genius because there's no need to check the input for every conceivable problem.
pass
# Add the result to the song.
song["voice4"].append(result+' ')
# ---------------------------------------------------------Output----------------------------------------------
output=";[ "+title+" ];\n\n"
output+="CH1Verse_1.s = \"t"+str(tempo)+" \\12 w5 v50 l8 " # Set up basic variables for the drum track.
for x in range(songLength):
output+=song["voice1"][x] # And then add it to the output
# Add the first three bars again after every 50 bars.
if x%10==0 and x>0:
for y in range(3):
output+=song["voice1"][y]
output+="\"\n\nCH2Verse_1.s = \"t"+str(tempo)+" \\6 w3 v50 o4 l16 " # Set up basic variables for the lead track.
w=3
for x in range(songLength):
output+=song["voice2"][x] # This should be fine as long as x remains an index.
if x%10==0 and x>0: # Add the first three bars again after every 20 bars.
w=1 if (w+1)%5==0 else (w+1)%5 # Also change the instrument (again).
output+="o4w"+str(w) # Set the octave to match the start of the song.
for y in range(3):
output+=song["voice2"][y]
output+="o"+str(song["leadOctaves"][x]) # set the octave back.
'''
output+="\"\n\nCH3Verse_1.s = \"t"+str(tempo)+" \\6 w3 v40 o4 l8 " # Set up basic variables for the track.
for x in range(songLength):
output+=song["voice3"][x]
'''
output+="\n\nCH4Verse_1.s = \"t"+str(tempo)+" \\2 w1 v50 o2 l2 " # Set up basic variables for the bass track.
for x in range(songLength):
# Add the track to the song.
output+=song["voice4"][x]
# Add the first three bars again after every 50 bars.
if x %10 == 0 and x>0:
for y in range(3):
output += song["voice4"][y]
if x==songLength/2:
output+='>'
output+="c1\""
# This next part is for multiple verses.
#output+="\n\nChannel_1.s = CH1Verse_1\nChannel_2.s = CH2Verse_1\nChannel_3.s = CH3Verse_1\nChannel_4.s = CH4Verse_1"
file=open(title+".txt","w")
file.write(output)
file.close()
"""
Macrotune MML sample
;[ The riddle ];
CH1Verse_1.s = "t12 w5 o1 \4 v40 l8r4"
CH1Verse_2.s = "o1gg>>g<<ggg>>gr"
CH2Verse_1.s = "t12 w4 o4 \4 v40 l8ef+g4gagf+edd4ef+f+4ef+g4a4gf+ede4edd4b>dc<bage4d4e2r4"
Channel_1.s = CH1Verse_1 + CH1Verse_2 + CH1Verse_2 + CH1Verse_2 + CH1Verse_2 + CH1Verse_2 + CH1Verse_2
Channel_2.s = CH2Verse_1
"""