We all using smartphones nowadays it will generate different types of sounds for different purposes. One for the alarm sound, one for notifications, and so on. Have you ever thought about how to generate this type of sound? And as a python programmer have you thought about how to generate these sounds in python? If not, means no need to worry, this article will give a solution for all these questions. Python contains a winsound module to generate sounds.
Winsound module is a built-in module in python. The module is already available in python, so it is not necessary to install it again. This is useful to generate some specified sounds at a specific time. This module is specially made for Windows. To use the winsound module, we need to import this using the below-given command:
This beeps the PC or laptop speaker. This accepts two arguments: frequency and duration. The frequency parameter is to specify the frequency of the beep sound. The sound should be given in hertz, and the range will be 37 through 32,767. The duration parameter is to specify the time duration of the beep sound. And the duration is given in milliseconds. This raises the RunTimeError if the system is not able to play the sound.
- frequency: frequency of the sound and in hertz
- duration: time duration of the sound and in milliseconds
import winsound frequency=int(input('Enter a frequency in hertz:')) duration=int(input('Enter the time in milliseconds:')) winsound.Beep(frequency,duration)
Import the module. Get the frequency and duration from the user.
Enter a frequency in hertz:800 Enter the time in milliseconds:100 >>>
After getting input from the user. There will be one beep sound from the PC system.
Code to get the beep sound continuously
import winsound frequency=int(input('Enter a frequency in hertz:')) duration=int(input('Enter the time in milliseconds:')) number_of_times=int(input('How many times do you need the sound:')) for i in range(0,number_of_times): winsound.Beep(frequency,duration)
Creating a loop so that we can produce the beep sound a specific number of times.
Enter a frequency in hertz:400 Enter the time in milliseconds:200 How many times do you need the sound:5 #Winsound
This function calls the PlaySound function. It accepts two arguments namely sound and flags. The sound parameter is useful to pass the audio data or the filename that contains the audio.
- sound: file name
- flag: SND_FILENAME
import winsound sound=input("Enter the sound wave:") flags=winsound.SND_FILENAME winsound.PlaySound(sound,flags)
Import the module. Get the sound wave from the user. Declare the winsound.PlaySound as it declared in the syntax.
Enter the sound wave:Welcome.wav >>>
Code to Play the sound continuously for 5 times
import winsound sound="Welcome.wav" flags=winsound.SND_FILENAME for i in range(0,5): winsound.PlaySound(sound,flags)
This will play the sound the 5 times
Sounds the specified sound in the registry. It accepts only one argument type. This type of parameter tells which sound has to play. The possible values for the type parameter are -1, MB_ICONASTERISK, MB_ICONEXCLAMATION, MB_ICONHAND, MB_ICONQUESTION, AND MB_OK.
Now let us see about the parameters in a detailed manner.
- MB_ICONASTERISK: This plays the Default system sound.
- MB_ICONEXCLAMATION: This plays the Exclamation system sound.
- MB_ICONHAND: This plays the SystemHand sound.
- MB_ICONQUESTION: This plays the question sound of the system.
- MB_OK: This also plays the default sound of the system.
This is useful to play the sound repeatedly. It always uses SND_ASYNC so that it can avoid blocking. But the SND_LOOP is cannot be used with SND_MEMORY.
This will stop all instances of the playing sound.
This is a parameter for winsound.PlaySound. It cannot be used with SND_ALIAS. This is the name of the wav file.
This is a sound association name from the registry. It will play the system default sound if the name and the SND_NODEFAULT are not specified. It should not be used with SND_FILENAME.
It won’t play the system default sound if the sound is not specified.
This does not interrupt the currently playing sound.
Are there any equivalent of winsound for mac?
There is a module available for mac that is equivalent to python winsound. The module is Pygame. Following code can run the sound in the background. The pip command is sudo pip install pygame.
import pygame import time pygame.init() pygame.mixer.music.load("audio.mp3") pygame.mixer.music.play(-1) while pygame.mixer.music.get_busy(): time.sleep(1)
FAQs Related to Python Winsound module
winsound.SND_NODEFAULT won’t play the system default sound.
winsound.SND_NOWAIT returns immediately if the sound driver is busy.
No Python winsound doesn’t play MP3 files.
To play the music through winsound, we have to use PlaySound.
To change the volume of winsound beeps, we have to change the frequency.
In this article, we have completely learned about the winsound module. This module will be very useful when you are developing some real-time projects. We hope this article is easy to understand. Try to implement the programs and if you find any difficulties feel free to ask your doubts in the comment section.