Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`winsound` --- Sound-playing interface for Windows |
| 2 | ======================================================= |
| 3 | |
| 4 | .. module:: winsound |
| 5 | :platform: Windows |
| 6 | :synopsis: Access to the sound-playing machinery for Windows. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. moduleauthor:: Toby Dickenson <htrd90@zepler.org> |
| 9 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 10 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | The :mod:`winsound` module provides access to the basic sound-playing machinery |
| 14 | provided by Windows platforms. It includes functions and several constants. |
| 15 | |
| 16 | |
| 17 | .. function:: Beep(frequency, duration) |
| 18 | |
| 19 | Beep the PC's speaker. The *frequency* parameter specifies frequency, in hertz, |
| 20 | of the sound, and must be in the range 37 through 32,767. The *duration* |
| 21 | parameter specifies the number of milliseconds the sound should last. If the |
| 22 | system is not able to beep the speaker, :exc:`RuntimeError` is raised. |
| 23 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
| 25 | .. function:: PlaySound(sound, flags) |
| 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | Call the underlying :c:func:`PlaySound` function from the Platform API. The |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | *sound* parameter may be a filename, audio data as a string, or ``None``. Its |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 29 | interpretation depends on the value of *flags*, which can be a bitwise ORed |
Benjamin Peterson | da10d3b | 2009-01-01 00:23:30 +0000 | [diff] [blame] | 30 | combination of the constants described below. If the *sound* parameter is |
| 31 | ``None``, any currently playing waveform sound is stopped. If the system |
| 32 | indicates an error, :exc:`RuntimeError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 35 | .. function:: MessageBeep(type=MB_OK) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | Call the underlying :c:func:`MessageBeep` function from the Platform API. This |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | plays a sound as specified in the registry. The *type* argument specifies which |
| 39 | sound to play; possible values are ``-1``, ``MB_ICONASTERISK``, |
| 40 | ``MB_ICONEXCLAMATION``, ``MB_ICONHAND``, ``MB_ICONQUESTION``, and ``MB_OK``, all |
| 41 | described below. The value ``-1`` produces a "simple beep"; this is the final |
| 42 | fallback if a sound cannot be played otherwise. |
| 43 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
| 45 | .. data:: SND_FILENAME |
| 46 | |
| 47 | The *sound* parameter is the name of a WAV file. Do not use with |
| 48 | :const:`SND_ALIAS`. |
| 49 | |
| 50 | |
| 51 | .. data:: SND_ALIAS |
| 52 | |
| 53 | The *sound* parameter is a sound association name from the registry. If the |
| 54 | registry contains no such name, play the system default sound unless |
| 55 | :const:`SND_NODEFAULT` is also specified. If no default sound is registered, |
| 56 | raise :exc:`RuntimeError`. Do not use with :const:`SND_FILENAME`. |
| 57 | |
| 58 | All Win32 systems support at least the following; most systems support many |
| 59 | more: |
| 60 | |
| 61 | +--------------------------+----------------------------------------+ |
| 62 | | :func:`PlaySound` *name* | Corresponding Control Panel Sound name | |
| 63 | +==========================+========================================+ |
| 64 | | ``'SystemAsterisk'`` | Asterisk | |
| 65 | +--------------------------+----------------------------------------+ |
| 66 | | ``'SystemExclamation'`` | Exclamation | |
| 67 | +--------------------------+----------------------------------------+ |
| 68 | | ``'SystemExit'`` | Exit Windows | |
| 69 | +--------------------------+----------------------------------------+ |
| 70 | | ``'SystemHand'`` | Critical Stop | |
| 71 | +--------------------------+----------------------------------------+ |
| 72 | | ``'SystemQuestion'`` | Question | |
| 73 | +--------------------------+----------------------------------------+ |
| 74 | |
| 75 | For example:: |
| 76 | |
| 77 | import winsound |
| 78 | # Play Windows exit sound. |
| 79 | winsound.PlaySound("SystemExit", winsound.SND_ALIAS) |
| 80 | |
| 81 | # Probably play Windows default sound, if any is registered (because |
| 82 | # "*" probably isn't the registered name of any sound). |
| 83 | winsound.PlaySound("*", winsound.SND_ALIAS) |
| 84 | |
| 85 | |
| 86 | .. data:: SND_LOOP |
| 87 | |
| 88 | Play the sound repeatedly. The :const:`SND_ASYNC` flag must also be used to |
| 89 | avoid blocking. Cannot be used with :const:`SND_MEMORY`. |
| 90 | |
| 91 | |
| 92 | .. data:: SND_MEMORY |
| 93 | |
| 94 | The *sound* parameter to :func:`PlaySound` is a memory image of a WAV file, as a |
| 95 | string. |
| 96 | |
| 97 | .. note:: |
| 98 | |
| 99 | This module does not support playing from a memory image asynchronously, so a |
| 100 | combination of this flag and :const:`SND_ASYNC` will raise :exc:`RuntimeError`. |
| 101 | |
| 102 | |
| 103 | .. data:: SND_PURGE |
| 104 | |
| 105 | Stop playing all instances of the specified sound. |
| 106 | |
Benjamin Peterson | da10d3b | 2009-01-01 00:23:30 +0000 | [diff] [blame] | 107 | .. note:: |
| 108 | |
| 109 | This flag is not supported on modern Windows platforms. |
| 110 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
| 112 | .. data:: SND_ASYNC |
| 113 | |
| 114 | Return immediately, allowing sounds to play asynchronously. |
| 115 | |
| 116 | |
| 117 | .. data:: SND_NODEFAULT |
| 118 | |
| 119 | If the specified sound cannot be found, do not play the system default sound. |
| 120 | |
| 121 | |
| 122 | .. data:: SND_NOSTOP |
| 123 | |
| 124 | Do not interrupt sounds currently playing. |
| 125 | |
| 126 | |
| 127 | .. data:: SND_NOWAIT |
| 128 | |
| 129 | Return immediately if the sound driver is busy. |
| 130 | |
Georg Brandl | aeaecfd | 2013-10-13 10:49:41 +0200 | [diff] [blame] | 131 | .. note:: |
| 132 | |
| 133 | This flag is not supported on modern Windows platforms. |
| 134 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 135 | |
| 136 | .. data:: MB_ICONASTERISK |
| 137 | |
| 138 | Play the ``SystemDefault`` sound. |
| 139 | |
| 140 | |
| 141 | .. data:: MB_ICONEXCLAMATION |
| 142 | |
| 143 | Play the ``SystemExclamation`` sound. |
| 144 | |
| 145 | |
| 146 | .. data:: MB_ICONHAND |
| 147 | |
| 148 | Play the ``SystemHand`` sound. |
| 149 | |
| 150 | |
| 151 | .. data:: MB_ICONQUESTION |
| 152 | |
| 153 | Play the ``SystemQuestion`` sound. |
| 154 | |
| 155 | |
| 156 | .. data:: MB_OK |
| 157 | |
| 158 | Play the ``SystemDefault`` sound. |
| 159 | |