python-drum-sequencer/audiohelp.py

79 lines
No EOL
2.6 KiB
Python

import pygame
import sys
import os
import time
def test_audio():
print("=== Pygame Audio System Test ===")
print("Initializing Pygame Audio System...")
# Initialize pygame mixer with specific settings
try:
pygame.mixer.pre_init(22050, -16, 8, 512)
pygame.init()
print("✓ Pygame initialized successfully")
except Exception as e:
print(f"✗ Failed to initialize pygame: {e}")
return False
# Check mixer initialization
try:
print("\nChecking audio configuration...")
mixer_settings = pygame.mixer.get_init()
if mixer_settings:
freq, format, channels = mixer_settings
print(f"✓ Sample rate: {freq}Hz")
print(f"✓ Format: {format}")
print(f"✓ Channels: {channels}")
else:
print("✗ Mixer not initialized")
return False
except Exception as e:
print(f"✗ Failed to get mixer info: {e}")
return False
# Try to load and play the metronome sound
try:
print("\nTesting sound playback...")
if not os.path.exists("metronome.wav"):
print("✗ Could not find metronome.wav")
return False
test_sound = pygame.mixer.Sound("metronome.wav")
print("✓ Metronome sound loaded successfully")
print("\nPlaying metronome sound on loop...")
print("Can you hear the sound? (y/n)")
# Play sound on loop
channel = test_sound.play(-1) # -1 means loop indefinitely
response = input().lower()
channel.stop() # Stop the sound
if response == 'y':
print("✓ Audio test successful!")
return True
else:
print("✗ Audio test failed - no sound heard")
return False
except Exception as e:
print(f"✗ Failed to play test sound: {e}")
return False
if __name__ == "__main__":
success = test_audio()
if success:
print("\n✓ Audio system is working correctly!")
else:
print("\n✗ Audio system test failed. Please check your audio configuration.")
print("Troubleshooting tips:")
print("1. Check if your speakers/headphones are connected and turned on")
print("2. Check if system volume is unmuted and turned up")
print("3. Verify metronome.wav exists in the same directory as this script")
print("4. Try running 'pygame.mixer.quit()' and reinitializing if audio stops working")
print("\nPress Enter to exit...")
input()