-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongs.py
39 lines (32 loc) · 1.04 KB
/
songs.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
import config
import subprocess
import os
import numpy as np
from scipy.io.wavfile import read
def convert_to_wav(filepath):
path_to_ffmpeg = config.get("pathToFfmpeg")
_,file_format = os.path.splitext(filepath)
if file_format == '.mp3':
name_without_extension = os.path.basename(filepath).split(file_format)[0]
output_file = os.path.join(os.path.dirname(filepath), name_without_extension+".wav")
ret = subprocess.call([path_to_ffmpeg, '-i', filepath, output_file])
if ret > 0:
return output_file
else:
return None
elif file_format == '.wav':
return filepath
else:
return None
def stereo_to_mono(song):
song = np.reshape(song, (song.shape[0], 1))
if song.shape[1] == 2:
song = song.astype(np.int)
song = (song.sum(axis=1)/2).astype(np.int16)
return song
def pre_process(file):
wav_file = convert_to_wav(file)
if wav_file is None:
return None
rate, song = read(wav_file)
return rate, stereo_to_mono(song)