Playing a Beep Sound in Python: winsound Module


The Python winsound module is a simple way to play audio files on a Windows machine using Python. It has a straightforward interface that allows you to play sound files and MIDI files, and control MIDI devices with just a few lines of code.

The winsound module is part of the Python standard library, so you don't need to install it separately. While it has some limitations such as only supporting a limited number of audio file formats, it can be a useful tool for simple audio tasks in Python.

In this tutorial, we'll explore the features and capabilities of the winsound module and see how to use it to play sounds and work with sound files and MIDI devices in Python.

Installing and importing winsound

The winsound module is part of the Python standard library, so you don't need to install it separately. To use it in your code, simply import it with the following line −

import winsound

Playing Sounds

The main function of the winsound module is PlaySound, which plays a sound from a file or from a buffer of data. Here is an example of how to play a sound from a file −

import winsound
winsound.PlaySound("sound.wav", winsound.SND_FILENAME)

In this example, "sound.wav" is the name of the file that contains the sound, and winsound.SND_FILENAME is a flag that tells PlaySound to treat the first argument as a file name.

You can also play a sound from a buffer of data by passing a bytes object as the first argument and using the winsound.SND_MEMORY flag −

import winsound
# bytes object containing sound data
data = b'\x00\x00\x02\x00\x10\x00...'
winsound.PlaySound(data, winsound.SND_MEMORY)

The PlaySound function has a few other optional arguments that you can use to customize the way the sound is played. For example, you can specify the volume of the sound using the winsound.SND_ASYNC and winsound.SND_LOOP flags −

import winsound
winsound.PlaySound("sound.wav", winsound.SND_FILENAME | winsound.SND_ASYNC |
winsound.SND_LOOP)

In this example, the winsound.SND_ASYNC flag tells PlaySound to play the sound asynchronously, so the function returns immediately and the sound is played in the background. The winsound.SND_LOOP flag tells PlaySound to loop the sound until it is stopped.

To stop a sound that is playing in the background, you can use the winsound.PlaySound function with the winsound.SND_PURGE flag −

import winsound
winsound.PlaySound(None, winsound.SND_PURGE)

Working with Sound Files

The winsound module also provides a few functions for working with sound files.

Beep Function

The Beep function is a part of the Python winsound module, which provides a simple interface for playing audio files and controlling MIDI devices on a Windows machine.

The Beep function plays a simple beep sound using the default system speaker. It takes two arguments: the frequency of the beep in Hertz, and the duration of the beep in milliseconds.

Example

Here is an example of how to use the Beep function to play a series of beeps at different frequencies −

import winsound
winsound.Beep(440, 500) # beep at 440 Hz for 0.5 seconds
winsound.Beep(880, 500) # beep at 880 Hz for 0.5 seconds
winsound.Beep(1760, 500) # beep at 1760 Hz for 0.5 seconds
winsound.Beep(3520, 500) # beep at 3520 Hz for 0.5 seconds

The Beep function is a simple way to produce simple beep sounds in Python, but it has some limitations. It only produces a simple sine wave sound, and it does not provide any way to control the volume or other sound settings. If you need more advanced audio capabilities, you may want to consider using a different Python module, such as the pyaudio module or the sounddevice module.

MessageBeep

The MessageBeep function is a part of the Python winsound module, which provides a simple interface for playing audio files and controlling MIDI devices on a Windows machine.

The MessageBeep function plays a system sound or a user-defined sound. You can use it to play one of the standard Windows system sounds, such as the "exclamation" sound or the "question" sound, or you can use it to play a user-defined sound from a file or a buffer of data.

Example

Here is an example of how to use the MessageBeep function to play a standard system sound −

import winsound
# play the "exclamation" sound
winsound.MessageBeep(winsound.MB_ICONEXCLAMATION) 

In this example, the winsound.MB_ICONEXCLAMATION constant specifies the type of system sound to play. The MessageBeep function has several constants that you can use to specify the type of sound to play. For example, winsound.MB_ICONERROR plays the standard "error" sound, and winsound.MB_ICONWARNING plays the standard "warning" sound

You can also use the MessageBeep function to play a user-defined sound from a file or a buffer of data. To do this, you can pass the winsound.MB_OK constant as the first argument, and the file name or data as the second argument −

import winsound
# play a sound from a file
winsound.MessageBeep(winsound.MB_OK, "sound.wav")
# bytes object containing sound data
data = b'\x00\x00\x02\x00\x10\x00...'
# play a sound from a buffer of data
winsound.MessageBeep(winsound.MB_OK, data) 

midiOutOpen

The winsound module also provides a few functions for working with MIDI devices. For example, the midiOutOpen function opens a MIDI output device, and the midiOutClose function closes it.

The midiOutOpen function is a part of the Python winsound module, which provides a simple interface for playing audio files and controlling MIDI devices on a Windows machine.

The midiOutOpen function opens a MIDI output device, allowing you to send MIDI messages to the device. It takes two arguments: the device identifier, and a handle to a HMIDIOUT object. The device identifier specifies the MIDI device to open, and the handle is used to reference the device in subsequent calls to the winsound module.

Example

Here is an example of how to use the midiOutOpen function to open the first MIDI output device −

import winsound
# use the first MIDI device
device_id = 0
# create a handle to a MIDIOUT object
handle = winsound.HMIDIOUT()
winsound.midiOutOpen(handle, device_id, 0, 0, 0)

In this example, the device_id is set to 0, which specifies the first MIDI output device on the system. The handle is a HMIDIOUT object that is used to reference the MIDI device in subsequent calls to the winsound module. The last three arguments are reserved and should be set to 0.

Once you have opened a MIDI output device using the midiOutOpen function, you can use other functions from the winsound module to send MIDI messages to the device. For example, you can use the midiOutShortMsg function to send a short MIDI message to the device −

import winsound
# MIDI message to play a note on channel 1
message = 0x904040
winsound.midiOutShortMsg(handle, message)

In this example, the message is a MIDI message that specifies a "note on" event on channel 1. The midiOutShortMsg function sends the message to the MIDI device using the handle that was obtained from the midiOutOpen function.

When you are finished using the MIDI output device, you should close it using the midiOutClose function:

import winsound
winsound.midiOutClose(handle)

The midiOutClose function closes the MIDI output device and releases any resources that were allocated when the device was opened. It is important to close the device when you are finished using it to avoid wasting system resources and to prevent potential errors.

Limitations of the winsound Module

The winsound module is a very simple way to play audio files on a Windows machine, but it has some limitations. It can only play a limited number of audio file formats, including WAV, MIDI, and RMI. It also has limited support for controlling the volume or other sound settings.

If you need more advanced audio capabilities, you may want to consider using a different Python module, such as the pyaudio module or the sounddevice module. These modules offer more features and support a wider range of audio formats, but they may be more complex to use.

Conclusion

The Python winsound module is a simple and easy-to-use tool for playing audio files and controlling MIDI devices on a Windows machine using Python. It has a straightforward interface that allows you to play sound files and MIDI files, and control MIDI devices with just a few lines of code

Updated on: 22-Feb-2024

28 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements