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.
10 /* Modified by Guido van Rossum */
11 /* Beep added by Mark Hammond */
12 /* Win9X Beep and platform identification added by Uncle Timmy */
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...
34 # ...Before stopping it
35 winsound.PlaySound(None, 0)
42 PyDoc_STRVAR(sound_playsound_doc
,
43 "PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n"
45 "The sound argument can be a filename, data, or None.\n"
46 "For flag values, ored together, see module documentation.");
48 PyDoc_STRVAR(sound_beep_doc
,
49 "Beep(frequency, duration) - a wrapper around the Windows Beep API\n"
51 "The frequency argument specifies frequency, in hertz, of the sound.\n"
52 "This parameter must be in the range 37 through 32,767.\n"
53 "The duration argument specifies the number of milliseconds.\n");
55 PyDoc_STRVAR(sound_msgbeep_doc
,
56 "MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK.");
58 PyDoc_STRVAR(sound_module_doc
,
59 "PlaySound(sound, flags) - play a sound\n"
60 "SND_FILENAME - sound is a wav file name\n"
61 "SND_ALIAS - sound is a registry sound association name\n"
62 "SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n"
63 "SND_MEMORY - sound is a memory image of a wav file\n"
64 "SND_PURGE - stop all instances of the specified sound\n"
65 "SND_ASYNC - PlaySound returns immediately\n"
66 "SND_NODEFAULT - Do not play a default beep if the sound can not be found\n"
67 "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed
68 "SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
70 "Beep(frequency, duration) - Make a beep through the PC speaker.");
73 sound_playsound(PyObject
*s
, PyObject
*args
)
80 if(!PyArg_ParseTuple(args
,"z#i:PlaySound",&sound
,&length
,&flags
)) {
84 if(flags
&SND_ASYNC
&& flags
&SND_MEMORY
) {
85 /* Sidestep reference counting headache; unfortunately this also
86 prevent SND_LOOP from memory. */
87 PyErr_SetString(PyExc_RuntimeError
,"Cannot play asynchronously from memory");
91 Py_BEGIN_ALLOW_THREADS
92 ok
= PlaySound(sound
,NULL
,flags
);
96 PyErr_SetString(PyExc_RuntimeError
,"Failed to play sound");
105 sound_beep(PyObject
*self
, PyObject
*args
)
111 if (!PyArg_ParseTuple(args
, "ii:Beep", &freq
, &dur
))
114 if (freq
< 37 || freq
> 32767) {
115 PyErr_SetString(PyExc_ValueError
,
116 "frequency must be in 37 thru 32767");
120 Py_BEGIN_ALLOW_THREADS
121 ok
= Beep(freq
, dur
);
124 PyErr_SetString(PyExc_RuntimeError
,"Failed to beep");
133 sound_msgbeep(PyObject
*self
, PyObject
*args
)
136 if (!PyArg_ParseTuple(args
, "|i:MessageBeep", &x
))
143 static struct PyMethodDef sound_methods
[] =
145 {"PlaySound", sound_playsound
, METH_VARARGS
, sound_playsound_doc
},
146 {"Beep", sound_beep
, METH_VARARGS
, sound_beep_doc
},
147 {"MessageBeep", sound_msgbeep
, METH_VARARGS
, sound_msgbeep_doc
},
152 add_define(PyObject
*dict
, const char *key
, long value
)
154 PyObject
*k
=PyString_FromString(key
);
155 PyObject
*v
=PyLong_FromLong(value
);
158 PyDict_SetItem(dict
,k
,v
);
164 #define ADD_DEFINE(tok) add_define(dict,#tok,tok)
170 PyObject
*module
= Py_InitModule3("winsound",
175 dict
= PyModule_GetDict(module
);
177 ADD_DEFINE(SND_ASYNC
);
178 ADD_DEFINE(SND_NODEFAULT
);
179 ADD_DEFINE(SND_NOSTOP
);
180 ADD_DEFINE(SND_NOWAIT
);
181 ADD_DEFINE(SND_ALIAS
);
182 ADD_DEFINE(SND_FILENAME
);
183 ADD_DEFINE(SND_MEMORY
);
184 ADD_DEFINE(SND_PURGE
);
185 ADD_DEFINE(SND_LOOP
);
186 ADD_DEFINE(SND_APPLICATION
);
189 ADD_DEFINE(MB_ICONASTERISK
);
190 ADD_DEFINE(MB_ICONEXCLAMATION
);
191 ADD_DEFINE(MB_ICONHAND
);
192 ADD_DEFINE(MB_ICONQUESTION
);