diff -r f71f65caf53e PC/winsound.c --- a/PC/winsound.c Fri Jan 10 09:24:59 2014 -0600 +++ b/PC/winsound.c Fri Jan 10 14:32:05 2014 -0600 @@ -39,21 +39,10 @@ #include #include -PyDoc_STRVAR(sound_playsound_doc, -"PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n" -"\n" -"The sound argument can be a filename, data, or None.\n" -"For flag values, ored together, see module documentation."); - -PyDoc_STRVAR(sound_beep_doc, -"Beep(frequency, duration) - a wrapper around the Windows Beep API\n" -"\n" -"The frequency argument specifies frequency, in hertz, of the sound.\n" -"This parameter must be in the range 37 through 32,767.\n" -"The duration argument specifies the number of milliseconds.\n"); - -PyDoc_STRVAR(sound_msgbeep_doc, -"MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK."); +/*[clinic input] +module winsound +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ PyDoc_STRVAR(sound_module_doc, "PlaySound(sound, flags) - play a sound\n" @@ -67,53 +56,137 @@ "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed "SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors "\n" -"Beep(frequency, duration) - Make a beep through the PC speaker."); +"Beep(frequency, duration) - Make a beep through the PC speaker.\n" +"MessageBeep(x) - Call Windows MessageBeep."); + +/*[clinic input] +winsound.PlaySound + + sound: Py_UNICODE(nullable=True) + The sound to play; a filename, data, or None. + flags: int + Flag values, ored together. See module documentation. + / + +A wrapper around the Windows PlaySound API. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_PlaySound__doc__, +"PlaySound(sound, flags)\n" +"A wrapper around the Windows PlaySound API.\n" +"\n" +" sound\n" +" The sound to play; a filename, data, or None.\n" +" flags\n" +" Flag values, ored together. See module documentation."); + +#define WINSOUND_PLAYSOUND_METHODDEF \ + {"PlaySound", (PyCFunction)winsound_PlaySound, METH_VARARGS, winsound_PlaySound__doc__}, static PyObject * -sound_playsound(PyObject *s, PyObject *args) +winsound_PlaySound_impl(PyModuleDef *module, Py_UNICODE *sound, int flags); + +static PyObject * +winsound_PlaySound(PyModuleDef *module, PyObject *args) { - wchar_t *wsound; + PyObject *return_value = NULL; + Py_UNICODE *sound; int flags; - int ok; - if (PyArg_ParseTuple(args, "Zi:PlaySound", &wsound, &flags)) { - if (flags & SND_ASYNC && flags & SND_MEMORY) { - /* Sidestep reference counting headache; unfortunately this also - prevent SND_LOOP from memory. */ - PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); - return NULL; - } - Py_BEGIN_ALLOW_THREADS - ok = PlaySoundW(wsound, NULL, flags); - Py_END_ALLOW_THREADS - if (!ok) { - PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); - return NULL; - } - Py_INCREF(Py_None); - return Py_None; - } - return NULL; + if (!PyArg_ParseTuple(args, + "Zi:PlaySound", + &sound, &flags)) + goto exit; + return_value = winsound_PlaySound_impl(module, sound, flags); + +exit: + return return_value; } static PyObject * -sound_beep(PyObject *self, PyObject *args) +winsound_PlaySound_impl(PyModuleDef *module, Py_UNICODE *sound, int flags) +/*[clinic end generated code: checksum=e6b585306e24bf0b9d1b3b17cb75ec5f8fb61c5a]*/ { - int freq; - int dur; + int ok; + + if (flags & SND_ASYNC && flags & SND_MEMORY) { + /* Sidestep reference counting headache; unfortunately this also + prevent SND_LOOP from memory. */ + PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + ok = PlaySoundW(sound, NULL, flags); + Py_END_ALLOW_THREADS + if (!ok) { + PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); + return NULL; + } + Py_INCREF(Py_None); + return Py_None; +} + +/*[clinic input] +winsound.Beep + + frequency: int + Frequency of the sound in hertz. + Must be in the range 37 through 32,767. + duration: int + How long the sound should play, in milliseconds. + / + +A wrapper around the Windows Beep API. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_Beep__doc__, +"Beep(frequency, duration)\n" +"A wrapper around the Windows Beep API.\n" +"\n" +" frequency\n" +" Frequency of the sound in hertz.\n" +" Must be in the range 37 through 32,767.\n" +" duration\n" +" How long the sound should play, in milliseconds."); + +#define WINSOUND_BEEP_METHODDEF \ + {"Beep", (PyCFunction)winsound_Beep, METH_VARARGS, winsound_Beep__doc__}, + +static PyObject * +winsound_Beep_impl(PyModuleDef *module, int frequency, int duration); + +static PyObject * +winsound_Beep(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + int frequency; + int duration; + + if (!PyArg_ParseTuple(args, + "ii:Beep", + &frequency, &duration)) + goto exit; + return_value = winsound_Beep_impl(module, frequency, duration); + +exit: + return return_value; +} + +static PyObject * +winsound_Beep_impl(PyModuleDef *module, int frequency, int duration) +/*[clinic end generated code: checksum=387eb7d09a780e7a47f17cedfd87c9b5573934c6]*/ +{ BOOL ok; - if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) - return NULL; - - if (freq < 37 || freq > 32767) { + if (frequency < 37 || frequency > 32767) { PyErr_SetString(PyExc_ValueError, "frequency must be in 37 thru 32767"); return NULL; } Py_BEGIN_ALLOW_THREADS - ok = Beep(freq, dur); + ok = Beep(frequency, duration); Py_END_ALLOW_THREADS if (!ok) { PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); @@ -124,12 +197,49 @@ return Py_None; } +/*[clinic input] +winsound.MessageBeep + + x: int(c_default="MB_OK") = winsound.MB_OK + / + +Call Windows MessageBeep(x). + +x defaults to MB_OK. +[clinic start generated code]*/ + +PyDoc_STRVAR(winsound_MessageBeep__doc__, +"MessageBeep(x=winsound.MB_OK)\n" +"Call Windows MessageBeep(x).\n" +"\n" +"x defaults to MB_OK."); + +#define WINSOUND_MESSAGEBEEP_METHODDEF \ + {"MessageBeep", (PyCFunction)winsound_MessageBeep, METH_VARARGS, winsound_MessageBeep__doc__}, + static PyObject * -sound_msgbeep(PyObject *self, PyObject *args) +winsound_MessageBeep_impl(PyModuleDef *module, int x); + +static PyObject * +winsound_MessageBeep(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; int x = MB_OK; - if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x)) - return NULL; + + if (!PyArg_ParseTuple(args, + "|i:MessageBeep", + &x)) + goto exit; + return_value = winsound_MessageBeep_impl(module, x); + +exit: + return return_value; +} + +static PyObject * +winsound_MessageBeep_impl(PyModuleDef *module, int x) +/*[clinic end generated code: checksum=9649c3341e1acb1084d2551074eb92caa19ee66a]*/ +{ MessageBeep(x); Py_INCREF(Py_None); return Py_None; @@ -137,9 +247,9 @@ static struct PyMethodDef sound_methods[] = { - {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc}, - {"Beep", sound_beep, METH_VARARGS, sound_beep_doc}, - {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc}, + WINSOUND_PLAYSOUND_METHODDEF + WINSOUND_BEEP_METHODDEF + WINSOUND_MESSAGEBEEP_METHODDEF {NULL, NULL} };