blob: 7feebcbcf4377f76290364b163b38b81ed87ba55 [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"
Zachary Warec4018812016-09-06 16:32:43 -050055"MessageBeep(type) - Call Windows MessageBeep.");
Zachary Ware73f8cfb2015-05-13 01:21:21 -050056
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.
Zachary Ware73f8cfb2015-05-13 01:21:21 -050071
72A wrapper around the Windows PlaySound API.
73[clinic start generated code]*/
Guido van Rossume6005781999-02-04 22:40:42 +000074
Walter Dörwaldfee100402003-05-22 17:22:54 +000075static PyObject *
Zachary Wareae8298b2016-09-05 16:31:21 -050076winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags)
Zachary Warec4018812016-09-06 16:32:43 -050077/*[clinic end generated code: output=49a0fd16a372ebeb input=c63e1f2d848da2f2]*/
Guido van Rossume6005781999-02-04 22:40:42 +000078{
Guido van Rossume6005781999-02-04 22:40:42 +000079 int ok;
Zachary Wareae8298b2016-09-05 16:31:21 -050080 wchar_t *wsound;
81 Py_buffer view = {NULL, NULL};
Guido van Rossume6005781999-02-04 22:40:42 +000082
Zachary Wareae8298b2016-09-05 16:31:21 -050083 if (sound == Py_None) {
84 wsound = NULL;
85 } else if (flags & SND_MEMORY) {
86 if (flags & SND_ASYNC) {
87 /* Sidestep reference counting headache; unfortunately this also
88 prevent SND_LOOP from memory. */
89 PyErr_SetString(PyExc_RuntimeError,
90 "Cannot play asynchronously from memory");
91 return NULL;
92 }
93 if (PyObject_GetBuffer(sound, &view, PyBUF_SIMPLE) < 0) {
94 return NULL;
95 }
96 wsound = (wchar_t *)view.buf;
97 } else {
98 if (!PyUnicode_Check(sound)) {
99 PyErr_Format(PyExc_TypeError,
100 "'sound' must be str or None, not '%s'",
101 Py_TYPE(sound)->tp_name);
102 return NULL;
103 }
104 wsound = PyUnicode_AsWideCharString(sound, NULL);
105 if (wsound == NULL) {
106 return NULL;
107 }
Guido van Rossume6005781999-02-04 22:40:42 +0000108 }
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500109
Zachary Wareae8298b2016-09-05 16:31:21 -0500110
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500111 Py_BEGIN_ALLOW_THREADS
Zachary Wareae8298b2016-09-05 16:31:21 -0500112 ok = PlaySoundW(wsound, NULL, flags);
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500113 Py_END_ALLOW_THREADS
Zachary Wareae8298b2016-09-05 16:31:21 -0500114 if (view.obj) {
115 PyBuffer_Release(&view);
116 } else if (sound != Py_None) {
117 PyMem_Free(wsound);
118 }
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500119 if (!ok) {
120 PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
121 return NULL;
122 }
123 Py_RETURN_NONE;
Guido van Rossume6005781999-02-04 22:40:42 +0000124}
125
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500126/*[clinic input]
127winsound.Beep
128
129 frequency: int
130 Frequency of the sound in hertz.
131 Must be in the range 37 through 32,767.
132 duration: int
133 How long the sound should play, in milliseconds.
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500134
135A wrapper around the Windows Beep API.
136[clinic start generated code]*/
137
Tim Peters25a9ce32001-02-19 07:06:36 +0000138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300139winsound_Beep_impl(PyObject *module, int frequency, int duration)
Zachary Warec4018812016-09-06 16:32:43 -0500140/*[clinic end generated code: output=f32382e52ee9b2fb input=40e360cfa00a5cf0]*/
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 BOOL ok;
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000143
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500144 if (frequency < 37 || frequency > 32767) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyErr_SetString(PyExc_ValueError,
146 "frequency must be in 37 thru 32767");
147 return NULL;
148 }
Tim Peters25a9ce32001-02-19 07:06:36 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 Py_BEGIN_ALLOW_THREADS
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500151 ok = Beep(frequency, duration);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 Py_END_ALLOW_THREADS
153 if (!ok) {
154 PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
155 return NULL;
156 }
Tim Peters25a9ce32001-02-19 07:06:36 +0000157
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500158 Py_RETURN_NONE;
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000159}
160
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500161/*[clinic input]
162winsound.MessageBeep
163
Zachary Warec4018812016-09-06 16:32:43 -0500164 type: int(c_default="MB_OK") = MB_OK
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500165
166Call Windows MessageBeep(x).
167
168x defaults to MB_OK.
169[clinic start generated code]*/
170
Guido van Rossume1252682003-04-09 19:38:08 +0000171static PyObject *
Zachary Warec4018812016-09-06 16:32:43 -0500172winsound_MessageBeep_impl(PyObject *module, int type)
173/*[clinic end generated code: output=120875455121121f input=db185f741ae21401]*/
Guido van Rossume1252682003-04-09 19:38:08 +0000174{
Zachary Ware625cb372016-09-05 17:32:28 -0500175 BOOL ok;
176
177 Py_BEGIN_ALLOW_THREADS
Zachary Warec4018812016-09-06 16:32:43 -0500178 ok = MessageBeep(type);
Zachary Ware625cb372016-09-05 17:32:28 -0500179 Py_END_ALLOW_THREADS
180
181 if (!ok) {
182 PyErr_SetExcFromWindowsErr(PyExc_RuntimeError, 0);
183 return NULL;
184 }
185
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500186 Py_RETURN_NONE;
Guido van Rossume1252682003-04-09 19:38:08 +0000187}
188
Guido van Rossume6005781999-02-04 22:40:42 +0000189static struct PyMethodDef sound_methods[] =
190{
Zachary Ware73f8cfb2015-05-13 01:21:21 -0500191 WINSOUND_PLAYSOUND_METHODDEF
192 WINSOUND_BEEP_METHODDEF
193 WINSOUND_MESSAGEBEEP_METHODDEF
Guido van Rossume6005781999-02-04 22:40:42 +0000194 {NULL, NULL}
195};
196
Tim Peters25a9ce32001-02-19 07:06:36 +0000197static void
198add_define(PyObject *dict, const char *key, long value)
Guido van Rossume6005781999-02-04 22:40:42 +0000199{
Hirokazu Yamamoto62fbdd92010-11-07 02:45:19 +0000200 PyObject *k = PyUnicode_FromString(key);
201 PyObject *v = PyLong_FromLong(value);
202 if (v && k) {
Hirokazu Yamamotocdc8cdd2010-11-07 11:07:44 +0000203 PyDict_SetItem(dict, k, v);
Guido van Rossume6005781999-02-04 22:40:42 +0000204 }
205 Py_XDECREF(k);
206 Py_XDECREF(v);
207}
208
209#define ADD_DEFINE(tok) add_define(dict,#tok,tok)
210
Martin v. Löwis1a214512008-06-11 05:26:20 +0000211
212static struct PyModuleDef winsoundmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 PyModuleDef_HEAD_INIT,
214 "winsound",
215 sound_module_doc,
216 -1,
217 sound_methods,
218 NULL,
219 NULL,
220 NULL,
221 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000222};
223
Mark Hammonde407e2a2002-07-22 13:26:41 +0000224PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000225PyInit_winsound(void)
Guido van Rossume6005781999-02-04 22:40:42 +0000226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 PyObject *dict;
228 PyObject *module = PyModule_Create(&winsoundmodule);
229 if (module == NULL)
230 return NULL;
231 dict = PyModule_GetDict(module);
Tim Peters25a9ce32001-02-19 07:06:36 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 ADD_DEFINE(SND_ASYNC);
234 ADD_DEFINE(SND_NODEFAULT);
235 ADD_DEFINE(SND_NOSTOP);
236 ADD_DEFINE(SND_NOWAIT);
237 ADD_DEFINE(SND_ALIAS);
238 ADD_DEFINE(SND_FILENAME);
239 ADD_DEFINE(SND_MEMORY);
240 ADD_DEFINE(SND_PURGE);
241 ADD_DEFINE(SND_LOOP);
242 ADD_DEFINE(SND_APPLICATION);
Tim Peters25a9ce32001-02-19 07:06:36 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 ADD_DEFINE(MB_OK);
245 ADD_DEFINE(MB_ICONASTERISK);
246 ADD_DEFINE(MB_ICONEXCLAMATION);
247 ADD_DEFINE(MB_ICONHAND);
248 ADD_DEFINE(MB_ICONQUESTION);
249 return module;
Guido van Rossume6005781999-02-04 22:40:42 +0000250}