-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathaudio_processing.py
executable file
·56 lines (40 loc) · 1.57 KB
/
audio_processing.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
_author__ = "Patrice Guyot"
__email__ = "[email protected]"
__version__ = "1.1.0"
"""
***********************************************************************
Name : audio_processing.py
Description : The module implements a function to read audio file.
Author : Patrice Guyot.
***********************************************************************
"""
from scipy.io.wavfile import read as wavread
import logging
from os import remove, sep
import subprocess
def audio_read(audioFilePath, formatsox=False):
"""
Read an audio file (from scipy.io.wavfile)
A conversation of the aufio file can be processed using SOX (by default set at False).
:param audioFilePath: audio file name (with eventually the full path)
:type audioFilePath: str
:param formatsox: if set at True, us SOX to convert audio file in "wav file, 1 channel, 16 kHz, 16bits". Default: False
:type audioFilePath: str
:returns:
* sr: sampling rate of the signal
* sig: list of values of the signal
:rtype: tuple
"""
logging.info('Reading of the audio file : ' + audioFilePath)
if formatsox:
tmpFile = "tmp.wav"
logging.info('\t- Conversion en wav 16k with SOX.')
cmd = "sox " + audioFilePath + " -c 1 -r 16k -b 16 -G " + tmpFile + " rate -m"
subprocess.check_call(cmd)
[sr, sig] = wavread(tmpFile)
remove(tmpFile)
else:
[sr, sig] = wavread(audioFilePath)
return sr, sig