1

在以下代码中,我创建了一个缓冲区,该缓冲区在每次循环迭代中保存 10 帧音频文件。

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 数组。任何想法?

4

1 回答 1

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 帧,您可以将其输入到下一个块中。

于 2017-10-02T16:45:04.800 回答