forked from Js-Mim/aes_wimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOMethods.py
384 lines (333 loc) · 13 KB
/
IOMethods.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: utf-8 -*-
__author__ = 'S.I. Mimilakis'
__copyright__ = 'MacSeNet'
import os, subprocess, csv
import numpy as np
import wave as _wave
from scipy.io.wavfile import write, read
from sys import platform
class AudioIO:
""" Class for handling audio input/output operations.
It supports reading and writing of various audio formats
via 'audioRead' & 'audioWrite' methods. Moreover playback
can be performed by using 'sound' method. For formats
different than '.wav' a coder is needed. In this case
libffmpeg is being used, where the absolute path of
the static build should be given to the class variable.
Finally, energy normalisation and anti-clipping methods
are also covered in the last two methods.
Basic Usage examples:
Import the class :
import IOMethods as IO
-For loading wav files:
x, fs = IO.AudioIO.wavRead('myWavFile.wav', mono = True)
-In case that compressed files are about to be read specify
the path to the libffmpeg library by changing the 'pathToffmpeg'
variable and then type:
x, fs = IO.AudioIO.audioRead()
-For writing wav files:
IO.AudioIO.audioWrite(x, fs, 16, 'myNewWavFile.wav', 'wav')
-For listening wav files:
IO.AudioIO.sound(x,fs)
"""
# Normalisation parameters for wavreading and writing
normFact = {'int8' : (2**7) -1,
'int16': (2**15)-1,
'int24': (2**23)-1,
'int32': (2**31)-1,
'int64': (2**63)-1,
'float32': 1.0,
'float64': 1.0}
# 'Silence' the bash output
FNULL = open(os.devnull, 'w')
# Absolute path needed here
pathToffmpeg = '/home/mis/Documents/Python/Projects/SourceSeparation/MiscFiles'
def __init__(self):
pass
@staticmethod
def audioRead(fileName, mono=False):
""" Function to load audio files such as *.mp3, *.au, *.wma & *.aiff.
It first converts them to .wav and reads them with the methods below.
Currently, it uses a static build of ffmpeg.
Args:
fileName: (str) Absolute filename of WAV file
mono: (bool) Switch if samples should be converted to mono
Returns:
samples: (np array) Audio samples (between [-1,1]
(if stereo: numSamples x numChannels,
if mono: numSamples)
sampleRate: (float): Sampling frequency [Hz]
"""
# Get the absolute path
fileName = os.path.abspath(fileName)
# Linux
if (platform == "linux") or (platform == "linux2"):
convDict = {
'mp3':[os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux')
+ ' -i ' + fileName + ' ', -3],
'au': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux')
+ ' -i ' + fileName + ' ', -2],
'wma':[os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux')
+ ' -i ' + fileName + ' ', -3],
'aiff': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux')
+ ' -i ' + fileName + ' ', -4]
}
# MacOSX
elif (platform == "darwin"):
convDict = {
'mp3':[os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx')
+ ' -i ' + fileName + ' ', -3],
'au': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx')
+ ' -i ' + fileName + ' ', -2],
'wma':[os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx')
+ ' -i ' + fileName + ' ', -3],
'aiff': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx')
+ ' -i ' + fileName + ' ', -4]
}
# Add windows support!
else :
raise Exception('This OS is not supported.')
# Construct
if fileName[convDict['mp3'][1]:] == 'mp3':
print(fileName[convDict['mp3'][1]:])
modfileName = os.path.join(os.path.abspath(fileName[:convDict['mp3'][1]] + 'wav'))
subprocess.call(convDict['mp3'][0]+modfileName, shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
samples, sampleRate = AudioIO.wavRead(modfileName, mono)
os.remove(modfileName)
elif fileName[convDict['au'][1]:] == 'au':
print(fileName[convDict['au'][1]:])
modfileName = os.path.join(os.path.abspath(fileName[:convDict['au'][1]] + 'wav'))
subprocess.call(convDict['au'][0]+modfileName, shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
samples, sampleRate = AudioIO.wavRead(modfileName, mono)
os.remove(modfileName)
elif fileName[convDict['wma'][1]:] == 'wma':
print(fileName[convDict['wma'][1]:])
modfileName = os.path.join(os.path.abspath(fileName[:convDict['wma'][1]] + 'wav'))
subprocess.call(convDict['wma'][0]+modfileName, shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
samples, sampleRate = AudioIO.wavRead(modfileName, mono)
os.remove(modfileName)
elif fileName[convDict['aiff'][1]:] == 'aiff':
print(fileName[convDict['aiff'][1]:])
modfileName = os.path.join(os.path.abspath(fileName[:convDict['aiff'][1]] + 'wav'))
subprocess.call(convDict['aiff'][0]+modfileName, shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
samples, sampleRate = AudioIO.wavRead(modfileName, mono)
os.remove(modfileName)
else :
raise Exception('This format is not supported.')
return samples, sampleRate
@staticmethod
def audioWrite(y, fs, nbits, audioFile, format):
""" Write samples to WAV file and then converts to selected
format using ffmpeg.
Args:
samples: (ndarray / 2D ndarray) (floating point) sample vector
mono: DIM: nSamples
stereo: DIM: nSamples x nChannels
fs: (int) Sample rate in Hz
nBits: (int) Number of bits
audioFile: (string) WAV file name to write
format: (string) Selected format
'mp3' : Writes to .mp3
'wma' : Writes to .wma
'wav' : Writes to .wav
'aiff' : Writes to .aiff
'au' : Writes to .au
"""
# Linux
if (platform == "linux") or (platform == "linux2"):
convDict = {
'mp3': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux') + ' -i ', -3],
'au': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux') + ' -i ', -2],
'wma': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux') + ' -i ', -3],
'aiff': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_linux') + ' -i ', -4]
}
# MacOSX
elif (platform == "darwin"):
convDict = {
'mp3': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx') + ' -i ', -3],
'au': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx') + ' -i ', -2],
'wma': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx') + ' -i ', -3],
'aiff': [os.path.join(AudioIO.pathToffmpeg, 'ffmpeg_osx') + ' -i ', -4]
}
# Add windows support!
else :
raise Exception('This OS is not supported.')
if (format == 'mp3'):
prmfileName = os.path.join(os.path.abspath(audioFile[:convDict['mp3'][1]] + 'wav'))
AudioIO.wavWrite(y, fs, nbits, prmfileName)
subprocess.call(convDict['mp3'][0] + prmfileName + ' ' + audioFile,
shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
os.remove(prmfileName)
elif (format == 'wav'):
AudioIO.wavWrite(y, fs, nbits, audioFile)
elif (format == 'wma'):
prmfileName = os.path.join(os.path.abspath(audioFile[:convDict['wma'][1]] + 'wav'))
AudioIO.wavWrite(y, fs, nbits, prmfileName)
subprocess.call(convDict['wma'][0] + prmfileName + ' ' + audioFile,
shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
os.remove(prmfileName)
elif (format == 'aiff'):
prmfileName = os.path.join(os.path.abspath(audioFile[:convDict['aiff'][1]] + 'wav'))
AudioIO.wavWrite(y, fs, nbits, prmfileName)
subprocess.call(convDict['aiff'][0] + prmfileName + ' ' + audioFile,
shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
os.remove(prmfileName)
elif (format == 'au'):
prmfileName = os.path.join(os.path.abspath(audioFile[:convDict['au'][1]] + 'wav'))
AudioIO.wavWrite(y, fs, nbits, prmfileName)
subprocess.call(convDict['au'][0] + prmfileName + ' ' + audioFile,
shell = True, stdout=AudioIO.FNULL, stderr=subprocess.STDOUT)
os.remove(prmfileName)
else :
raise Exception('This format is not supported.')
@staticmethod
def wavRead(fileName, mono=False):
""" Function to load WAV file.
Args:
fileName: (str) Absolute filename of WAV file
mono: (bool) Switch if samples should be converted to mono
Returns:
samples: (np array) Audio samples (between [-1,1]
(if stereo: numSamples x numChannels,
if mono: numSamples)
sampleRate: (float): Sampling frequency [Hz]
"""
try:
samples, sampleRate = AudioIO._loadWAVWithWave(fileName)
sWidth = _wave.open(fileName).getsampwidth()
if sWidth == 1:
#print('8bit case')
samples = samples.astype(float) / AudioIO.normFact['int8'] - 1.0
elif sWidth == 2:
#print('16bit case')
samples = samples.astype(float) / AudioIO.normFact['int16']
elif sWidth == 3:
#print('24bit case')
samples = samples.astype(float) / AudioIO.normFact['int24']
except:
#print('32bit case')
samples, sampleRate = AudioIO._loadWAVWithScipy(fileName)
# mono conversion
if mono:
if samples.ndim == 2 and samples.shape[1] > 1:
samples = (samples[:, 0] + samples[:, 1])*0.5
return samples, sampleRate
@staticmethod
def _loadWAVWithWave(fileName):
""" Load samples & sample rate from 24 bit WAV file """
wav = _wave.open(fileName)
rate = wav.getframerate()
nchannels = wav.getnchannels()
sampwidth = wav.getsampwidth()
nframes = wav.getnframes()
data = wav.readframes(nframes)
wav.close()
array = AudioIO._wav2array(nchannels, sampwidth, data)
return array, rate
@staticmethod
def _loadWAVWithScipy(fileName):
""" Load samples & sample rate from WAV file """
inputData = read(fileName)
samples = inputData[1]
sampleRate = inputData[0]
return samples, sampleRate
@staticmethod
def _wav2array(nchannels, sampwidth, data):
"""data must be the string containing the bytes from the wav file."""
num_samples, remainder = divmod(len(data), sampwidth * nchannels)
if remainder > 0:
raise ValueError('The length of data is not a multiple of '
'sampwidth * num_channels.')
if sampwidth > 4:
raise ValueError("sampwidth must not be greater than 4.")
if sampwidth == 3:
a = np.empty((num_samples, nchannels, 4), dtype = np.uint8)
raw_bytes = np.fromstring(data, dtype = np.uint8)
a[:, :, :sampwidth] = raw_bytes.reshape(-1, nchannels, sampwidth)
a[:, :, sampwidth:] = (a[:, :, sampwidth - 1:sampwidth] >> 7) * 255
result = a.view('<i4').reshape(a.shape[:-1])
else:
# 8 bit samples are stored as unsigned ints; others as signed ints.
dt_char = 'u' if sampwidth == 1 else 'i'
a = np.fromstring(data, dtype='<%s%d' % (dt_char, sampwidth))
result = a.reshape(-1, nchannels)
return result
@staticmethod
def wavWrite(y, fs, nbits, audioFile):
""" Write samples to WAV file
Args:
samples: (ndarray / 2D ndarray) (floating point) sample vector
mono: DIM: nSamples
stereo: DIM: nSamples x nChannels
fs: (int) Sample rate in Hz
nBits: (int) Number of bits
fnWAV: (string) WAV file name to write
"""
if nbits == 8:
intsamples = (y+1.0) * AudioIO.normFact['int' + str(nbits)]
fX = np.int8(intsamples)
elif nbits == 16:
intsamples = y * AudioIO.normFact['int' + str(nbits)]
fX = np.int16(intsamples)
elif nbits > 16:
fX = y
write(audioFile, fs, fX)
@staticmethod
def sound(x,fs):
""" Plays a wave file using the pyglet library. But first, it has to be written.
Termination of the playback is being performed by any keyboard input and Enter.
Args:
x: (array) Floating point samples
fs: (int) The sampling rate
"""
import pyglet as pg
global player
# Call the writing function
AudioIO.wavWrite(x, fs, 16, 'testPlayback.wav')
# Initialize playback engine
player = pg.media.Player()
# Initialize the object with the audio file
playback = pg.media.load('testPlayback.wav')
# Set it to player
player.queue(playback)
# Sound call
player.play()
# Killed by "keyboard"
# kill = raw_input()
kill = input()
if kill or kill == '':
AudioIO.stop()
# Remove the dummy wave write
os.remove('testPlayback.wav')
@staticmethod
def stop():
""" Stops a playback object of the pyglet library.
It does not accept arguments, but a player has to be
already initialized by the above "sound" method.
"""
global player
# Just Pause & Destruct
player.pause()
player = None
return None
if __name__ == "__main__":
# Define File
# myReadFile = 'EnterYourWavFile.wav'
myReadFile = './wav/vald_1.wav'
# Read the file
x, fs = AudioIO.wavRead(myReadFile, mono = True)
# Gain parameter
g = 0.2
# Listen to it
AudioIO.sound(x*g,fs)
# Make it better and write it to disk
x2 = np.empty((len(x),2), dtype = np.float32)
try :
x2[:,0] = x * g
x2[:,1] = np.roll(x*g, 512)
except ValueError:
x2[:,0] = x[:,0] * g
x2[:,1] = np.roll(x[:,0] * g, 256)
# Listen to stereo processed
AudioIO.sound(x2*g,fs)
AudioIO.audioWrite(x2, fs, 16, 'myNewWavFile.wav', 'wav')