gpt4 book ai didi

python - ValueError:缓冲区大小必须是元素大小的倍数

转载 作者:行者123 更新时间:2023-12-02 22:36:28 25 4
gpt4 key购买 nike

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

提前致谢!

最佳答案

np.frombuffer()尝试从API接收响应并解释为4字节整数(32位)序列时,会发生这种情况。在这种情况下,响应必须是4字节的倍数。如果不是这种情况,则numpy引发异常。 (例如,如果流末尾还剩3个字节,该怎么办?)

没有有关API的更多信息,很难对此进行调查。您检查了回复吗?我可以想到三个可能的问题:

  • 响应可能是错误消息。还要检查response.status_code
  • 响应仅包含二进制流吗?处理后的数据可能会嵌入到json文档中。
  • 返回的音频是否真的编码为原始4字节整数?可以将其编码为2字节整数。 (或者它可能是mp3。在这种情况下,无需使用scipy.io.wavfile.write()将响应保存到文件中即可)。
  • 关于python - ValueError:缓冲区大小必须是元素大小的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58733168/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com