我是python的新手,我正在尝试使用API对音频文件进行降噪。下面是我正在使用的代码,执行后它给我错误“ ValueError:缓冲区大小必须是元素大小的倍数”。任何人都可以帮助我解决问题吗?
import requests
import scipy.io.wavfile
import numpy as np
file_name = 'audio.mp3'
files = {'file': open(file_name, 'rb')}
denoise_level = 20
querystring = {"denoise_control": denoise_level}
headers = {
'x-api-key': "my-api-key"
}
url = "https://noise-reduction-service.p.rapidapi.com/denoise"
response = requests.request("POST", url, files=files, headers=headers, params=querystring)
content = np.frombuffer(response.content, dtype=np.int32)
sample_rate = 44100
scipy.io.wavfile.write('denoised_speech.wav', sample_rate, content)
谢谢!
答案 0 :(得分:0)
当np.frombuffer()
尝试从API接收响应并解释为4字节整数(32位)序列时,会发生这种情况。在这种情况下,响应必须是4字节的倍数。如果不是这种情况,则numpy引发异常。 (例如,如果流末尾还剩3个字节,该怎么办?)
没有有关API的更多信息,很难对此进行调查。您检查了回复吗?我可以想到三个可能的问题:
response.status_code
。scipy.io.wavfile.write()
,将响应保存到文件中即可。)