Windows sound manager python

Windows-Sound-Manager

Control your volume mixer on Windows with Python 3

How it works

This script emulates keystrokes using the built in windows VK_VOLUME_MUTE, VK_VOLUME_DOWN and VK_VOLUME_UP keys, which are used to control the default audio settings.

Why would I use this?

As I found out, changing your audio settings programmatically with programming language is incredibly tedious. It requires knowledge of tons of DLL’s and tens to force you to change the sound on every single device at hardware level individually. By using a small hack that uses the built in media keys, you can avoid all of this by using something which was built into the OS already.

This isn’t a fool-proof solution, but it works if you just need to build a quick application that requires your sound to to controlled with code.

Specifications

  • Tested on: Windows 10
  • Python version: Python 3.5

Links

  • Virtual-Key Code list
  • C struct code
  • C struct code credit

New in version 1.5.2.

The winsound module provides access to the basic sound-playing machinery
provided by Windows platforms. It includes functions and several constants.

winsound.Beep(frequency, duration)

Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz,
of the sound, and must be in the range 37 through 32,767. The duration
parameter specifies the number of milliseconds the sound should last. If the
system is not able to beep the speaker, RuntimeError is raised.

New in version 1.6.

winsound.PlaySound(sound, flags)

Call the underlying PlaySound() function from the Platform API. The
sound parameter may be a filename, audio data as a string, or None. Its
interpretation depends on the value of flags, which can be a bitwise ORed
combination of the constants described below. If the sound parameter is
None, any currently playing waveform sound is stopped. If the system
indicates an error, RuntimeError is raised.

winsound.MessageBeep([type=MB_OK])

Call the underlying MessageBeep() function from the Platform API. This
plays a sound as specified in the registry. The type argument specifies which
sound to play; possible values are -1, MB_ICONASTERISK,
MB_ICONEXCLAMATION, MB_ICONHAND, MB_ICONQUESTION, and MB_OK, all
described below. The value -1 produces a “simple beep”; this is the final
fallback if a sound cannot be played otherwise.

New in version 2.3.

winsound.SND_FILENAME

The sound parameter is the name of a WAV file. Do not use with
SND_ALIAS.

winsound.SND_ALIAS

The sound parameter is a sound association name from the registry. If the
registry contains no such name, play the system default sound unless
SND_NODEFAULT is also specified. If no default sound is registered,
raise RuntimeError. Do not use with SND_FILENAME.

All Win32 systems support at least the following; most systems support many
more:

PlaySound() name Corresponding Control Panel Sound name
'SystemAsterisk' Asterisk
'SystemExclamation' Exclamation
'SystemExit' Exit Windows
'SystemHand' Critical Stop
'SystemQuestion' Question

For example:

import winsound
# Play Windows exit sound.
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

# Probably play Windows default sound, if any is registered (because
# "*" probably isn't the registered name of any sound).
winsound.PlaySound("*", winsound.SND_ALIAS)
winsound.SND_LOOP

Play the sound repeatedly. The SND_ASYNC flag must also be used to
avoid blocking. Cannot be used with SND_MEMORY.

winsound.SND_MEMORY

The sound parameter to PlaySound() is a memory image of a WAV file, as a
string.

Note

This module does not support playing from a memory image asynchronously, so a
combination of this flag and SND_ASYNC will raise RuntimeError.

winsound.SND_PURGE

Stop playing all instances of the specified sound.

Note

This flag is not supported on modern Windows platforms.

winsound.SND_ASYNC

Return immediately, allowing sounds to play asynchronously.

winsound.SND_NODEFAULT

If the specified sound cannot be found, do not play the system default sound.

winsound.SND_NOSTOP

Do not interrupt sounds currently playing.

winsound.SND_NOWAIT

Return immediately if the sound driver is busy.

Note

This flag is not supported on modern Windows platforms.

winsound.MB_ICONASTERISK

Play the SystemDefault sound.

winsound.MB_ICONEXCLAMATION

Play the SystemExclamation sound.

winsound.MB_ICONHAND

Play the SystemHand sound.

winsound.MB_ICONQUESTION

Play the SystemQuestion sound.

winsound.MB_OK

Play the SystemDefault sound.

kandi X-RAY | Windows-Sound-Manager Summary

kandi X-RAY | Windows-Sound-Manager Summary

Windows-Sound-Manager is a Python library. Windows-Sound-Manager has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. However Windows-Sound-Manager build file is not available. You can download it from GitHub.

Control your volume mixer on windows with Python 3

Support

    Windows-Sound-Manager has a low active ecosystem.

    It has 39 star(s) with 18 fork(s). There are 3 watchers for this library.

    It had no major release in the last 6 months.

    There are 1 open issues and 0 have been closed. On average issues are closed in 861 days. There are no pull requests.

    It has a neutral sentiment in the developer community.

    The latest version of Windows-Sound-Manager is current.

Quality

    Windows-Sound-Manager has 0 bugs and 12 code smells.

Security

    Windows-Sound-Manager has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.

    Windows-Sound-Manager code analysis shows 0 unresolved vulnerabilities.

    There are 0 security hotspots that need review.

License

    Windows-Sound-Manager is licensed under the MIT License. This license is Permissive.

    Permissive licenses have the least restrictions, and you can use them in most projects.

Reuse

    Windows-Sound-Manager releases are not available. You will need to build from source code and install.

    Windows-Sound-Manager has no build file. You will be need to create the build yourself to build the component from source.

    Windows-Sound-Manager saves you 133 person hours of effort in developing the same functionality from scratch.

    It has 334 lines of code, 13 functions and 3 files.

    It has low code complexity. Code complexity directly impacts maintainability of the code.

Top functions reviewed by kandi — BETA

kandi has reviewed

Windows-Sound-Manager

and discovered the below as its top functions. This is intended to give you an instant insight into

Windows-Sound-Manager

implemented functionality, and help decide if they suit your requirements.

  • Set current volume
  • Sets the current volume
  • Press key down
  • Track the sound
  • Play volume up
  • Returns the current volume
  • Pause the sound
  • Set the current volume down
  • Sends a key down
  • Mute the sound
  • Returns True if the sound is muted
  • Set the volume minimum
  • Set the volume max

Get all kandi verified functions for this library.

Windows-Sound-Manager Key Features

No Key Features are available at this moment for

Windows-Sound-Manager

.

Windows-Sound-Manager Examples and Code Snippets

No Code Snippets are available at this moment for

Windows-Sound-Manager

.

Vulnerabilities

No vulnerabilities reported

Install Windows-Sound-Manager

You can download it from GitHub.
You can use Windows-Sound-Manager like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

Support

Virtual-Key Code listC struct codeC struct code credit

Find more information at:

Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

Find more libraries

Last Updated :
01 Jul, 2022

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

freq = 500

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

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('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

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.

Post Views: 359

In this tutorial, we will introduce you to the Winsound module and its basic functionalities in Python. After this tutorial, you will be able to play the default sound and music files of your system.

What will you learn:

Playing audio files, the default sound of your system.

Introduction Of Winsound

Basically, Winsound is a module that is used for playing “.wav” music files in your system. You can use this module on windows only. For other operating systems, you can prefer the “PlaySound” module.

You don’t need to install this module. It’s preinstalled.

Note: You can play only “.wav” files. For other files, you can use “PlaySound”.

Use Of Winsound In Python

First of all, import the Winsound module. For the first method, we are going to use the Beep(). It takes two arguments frequency(37 to 32000 range) and time in milliseconds.

import winsound

#produce beep sound
winsound.Beep(250,100) 

Another method is the PlaySound(). This method also takes two arguments file path and flag(there are too many flags that you can use to apply conditions on file like SND_LOOP, SND_NOSTOP, etc). Here in this method, we put the first argument “*”, which will play the default sound of your system.

To check other flags, you can go here.

#play default sound of your system 
winsound.PlaySound("*", window.SND_LOOP)

In this segment of the code, we put the file path.

#play the file of your system 
winsound.PlaySound("C:\\music\\file.wav", window.SND_LOOP)

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Сколько стоит переустановка windows с сохранением данных
  • Game booster download windows 8
  • Ardp free для windows
  • Twain driver setting windows 10
  • Как откатить драйвера блютуз на windows 10