blob: bc30ccc584f2d456778a545a3f47ee8a45f87b74 [file] [log] [blame]
Guido van Rossume6005781999-02-04 22:40:42 +00001/* Author: Toby Dickenson <htrd90@zepler.org>
2 *
3 * Copyright (c) 1999 Toby Dickenson
4 *
5 * Permission to use this software in any way is granted without
6 * fee, provided that the copyright notice above appears in all
7 * copies. This software is provided "as is" without any warranty.
8 */
9
10/* Modified by Guido van Rossum */
Guido van Rossum99eb7a11999-10-01 14:29:17 +000011/* Beep added by Mark Hammond */
Tim Peters25a9ce32001-02-19 07:06:36 +000012/* Win9X Beep and platform identification added by Uncle Timmy */
Guido van Rossume6005781999-02-04 22:40:42 +000013
14/* Example:
15
16 import winsound
17 import time
18
Tim Peters25a9ce32001-02-19 07:06:36 +000019 # Play wav file
Guido van Rossume6005781999-02-04 22:40:42 +000020 winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME)
21
22 # Play sound from control panel settings
23 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
24
25 # Play wav file from memory
26 data=open('c:/windows/media/Chimes.wav',"rb").read()
27 winsound.PlaySound(data, winsound.SND_MEMORY)
28
29 # Start playing the first bit of wav file asynchronously
30 winsound.PlaySound('c:/windows/media/Chord.wav',
31 winsound.SND_FILENAME|winsound.SND_ASYNC)
32 # But dont let it go for too long...
33 time.sleep(0.1)
34 # ...Before stopping it
35 winsound.PlaySound(None, 0)
36*/
37
38#include <windows.h>
39#include <mmsystem.h>
Tim Peters25a9ce32001-02-19 07:06:36 +000040#include <conio.h> /* port functions on Win9x */
Guido van Rossume6005781999-02-04 22:40:42 +000041#include <Python.h>
42
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043PyDoc_STRVAR(sound_playsound_doc,
Guido van Rossume6005781999-02-04 22:40:42 +000044"PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n"
45"\n"
46"The sound argument can be a filename, data, or None.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047"For flag values, ored together, see module documentation.");
Guido van Rossume6005781999-02-04 22:40:42 +000048
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049PyDoc_STRVAR(sound_beep_doc,
Guido van Rossum99eb7a11999-10-01 14:29:17 +000050"Beep(frequency, duration) - a wrapper around the Windows Beep API\n"
51"\n"
52"The frequency argument specifies frequency, in hertz, of the sound.\n"
Tim Peters25a9ce32001-02-19 07:06:36 +000053"This parameter must be in the range 37 through 32,767.\n"
54"The duration argument specifies the number of milliseconds.\n"
55"On WinNT and 2000, the platform Beep API is used directly. Else funky\n"
56"code doing direct port manipulation is used; it's unknown whether that\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057"will work on all systems.");
Guido van Rossum99eb7a11999-10-01 14:29:17 +000058
Guido van Rossume1252682003-04-09 19:38:08 +000059PyDoc_STRVAR(sound_msgbeep_doc,
60"MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK.");
61
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000062PyDoc_STRVAR(sound_module_doc,
Guido van Rossume6005781999-02-04 22:40:42 +000063"PlaySound(sound, flags) - play a sound\n"
64"SND_FILENAME - sound is a wav file name\n"
Tim Peterse79af272001-02-20 10:02:21 +000065"SND_ALIAS - sound is a registry sound association name\n"
Guido van Rossume6005781999-02-04 22:40:42 +000066"SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n"
67"SND_MEMORY - sound is a memory image of a wav file\n"
68"SND_PURGE - stop all instances of the specified sound\n"
69"SND_ASYNC - PlaySound returns immediately\n"
70"SND_NODEFAULT - Do not play a default beep if the sound can not be found\n"
71"SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed
72"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
Guido van Rossum99eb7a11999-10-01 14:29:17 +000073"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000074"Beep(frequency, duration) - Make a beep through the PC speaker.");
Guido van Rossume6005781999-02-04 22:40:42 +000075
Walter Dörwaldfee100402003-05-22 17:22:54 +000076static PyObject *
Tim Peters25a9ce32001-02-19 07:06:36 +000077sound_playsound(PyObject *s, PyObject *args)
Guido van Rossume6005781999-02-04 22:40:42 +000078{
79 const char *sound;
80 int flags;
81 int length;
82 int ok;
83
Tim Peters25a9ce32001-02-19 07:06:36 +000084 if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags)) {
Guido van Rossume6005781999-02-04 22:40:42 +000085 return NULL;
86 }
87
Tim Peters25a9ce32001-02-19 07:06:36 +000088 if(flags&SND_ASYNC && flags &SND_MEMORY) {
Guido van Rossume6005781999-02-04 22:40:42 +000089 /* Sidestep reference counting headache; unfortunately this also
90 prevent SND_LOOP from memory. */
91 PyErr_SetString(PyExc_RuntimeError,"Cannot play asynchronously from memory");
92 return NULL;
93 }
94
95 Py_BEGIN_ALLOW_THREADS
96 ok = PlaySound(sound,NULL,flags);
97 Py_END_ALLOW_THREADS
98 if(!ok)
99 {
100 PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
101 return NULL;
102 }
103
104 Py_INCREF(Py_None);
105 return Py_None;
106}
107
Tim Peters25a9ce32001-02-19 07:06:36 +0000108enum OSType {Win9X, WinNT2000};
109static enum OSType whichOS; /* set by module init */
110
111static PyObject *
112sound_beep(PyObject *self, PyObject *args)
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000113{
114 int freq;
115 int dur;
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000116
117 if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur))
118 return NULL;
Tim Peters25a9ce32001-02-19 07:06:36 +0000119
120 if (freq < 37 || freq > 32767) {
121 PyErr_SetString(PyExc_ValueError,
122 "frequency must be in 37 thru 32767");
123 return NULL;
124 }
125
126 /* On NT and 2000, the SDK Beep() function does the whole job.
127 * But while Beep() exists before NT, it ignores its arguments and
128 * plays the system default sound. Sheesh ...
129 * The Win9X code is mondo bizarre. I (Tim) pieced it together from
130 * crap all over the web. The original IBM PC used some particular
131 * pieces of hardware (Intel 8255 and 8254 chips) hardwired to
132 * particular port addresses and running at particular clock speeds,
133 * and the poor sound card folks have been forced to emulate that in
134 * all particulars ever since. But NT and 2000 don't support port
Tim Peters373d1512001-02-19 08:36:41 +0000135 * manipulation. Don't know about WinME; guessing it's like 98.
Tim Peters25a9ce32001-02-19 07:06:36 +0000136 */
137
138 if (whichOS == WinNT2000) {
139 BOOL ok;
140 Py_BEGIN_ALLOW_THREADS
141 ok = Beep(freq, dur);
142 Py_END_ALLOW_THREADS
143 if (!ok) {
144 PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
145 return NULL;
146 }
147 }
Martin v. Löwis30b49752005-11-29 17:08:24 +0000148#ifdef _M_IX86
Tim Peters25a9ce32001-02-19 07:06:36 +0000149 else if (whichOS == Win9X) {
150 int speaker_state;
151 /* Force timer into oscillator mode via timer control port. */
152 _outp(0x43, 0xb6);
153 /* Compute ratio of ancient hardcoded timer frequency to
154 * frequency we want. Then feed that ratio (lowest byte
155 * first) into timer data port.
156 */
157 freq = 1193180 / freq;
158 _outp(0x42, freq & 0xff);
159 _outp(0x42, (freq >> 8) & 0xff);
160 /* Get speaker control state. */
161 speaker_state = _inp(0x61);
162 /* Turn the speaker on (bit 1)
163 * and drive speaker from timer (bit 0).
164 */
165 _outp(0x61, speaker_state | 0x3);
166 /* Let it blast in peace for the duration. */
167 Py_BEGIN_ALLOW_THREADS
168 Sleep(dur);
169 Py_END_ALLOW_THREADS
170 /* Restore speaker control to original state. */
171 _outp(0x61, speaker_state);
172 }
Martin v. Löwis30b49752005-11-29 17:08:24 +0000173#endif /* _M_IX86 */
Tim Peters25a9ce32001-02-19 07:06:36 +0000174 else {
175 assert(!"winsound's whichOS has insane value");
176 }
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000177 Py_INCREF(Py_None);
178 return Py_None;
179}
180
Guido van Rossume1252682003-04-09 19:38:08 +0000181static PyObject *
182sound_msgbeep(PyObject *self, PyObject *args)
183{
184 int x = MB_OK;
185 if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x))
186 return NULL;
187 MessageBeep(x);
188 Py_INCREF(Py_None);
189 return Py_None;
190}
191
Guido van Rossume6005781999-02-04 22:40:42 +0000192static struct PyMethodDef sound_methods[] =
193{
Neal Norwitz031829d2002-03-31 14:37:44 +0000194 {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc},
195 {"Beep", sound_beep, METH_VARARGS, sound_beep_doc},
Guido van Rossume1252682003-04-09 19:38:08 +0000196 {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc},
Guido van Rossume6005781999-02-04 22:40:42 +0000197 {NULL, NULL}
198};
199
Tim Peters25a9ce32001-02-19 07:06:36 +0000200static void
201add_define(PyObject *dict, const char *key, long value)
Guido van Rossume6005781999-02-04 22:40:42 +0000202{
203 PyObject *k=PyString_FromString(key);
204 PyObject *v=PyLong_FromLong(value);
205 if(v&&k)
206 {
207 PyDict_SetItem(dict,k,v);
208 }
209 Py_XDECREF(k);
210 Py_XDECREF(v);
211}
212
213#define ADD_DEFINE(tok) add_define(dict,#tok,tok)
214
Mark Hammonde407e2a2002-07-22 13:26:41 +0000215PyMODINIT_FUNC
Thomas Wouters78890102000-07-22 19:25:51 +0000216initwinsound(void)
Guido van Rossume6005781999-02-04 22:40:42 +0000217{
Tim Peters25a9ce32001-02-19 07:06:36 +0000218 OSVERSIONINFO version;
Guido van Rossume6005781999-02-04 22:40:42 +0000219
Tim Peters773feaf2006-01-19 15:25:07 +0000220 PyObject *dict;
Tim Peters25a9ce32001-02-19 07:06:36 +0000221 PyObject *module = Py_InitModule3("winsound",
222 sound_methods,
223 sound_module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000224 if (module == NULL)
225 return;
Tim Peters773feaf2006-01-19 15:25:07 +0000226 dict = PyModule_GetDict(module);
Tim Peters25a9ce32001-02-19 07:06:36 +0000227
228 ADD_DEFINE(SND_ASYNC);
229 ADD_DEFINE(SND_NODEFAULT);
230 ADD_DEFINE(SND_NOSTOP);
231 ADD_DEFINE(SND_NOWAIT);
232 ADD_DEFINE(SND_ALIAS);
233 ADD_DEFINE(SND_FILENAME);
234 ADD_DEFINE(SND_MEMORY);
235 ADD_DEFINE(SND_PURGE);
236 ADD_DEFINE(SND_LOOP);
237 ADD_DEFINE(SND_APPLICATION);
238
Guido van Rossume1252682003-04-09 19:38:08 +0000239 ADD_DEFINE(MB_OK);
240 ADD_DEFINE(MB_ICONASTERISK);
241 ADD_DEFINE(MB_ICONEXCLAMATION);
242 ADD_DEFINE(MB_ICONHAND);
243 ADD_DEFINE(MB_ICONQUESTION);
244
Tim Peters25a9ce32001-02-19 07:06:36 +0000245 version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
246 GetVersionEx(&version);
Tim Peters3e502422001-02-19 07:33:23 +0000247 whichOS = Win9X;
Tim Peters25a9ce32001-02-19 07:06:36 +0000248 if (version.dwPlatformId != VER_PLATFORM_WIN32s &&
249 version.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
250 whichOS = WinNT2000;
Guido van Rossume6005781999-02-04 22:40:42 +0000251}