/
helper_audio.py
executable file
·71 lines (52 loc) · 2.13 KB
/
helper_audio.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
# -*- coding: utf-8 -*-
"""
Functions to open audio files and to record from default microphone
:platform: Linux
:synopsis: Audio Helper
.. moduleauthor:: Dominik Schuermann <d.schuermann@tu-braunschweig.de>
"""
import logging
# get logger
log = logging.getLogger("fuzzy_pairing")
from scipy.io import wavfile
from helper_audio_decoder import read_as_array
def load_stereo(filename):
"""load stereo audio file with gstreamer
:param filename: name of file, relative path
:type filename: str
:return: left_channel -- Left channel as list
:return: right_channel -- Right channel as list
:return: samplerate -- Samplerate of audio file
"""
data, duration, channels, samplerate = read_as_array(filename)
log.debug("Load File "+filename+"\nduration: "+str(duration)+" seconds\nchannels: "+str(channels)+"\nsamplerate: "+str(samplerate))
# seperate left and right channel
left_channel = data[0,:]
right_channel = data[1,:]
return left_channel, right_channel, samplerate
def load_mono(filename):
"""load mono audio file with gstreamer
:param filename: name of file, relative path
:type filename: str
:return: data -- Channel as list
:return: samplerate -- Samplerate of audio file
"""
data, duration, channels, samplerate = read_as_array(filename)
log.debug("Load File "+filename+"\nduration: "+str(duration)+" seconds\nchannels: "+str(channels)+"\nsamplerate: "+str(samplerate))
return data, samplerate
def load_wave_with_scipy(wavFile):
"""load Wave File with build in function from Scipy
Can only open **.wav** files
.. warning:: It has problems with many codecs
:param filename: name of file, relative path
:type filename: str
:return: left_channel -- Left channel as list
:return: right_channel -- Right channel as list
:return: samplerate -- Samplerate of audio file
"""
# open stereo wavefile
samplerate, data = wavfile.read(wavFile)
# seperate left and right channel
left_channel = data[:,0]
right_channel = data[:,1]
return left_channel, right_channel, samplerate