_make_boundary(): Fix for SF bug #745478, broken boundary calculation
[python/dscho.git] / PC / winsound.c
blobc6f3a534fa1f571bcd97f22cfdd464c4bb3d2dd9
1 /* Author: Toby Dickenson <htrd90@zepler.org>
3 * Copyright (c) 1999 Toby Dickenson
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 */
10 /* Modified by Guido van Rossum */
11 /* Beep added by Mark Hammond */
12 /* Win9X Beep and platform identification added by Uncle Timmy */
14 /* Example:
16 import winsound
17 import time
19 # Play wav file
20 winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME)
22 # Play sound from control panel settings
23 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
25 # Play wav file from memory
26 data=open('c:/windows/media/Chimes.wav',"rb").read()
27 winsound.PlaySound(data, winsound.SND_MEMORY)
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)
38 #include <windows.h>
39 #include <mmsystem.h>
40 #include <conio.h> /* port functions on Win9x */
41 #include <Python.h>
43 PyDoc_STRVAR(sound_playsound_doc,
44 "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"
47 "For flag values, ored together, see module documentation.");
49 PyDoc_STRVAR(sound_beep_doc,
50 "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"
53 "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"
57 "will work on all systems.");
59 PyDoc_STRVAR(sound_msgbeep_doc,
60 "MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK.");
62 PyDoc_STRVAR(sound_module_doc,
63 "PlaySound(sound, flags) - play a sound\n"
64 "SND_FILENAME - sound is a wav file name\n"
65 "SND_ALIAS - sound is a registry sound association name\n"
66 "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
73 "\n"
74 "Beep(frequency, duration) - Make a beep through the PC speaker.");
76 static PyObject *
77 sound_playsound(PyObject *s, PyObject *args)
79 const char *sound;
80 int flags;
81 int length;
82 int ok;
84 if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags)) {
85 return NULL;
88 if(flags&SND_ASYNC && flags &SND_MEMORY) {
89 /* 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;
95 Py_BEGIN_ALLOW_THREADS
96 ok = PlaySound(sound,NULL,flags);
97 Py_END_ALLOW_THREADS
98 if(!ok)
100 PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
101 return NULL;
104 Py_INCREF(Py_None);
105 return Py_None;
108 enum OSType {Win9X, WinNT2000};
109 static enum OSType whichOS; /* set by module init */
111 static PyObject *
112 sound_beep(PyObject *self, PyObject *args)
114 int freq;
115 int dur;
117 if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur))
118 return NULL;
120 if (freq < 37 || freq > 32767) {
121 PyErr_SetString(PyExc_ValueError,
122 "frequency must be in 37 thru 32767");
123 return NULL;
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
135 * manipulation. Don't know about WinME; guessing it's like 98.
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;
148 else if (whichOS == Win9X) {
149 int speaker_state;
150 /* Force timer into oscillator mode via timer control port. */
151 _outp(0x43, 0xb6);
152 /* Compute ratio of ancient hardcoded timer frequency to
153 * frequency we want. Then feed that ratio (lowest byte
154 * first) into timer data port.
156 freq = 1193180 / freq;
157 _outp(0x42, freq & 0xff);
158 _outp(0x42, (freq >> 8) & 0xff);
159 /* Get speaker control state. */
160 speaker_state = _inp(0x61);
161 /* Turn the speaker on (bit 1)
162 * and drive speaker from timer (bit 0).
164 _outp(0x61, speaker_state | 0x3);
165 /* Let it blast in peace for the duration. */
166 Py_BEGIN_ALLOW_THREADS
167 Sleep(dur);
168 Py_END_ALLOW_THREADS
169 /* Restore speaker control to original state. */
170 _outp(0x61, speaker_state);
172 else {
173 assert(!"winsound's whichOS has insane value");
175 Py_INCREF(Py_None);
176 return Py_None;
179 static PyObject *
180 sound_msgbeep(PyObject *self, PyObject *args)
182 int x = MB_OK;
183 if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x))
184 return NULL;
185 MessageBeep(x);
186 Py_INCREF(Py_None);
187 return Py_None;
190 static struct PyMethodDef sound_methods[] =
192 {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc},
193 {"Beep", sound_beep, METH_VARARGS, sound_beep_doc},
194 {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc},
195 {NULL, NULL}
198 static void
199 add_define(PyObject *dict, const char *key, long value)
201 PyObject *k=PyString_FromString(key);
202 PyObject *v=PyLong_FromLong(value);
203 if(v&&k)
205 PyDict_SetItem(dict,k,v);
207 Py_XDECREF(k);
208 Py_XDECREF(v);
211 #define ADD_DEFINE(tok) add_define(dict,#tok,tok)
213 PyMODINIT_FUNC
214 initwinsound(void)
216 OSVERSIONINFO version;
218 PyObject *module = Py_InitModule3("winsound",
219 sound_methods,
220 sound_module_doc);
221 PyObject *dict = PyModule_GetDict(module);
223 ADD_DEFINE(SND_ASYNC);
224 ADD_DEFINE(SND_NODEFAULT);
225 ADD_DEFINE(SND_NOSTOP);
226 ADD_DEFINE(SND_NOWAIT);
227 ADD_DEFINE(SND_ALIAS);
228 ADD_DEFINE(SND_FILENAME);
229 ADD_DEFINE(SND_MEMORY);
230 ADD_DEFINE(SND_PURGE);
231 ADD_DEFINE(SND_LOOP);
232 ADD_DEFINE(SND_APPLICATION);
234 ADD_DEFINE(MB_OK);
235 ADD_DEFINE(MB_ICONASTERISK);
236 ADD_DEFINE(MB_ICONEXCLAMATION);
237 ADD_DEFINE(MB_ICONHAND);
238 ADD_DEFINE(MB_ICONQUESTION);
240 version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
241 GetVersionEx(&version);
242 whichOS = Win9X;
243 if (version.dwPlatformId != VER_PLATFORM_WIN32s &&
244 version.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
245 whichOS = WinNT2000;