Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 1 | \section{\module{winsound} --- |
| 2 | Sound-playing interface for Windows} |
| 3 | |
| 4 | \declaremodule{builtin}{winsound} |
Fred Drake | f6863c1 | 1999-03-02 16:37:17 +0000 | [diff] [blame] | 5 | \platform{Windows} |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 6 | \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 Drake | f634634 | 1999-02-19 15:46:38 +0000 | [diff] [blame] | 10 | \versionadded{1.5.2} |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 11 | |
| 12 | The \module{winsound} module provides access to the basic |
Fred Drake | 714fd26 | 1999-10-22 21:08:56 +0000 | [diff] [blame] | 13 | sound-playing machinery provided by Windows platforms. It includes |
| 14 | two functions and several constants. |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 15 | |
| 16 | |
Fred Drake | 714fd26 | 1999-10-22 21:08:56 +0000 | [diff] [blame] | 17 | \begin{funcdesc}{Beep}{frequency, duration} |
| 18 | Beep the PC's speaker. |
| 19 | The \var{frequency} parameter specifies frequency, in hertz, of the |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 20 | 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 Drake | 714fd26 | 1999-10-22 21:08:56 +0000 | [diff] [blame] | 23 | able to beep the speaker, \exception{RuntimeError} is raised. |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 24 | \note{Under Windows 95 and 98, the Windows \cfunction{Beep()} |
Tim Peters | 373d151 | 2001-02-19 08:36:41 +0000 | [diff] [blame] | 25 | function exists but is useless (it ignores its arguments). In that |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 26 | case Python simulates it via direct port manipulation (added in version |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 27 | 2.1). It's unknown whether that will work on all systems.} |
Fred Drake | 2905248 | 2001-01-25 17:29:18 +0000 | [diff] [blame] | 28 | \versionadded{1.6} |
Fred Drake | 714fd26 | 1999-10-22 21:08:56 +0000 | [diff] [blame] | 29 | \end{funcdesc} |
| 30 | |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 31 | \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 | |
| 40 | |
| 41 | \begin{datadesc}{SND_FILENAME} |
| 42 | The \var{sound} parameter is the name of a WAV file. |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 43 | Do not use with \constant{SND_ALIAS}. |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 44 | \end{datadesc} |
| 45 | |
| 46 | \begin{datadesc}{SND_ALIAS} |
Fred Drake | afdc8fc | 2001-02-23 19:10:41 +0000 | [diff] [blame] | 47 | The \var{sound} parameter is a sound association name from the |
| 48 | registry. If the registry contains no such name, play the system |
| 49 | default sound unless \constant{SND_NODEFAULT} is also specified. |
| 50 | If no default sound is registered, raise \exception{RuntimeError}. |
| 51 | Do not use with \constant{SND_FILENAME}. |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 52 | |
| 53 | All Win32 systems support at least the following; most systems support |
| 54 | many more: |
| 55 | |
Fred Drake | afdc8fc | 2001-02-23 19:10:41 +0000 | [diff] [blame] | 56 | \begin{tableii}{l|l}{code} |
| 57 | {\function{PlaySound()} \var{name}} |
| 58 | {Corresponding Control Panel Sound name} |
| 59 | \lineii{'SystemAsterisk'} {Asterisk} |
| 60 | \lineii{'SystemExclamation'}{Exclamation} |
| 61 | \lineii{'SystemExit'} {Exit Windows} |
| 62 | \lineii{'SystemHand'} {Critical Stop} |
| 63 | \lineii{'SystemQuestion'} {Question} |
| 64 | \end{tableii} |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 65 | |
Fred Drake | afdc8fc | 2001-02-23 19:10:41 +0000 | [diff] [blame] | 66 | For example: |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 67 | |
Fred Drake | afdc8fc | 2001-02-23 19:10:41 +0000 | [diff] [blame] | 68 | \begin{verbatim} |
| 69 | import winsound |
| 70 | # Play Windows exit sound. |
| 71 | winsound.PlaySound("SystemExit", winsound.SND_ALIAS) |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 72 | |
Fred Drake | afdc8fc | 2001-02-23 19:10:41 +0000 | [diff] [blame] | 73 | # Probably play Windows default sound, if any is registered (because |
| 74 | # "*" probably isn't the registered name of any sound). |
| 75 | winsound.PlaySound("*", winsound.SND_ALIAS) |
| 76 | \end{verbatim} |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 77 | \end{datadesc} |
| 78 | |
| 79 | \begin{datadesc}{SND_LOOP} |
| 80 | Play the sound repeatedly. The \constant{SND_ASYNC} flag must also |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 81 | be used to avoid blocking. Cannot be used with \constant{SND_MEMORY}. |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 82 | \end{datadesc} |
| 83 | |
| 84 | \begin{datadesc}{SND_MEMORY} |
| 85 | The \var{sound} parameter to \function{PlaySound()} is a memory |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 86 | image of a WAV file, as a string. |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 87 | |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 88 | \note{This module does not support playing from a memory |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 89 | image asynchronously, so a combination of this flag and |
Fred Drake | 0aa811c | 2001-10-20 04:24:09 +0000 | [diff] [blame] | 90 | \constant{SND_ASYNC} will raise \exception{RuntimeError}.} |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 91 | \end{datadesc} |
| 92 | |
| 93 | \begin{datadesc}{SND_PURGE} |
| 94 | Stop playing all instances of the specified sound. |
| 95 | \end{datadesc} |
| 96 | |
| 97 | \begin{datadesc}{SND_ASYNC} |
| 98 | Return immediately, allowing sounds to play asynchronously. |
| 99 | \end{datadesc} |
| 100 | |
| 101 | \begin{datadesc}{SND_NODEFAULT} |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 102 | If the specified sound cannot be found, do not play the system default |
| 103 | sound. |
Fred Drake | c7b72db | 1999-02-16 19:18:38 +0000 | [diff] [blame] | 104 | \end{datadesc} |
| 105 | |
| 106 | \begin{datadesc}{SND_NOSTOP} |
| 107 | Do not interrupt sounds currently playing. |
| 108 | \end{datadesc} |
| 109 | |
| 110 | \begin{datadesc}{SND_NOWAIT} |
| 111 | Return immediately if the sound driver is busy. |
| 112 | \end{datadesc} |