我在运行 numpy.frombuffer 的 python 代码时收到错误,需要类似字节的对象,而不是“NoneType”;

发布于 2025-01-16 22:46:31 字数 1222 浏览 2 评论 0原文

此代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文