2017-09-25 165 views
0

在下面的代碼中,我創建了一個緩衝區,每個循環迭代中保存10個音頻文件的幀。帶有librosa的音頻緩衝區

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)) 

在緩衝區外,我需要用librosa得到一個代表每個採樣點音頻振幅的numpy數組。任何想法?

+0

使用'scipy.io.wavfile'添加答案。請檢查並讓我知道它是否適用於您。 –

回答

0

如果使用scipy.io.wavfile,它將直接讀取波形文件並將數據加載到numpy數組。你可以按照你的要求切片。

scipy.io.wavfile讀取WAV文件,並從WAV文件返回採樣速率(在採樣/秒)和數據

>>> type(f) 
<type 'tuple'> 
>>> f 
(44100, array([-36, 57, 156, ..., 66, 64, 77], dtype=int16)) 
>>> 

源代碼

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內容:

>>> 
[ -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.] 
..... 
..... 

這裏我們有my_buffer,每次迭代10幀,您可以將其饋入下一個塊。