blob: 5f5b15e073f63b640cfb64baaa87329c918513a2 [file] [log] [blame]
Fred Drakec7b72db1999-02-16 19:18:38 +00001\section{\module{winsound} ---
2 Sound-playing interface for Windows}
3
4\declaremodule{builtin}{winsound}
Fred Drakef6863c11999-03-02 16:37:17 +00005 \platform{Windows}
Fred Drakec7b72db1999-02-16 19:18:38 +00006\modulesynopsis{Access to the sound-playing machinery for Windows.}
7\moduleauthor{Toby Dickenson}{htrd90@zepler.org}
8\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
9
Fred Drakef6346341999-02-19 15:46:38 +000010\versionadded{1.5.2}
Fred Drakec7b72db1999-02-16 19:18:38 +000011
12The \module{winsound} module provides access to the basic
Fred Drake714fd261999-10-22 21:08:56 +000013sound-playing machinery provided by Windows platforms. It includes
Fred Drakeceeb1912004-04-28 03:57:47 +000014functions and several constants.
Fred Drakec7b72db1999-02-16 19:18:38 +000015
16
Fred Drake714fd261999-10-22 21:08:56 +000017\begin{funcdesc}{Beep}{frequency, duration}
18 Beep the PC's speaker.
19 The \var{frequency} parameter specifies frequency, in hertz, of the
Tim Peters25a9ce32001-02-19 07:06:36 +000020 sound, and must be in the range 37 through 32,767.
21 The \var{duration} parameter specifies the number of milliseconds the
22 sound should last. If the system is not
Fred Drake714fd261999-10-22 21:08:56 +000023 able to beep the speaker, \exception{RuntimeError} is raised.
Fred Drake0aa811c2001-10-20 04:24:09 +000024 \note{Under Windows 95 and 98, the Windows \cfunction{Beep()}
Tim Peters373d1512001-02-19 08:36:41 +000025 function exists but is useless (it ignores its arguments). In that
Tim Peters25a9ce32001-02-19 07:06:36 +000026 case Python simulates it via direct port manipulation (added in version
Fred Drake0aa811c2001-10-20 04:24:09 +000027 2.1). It's unknown whether that will work on all systems.}
Fred Drake29052482001-01-25 17:29:18 +000028 \versionadded{1.6}
Fred Drake714fd261999-10-22 21:08:56 +000029\end{funcdesc}
30
Fred Drakec7b72db1999-02-16 19:18:38 +000031\begin{funcdesc}{PlaySound}{sound, flags}
32 Call the underlying \cfunction{PlaySound()} function from the
33 Platform API. The \var{sound} parameter may be a filename, audio
34 data as a string, or \code{None}. Its interpretation depends on the
35 value of \var{flags}, which can be a bit-wise ORed combination of
36 the constants described below. If the system indicates an error,
37 \exception{RuntimeError} is raised.
38\end{funcdesc}
39
Guido van Rossum8f512a22003-05-16 01:42:22 +000040\begin{funcdesc}{MessageBeep}{\optional{type=\code{MB_OK}}}
41 Call the underlying \cfunction{MessageBeep()} function from the
42 Platform API. This plays a sound as specified in the registry. The
43 \var{type} argument specifies which sound to play; possible values
44 are \code{-1}, \code{MB_ICONASTERISK}, \code{MB_ICONEXCLAMATION},
45 \code{MB_ICONHAND}, \code{MB_ICONQUESTION}, and \code{MB_OK}, all
46 described below. The value \code{-1} produces a ``simple beep'';
47 this is the final fallback if a sound cannot be played otherwise.
48 \versionadded{2.3}
49\end{funcdesc}
Fred Drakec7b72db1999-02-16 19:18:38 +000050
51\begin{datadesc}{SND_FILENAME}
52 The \var{sound} parameter is the name of a WAV file.
Tim Peterse79af272001-02-20 10:02:21 +000053 Do not use with \constant{SND_ALIAS}.
Fred Drakec7b72db1999-02-16 19:18:38 +000054\end{datadesc}
55
56\begin{datadesc}{SND_ALIAS}
Fred Drakeafdc8fc2001-02-23 19:10:41 +000057 The \var{sound} parameter is a sound association name from the
58 registry. If the registry contains no such name, play the system
59 default sound unless \constant{SND_NODEFAULT} is also specified.
60 If no default sound is registered, raise \exception{RuntimeError}.
61 Do not use with \constant{SND_FILENAME}.
Tim Peterse79af272001-02-20 10:02:21 +000062
63 All Win32 systems support at least the following; most systems support
64 many more:
65
Fred Drakeafdc8fc2001-02-23 19:10:41 +000066\begin{tableii}{l|l}{code}
67 {\function{PlaySound()} \var{name}}
68 {Corresponding Control Panel Sound name}
69 \lineii{'SystemAsterisk'} {Asterisk}
70 \lineii{'SystemExclamation'}{Exclamation}
71 \lineii{'SystemExit'} {Exit Windows}
72 \lineii{'SystemHand'} {Critical Stop}
73 \lineii{'SystemQuestion'} {Question}
74\end{tableii}
Tim Peterse79af272001-02-20 10:02:21 +000075
Fred Drakeafdc8fc2001-02-23 19:10:41 +000076 For example:
Tim Peterse79af272001-02-20 10:02:21 +000077
Fred Drakeafdc8fc2001-02-23 19:10:41 +000078\begin{verbatim}
79import winsound
80# Play Windows exit sound.
81winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
Tim Peterse79af272001-02-20 10:02:21 +000082
Fred Drakeafdc8fc2001-02-23 19:10:41 +000083# Probably play Windows default sound, if any is registered (because
84# "*" probably isn't the registered name of any sound).
85winsound.PlaySound("*", winsound.SND_ALIAS)
86\end{verbatim}
Fred Drakec7b72db1999-02-16 19:18:38 +000087\end{datadesc}
88
89\begin{datadesc}{SND_LOOP}
90 Play the sound repeatedly. The \constant{SND_ASYNC} flag must also
Tim Peterse79af272001-02-20 10:02:21 +000091 be used to avoid blocking. Cannot be used with \constant{SND_MEMORY}.
Fred Drakec7b72db1999-02-16 19:18:38 +000092\end{datadesc}
93
94\begin{datadesc}{SND_MEMORY}
95 The \var{sound} parameter to \function{PlaySound()} is a memory
Tim Peterse79af272001-02-20 10:02:21 +000096 image of a WAV file, as a string.
Fred Drakec7b72db1999-02-16 19:18:38 +000097
Fred Drake0aa811c2001-10-20 04:24:09 +000098 \note{This module does not support playing from a memory
Thomas Woutersf8316632000-07-16 19:01:10 +000099 image asynchronously, so a combination of this flag and
Fred Drake0aa811c2001-10-20 04:24:09 +0000100 \constant{SND_ASYNC} will raise \exception{RuntimeError}.}
Fred Drakec7b72db1999-02-16 19:18:38 +0000101\end{datadesc}
102
103\begin{datadesc}{SND_PURGE}
104 Stop playing all instances of the specified sound.
105\end{datadesc}
106
107\begin{datadesc}{SND_ASYNC}
108 Return immediately, allowing sounds to play asynchronously.
109\end{datadesc}
110
111\begin{datadesc}{SND_NODEFAULT}
Tim Peterse79af272001-02-20 10:02:21 +0000112 If the specified sound cannot be found, do not play the system default
113 sound.
Fred Drakec7b72db1999-02-16 19:18:38 +0000114\end{datadesc}
115
116\begin{datadesc}{SND_NOSTOP}
117 Do not interrupt sounds currently playing.
118\end{datadesc}
119
120\begin{datadesc}{SND_NOWAIT}
121 Return immediately if the sound driver is busy.
122\end{datadesc}
Guido van Rossum8f512a22003-05-16 01:42:22 +0000123
124\begin{datadesc}{MB_ICONASTERISK}
125 Play the \code{SystemDefault} sound.
126\end{datadesc}
127
128\begin{datadesc}{MB_ICONEXCLAMATION}
129 Play the \code{SystemExclamation} sound.
130\end{datadesc}
131
132\begin{datadesc}{MB_ICONHAND}
133 Play the \code{SystemHand} sound.
134\end{datadesc}
135
136\begin{datadesc}{MB_ICONQUESTION}
137 Play the \code{SystemQuestion} sound.
138\end{datadesc}
139
140\begin{datadesc}{MB_OK}
141 Play the \code{SystemDefault} sound.
142\end{datadesc}