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_playsound_doc, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 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" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 46 | "For flag values, ored together, see module documentation."); |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(sound_beep_doc, |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 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" |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 52 | "This parameter must be in the range 37 through 32,767.\n" |
Alexandre Vassalotti | eca20b6 | 2008-05-16 02:54:33 +0000 | [diff] [blame] | 53 | "The duration argument specifies the number of milliseconds.\n"); |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 55 | PyDoc_STRVAR(sound_msgbeep_doc, |
| 56 | "MessageBeep(x) - call Windows MessageBeep(x). x defaults to MB_OK."); |
| 57 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 58 | PyDoc_STRVAR(sound_module_doc, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 59 | "PlaySound(sound, flags) - play a sound\n" |
| 60 | "SND_FILENAME - sound is a wav file name\n" |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 61 | "SND_ALIAS - sound is a registry sound association name\n" |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 62 | "SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n" |
| 63 | "SND_MEMORY - sound is a memory image of a wav file\n" |
| 64 | "SND_PURGE - stop all instances of the specified sound\n" |
| 65 | "SND_ASYNC - PlaySound returns immediately\n" |
| 66 | "SND_NODEFAULT - Do not play a default beep if the sound can not be found\n" |
| 67 | "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed |
| 68 | "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] | 69 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 70 | "Beep(frequency, duration) - Make a beep through the PC speaker."); |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 71 | |
Walter Dörwald | fee10040 | 2003-05-22 17:22:54 +0000 | [diff] [blame] | 72 | static PyObject * |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 73 | sound_playsound(PyObject *s, PyObject *args) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 74 | { |
Hirokazu Yamamoto | c08c9bc | 2010-11-07 09:23:15 +0000 | [diff] [blame] | 75 | Py_UNICODE *wsound; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 76 | int flags; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 77 | int ok; |
| 78 | |
Hirokazu Yamamoto | c08c9bc | 2010-11-07 09:23:15 +0000 | [diff] [blame] | 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; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 95 | } |
Hirokazu Yamamoto | 4ae5f13 | 2010-11-07 14:29:26 +0000 | [diff] [blame] | 96 | return NULL; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 99 | static PyObject * |
| 100 | sound_beep(PyObject *self, PyObject *args) |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | int freq; |
| 103 | int dur; |
| 104 | BOOL ok; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) |
| 107 | return NULL; |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | if (freq < 37 || freq > 32767) { |
| 110 | PyErr_SetString(PyExc_ValueError, |
| 111 | "frequency must be in 37 thru 32767"); |
| 112 | return NULL; |
| 113 | } |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | Py_BEGIN_ALLOW_THREADS |
| 116 | ok = Beep(freq, dur); |
| 117 | Py_END_ALLOW_THREADS |
| 118 | if (!ok) { |
| 119 | PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
| 120 | return NULL; |
| 121 | } |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | Py_INCREF(Py_None); |
| 124 | return Py_None; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 127 | static PyObject * |
| 128 | sound_msgbeep(PyObject *self, PyObject *args) |
| 129 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | int x = MB_OK; |
| 131 | if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x)) |
| 132 | return NULL; |
| 133 | MessageBeep(x); |
| 134 | Py_INCREF(Py_None); |
| 135 | return Py_None; |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 138 | static struct PyMethodDef sound_methods[] = |
| 139 | { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 140 | {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc}, |
| 141 | {"Beep", sound_beep, METH_VARARGS, sound_beep_doc}, |
Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 142 | {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc}, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 143 | {NULL, NULL} |
| 144 | }; |
| 145 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 146 | static void |
| 147 | add_define(PyObject *dict, const char *key, long value) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 148 | { |
Hirokazu Yamamoto | 62fbdd9 | 2010-11-07 02:45:19 +0000 | [diff] [blame] | 149 | PyObject *k = PyUnicode_FromString(key); |
| 150 | PyObject *v = PyLong_FromLong(value); |
| 151 | if (v && k) { |
Hirokazu Yamamoto | cdc8cdd | 2010-11-07 11:07:44 +0000 | [diff] [blame] | 152 | PyDict_SetItem(dict, k, v); |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 153 | } |
| 154 | Py_XDECREF(k); |
| 155 | Py_XDECREF(v); |
| 156 | } |
| 157 | |
| 158 | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
| 159 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 160 | |
| 161 | static struct PyModuleDef winsoundmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | PyModuleDef_HEAD_INIT, |
| 163 | "winsound", |
| 164 | sound_module_doc, |
| 165 | -1, |
| 166 | sound_methods, |
| 167 | NULL, |
| 168 | NULL, |
| 169 | NULL, |
| 170 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
Mark Hammond | e407e2a | 2002-07-22 13:26:41 +0000 | [diff] [blame] | 173 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 174 | PyInit_winsound(void) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | PyObject *dict; |
| 177 | PyObject *module = PyModule_Create(&winsoundmodule); |
| 178 | if (module == NULL) |
| 179 | return NULL; |
| 180 | dict = PyModule_GetDict(module); |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | ADD_DEFINE(SND_ASYNC); |
| 183 | ADD_DEFINE(SND_NODEFAULT); |
| 184 | ADD_DEFINE(SND_NOSTOP); |
| 185 | ADD_DEFINE(SND_NOWAIT); |
| 186 | ADD_DEFINE(SND_ALIAS); |
| 187 | ADD_DEFINE(SND_FILENAME); |
| 188 | ADD_DEFINE(SND_MEMORY); |
| 189 | ADD_DEFINE(SND_PURGE); |
| 190 | ADD_DEFINE(SND_LOOP); |
| 191 | ADD_DEFINE(SND_APPLICATION); |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | ADD_DEFINE(MB_OK); |
| 194 | ADD_DEFINE(MB_ICONASTERISK); |
| 195 | ADD_DEFINE(MB_ICONEXCLAMATION); |
| 196 | ADD_DEFINE(MB_ICONHAND); |
| 197 | ADD_DEFINE(MB_ICONQUESTION); |
| 198 | return module; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 199 | } |