blob: d2c421047ed07b3b83d848c82aba67ed15afbb68 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. moduleauthor:: Toby Dickenson <htrd90@zepler.org>
9.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
10
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040011--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013The :mod:`winsound` module provides access to the basic sound-playing machinery
14provided 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 Brandl116aa622007-08-15 14:28:22 +000024
25.. function:: PlaySound(sound, flags)
26
Georg Brandl60203b42010-10-06 10:11:56 +000027 Call the underlying :c:func:`PlaySound` function from the Platform API. The
Georg Brandl116aa622007-08-15 14:28:22 +000028 *sound* parameter may be a filename, audio data as a string, or ``None``. Its
Christian Heimesfaf2f632008-01-06 16:59:19 +000029 interpretation depends on the value of *flags*, which can be a bitwise ORed
Benjamin Petersonda10d3b2009-01-01 00:23:30 +000030 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 Brandl116aa622007-08-15 14:28:22 +000033
34
Georg Brandl7f01a132009-09-16 15:58:14 +000035.. function:: MessageBeep(type=MB_OK)
Georg Brandl116aa622007-08-15 14:28:22 +000036
Georg Brandl60203b42010-10-06 10:11:56 +000037 Call the underlying :c:func:`MessageBeep` function from the Platform API. This
Georg Brandl116aa622007-08-15 14:28:22 +000038 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 Brandl116aa622007-08-15 14:28:22 +000044
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 Petersonda10d3b2009-01-01 00:23:30 +0000107 .. note::
108
109 This flag is not supported on modern Windows platforms.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
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 Brandlaeaecfd2013-10-13 10:49:41 +0200131 .. note::
132
133 This flag is not supported on modern Windows platforms.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
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