This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
/
generate.py
70 lines (67 loc) · 1.83 KB
/
generate.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
import math
from pyknon.MidiFile import MIDIFile
ntracks = 8
m = MIDIFile(ntracks)
length = 1024
prev = [-1]*ntracks
def add(trk,chn,note,time,dur,vol,rep=False):
if time >= length:
return
if rep or note != prev[trk]:
m.addNote(trk,chn,note,time,dur,vol)
else:
idx = -1
while 'duration' not in dir(m.tracks[trk].eventList[idx]):
idx -= 1
m.tracks[trk].eventList[idx].duration += dur
prev[trk] = note
def popcnt(x):
return bin(x).count('1')
def toscale(scale, note):
poct = popcnt(scale)
n = note%(poct+1)
c = 0
o = 0
for i in format(scale,'012b'):
if i == '1':
c += 1
if c-1 == n:
break
o += 1
return o + note//12*12
scale = int('101001010100',2)
# set instruments
m.addProgramChange(0,0,0,5)
m.addProgramChange(1,1,0,63)
m.addProgramChange(2,2,0,5)
m.addProgramChange(3,3,0,5)
m.addProgramChange(4,4,0,5)
m.addProgramChange(5,5,0,116)
m.addProgramChange(6,6,0,117)
m.addProgramChange(7,7,0,118)
t = 0
dt = 0
for _ in range(65536):
if dt%64==0:
shift=1
scale = (scale >> (12-shift)) | ((scale & ((1<<11)-1))<<shift)
sh = 4
p = (t&t//5|t//16&t//64)%48+44
add(0,0,toscale(scale,p),t/16,sh/16,70)
p = (t//100&t//64)%48+32
add(1,1,toscale(scale,p),t/16,sh/16,127)
p = (t&t//3|t//17&t//65)%48+44
add(2,2,toscale(scale,p),t/16,sh/16,70)
p = (t&t//2|t//18&t//66)%48+44
add(3,3,toscale(scale,p),t/16,sh/16,70)
p = (t&t//1|t//19&t//67)%48+44
add(4,4,toscale(scale,p),t/16,sh/16,70)
p = (t//100&t//64)%48+32
add(5,5,toscale(scale,p),t/16,sh/16,120)
p = (t&t//16|t//75&t//67)%36+32
add(6,6,toscale(scale,p),t/16,sh/16,70)
p = (t&t//34|t//78&t//64)%24+32
add(7,7,toscale(scale,p),t/16,sh/16,70)
t += sh
dt += 1
m.writeFile(open('out.mid','wb'))