Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 1 | /* 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 Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 11 | /* Beep added by Mark Hammond */ |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 12 | /* Win9X Beep and platform identification added by Uncle Timmy */ |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 13 | |
| 14 | /* Example: |
| 15 | |
| 16 | import winsound |
| 17 | import time |
| 18 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 19 | # Play wav file |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 20 | 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 Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 38 | #include <Python.h> |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 39 | #include <windows.h> |
| 40 | #include <mmsystem.h> |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 41 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 42 | PyDoc_STRVAR(sound_module_doc, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 43 | "PlaySound(sound, flags) - play a sound\n" |
| 44 | "SND_FILENAME - sound is a wav file name\n" |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 45 | "SND_ALIAS - sound is a registry sound association name\n" |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 46 | "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 Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 53 | "\n" |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 54 | "Beep(frequency, duration) - Make a beep through the PC speaker.\n" |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 55 | "MessageBeep(type) - Call Windows MessageBeep."); |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 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 | |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 67 | sound: object |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 68 | The sound to play; a filename, data, or None. |
| 69 | flags: int |
| 70 | Flag values, ored together. See module documentation. |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 71 | |
| 72 | A wrapper around the Windows PlaySound API. |
| 73 | [clinic start generated code]*/ |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 74 | |
Walter Dörwald | fee10040 | 2003-05-22 17:22:54 +0000 | [diff] [blame] | 75 | static PyObject * |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 76 | winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags) |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 77 | /*[clinic end generated code: output=49a0fd16a372ebeb input=c63e1f2d848da2f2]*/ |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 78 | { |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 79 | int ok; |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 80 | wchar_t *wsound; |
| 81 | Py_buffer view = {NULL, NULL}; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 82 | |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 83 | 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 Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 108 | } |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 109 | |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 110 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 111 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 112 | ok = PlaySoundW(wsound, NULL, flags); |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 113 | Py_END_ALLOW_THREADS |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 114 | if (view.obj) { |
| 115 | PyBuffer_Release(&view); |
| 116 | } else if (sound != Py_None) { |
| 117 | PyMem_Free(wsound); |
| 118 | } |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 119 | if (!ok) { |
| 120 | PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); |
| 121 | return NULL; |
| 122 | } |
| 123 | Py_RETURN_NONE; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 126 | /*[clinic input] |
| 127 | winsound.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 Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 134 | |
| 135 | A wrapper around the Windows Beep API. |
| 136 | [clinic start generated code]*/ |
| 137 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 138 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 139 | winsound_Beep_impl(PyObject *module, int frequency, int duration) |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 140 | /*[clinic end generated code: output=f32382e52ee9b2fb input=40e360cfa00a5cf0]*/ |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | BOOL ok; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 143 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 144 | if (frequency < 37 || frequency > 32767) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | PyErr_SetString(PyExc_ValueError, |
| 146 | "frequency must be in 37 thru 32767"); |
| 147 | return NULL; |
| 148 | } |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 151 | ok = Beep(frequency, duration); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | Py_END_ALLOW_THREADS |
| 153 | if (!ok) { |
| 154 | PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
| 155 | return NULL; |
| 156 | } |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 157 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 158 | Py_RETURN_NONE; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 161 | /*[clinic input] |
| 162 | winsound.MessageBeep |
| 163 | |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 164 | type: int(c_default="MB_OK") = MB_OK |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 165 | |
| 166 | Call Windows MessageBeep(x). |
| 167 | |
| 168 | x defaults to MB_OK. |
| 169 | [clinic start generated code]*/ |
| 170 | |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 171 | static PyObject * |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 172 | winsound_MessageBeep_impl(PyObject *module, int type) |
| 173 | /*[clinic end generated code: output=120875455121121f input=db185f741ae21401]*/ |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 174 | { |
Zachary Ware | 625cb37 | 2016-09-05 17:32:28 -0500 | [diff] [blame] | 175 | BOOL ok; |
| 176 | |
| 177 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 178 | ok = MessageBeep(type); |
Zachary Ware | 625cb37 | 2016-09-05 17:32:28 -0500 | [diff] [blame] | 179 | Py_END_ALLOW_THREADS |
| 180 | |
| 181 | if (!ok) { |
| 182 | PyErr_SetExcFromWindowsErr(PyExc_RuntimeError, 0); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 186 | Py_RETURN_NONE; |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 189 | static struct PyMethodDef sound_methods[] = |
| 190 | { |
Zachary Ware | 73f8cfb | 2015-05-13 01:21:21 -0500 | [diff] [blame] | 191 | WINSOUND_PLAYSOUND_METHODDEF |
| 192 | WINSOUND_BEEP_METHODDEF |
| 193 | WINSOUND_MESSAGEBEEP_METHODDEF |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 194 | {NULL, NULL} |
| 195 | }; |
| 196 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 197 | static void |
| 198 | add_define(PyObject *dict, const char *key, long value) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 199 | { |
Hirokazu Yamamoto | 62fbdd9 | 2010-11-07 02:45:19 +0000 | [diff] [blame] | 200 | PyObject *k = PyUnicode_FromString(key); |
| 201 | PyObject *v = PyLong_FromLong(value); |
| 202 | if (v && k) { |
Hirokazu Yamamoto | cdc8cdd | 2010-11-07 11:07:44 +0000 | [diff] [blame] | 203 | PyDict_SetItem(dict, k, v); |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 204 | } |
| 205 | Py_XDECREF(k); |
| 206 | Py_XDECREF(v); |
| 207 | } |
| 208 | |
| 209 | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
| 210 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 211 | |
| 212 | static struct PyModuleDef winsoundmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | 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öwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Mark Hammond | e407e2a | 2002-07-22 13:26:41 +0000 | [diff] [blame] | 224 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 225 | PyInit_winsound(void) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | PyObject *dict; |
| 228 | PyObject *module = PyModule_Create(&winsoundmodule); |
| 229 | if (module == NULL) |
| 230 | return NULL; |
| 231 | dict = PyModule_GetDict(module); |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | 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 Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 244 | 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 Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 250 | } |