3939#include <windows.h>
4040#include <mmsystem.h>
4141
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-
5842PyDoc_STRVAR (sound_module_doc ,
5943"PlaySound(sound, flags) - play a sound\n"
6044"SND_FILENAME - sound is a wav file name\n"
@@ -67,79 +51,112 @@ PyDoc_STRVAR(sound_module_doc,
6751"SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed
6852"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
6953"\n"
70- "Beep(frequency, duration) - Make a beep through the PC speaker." );
54+ "Beep(frequency, duration) - Make a beep through the PC speaker.\n"
55+ "MessageBeep(x) - Call Windows MessageBeep." );
56+
57+ /*[clinic input]
58+ module winsound
59+ [clinic start generated code]*/
60+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a18401142d97b8d5]*/
61+
62+ #include "clinic/winsound.c.h"
63+
64+ /*[clinic input]
65+ winsound.PlaySound
66+
67+ sound: Py_UNICODE(nullable=True)
68+ The sound to play; a filename, data, or None.
69+ flags: int
70+ Flag values, ored together. See module documentation.
71+ /
72+
73+ A wrapper around the Windows PlaySound API.
74+ [clinic start generated code]*/
7175
7276static PyObject *
73- sound_playsound (PyObject * s , PyObject * args )
77+ winsound_PlaySound_impl (PyModuleDef * module , Py_UNICODE * sound , int flags )
78+ /*[clinic end generated code: output=614273784bf59e5c input=c86fab5d8e86f31d]*/
7479{
75- wchar_t * wsound ;
76- int flags ;
7780 int ok ;
7881
79- if (PyArg_ParseTuple (args , "Zi:PlaySound" , & wsound , & flags )) {
80- if (flags & SND_ASYNC && flags & SND_MEMORY ) {
81- /* Sidestep reference counting headache; unfortunately this also
82- prevent SND_LOOP from memory. */
83- PyErr_SetString (PyExc_RuntimeError , "Cannot play asynchronously from memory" );
84- return NULL ;
85- }
86- Py_BEGIN_ALLOW_THREADS
87- ok = PlaySoundW (wsound , NULL , flags );
88- Py_END_ALLOW_THREADS
89- if (!ok ) {
90- PyErr_SetString (PyExc_RuntimeError , "Failed to play sound" );
91- return NULL ;
92- }
93- Py_INCREF (Py_None );
94- return Py_None ;
82+ if (flags & SND_ASYNC && flags & SND_MEMORY ) {
83+ /* Sidestep reference counting headache; unfortunately this also
84+ prevent SND_LOOP from memory. */
85+ PyErr_SetString (PyExc_RuntimeError ,
86+ "Cannot play asynchronously from memory" );
87+ return NULL ;
9588 }
96- return NULL ;
89+
90+ Py_BEGIN_ALLOW_THREADS
91+ ok = PlaySoundW (sound , NULL , flags );
92+ Py_END_ALLOW_THREADS
93+ if (!ok ) {
94+ PyErr_SetString (PyExc_RuntimeError , "Failed to play sound" );
95+ return NULL ;
96+ }
97+ Py_RETURN_NONE ;
9798}
9899
100+ /*[clinic input]
101+ winsound.Beep
102+
103+ frequency: int
104+ Frequency of the sound in hertz.
105+ Must be in the range 37 through 32,767.
106+ duration: int
107+ How long the sound should play, in milliseconds.
108+ /
109+
110+ A wrapper around the Windows Beep API.
111+ [clinic start generated code]*/
112+
99113static PyObject *
100- sound_beep (PyObject * self , PyObject * args )
114+ winsound_Beep_impl (PyModuleDef * module , int frequency , int duration )
115+ /*[clinic end generated code: output=c75f282035a872bd input=628a99d2ddf73798]*/
101116{
102- int freq ;
103- int dur ;
104117 BOOL ok ;
105118
106- if (!PyArg_ParseTuple (args , "ii:Beep" , & freq , & dur ))
107- return NULL ;
108-
109- if (freq < 37 || freq > 32767 ) {
119+ if (frequency < 37 || frequency > 32767 ) {
110120 PyErr_SetString (PyExc_ValueError ,
111121 "frequency must be in 37 thru 32767" );
112122 return NULL ;
113123 }
114124
115125 Py_BEGIN_ALLOW_THREADS
116- ok = Beep (freq , dur );
126+ ok = Beep (frequency , duration );
117127 Py_END_ALLOW_THREADS
118128 if (!ok ) {
119129 PyErr_SetString (PyExc_RuntimeError ,"Failed to beep" );
120130 return NULL ;
121131 }
122132
123- Py_INCREF (Py_None );
124- return Py_None ;
133+ Py_RETURN_NONE ;
125134}
126135
136+ /*[clinic input]
137+ winsound.MessageBeep
138+
139+ x: int(c_default="MB_OK") = MB_OK
140+ /
141+
142+ Call Windows MessageBeep(x).
143+
144+ x defaults to MB_OK.
145+ [clinic start generated code]*/
146+
127147static PyObject *
128- sound_msgbeep (PyObject * self , PyObject * args )
148+ winsound_MessageBeep_impl (PyModuleDef * module , int x )
149+ /*[clinic end generated code: output=92aa6a822bdc66ad input=a776c8a85c9853f6]*/
129150{
130- int x = MB_OK ;
131- if (!PyArg_ParseTuple (args , "|i:MessageBeep" , & x ))
132- return NULL ;
133151 MessageBeep (x );
134- Py_INCREF (Py_None );
135- return Py_None ;
152+ Py_RETURN_NONE ;
136153}
137154
138155static struct PyMethodDef sound_methods [] =
139156{
140- { "PlaySound" , sound_playsound , METH_VARARGS , sound_playsound_doc },
141- { "Beep" , sound_beep , METH_VARARGS , sound_beep_doc },
142- { "MessageBeep" , sound_msgbeep , METH_VARARGS , sound_msgbeep_doc },
157+ WINSOUND_PLAYSOUND_METHODDEF
158+ WINSOUND_BEEP_METHODDEF
159+ WINSOUND_MESSAGEBEEP_METHODDEF
143160 {NULL , NULL }
144161};
145162
0 commit comments