| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <Python.h> |
| 39 |
#include <windows.h> |
| 40 |
#include <mmsystem.h> |
| 41 |
|
| 42 |
PyDoc_STRVAR(sound_playsound_doc, |
| 43 |
"PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n" |
| 44 |
"\n" |
| 45 |
"The sound argument can be a filename, data, or None.\n" |
| 46 |
"For flag values, ored together, see module documentation."); |
| 47 |
|
| 48 |
PyDoc_STRVAR(sound_beep_doc, |
| 49 |
"Beep(frequency, duration) - a wrapper around the Windows Beep API\n" |
| 50 |
"\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"); |
| 54 |
|
| 55 |
PyDoc_STRVAR(sound_msgbeep_doc, |
| 56 |
"MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK."); |
| 57 |
|
| 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" |
| 68 |
"SND_NOWAIT - Return immediately if the sound driver is busy\n" |
| 69 |
"\n" |
| 70 |
"Beep(frequency, duration) - Make a beep through the PC speaker."); |
| 71 |
|
| 72 |
static PyObject * |
| 73 |
sound_playsound(PyObject *s, PyObject *args) |
| 74 |
{ |
| 75 |
const char *sound; |
| 76 |
int flags; |
| 77 |
int length; |
| 78 |
int ok; |
| 79 |
|
| 80 |
if (!PyArg_ParseTuple(args, "z#i:PlaySound", &sound, &length, &flags)) { |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
if (flags & SND_ASYNC && flags & SND_MEMORY) { |
| 85 |
|
| 86 |
|
| 87 |
PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
|
| 91 |
Py_BEGIN_ALLOW_THREADS |
| 92 |
ok = PlaySound(sound, NULL, flags); |
| 93 |
Py_END_ALLOW_THREADS |
| 94 |
if (!ok) { |
| 95 |
PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
Py_INCREF(Py_None); |
| 100 |
return Py_None; |
| 101 |
} |
| 102 |
|
| 103 |
static PyObject * |
| 104 |
sound_beep(PyObject *self, PyObject *args) |
| 105 |
{ |
| 106 |
int freq; |
| 107 |
int dur; |
| 108 |
BOOL ok; |
| 109 |
|
| 110 |
if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) |
| 111 |
return NULL; |
| 112 |
|
| 113 |
if (freq < 37 || freq > 32767) { |
| 114 |
PyErr_SetString(PyExc_ValueError, |
| 115 |
"frequency must be in 37 thru 32767"); |
| 116 |
return NULL; |
| 117 |
} |
| 118 |
|
| 119 |
Py_BEGIN_ALLOW_THREADS |
| 120 |
ok = Beep(freq, dur); |
| 121 |
Py_END_ALLOW_THREADS |
| 122 |
if (!ok) { |
| 123 |
PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
Py_INCREF(Py_None); |
| 128 |
return Py_None; |
| 129 |
} |
| 130 |
|
| 131 |
static PyObject * |
| 132 |
sound_msgbeep(PyObject *self, PyObject *args) |
| 133 |
{ |
| 134 |
int x = MB_OK; |
| 135 |
if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x)) |
| 136 |
return NULL; |
| 137 |
MessageBeep(x); |
| 138 |
Py_INCREF(Py_None); |
| 139 |
return Py_None; |
| 140 |
} |
| 141 |
|
| 142 |
static struct PyMethodDef sound_methods[] = |
| 143 |
{ |
| 144 |
{"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc}, |
| 145 |
{"Beep", sound_beep, METH_VARARGS, sound_beep_doc}, |
| 146 |
{"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc}, |
| 147 |
{NULL, NULL} |
| 148 |
}; |
| 149 |
|
| 150 |
static void |
| 151 |
add_define(PyObject *dict, const char *key, long value) |
| 152 |
{ |
| 153 |
PyObject *k = PyString_FromString(key); |
| 154 |
PyObject *v = PyLong_FromLong(value); |
| 155 |
if (v && k) { |
| 156 |
PyDict_SetItem(dict, k, v); |
| 157 |
} |
| 158 |
Py_XDECREF(k); |
| 159 |
Py_XDECREF(v); |
| 160 |
} |
| 161 |
|
| 162 |
#define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
| 163 |
|
| 164 |
PyMODINIT_FUNC |
| 165 |
initwinsound(void) |
| 166 |
{ |
| 167 |
PyObject *dict; |
| 168 |
PyObject *module = Py_InitModule3("winsound", |
| 169 |
sound_methods, |
| 170 |
sound_module_doc); |
| 171 |
if (module == NULL) |
| 172 |
return; |
| 173 |
dict = PyModule_GetDict(module); |
| 174 |
|
| 175 |
ADD_DEFINE(SND_ASYNC); |
| 176 |
ADD_DEFINE(SND_NODEFAULT); |
| 177 |
ADD_DEFINE(SND_NOSTOP); |
| 178 |
ADD_DEFINE(SND_NOWAIT); |
| 179 |
ADD_DEFINE(SND_ALIAS); |
| 180 |
ADD_DEFINE(SND_FILENAME); |
| 181 |
ADD_DEFINE(SND_MEMORY); |
| 182 |
ADD_DEFINE(SND_PURGE); |
| 183 |
ADD_DEFINE(SND_LOOP); |
| 184 |
ADD_DEFINE(SND_APPLICATION); |
| 185 |
|
| 186 |
ADD_DEFINE(MB_OK); |
| 187 |
ADD_DEFINE(MB_ICONASTERISK); |
| 188 |
ADD_DEFINE(MB_ICONEXCLAMATION); |
| 189 |
ADD_DEFINE(MB_ICONHAND); |
| 190 |
ADD_DEFINE(MB_ICONQUESTION); |
| 191 |
} |