Python звуковой сигнал windows

34.4. winsound — Sound-playing interface for Windows¶

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.

winsound. PlaySound ( sound, flags ) ¶

Call the underlying PlaySound() function from the Platform API. The sound parameter may be a filename, a system sound alias, audio data as a bytes-like object , 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. If the system indicates an error, RuntimeError is raised.

Читайте также:  Callbackquery telegram php пример

The sound parameter is the name of a WAV file. Do not use with 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
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) 

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

The sound parameter to PlaySound() is a memory image of a WAV file, as a bytes-like object .

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

Stop playing all instances of the specified sound.

This flag is not supported on modern Windows platforms.

Return immediately, allowing sounds to play asynchronously.

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

Do not interrupt sounds currently playing.

Return immediately if the sound driver is busy.

This flag is not supported on modern Windows platforms.

Play the SystemDefault sound.

Play the SystemExclamation sound.

Play the SystemHand sound.

Play the SystemQuestion sound.

Play the SystemDefault sound.

Источник

winsound — Sound-playing interface for Windows¶

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.

winsound. PlaySound ( sound , flags ) ¶

Call the underlying PlaySound() function from the Platform API. The sound parameter may be a filename, a system sound alias, audio data as a bytes-like object , 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. If the system indicates an error, RuntimeError is raised.

The sound parameter is the name of a WAV file. Do not use with 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:

Corresponding Control Panel Sound name

Источник

How To Beep In Python — 5 Simple Ways

In this tutorial I will show you 5 simple ways to generate a beeping sound in Python.

To generate a beeping sound in Python you have the following options:

  • Use the bell character on the terminal
  • Use AppKit to play MacOS system sounds
  • Use winsound to play Windows system sounds
  • Use pygame to play custom sound files
  • Use simpleaudio to play custom sound files
  • Use the beepy package

To put it in a more a pythonic way: how to make your machine go PING! (HINT: check the end of the article if you don’t get the reference.)

1. Using The Bell On The Terminal

There is a so-called bell character that you can use to issue a warning on a terminal. It is a nonprintable control code character, with the ASCII character code of 0x07 (BEL). You can trigger it by simply sending it to the terminal (note the backslash):

This is probably the most simple way of sounding a beep, though it is not 100% guaranteed to work on all systems. Most UNIX-like operating systems like macOS and Linux will recognize it, but depending on the current settings the bell might be muted or be represented as a flash on the screen (visual bell).

2. Using AppKit.NSBeep On MacOS

If you’re on a Mac, you can tap into the Objective-C libraries to generate a sound.

First you’ll need to install the PyObjC library:

Then you can simply use the AppKit interface the ring the default system sound, like so:

import AppKit AppKit.NSBeep()

3. Using winsound On Windows

On Windows operating systems you can use the winsound library. winsound needs no installation it is a builtin module on windows, so you should be able to access it by default

windound a have a handy Beep API, you can even choose the duration and the frequency of the beep. This is how you generate a 440Hz sound that lasts 500 milliseconds:

import winsound winsound.Beep(440, 500)

You can also play different windows system sound effects using the PlaySound method:

import winsound winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)

The same API can be used to play custom sound files using the SND_FILENAME flag instead of SND_ALIAS :

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

4. Playing Sound Files With pygame

Pygame is a modular Python library for developing video games. It provides a portable, cross-platform solution for a lot of video game and media related tasks, one of which is playing sound files.

To take advantage of this feature, first you’ll need to install pygame:

Then you can simply use the mixer the play an arbitrary sound file:

from pygame import mixer mixer.init() sound=mixer.Sound("bell.wav") sound.play()

Just like with the previous solution, you’ll need to provide you own sound file for this to work. This API supports OGG and WAV files.

5. Playing Sound Files With Simpleaudio

Simpleaudio is a cross-platform audio library for Python, you can use it to play audio files on Windows, OSX and Linux as well.

To install the simpleaudio package simply run:

Then use it to play the desired sound file:

import simpleaudio wave_obj = simpleaudio.WaveObject.from_wave_file("bell.wav") play_obj = wave_obj.play() play_obj.wait_done()

6. Use Package Made For Cross-Platform Beeping — Beepy

If you want a ready-made solution you can check out the beepy package. Basically it’s a thin wrapper around simpleaudio, that comes bundled together with a few audio files.

As always, you can install it with pip:

And then playing a beep sound is as simple as:

import beepy beep(sound="ping")

Summary

As you can see there are several different ways to go about beeping in Python, but which one is the best?

If you just want a quick and dirty solution I’d recommend trying to sound the terminal bell. If you want something more fancy or robust I’d go with winsound on windows or AppKit on a Mac. If you need a cross-platform solution your best bet will be using simpleaudio or pygame, to get a custom sound file played.

Congratulations, now you’ll be able to turn your computer into “The Machine That Goes PING”.

References

About the Author

Csongor Jozsa

Main author and editor at pythonin1minute.com. He’s been working as a Software Developer/Security Engineer since 2005. Currently Lead Engineer at MBA.

We’re looking for talented developers

  • Work with prestigious clients
  • Set your own rate and hours
  • Flexible schedule and time off
  • $2500 starting bonus

Источник

Оцените статью