You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用pyAudio生成的音频无法在Pygame中正常使用的问题求助

Fixing the TypeError: Unrecognized argument (type NoneType) with Pygame Sound

Hey there! Let's break down exactly what's going wrong here and get your sine wave playing with Pygame properly.

Why the Error Happens

The issue boils down to what stream.write() returns in PyAudio. That method's only job is to send your audio samples to the output device for playback—it doesn't return the audio data itself. Instead, it returns None.

When you try to pass that None value to pygame.mixer.Sound(), Pygame has no idea what to do with it, hence the TypeError about unrecognized NoneType arguments.

How to Fix It

You have two solid options here, depending on whether you want to stick with your existing sample generation or switch to generating audio directly for Pygame.

Option 1: Generate Sine Wave Directly for Pygame

Skip the PyAudio middleman entirely and create a sine wave in a format Pygame understands right away. Here's a complete example:

import pygame
import numpy as np

# Initialize Pygame mixer with standard audio settings
pygame.mixer.init(frequency=44100, size=-16, channels=1)

# Define your sine wave parameters
sample_rate = 44100
duration = 2.0  # Seconds
frequency = 440.0  # A4 note

# Generate the sine wave samples
samples = np.sin(2 * np.pi * np.arange(sample_rate * duration) * frequency / sample_rate)
# Convert to 16-bit signed integers (Pygame's preferred audio format)
samples = (samples * 32767).astype(np.int16)

# Create the Pygame Sound object and play it
sound = pygame.mixer.Sound(samples)
sound.play()

# Keep the program running until the sound finishes
pygame.time.wait(int(duration * 1000))

Option 2: Reuse Your Existing PyAudio Sample Data

If you want to keep using your original sample generation code, just don't rely on stream.write()'s return value. Instead, take the samples you already created, convert it to a format Pygame accepts, and pass that directly to Sound():

import pygame
import numpy as np
import pyaudio

# Your existing sample setup
volume = 0.5
sample_rate = 44100
duration = 2.0
frequency = 440.0

# Generate your sine wave samples (same as before)
samples = np.sin(2 * np.pi * np.arange(sample_rate * duration) * frequency / sample_rate).astype(np.float32)
samples *= volume

# Convert samples to Pygame-compatible 16-bit signed integers
pygame_ready_samples = (samples * 32767).astype(np.int16)

# Initialize Pygame mixer and create the Sound object
pygame.mixer.init(frequency=sample_rate, size=-16, channels=1)
sound = pygame.mixer.Sound(pygame_ready_samples)
sound.play()

# Optional: If you still want to play via PyAudio too
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=sample_rate,
                output=True)
stream.write(samples.tobytes())
stream.stop_stream()
stream.close()
p.terminate()

# Wait for playback to finish
pygame.time.wait(int(duration * 1000))

Quick Recap

  • Forget using stream.write()'s return value: It only handles playback, not returning audio data.
  • Pygame needs specific audio formats: Stick to 16-bit signed integers (the -16 in mixer.init() refers to this) for reliable compatibility.

内容的提问来源于stack exchange,提问作者Delly

火山引擎 最新活动