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