-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen_orch.py
225 lines (176 loc) · 5.72 KB
/
open_orch.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
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
#!/usr/bin/env python
import os
import sys
import subprocess
import string
import zipfile
import requests
import json
import struct
import re
import shutil
import trim
import sfz
###########################
#sort phil sample by length
###########################
def lgth_filter(filename):
elem = filename.split("_")
if re.match("[0-9]+", elem[2]):
return True
else:
return False
def sort_lgth(filename):
path=""
elem = os.path.basename(filename).split("_")
if elem[2] == "025":
path = "/very-short"
elif elem[2] == "05":
path = "/short"
elif elem[2] == "1":
path = "/long"
elif elem[2] == "15":
path = "/very-long"
if "pizz" in elem[-1]:
path += "_pizz"
if "normal" in elem[-1]:
path += "/"
else:
path += "_" + os.path.splitext(elem[-1])[0] + "/"
return path
##########
#Main Code
##########
instru_group = ["brass", "wood", "string", "perc"]
if len(sys.argv) < 2:
print("Give a json sample dictionary please")
exit()
print("Using orchestra ", str(sys.argv[1]))
############
#Get samples
############
#open json dict for corresponding orchestra
with open(str(sys.argv[1])) as data_file:
oodict = json.load(data_file)
tmp_dir = oodict['temp directory']
dwnld_dir = oodict['download directory']
out_dir = oodict['output directory']
#check temp and dwlnd directory
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
if not os.path.exists(dwnld_dir):
os.makedirs(dwnld_dir)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
shutil.copyfile("./" + oodict['license'], out_dir + "/License")
#get trim algorithm choice
if(len(sys.argv) > 2):
trimA = sys.argv[2]
else:
trimA = "NRJ"
if(len(sys.argv) > 3):
sensitivity = sys.argv[3]
else:
sensitivity = 0.05
for grp in instru_group :
if grp in oodict:
for instru in oodict[grp] :
############################
#Downlad and extract archive
############################
#zip destination file
dst_file = dwnld_dir + os.path.basename(instru["url"]).replace("%20", "_")
if not os.path.exists(dst_file) :
#Download
print("Downloading ", oodict["input url"], instru["url"] )
response = requests.get(oodict["input url"] + instru["url"], stream=True)
with open(dst_file, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
#Create output dir for unzipped sample
xtract_dir = tmp_dir + "/xtract/" + grp + "/" + instru["name"] + "/"
if not os.path.exists(xtract_dir):
os.makedirs(xtract_dir)
#Extract
if not os.listdir(xtract_dir):
print("Extract ", dst_file)
file_zip = zipfile.ZipFile(dst_file, "r")
file_zip.extractall(xtract_dir)
file_zip.close()
###########
#Transcode
###########
#Create output dir for transcoded sample
wav_sample_dir = tmp_dir + "/transcode/" + grp + "/" + instru["name"] + "/"
if not os.path.exists(wav_sample_dir):
os.makedirs(wav_sample_dir)
#list dir recursively
audiofile = list()
for inifile in os.listdir(xtract_dir):
if (inifile==".") or (inifile==".."):
continue
if os.path.isdir(xtract_dir + inifile):
for iniifile in os.listdir(xtract_dir + inifile):
if (inifile==".") or (inifile==".."):
continue
audiofile.append(inifile + "/" + iniifile)
else:
audiofile.append(inifile)
#Transcode
for i, infile in enumerate(audiofile):
print("transcoding : ", infile)
#if oodict["key"] == "phil":
if instru["sort"] == "lgth":
if not lgth_filter(infile):
print("Jump file as it's not a note")
continue
if (infile==".") or (infile==".."):
continue
outfile = wav_sample_dir + os.path.splitext(os.path.basename(infile))[0] + ".wav"
try:
cmd = ["sox", xtract_dir + infile, outfile]
#print("command :", str(cmd))
subprocess.call(cmd)
except OSError as e:
print("SOX programm for converting audio files has encounter a problem : ")
print("ERRNO ", str(e.errno), " : ", e.strerror)
exit()
###############
#Blank remover
###############
#Create output dir for trimmed sample
sfz_sample_dir = out_dir + "/" + grp + "/" + instru["name"] + "/"
if not os.path.exists(sfz_sample_dir):
os.makedirs(sfz_sample_dir)
lgth = ""
if instru["sort"] == "lgth":
lgth = sort_lgth(outfile)
if not os.path.exists(sfz_sample_dir + lgth):
os.makedirs(sfz_sample_dir + lgth)
#more perc than midi note
if instru["sort"] == "perc_cut":
if i <= 60: lgth = "/1/"
if i > 60 and i <= 120: lgth = "/2/"
if i > 120: lgth = "/3/"
if not os.path.exists(sfz_sample_dir + lgth):
os.makedirs(sfz_sample_dir + lgth)
#trim with SOX
blank_out_file = sfz_sample_dir + lgth + os.path.splitext(os.path.basename(outfile))[0] + ".wav"
if trimA == "SOX":
try:
cmd = ["sox", outfile, blank_out_file, " silence ", sensitivity, " 1%"]
#print("command :", str(cmd))
subprocess.call(cmd)
except OSError as e:
print("SOX programm for trimming audio files has encounter a problem : ")
print("ERRNO ", str(e.errno), " : ", e.strerror)
exit()
elif trimA == "Simple":
trim.SimpleTrim(out_file, blank_out_file)
elif trimA == "NRJ":
trim.NRJTrim(outfile, blank_out_file)
else:
trim.NRJTrim(outfile, blank_out_file)
##################
#SFZ file writing
##################
sfz.create_file(out_dir, grp, instru, oodict)