blob: 923c7c4a1fc4598f0fbe1934c0fa5d656ac84f45 [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
23 .. note::
24
25 Under Windows 95 and 98, the Windows :cfunc:`Beep` function exists but is
26 useless (it ignores its arguments). In that case Python simulates it via direct
27 port manipulation (added in version 2.1). It's unknown whether that will work
28 on all systems.
29
Georg Brandl116aa622007-08-15 14:28:22 +000030
31.. function:: PlaySound(sound, flags)
32
33 Call the underlying :cfunc:`PlaySound` function from the Platform API. The
34 *sound* parameter may be a filename, audio data as a string, or ``None``. Its
35 interpretation depends on the value of *flags*, which can be a bit-wise ORed
36 combination of the constants described below. If the system indicates an error,
37 :exc:`RuntimeError` is raised.
38
39
40.. function:: MessageBeep([type=MB_OK])
41
42 Call the underlying :cfunc:`MessageBeep` function from the Platform API. This
43 plays a sound as specified in the registry. The *type* argument specifies which
44 sound to play; possible values are ``-1``, ``MB_ICONASTERISK``,
45 ``MB_ICONEXCLAMATION``, ``MB_ICONHAND``, ``MB_ICONQUESTION``, and ``MB_OK``, all
46 described below. The value ``-1`` produces a "simple beep"; this is the final
47 fallback if a sound cannot be played otherwise.
48
Georg Brandl116aa622007-08-15 14:28:22 +000049
50.. data:: SND_FILENAME
51
52 The *sound* parameter is the name of a WAV file. Do not use with
53 :const:`SND_ALIAS`.
54
55
56.. data:: SND_ALIAS
57
58 The *sound* parameter is a sound association name from the registry. If the
59 registry contains no such name, play the system default sound unless
60 :const:`SND_NODEFAULT` is also specified. If no default sound is registered,
61 raise :exc:`RuntimeError`. Do not use with :const:`SND_FILENAME`.
62
63 All Win32 systems support at least the following; most systems support many
64 more:
65
66 +--------------------------+----------------------------------------+
67 | :func:`PlaySound` *name* | Corresponding Control Panel Sound name |
68 +==========================+========================================+
69 | ``'SystemAsterisk'`` | Asterisk |
70 +--------------------------+----------------------------------------+
71 | ``'SystemExclamation'`` | Exclamation |
72 +--------------------------+----------------------------------------+
73 | ``'SystemExit'`` | Exit Windows |
74 +--------------------------+----------------------------------------+
75 | ``'SystemHand'`` | Critical Stop |
76 +--------------------------+----------------------------------------+
77 | ``'SystemQuestion'`` | Question |
78 +--------------------------+----------------------------------------+
79
80 For example::
81
82 import winsound
83 # Play Windows exit sound.
84 winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
85
86 # Probably play Windows default sound, if any is registered (because
87 # "*" probably isn't the registered name of any sound).
88 winsound.PlaySound("*", winsound.SND_ALIAS)
89
90
91.. data:: SND_LOOP
92
93 Play the sound repeatedly. The :const:`SND_ASYNC` flag must also be used to
94 avoid blocking. Cannot be used with :const:`SND_MEMORY`.
95
96
97.. data:: SND_MEMORY
98
99 The *sound* parameter to :func:`PlaySound` is a memory image of a WAV file, as a
100 string.
101
102 .. note::
103
104 This module does not support playing from a memory image asynchronously, so a
105 combination of this flag and :const:`SND_ASYNC` will raise :exc:`RuntimeError`.
106
107
108.. data:: SND_PURGE
109
110 Stop playing all instances of the specified sound.
111
112
113.. data:: SND_ASYNC
114
115 Return immediately, allowing sounds to play asynchronously.
116
117
118.. data:: SND_NODEFAULT
119
120 If the specified sound cannot be found, do not play the system default sound.
121
122
123.. data:: SND_NOSTOP
124
125 Do not interrupt sounds currently playing.
126
127
128.. data:: SND_NOWAIT
129
130 Return immediately if the sound driver is busy.
131
132
133.. data:: MB_ICONASTERISK
134
135 Play the ``SystemDefault`` sound.
136
137
138.. data:: MB_ICONEXCLAMATION
139
140 Play the ``SystemExclamation`` sound.
141
142
143.. data:: MB_ICONHAND
144
145 Play the ``SystemHand`` sound.
146
147
148.. data:: MB_ICONQUESTION
149
150 Play the ``SystemQuestion`` sound.
151
152
153.. data:: MB_OK
154
155 Play the ``SystemDefault`` sound.
156