Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python winsound module
The winsound module is a Windows-specific Python library that provides simple sound generation capabilities. It allows you to create tones, play sound files, and add audio feedback to your applications without requiring external audio libraries.
Basic Functions
The winsound module provides three main functions:
- Beep() ? Generates system beep tones at specified frequency and duration
- PlaySound() ? Plays WAV sound files with various options
- MessageBeep() ? Plays system alert sounds
Generating Simple Tones with Beep()
The Beep() function creates tones at specific frequencies ?
import winsound # Generate a 440Hz tone for 1 second frequency = 440 # Hz duration = 1000 # milliseconds (1 second) winsound.Beep(frequency, duration) # Generate a 500Hz tone for 2 seconds frequency = 500 # Hz duration = 2000 # milliseconds (2 seconds) winsound.Beep(frequency, duration)
Playing Sound Files with PlaySound()
The PlaySound() function can play WAV files with different playback modes ?
import winsound
# Play a sound file once
winsound.PlaySound("sound.wav", winsound.SND_FILENAME)
# Play a sound file in a loop
winsound.PlaySound("sound.wav", winsound.SND_FILENAME | winsound.SND_LOOP)
# Play a sound file asynchronously
winsound.PlaySound("sound.wav", winsound.SND_FILENAME | winsound.SND_ASYNC)
System Alert Sounds
Use MessageBeep() to play built-in Windows alert sounds ?
import winsound # Play different system sounds winsound.MessageBeep(winsound.MB_ICONASTERISK) # Information sound winsound.MessageBeep(winsound.MB_ICONEXCLAMATION) # Warning sound winsound.MessageBeep(winsound.MB_ICONHAND) # Error sound winsound.MessageBeep(winsound.MB_OK) # Default beep
Real-World Example: Simple Alarm Clock
Here's a practical example of an alarm clock that plays a sound at a specified time ?
import winsound
import datetime
import time
def set_alarm(alarm_hour, alarm_minute):
print(f"Alarm set for {alarm_hour:02d}:{alarm_minute:02d}")
while True:
current_time = datetime.datetime.now()
if current_time.hour == alarm_hour and current_time.minute == alarm_minute:
print("WAKE UP!")
# Play alarm sound for 3 seconds
for _ in range(6):
winsound.Beep(1000, 500)
time.sleep(0.1)
break
time.sleep(30) # Check every 30 seconds
# Set alarm for 8:30 AM
set_alarm(8, 30)
Multithreading for Non-blocking Sound
Use threading to play sounds without blocking the main program ?
import winsound
import threading
import time
def play_background_sound():
for i in range(5):
winsound.Beep(400 + i * 100, 200)
time.sleep(0.3)
# Start sound in background thread
sound_thread = threading.Thread(target=play_background_sound)
sound_thread.start()
# Continue with other tasks
print("Playing sound in background...")
for i in range(10):
print(f"Working on task {i+1}")
time.sleep(0.5)
sound_thread.join()
print("Sound finished!")
Common PlaySound Flags
| Flag | Description | Usage |
|---|---|---|
SND_FILENAME |
Play from file | Standard file playback |
SND_ASYNC |
Non-blocking playback | Continue program execution |
SND_LOOP |
Loop continuously | Repeat until stopped |
SND_NOSTOP |
Don't interrupt current sound | Prevent sound overlap |
Cross-Platform Considerations
Since winsound is Windows-only, consider platform detection for cross-platform apps ?
import sys
def play_notification_sound():
if sys.platform == "win32":
import winsound
winsound.MessageBeep(winsound.MB_ICONASTERISK)
elif sys.platform == "darwin": # macOS
import os
os.system("afplay /System/Library/Sounds/Glass.aiff")
else: # Linux and others
print("\a") # Terminal bell character
play_notification_sound()
Limitations
- Windows only ? Only works on Windows operating systems
- WAV files only ? PlaySound() supports only WAV format
- Basic features ? No advanced audio processing or effects
- No volume control ? Cannot programmatically adjust volume levels
Conclusion
The winsound module provides basic but effective sound capabilities for Windows Python applications. Use Beep() for simple tones, PlaySound() for WAV files, and MessageBeep() for system alerts. While limited to Windows and basic functionality, it's perfect for adding simple audio feedback to your applications.
