Reputation: 185
In the following code I created a buffer which holds 10 frames of an audio file in each loop iteration.
import collections
import librosa
import wave
my_buffer = collections.deque(maxlen=10)
f = wave.open('Desktop/0963.wav',"rb")
num_frames = f.getnframes()
for frame in range(num_frames):
my_buffer.append(f.readframes(frame))
Out of the buffer, I need to get a numpy array representing audio amplitude of each sample point with librosa. Any idea?
Upvotes: 2
Views: 1739
Reputation: 131
As mentioned above, scipy.io.wavfile is a good module for reading in and handling audio. If you want to stick with librosa, you can use this to do the same:
import librosa
filepath = 'Desktop/0963.wav'
samplerate = 44100
audio, samplerate = librosa.load(filepath, sr=samplerate)
audio.shape
What I like about librosa.load is that you can specify any sample rate to downsample an audio file to. Hope this helps.
Upvotes: 0
Reputation: 11453
If you use scipy.io.wavfile , it will directly read a wave file and load data to an numpy array. Which you can then slice as per your requirements.
scipy.io.wavfile
reads a WAV file and returns the sample rate (in samples/sec) and data from the WAV file
>>> type(f)
<type 'tuple'>
>>> f
(44100, array([-36, 57, 156, ..., 66, 64, 77], dtype=int16))
>>>
Source Code
from scipy.io.wavfile import read
import numpy as np
f = read('your_audio.wav')
n = np.array(f[1],dtype=float)
for i in xrange(0,len(n),10):
my_buffer = n[i:i+10]
my_buffer contents:
>>>
[ -36. 57. 156. 198. 191. 126. 70. 42. 43. 62.]
[ 69. 71. 83. 117. 159. 177. 151. 89. 14. -27.]
[ -33. -4. 21. 38. 42. 66. 94. 134. 144. 142.]
[ 118. 115. 111. 132. 122. 123. 103. 119. 125. 134.]
.....
.....
Here we have my_buffer
with 10 frames per iteration that you can feed into next block.
Upvotes: 1