Python winsound module

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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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

    chevron_right

    
    

    Output:

    Consecutive notes with frequency differences of 100Hz and time duration 50 milliseconds greater
    than the 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.