forked from BryanDavis999/Ambient-Music-Gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swek.py
executable file
·172 lines (139 loc) · 5.63 KB
/
swek.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
#! /usr/bin/python3
from mido import MidiFile
import os
import math
import time
import shutil
from threading import Thread
import random
import serial
import serial.tools.list_ports
MODEL = "hierdec-mel_16bar"
BDir = "raw_buffer"
os.makedirs(BDir, exist_ok=True)
RBThresh = 40 #numeric threshold of files in the raw buffer
FDir = "final_buffer"
os.makedirs(FDir+"/high", exist_ok=True)
os.makedirs(FDir+"/low", exist_ok=True)
FBThresh = 20 #numeric threshold of files in the final buffer
TThresh = 100 #threshold to tell whether music is high or low tempo
NThresh = 10 #threshold to measure ambient noise in surroundings
playlist = "playlist"
os.makedirs(playlist, exist_ok=True)
if(os.listdir(playlist)):
PNext = int(sorted(os.listdir(playlist))[-1].split('.')[0])
else:
PNext = 1
#NOTE : Req folder Music_VAE_models containing requistite MODEL
#--------------------------------------------------------------- INIT
GenString = "music_vae_generate --config={0} --checkpoint_file=Music_VAE_models/{0}.tar --mode=sample --num_outputs={1} --output_dir={2}"
IntString = "music_vae_generate --config={0} --checkpoint_file=Music_VAE_models/{0}.tar --mode=interpolate --num_outputs={1} --input_midi_1={3} --input_midi_2={4} --output_dir={2}"
def generate(n, outDir) : os.system(GenString.format(MODEL, n, outDir))
def interpolate(n, outDir, f1, f2) : os.system(GenString.format(MODEL, n ,outDir, f1, f2))
#---------------------------------------------------------------
#--------------------------------------------------------------- THREAD 1
def fill_buffers():
while True:
Blen = len(os.listdir(BDir))
if Blen < RBThresh:
generate(RBThresh-Blen, BDir)
Hlen = len(os.listdir(FDir+"/high"))
Llen = len(os.listdir(FDir+"/low"))
mainLen = Hlen if Hlen<Llen else Llen
if mainLen<FBThresh:
cd = os.listdir(BDir)
if '.DS_Store' in cd : cd.remove('.DS_Store') #MAC specific file removal
for i in cd[:FBThresh-mainLen]:
file_loc = BDir+'/'+i
mid = MidiFile(file_loc)
x = [i.time for i in mid.tracks[1]]
th = math.floor(len(x)*0.3)
start = sum(x[:th])/th
end = sum(x[-th:])/th
avg = sum(x)/len(x)
if(start>1.4*end or end>1.4*start):
os.remove(file_loc)
else:
if avg > TThresh: os.rename(file_loc, FDir+"/high/"+i)
else: os.rename(file_loc, FDir+"/low/"+i)
print('{} : {} -> {}'.format(i,start,end), end='\t')
print("deleted" if start>1.4*end or end>1.4*start else "saved")
time.sleep(5)
t1 = Thread(target = fill_buffers)
t1.start()
#---------------------------------------------------------------
#--------------------------------------------------------------- THREAD 2
def get_arduino_port():
ports_list = list(serial.tools.list_ports.comports())
if ports_list:
for p in ports_list:
print(p)
device = str(p.description)
if device=="Generic CDC": #MAC Specific
return p.device
elif "ACM" in device: #Linux Specific
return ("/dev/" + device)
else:
print("Arduino not found.\n")
else:
print("No device found.\n")
def mood_filter():
print("\n")
#port = get_arduino_port()
#print("Arduino found in " + str(port))
#ser = serial.Serial(port, 9600)
while True:
s=0
#end_time = time.time()+1
#while time.time()<end_time:
# s = 1000
# print("sampled")
# s+=int(ser.readline().decode())
time.sleep(0.5)
s = random.random()*10 + 10
print(s)
target = "high/" if s>NThresh else "low/"
file_loc = FDir +'/'+ target
file_loc += os.listdir(file_loc)[1]
print(file_loc)
os.system("mscore -o " + "swek.pdf "+ file_loc)
os.system("xdg-open swek.pdf")
os.system("cvlc --play-and-exit "+file_loc)
# os.remove(file_loc)
shutil.move(file_loc,"playlist")
dest_loc = str(PNext)+".mid"
PNext += 1
os.rename("playlist/"+file_loc, "playlist/"+dest_loc)
# shutil.move(dest_loc, )
'''
dest_loc = playlist+"/"+str(PNext)+".mid"
PNext+=1
if len(os.listdir(playlist))==0:
os.rename(file_loc, dest_loc)
os.system("mscore - o " +str(PNext)+".pdf " + dest_loc)
os.system("lpr " +str(PNext)+".pdf")
'''
t2 = Thread(target = mood_filter)
t2.start()
#---------------------------------------------------------------
#FOR FUTURE DEVELOPMENT
'''
def interpolate_2(file1,file2):
string = "music_vae_generate --config={0} --checkpoint_file=Music_VAE_models/{0}.tar --mode=interpolate --num_outputs=10 --input_midi_1={1} --input_midi_2={2} --output_dir={3}".format(MODEL, RDir+file1, RDir+file2, IDir)
os.system(string)
def getFiles(location):
return [i for i in ( os.listdir(location) if location else os.listdir() ) if i != '.DS_Store']
def interpolate_all(inputDir, outputDir):
sList = sorted(getFiles('samples2'))
for i in range(len(sList)-2):
interpolate_2(sList[i],sList[i+1])
#filter_tempo('Trio_Samples/samples')
#filter_tempo('Trio_Samples/samples2')
#filter_tempo('samples2')
#rand_sample_10("Samples2")
#interpolate_2("/hierdec-trio_16bar_sample_2019-04-25_104923-000-of-010.mid","/hierdec-trio_16bar_sample_2019-04-25_104923-001-of-010.mid")
for i, track in enumerate(mid.tracks):
print('Track {}: {}'.format(i, track.name))
for msg in track:
print(msg)
'''