The Wayback Machine - https://web.archive.org/web/20240710144430/https://www.geeksforgeeks.org/python-winsound-module/
Open In App

Python winsound module

Last Updated : 01 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Maybe you have just begun coding in Python. Maybe the entire outset seems inconclusive since you have been working with regular IDEs and would like to obtain a bit more out of what your code does on the respective console and maybe a little music or tune might just liven it up. This article serves the purpose of introducing to you the winsound module, an object or file with a set of attributes or functions, specific to the task of generating or playing sound or a sound file. Note: The winsound module is defined only for execution on a Windows Platform, hence the name WINsound. Since, the winsound module is a builtin, there is no need for you to install it prior to executing it. The basic action would be to

import winsound

and then based upon the kind of output you would like, type out the following functions:

  • winsound.Beep( ) The functionality devoted to this method is to generate a ‘Beep’ sound. However, the user is required to input the frequency value and the duration of the sound (these are parameters that shall be passed while calling the function). Note: The frequency must be in the range 37 through 32,767 hertz. 

Python3




import winsound
 
# frequency is set to 500Hz
freq = 500
 
# duration is set to 100 milliseconds            
dur = 100
              
winsound.Beep(freq, dur)


  • Output:

Windows system will produce a ‘Beep’ sound with the given frequency for the given duration of time.

  • Building further on the code above, things can be taken to another level by implementing a ‘for’ loop to increment the frequency and duration. This has been done in the code below: 

Python3




import winsound
 
freq = 100
dur = 50
 
# loop iterates 5 times i.e, 5 beeps will be produced.
for i in range(0, 5):   
    winsound.Beep(freq, dur)   
    freq+= 100
    dur+= 50


  • Output: 

Consecutive notes with frequency differences of 100Hz and time duration 50 milliseconds greater
than  the previous time duration are produced.

  • winsound.PlaySound( ) With the PlaySound function, things can get slightly advanced, not to mention interesting. Keep in mind that this function is only compatible with .wav files. Two parameters are passed in the function: ‘filename’ and the flag – winsound.SND_FILENAME, which is required for the Platform API to refer to the output file. The flags are as defined below:
Flags Description
SND_FILENAME The sound parameter is the name of a WAV file.
SND_LOOP Play the sound repeatedly
SND_MEMORY The sound parameter to PlaySound() is a memory image of a WAV file, as a bytes-like object.
SND_ASYNC Return immediately, allowing sounds to play asynchronously.
SND_NODEFAULT If the specified sound cannot be found, do not play the system default sound.
SND_NOSTOP Do not interrupt sounds currently playing.
  • Example: 

Python3




import winsound
 
print("Playing the file 'Welcome.wav'")
 
# winsound.PlaySound('filename', flag)
winsound.PlaySound('Welcome.wav', winsound.SND_FILENAME)      


  • Output:
The respective audio file named 'Welcome.wav' is executed.
  • SND_ALIAS The sound parameter should be interpreted as a control panel sound association name. The Windows registry keys are associated with sound names. If the registry contains no such name, play the system default sound unless SND_NODEFAULT. All Win32 systems support the following:
PlaySound() name Control panel sound name
SystemAsterisk Asterisk
SystemExclamation Exclamation
SystemExit Exit Windows
SystemHand Critical Stop
SystemQuestion Question
  • Example: 

Python3




import winsound
 
 
# Play Windows question sound
winsound.PlaySound("SystemQuestion", winsound.SND_ALIAS)


  • Output:
Play Windows question sound

There are various other winsound functions, most of which are particular to specific tasks, some of which deal with runtime parameters. Nonetheless, the functions mentioned above should suffice as long as the idea is to play around to get an idea of what can be accomplished using this module.



Similar Reads

MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. In this article, we will be disc
2 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Python calendar module : formatmonth() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.TextCalendar(firstweekday=0) can be used to generate plain text
2 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
PyMsgBox module in Python
PyMsgBox is simple, cross-platform, purely implemented in Python for message boxes as JavaScript has. It uses Python's built-in Tkinter module for its GUI. Installation This module does not built-in Python. To install it type the below command in the terminal. pip install PyMsgBox There are four functions in PyMsgBox, which follow JavaScript’s mess
2 min read
uniform() method in Python Random module
uniform() is a method specified in the random library in Python 3. Nowadays, in general, day-day tasks, there's always the need to generate random numbers in a range. Normal programming constructs require a method more than just one word to achieve this particular task. In python, there's an inbuilt method, "uniform()" which performs this task with
2 min read
mode() function in Python statistics module
The mode of a set of data values is the value that appears most often. It is the value at which the data is most likely to be sampled. A mode of a continuous probability distribution is often considered to be any value x at which its probability density function has a local maximum value, so any peak is a mode.Python is very robust when it comes to
5 min read
median_low() function in Python statistics module
Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The low median is always a member of the data set. When the numb
4 min read
median_high() function in Python statistics module
Median is often referred to as the robust measure of the central location and is less affected by the presence of outliers in data. statistics module in Python allows three options to deal with median / middle elements in a data set, which are median(), median_low() and median_high(). The high median is always a member of the data set. When the num
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg