-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrogram.py
61 lines (52 loc) · 1.5 KB
/
spectrogram.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
import os
import sys
import eyed3
import subprocess
from shutil import copyfile
from multiprocessing import Pool, Queue
eyed3.log.setLevel("ERROR")
def toMono(audiofile):
label = audiofile.tag.comments.get("label")
if not label:
print("Skipping " + audiofile.path)
return None
monoPath = "/tmp/mono-" + label.text + "-" + os.path.basename(audiofile.path)
if audiofile.info.mode == 'Mono':
copyfile(audiofile.path, monoPath)
else:
cmd = ['sox', audiofile.path, monoPath, 'remix', "1,2"]
p = subprocess.call(cmd)
return monoPath
def toSpectrogram(monoPath):
pixelsPerSec = "50"
spectPath = os.path.join("./data/labelled", os.path.basename(monoPath).replace("mp3", "png"))
cmd = [
"sox", monoPath, "-n", "spectrogram", "-Y", "200", "-X", pixelsPerSec,
"-m", "-r", "-o", spectPath
]
p = subprocess.call(cmd)
return spectPath
def process(q):
filePath = q.get(True)
while filePath:
audiofile = eyed3.load(filePath)
if audiofile:
monoPath = toMono(audiofile)
if monoPath:
spectPath = toSpectrogram(monoPath)
os.remove(monoPath)
print("Completed " + spectPath)
filePath = q.get(True)
q.put(None)
def main():
audiofiles = Queue()
pool = Pool(8, process, (audiofiles,))
for root, dirs, files in os.walk("/mnt/c/Users/tobybrad/Music/_genres"):
for f in files:
filePath = os.path.join(root, f)
audiofiles.put(filePath)
audiofiles.put(None)
pool.close()
pool.join()
if __name__ == "__main__":
main()