我在运行 numpy.frombuffer 的 python 代码时收到错误,需要类似字节的对象,而不是“NoneType”;
此代码在 Windows(python 版本 = 3.10.3)中运行良好,但是当我在 Linux(python 版本 = 3.9.7)中运行它时,它会抛出以下错误消息:
import numpy as np
sound_buffer = np.frombuffer(in_data, dtype=np.float32)
TypeError: a bytes-like object is required, not 'NoneType'
这是代码片段:
## Callback Thread ##
def callback(in_data, frame_count, time_info, status):
global sound_data
sound_buffer = np.frombuffer(in_data,np.uint8, dtype="float32")
sound_data = np.append(sound_data, sound_buffer)
if len(sound_data) >= 88200:
audio_analysis_queue.put(sound_data)
current_time = time.ctime(time.time())
audio_analysis_queue.put(current_time)
sound_data = np.zeros(0, dtype="float32")
return sound_buffer, pyaudio.paContinue
pa = pyaudio.PyAudio()
stream = pa.open(format=AUDIO_FORMAT,
rate=AUDIO_RATE,
channels=NUMBER_OF_AUDIO_CHANNELS,
input_device_index=AUDIO_DEVICE_INDEX,
frames_per_buffer=NUMBER_OF_FRAMES_PER_BUFFER,
output=True,
stream_callback=callback)
# Starts the callback thread
stream.start_stream()
print("--- Listening to Audio Stream ---")
This code is running well in Windows (python version = 3.10.3) but when I run it in Linux (python version = 3.9.7) it is throwing the following error message:
import numpy as np
sound_buffer = np.frombuffer(in_data, dtype=np.float32)
TypeError: a bytes-like object is required, not 'NoneType'
Here is the code snippet:
## Callback Thread ##
def callback(in_data, frame_count, time_info, status):
global sound_data
sound_buffer = np.frombuffer(in_data,np.uint8, dtype="float32")
sound_data = np.append(sound_data, sound_buffer)
if len(sound_data) >= 88200:
audio_analysis_queue.put(sound_data)
current_time = time.ctime(time.time())
audio_analysis_queue.put(current_time)
sound_data = np.zeros(0, dtype="float32")
return sound_buffer, pyaudio.paContinue
pa = pyaudio.PyAudio()
stream = pa.open(format=AUDIO_FORMAT,
rate=AUDIO_RATE,
channels=NUMBER_OF_AUDIO_CHANNELS,
input_device_index=AUDIO_DEVICE_INDEX,
frames_per_buffer=NUMBER_OF_FRAMES_PER_BUFFER,
output=True,
stream_callback=callback)
# Starts the callback thread
stream.start_stream()
print("--- Listening to Audio Stream ---")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论