-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAudioIO.py
43 lines (40 loc) · 1.3 KB
/
AudioIO.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
#Audio loading
import numpy as np
import subprocess
from scipy.io import wavfile
def getAudio(filename):
"""
Wrap around scipy to load audio. Since scipy only
loads wav files, call avconv through a subprocess to
convert any non-wav files to a temporary wav file,
which is removed after loading:
:param filename: Path to audio file
:return (XAudio, Fs): Audio in samples, sample rate
"""
import os
toload = filename
tempfilename = ""
if not filename[-3::] == 'wav':
tempfilename = '%s.wav'%filename[0:-4]
if os.path.exists(tempfilename):
os.remove(tempfilename)
subprocess.call(["avconv", "-i", filename, "-ar", "44100", tempfilename])
toload = tempfilename
Fs, XAudio = wavfile.read(toload)
#Convert shorts to floats
XAudio = np.array(XAudio, dtype = np.float32) / (2.0**16)
if len(XAudio.shape) > 1:
XAudio = np.mean(XAudio, 1)
if len(tempfilename) > 0:
os.remove(tempfilename)
return (XAudio, Fs)
def getAudioLibrosa(filename):
r"""
Use librosa to load audio
:param filename: Path to audio file
:return (XAudio, Fs): Audio in samples, sample rate
"""
import librosa
XAudio, Fs = librosa.load(filename)
XAudio = librosa.core.to_mono(XAudio)
return (XAudio, Fs)