-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_audio_noise.py
More file actions
137 lines (116 loc) ยท 4.49 KB
/
Copy pathtest_audio_noise.py
File metadata and controls
137 lines (116 loc) ยท 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
"""
Quick Audio Noise Fix Test
Test different audio configurations to eliminate noise
"""
import subprocess
import tempfile
import os
import time
def test_audio_with_settings():
"""Test audio with different pygame/ALSA settings"""
print("๐ต Testing Audio Noise Fixes")
print("=" * 40)
# First, let's test if the issue is with the audio system itself
print("1๏ธโฃ Testing system audio with clean tone...")
try:
# Generate a clean 1-second tone
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
test_wav = tmp.name
# Create clean test tone
subprocess.run([
'ffmpeg', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=1',
'-ar', '22050', '-ac', '1', '-sample_fmt', 's16', '-y', test_wav
], check=True, capture_output=True)
print(" โ
Clean test tone generated")
# Test with aplay
print(" ๐ Testing with aplay...")
result = subprocess.run(['aplay', '-q', test_wav], capture_output=True)
if result.returncode == 0:
print(" โ
aplay: Success (did you hear a clean tone?)")
else:
print(" โ aplay: Failed")
# Test with pygame
print(" ๐ฎ Testing with pygame...")
try:
import pygame
# Test with default settings
pygame.mixer.init()
pygame.mixer.music.load(test_wav)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
pygame.mixer.quit()
print(" โ pygame (default): Played (was there noise?)")
time.sleep(0.5) # Brief pause
# Test with optimized settings
pygame.mixer.pre_init(frequency=22050, size=-16, channels=1, buffer=1024)
pygame.mixer.init()
pygame.mixer.music.load(test_wav)
pygame.mixer.music.set_volume(0.8)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
pygame.mixer.quit()
print(" โ
pygame (optimized): Played (less noise?)")
except ImportError:
print(" โ pygame not available")
except Exception as e:
print(f" โ pygame error: {e}")
# Cleanup
os.unlink(test_wav)
except Exception as e:
print(f" โ Test failed: {e}")
print("\n2๏ธโฃ Audio system diagnosis...")
# Check for common audio issues
try:
# Check if pulseaudio is running (can cause conflicts)
result = subprocess.run(['pgrep', 'pulseaudio'], capture_output=True)
if result.returncode == 0:
print(" โ ๏ธ PulseAudio detected - may cause conflicts with direct ALSA")
print(" Consider: pulseaudio --kill")
else:
print(" โ
No PulseAudio conflicts")
except:
pass
# Check audio card configuration
try:
result = subprocess.run(['cat', '/proc/asound/card0/pcm0p/info'], capture_output=True, text=True)
if result.returncode == 0:
print(" ๐ฑ Audio card info:")
for line in result.stdout.split('\n')[:3]:
if line.strip():
print(f" {line.strip()}")
except:
pass
print("\n๐ก If you still hear noise, try these fixes on your Pi:")
print("=" * 50)
print("1. Force specific ALSA settings:")
print(" sudo nano /etc/asound.conf")
print(" Add:")
print(" pcm.!default {")
print(" type hw")
print(" card 0")
print(" device 0")
print(" rate 22050")
print(" format S16_LE")
print(" }")
print("")
print("2. Disable PulseAudio (if present):")
print(" pulseaudio --kill")
print(" sudo systemctl --global disable pulseaudio.service")
print("")
print("3. Update Pi audio firmware:")
print(" sudo rpi-update")
print("")
print("4. Test with volume adjustment:")
print(" amixer set Master 70%")
print("")
print("5. Check for electromagnetic interference:")
print(" - Move Pi away from power supplies/WiFi routers")
print(" - Use shielded audio cables")
print(" - Try a USB audio adapter instead of built-in audio")
def main():
test_audio_with_settings()
if __name__ == "__main__":
main()