有没有办法读取.wav格式的二进制数据帧?我正在将.wav二进制文件流式传输到python服务器,我希望能够从pyaudio的处理中读取和使用它,它会抱怨帧大小。
既然我不能使用wave.open('filename.wav')
,因为我已经有了流传输的二进制数据,那么有没有一种方法可以读取二进制数据,以便我可以使用wave
python库中的readframes
方法呢?
编辑:我试着从客户端流式传输readframes
,但是pyaudio给出一个错误,字节不是.wav格式。但是,如果我能在服务器上完成这项工作,那将是最理想的。
发布于 2020-07-11 17:36:41
from scipy.io import wavfile
fs, data = wavfile.read('your file path')
发布于 2020-07-17 21:58:58
正如@Vishesh Mangla指出的那样,使用librosa是一个很棒的音频信号库
import librosa
sr = librosa.get_samplerate('/path/to/file.wav')
# Set the frame parameters to be equivalent to the librosa defaults
# in the file's native sampling rate
frame_length = (2048 * sr) // 22050
hop_length = (512 * sr) // 22050
# Stream the data, working on 128 frames at a time
stream = librosa.stream('path/to/file.wav',
block_length=128,
frame_length=frame_length,
hop_length=hop_length)
chromas = []
for y in stream:
chroma_block = librosa.feature.chroma_stft(y=y, sr=sr,
n_fft=frame_length,
hop_length=hop_length,
center=False)
chromas.append(chromas)
在该示例中,每个音频片段y将包括128帧的样本,或者更具体地说,len(y) == frame_length + (block_length - 1) * hop_length。每个片段y将通过frame_length - hop_length样本与后续片段重叠,这确保了流处理将提供与在一个步骤中处理整个序列时等效的结果(假设填充/居中被禁用)。
有关流接口的更多详细信息,请参阅librosa.core.stream。
发布于 2020-07-11 17:52:00
对于我自己的恩惠,有一个优雅的解决方案,我borrow from this more general context relating to getting a virtual file object。
import io
audio = wave.open(io.BytesIO(bytes))
这将为python bytes对象启用所有wave的API,至少是我在与原始问题相同的场景中使用的子集。例如,使用上面的audio
变量,您现在可以:
format=self.audio.get_format_from_width(
audio.getsampwidth()),
channels=audio.getnchannels(),
rate=audio.getframerate(),
frames_per_buffer=self.chunk_size,
output=True)
由于wave库似乎只支持磁盘文件对象,所以这是一个很好的变通方法,至少它依赖于一个标准的python库(io),它提供了我们需要的东西来弥补API中的差距。
https://stackoverflow.com/questions/43899894
复制相似问题