blob: 000ddd8475ef812f1138ad1920b0506b02ae32e2 [file] [log] [blame]
Guido van Rossume6005781999-02-04 22:40:42 +00001/* Author: Toby Dickenson <htrd90@zepler.org>
2 *
3 * Copyright (c) 1999 Toby Dickenson
4 *
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 */
9
10/* Modified by Guido van Rossum */
Guido van Rossum99eb7a11999-10-01 14:29:17 +000011/* Beep added by Mark Hammond */
Tim Peters25a9ce32001-02-19 07:06:36 +000012/* Win9X Beep and platform identification added by Uncle Timmy */
Guido van Rossume6005781999-02-04 22:40:42 +000013
14/* Example:
15
16 import winsound
17 import time
18
Tim Peters25a9ce32001-02-19 07:06:36 +000019 # Play wav file
Guido van Rossume6005781999-02-04 22:40:42 +000020 winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME)
21
22 # Play sound from control panel settings
23 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
24
25 # Play wav file from memory
26 data=open('c:/windows/media/Chimes.wav',"rb").read()
27 winsound.PlaySound(data, winsound.SND_MEMORY)
28
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)
36*/
37
Guido van Rossume7ba4952007-06-06 23:52:48 +000038#include <Python.h>
Guido van Rossume6005781999-02-04 22:40:42 +000039#include <windows.h>
40#include <mmsystem.h>
Guido van Rossume6005781999-02-04 22:40:42 +000041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000042PyDoc_STRVAR(sound_module_doc,
Guido van Rossume6005781999-02-04 22:40:42 +000043"PlaySound(sound, flags) - play a sound\n"
44"SND_FILENAME - sound is a wav file name\n"
Tim Peterse79af272001-02-20 10:02:21 +000045"SND_ALIAS - sound is a registry sound association name\n"
Guido van Rossume6005781999-02-04 22:40:42 +000046"SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n"
47"SND_MEMORY - sound is a memory image of a wav file\n"
48"SND_PURGE - stop all instances of the specified sound\n"
49"SND_ASYNC - PlaySound returns immediately\n"
50"SND_NODEFAULT - Do not play a default beep if the sound can not be found\n"
51"SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed
52"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
Guido van Rossum99eb7a11999-10-01 14:29:17 +000053"\n"
Zachary Ware73f8cfb2015-05-13 01:21:21 -050054"Beep(frequency, duration) - Make a beep through the PC speaker.\n"
55"MessageBeep(x) - Call Windows MessageBeep.");
56
57/*[clinic input]
58module 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]
65winsound.PlaySound
66
Zachary Wareae8298b2016-09-05 16:31:21 -050067 sound: object
Zachary Ware73f8cfb2015-05-13 01:21:21 -050068 The sound to play; a filename, data, or None.
69 flags: int
70 Flag values, ored together. See module documentation.
71 /
72
73A wrapper around the Windows PlaySound API.
74[clinic start generated code]*/
Guido van Rossume6005781999-02-04 22:40:42 +000075
Walter Dörwaldfee100402003-05-22 17:22:54 +000076static PyObject *
Zachary Wareae8298b2016-09-05 16:31:21 -050077winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags)
78/*[clinic end generated code: output=49a0fd16a372ebeb input=7bdf637f10201d37]*/
Guido van Rossume6005781999-02-04 22:40:42 +000079{
Guido van Rossume6005781999-02-04 22:40:42 +000080 int ok;
Zachary Wareae8298b2016-09-05 16:31:21 -050081 wchar_t *wsound;
82 Py_buffer view = {NULL, NULL};
Guido van Rossume6005781999-02-04 22:40:42 +000083
Zachary Wareae8298b2016-09-05 16:31:21 -050084 if (sound == Py_None) {
85 wsound = NULL;
86 } else if (flags & SND_MEMORY) {
87 if (flags & SND_ASYNC) {
88 /* Sidestep reference counting headache; unfortunately this also
89 prevent SND_LOOP from memory. */
90 PyErr_SetString(PyExc_RuntimeError,
91 "Cannot play asynchronously from memory");
92 return NULL;
93 }
94 if (PyObject_GetBuffer(sound, &view, PyBUF_SIMPLE) < 0) {
95 return NULL;
96 }
97 wsound = (wchar_t *)view.buf;
98 } else {
99 if (!PyUnicode_Check(sound)) {
100 PyErr_Format(PyExc_TypeError,
101 "'sound' must be str or None, not '%s'",
102 Py_TYPE(sound)->tp_name);
103 return NULL;
104 }
105 wsound = PyUnicode_AsWideCharString(sound, NULL);
106 if (wsound == NULL) {
107 return NULL;
108 }
Guido van Rossume6005781999-02-04 22:40:42 +0000109 }
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500110
Zachary Wareae8298b2016-09-05 16:31:21 -0500111
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500112 Py_BEGIN_ALLOW_THREADS
Zachary Wareae8298b2016-09-05 16:31:21 -0500113 ok = PlaySoundW(wsound, NULL, flags);
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500114 Py_END_ALLOW_THREADS
Zachary Wareae8298b2016-09-05 16:31:21 -0500115 if (view.obj) {
116 PyBuffer_Release(&view);
117 } else if (sound != Py_None) {
118 PyMem_Free(wsound);
119 }
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500120 if (!ok) {
121 PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
122 return NULL;
123 }
124 Py_RETURN_NONE;
Guido van Rossume6005781999-02-04 22:40:42 +0000125}
126
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500127/*[clinic input]
128winsound.Beep
129
130 frequency: int
131 Frequency of the sound in hertz.
132 Must be in the range 37 through 32,767.
133 duration: int
134 How long the sound should play, in milliseconds.
135 /
136
137A wrapper around the Windows Beep API.
138[clinic start generated code]*/
139
Tim Peters25a9ce32001-02-19 07:06:36 +0000140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300141winsound_Beep_impl(PyObject *module, int frequency, int duration)
142/*[clinic end generated code: output=f32382e52ee9b2fb input=628a99d2ddf73798]*/
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 BOOL ok;
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000145
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500146 if (frequency < 37 || frequency > 32767) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 PyErr_SetString(PyExc_ValueError,
148 "frequency must be in 37 thru 32767");
149 return NULL;
150 }
Tim Peters25a9ce32001-02-19 07:06:36 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 Py_BEGIN_ALLOW_THREADS
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500153 ok = Beep(frequency, duration);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 Py_END_ALLOW_THREADS
155 if (!ok) {
156 PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
157 return NULL;
158 }
Tim Peters25a9ce32001-02-19 07:06:36 +0000159
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500160 Py_RETURN_NONE;
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000161}
162
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500163/*[clinic input]
164winsound.MessageBeep
165
166 x: int(c_default="MB_OK") = MB_OK
167 /
168
169Call Windows MessageBeep(x).
170
171x defaults to MB_OK.
172[clinic start generated code]*/
173
Guido van Rossume1252682003-04-09 19:38:08 +0000174static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300175winsound_MessageBeep_impl(PyObject *module, int x)
176/*[clinic end generated code: output=1ad89e4d8d30a957 input=a776c8a85c9853f6]*/
Guido van Rossume1252682003-04-09 19:38:08 +0000177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 MessageBeep(x);
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500179 Py_RETURN_NONE;
Guido van Rossume1252682003-04-09 19:38:08 +0000180}
181
Guido van Rossume6005781999-02-04 22:40:42 +0000182static struct PyMethodDef sound_methods[] =
183{
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500184 WINSOUND_PLAYSOUND_METHODDEF
185 WINSOUND_BEEP_METHODDEF
186 WINSOUND_MESSAGEBEEP_METHODDEF
Guido van Rossume6005781999-02-04 22:40:42 +0000187 {NULL, NULL}
188};
189
Tim Peters25a9ce32001-02-19 07:06:36 +0000190static void
191add_define(PyObject *dict, const char *key, long value)
Guido van Rossume6005781999-02-04 22:40:42 +0000192{
Hirokazu Yamamoto62fbdd92010-11-07 02:45:19 +0000193 PyObject *k = PyUnicode_FromString(key);
194 PyObject *v = PyLong_FromLong(value);
195 if (v && k) {
Hirokazu Yamamotocdc8cdd2010-11-07 11:07:44 +0000196 PyDict_SetItem(dict, k, v);
Guido van Rossume6005781999-02-04 22:40:42 +0000197 }
198 Py_XDECREF(k);
199 Py_XDECREF(v);
200}
201
202#define ADD_DEFINE(tok) add_define(dict,#tok,tok)
203
Martin v. Löwis1a214512008-06-11 05:26:20 +0000204
205static struct PyModuleDef winsoundmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 PyModuleDef_HEAD_INIT,
207 "winsound",
208 sound_module_doc,
209 -1,
210 sound_methods,
211 NULL,
212 NULL,
213 NULL,
214 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000215};
216
Mark Hammonde407e2a2002-07-22 13:26:41 +0000217PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000218PyInit_winsound(void)
Guido van Rossume6005781999-02-04 22:40:42 +0000219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyObject *dict;
221 PyObject *module = PyModule_Create(&winsoundmodule);
222 if (module == NULL)
223 return NULL;
224 dict = PyModule_GetDict(module);
Tim Peters25a9ce32001-02-19 07:06:36 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 ADD_DEFINE(SND_ASYNC);
227 ADD_DEFINE(SND_NODEFAULT);
228 ADD_DEFINE(SND_NOSTOP);
229 ADD_DEFINE(SND_NOWAIT);
230 ADD_DEFINE(SND_ALIAS);
231 ADD_DEFINE(SND_FILENAME);
232 ADD_DEFINE(SND_MEMORY);
233 ADD_DEFINE(SND_PURGE);
234 ADD_DEFINE(SND_LOOP);
235 ADD_DEFINE(SND_APPLICATION);
Tim Peters25a9ce32001-02-19 07:06:36 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 ADD_DEFINE(MB_OK);
238 ADD_DEFINE(MB_ICONASTERISK);
239 ADD_DEFINE(MB_ICONEXCLAMATION);
240 ADD_DEFINE(MB_ICONHAND);
241 ADD_DEFINE(MB_ICONQUESTION);
242 return module;
Guido van Rossume6005781999-02-04 22:40:42 +0000243}