从字节创建 .wav 文件

Har*_*art 3 python audio byte numpy wav

我正在从从 URL 下载的 wav 音频中读取字节。我想将这些字节“重建”为 .wav 文件。我尝试了下面的代码,但生成的文件几乎是静态的。例如,当我下载自己说话的音频时,生成的 .wav 文件只是静态的,但是当我知道音频应该播放我的声音时,我可以听到轻微的改变/失真。我究竟做错了什么?

from pprint import pprint
import scipy.io.wavfile
import numpy

#download a wav audio recording from a url
>>>response = client.get_recording(r"someurl.com")
>>>pprint(response)
(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00'
 ...
 b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
...
 b'\xea\xff\xfd\xff\x10\x00\x0c\x00\xf0\xff\x06\x00\x10\x00\x06\x00'
 ...)

>>>a=bytearray(response)
>>>pprint(a)
bytearray(b'RIFFv\xfc\x03\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00'       
      b'\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00LISTJ\x00\x00\x00INFOINAM'
      b'0\x00\x00\x00Conference d95ac842-08b7-4380-83ec-85ac6428cc41\x00'
      b'IART\x06\x00\x00\x00Nexmo\x00data\x00\xfc\x03\x00\xff\xff'
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
      ...
      b'\x12\x00\xf6\xff\t\x00\xed\xff\xf6\xff\xfc\xff\xea\xff\xfd\xff'
      ...)

>>>b = numpy.array(a, dtype=numpy.int16)
>>>pprint(b)
array([ 82,  73,  70, ..., 255, 248, 255], dtype=int16)

>>>scipy.io.wavfile.write(r"C:\Users\somefolder\newwavfile.wav", 
16000, b)
Run Code Online (Sandbox Code Playgroud)

Mat*_*ias 6

您可以简单地将数据写入response文件:

with open('myfile.wav', mode='bx') as f:
    f.write(response)
Run Code Online (Sandbox Code Playgroud)

如果您想将音频数据作为 NumPy 数组访问而不先将其写入文件,您可以使用soundfile模块执行此操作,如下所示:

import io
import soundfile as sf

data, samplerate = sf.read(io.BytesIO(response))
Run Code Online (Sandbox Code Playgroud)

另请参见此示例:https : //pysoundfile.readthedocs.io/en/0.9.0/#virtual-io


Wes*_* Na 5

我在流媒体时遇到了同样的问题,我使用上面的答案编写了一个完整的函数。就我而言,字节数组来自流式传输音频文件(前端),后端需要将其作为 ndarray 进行处理。

此函数模拟前端如何将音频文件作为累积到字节数组中的块发送:

audio_file_path = 'offline_input/zoom283.wav'

chunk = 1024

wf = wave.open(audio_file_path, 'rb')
audio_input = b''
d = wf.readframes(chunk)
while len(d) > 0:
    d = wf.readframes(chunk)
    audio_input = audio_input + d
Run Code Online (Sandbox Code Playgroud)

一些导入库:

import io
import wave

import numpy as np
import scipy.io.wavfile
import soundfile as sf
from scipy.io.wavfile import write
Run Code Online (Sandbox Code Playgroud)

最后,后端将获取一个字节数组并将其转换为 ndarray:

def convert_bytearray_to_wav_ndarray(input_bytearray: bytes, sampling_rate=16000):
    bytes_wav = bytes()
    byte_io = io.BytesIO(bytes_wav)
    write(byte_io, sampling_rate, np.frombuffer(input_bytearray, dtype=np.int16))
    output_wav = byte_io.read()
    output, samplerate = sf.read(io.BytesIO(output_wav))
    return output


output = convert_bytearray_to_wav_ndarray(input_bytearray=audio_input)
Run Code Online (Sandbox Code Playgroud)

输出表示要由后端处理的音频文件:

为了检查文件是否已正确接收,我们将其写入办公桌:

scipy.io.wavfile.write("output1.wav", 16000, output)
Run Code Online (Sandbox Code Playgroud)