| 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 |  | 
| Kristján Valur Jónsson | 629ec26 | 2007-05-26 19:31:39 +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" | 
| Amaury Forgeot d'Arc | f9b54c2 | 2008-05-12 21:30:24 +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 | fee1004 | 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 | { | 
 | 75 |     const char *sound; | 
 | 76 |     int flags; | 
 | 77 |     int length; | 
 | 78 |     int ok; | 
 | 79 |  | 
| Martin Panter | 5f75502 | 2016-09-15 01:50:53 +0000 | [diff] [blame] | 80 |     if (!PyArg_ParseTuple(args, "z#i:PlaySound", &sound, &length, &flags)) { | 
 | 81 |         return NULL; | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 82 |     } | 
 | 83 |  | 
| Martin Panter | 5f75502 | 2016-09-15 01:50:53 +0000 | [diff] [blame] | 84 |     if (flags & SND_ASYNC && flags & SND_MEMORY) { | 
 | 85 |         /* Sidestep reference counting headache; unfortunately this also | 
 | 86 |            prevent SND_LOOP from memory. */ | 
 | 87 |         PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory"); | 
 | 88 |         return NULL; | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 89 |     } | 
 | 90 |  | 
 | 91 |     Py_BEGIN_ALLOW_THREADS | 
| Martin Panter | 5f75502 | 2016-09-15 01:50:53 +0000 | [diff] [blame] | 92 |     ok = PlaySound(sound, NULL, flags); | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 93 |     Py_END_ALLOW_THREADS | 
| Martin Panter | 5f75502 | 2016-09-15 01:50:53 +0000 | [diff] [blame] | 94 |     if (!ok) { | 
 | 95 |         PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); | 
 | 96 |         return NULL; | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 97 |     } | 
 | 98 |  | 
 | 99 |     Py_INCREF(Py_None); | 
 | 100 |     return Py_None; | 
 | 101 | } | 
 | 102 |  | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 103 | static PyObject * | 
 | 104 | sound_beep(PyObject *self, PyObject *args) | 
| Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 105 | { | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 106 |     int freq; | 
 | 107 |     int dur; | 
 | 108 |     BOOL ok; | 
| Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 109 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 110 |     if (!PyArg_ParseTuple(args, "ii:Beep", &freq,  &dur)) | 
 | 111 |         return NULL; | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 112 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 113 |     if (freq < 37 || freq > 32767) { | 
 | 114 |         PyErr_SetString(PyExc_ValueError, | 
 | 115 |                         "frequency must be in 37 thru 32767"); | 
 | 116 |         return NULL; | 
 | 117 |     } | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 118 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 119 |     Py_BEGIN_ALLOW_THREADS | 
 | 120 |     ok = Beep(freq, dur); | 
 | 121 |     Py_END_ALLOW_THREADS | 
 | 122 |     if (!ok) { | 
 | 123 |         PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); | 
 | 124 |         return NULL; | 
 | 125 |     } | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 126 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 127 |     Py_INCREF(Py_None); | 
 | 128 |     return Py_None; | 
| Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 129 | } | 
 | 130 |  | 
| Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 131 | static PyObject * | 
 | 132 | sound_msgbeep(PyObject *self, PyObject *args) | 
 | 133 | { | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 134 |     int x = MB_OK; | 
 | 135 |     if (!PyArg_ParseTuple(args, "|i:MessageBeep", &x)) | 
 | 136 |         return NULL; | 
 | 137 |     MessageBeep(x); | 
 | 138 |     Py_INCREF(Py_None); | 
 | 139 |     return Py_None; | 
| Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 140 | } | 
 | 141 |  | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 142 | static struct PyMethodDef sound_methods[] = | 
 | 143 | { | 
| Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 144 |     {"PlaySound", sound_playsound, METH_VARARGS, sound_playsound_doc}, | 
 | 145 |     {"Beep",      sound_beep,      METH_VARARGS, sound_beep_doc}, | 
| Guido van Rossum | e125268 | 2003-04-09 19:38:08 +0000 | [diff] [blame] | 146 |     {"MessageBeep", sound_msgbeep, METH_VARARGS, sound_msgbeep_doc}, | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 147 |     {NULL,  NULL} | 
 | 148 | }; | 
 | 149 |  | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 150 | static void | 
 | 151 | add_define(PyObject *dict, const char *key, long value) | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 152 | { | 
| Martin Panter | 5f75502 | 2016-09-15 01:50:53 +0000 | [diff] [blame] | 153 |     PyObject *k = PyString_FromString(key); | 
 | 154 |     PyObject *v = PyLong_FromLong(value); | 
 | 155 |     if (v && k) { | 
 | 156 |         PyDict_SetItem(dict, k, v); | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 157 |     } | 
 | 158 |     Py_XDECREF(k); | 
 | 159 |     Py_XDECREF(v); | 
 | 160 | } | 
 | 161 |  | 
 | 162 | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) | 
 | 163 |  | 
| Mark Hammond | e407e2a | 2002-07-22 13:26:41 +0000 | [diff] [blame] | 164 | PyMODINIT_FUNC | 
| Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 165 | initwinsound(void) | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 166 | { | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 167 |     PyObject *dict; | 
 | 168 |     PyObject *module = Py_InitModule3("winsound", | 
 | 169 |                                       sound_methods, | 
 | 170 |                                       sound_module_doc); | 
 | 171 |     if (module == NULL) | 
 | 172 |         return; | 
 | 173 |     dict = PyModule_GetDict(module); | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 174 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 175 |     ADD_DEFINE(SND_ASYNC); | 
 | 176 |     ADD_DEFINE(SND_NODEFAULT); | 
 | 177 |     ADD_DEFINE(SND_NOSTOP); | 
 | 178 |     ADD_DEFINE(SND_NOWAIT); | 
 | 179 |     ADD_DEFINE(SND_ALIAS); | 
 | 180 |     ADD_DEFINE(SND_FILENAME); | 
 | 181 |     ADD_DEFINE(SND_MEMORY); | 
 | 182 |     ADD_DEFINE(SND_PURGE); | 
 | 183 |     ADD_DEFINE(SND_LOOP); | 
 | 184 |     ADD_DEFINE(SND_APPLICATION); | 
| Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 185 |  | 
| Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 186 |     ADD_DEFINE(MB_OK); | 
 | 187 |     ADD_DEFINE(MB_ICONASTERISK); | 
 | 188 |     ADD_DEFINE(MB_ICONEXCLAMATION); | 
 | 189 |     ADD_DEFINE(MB_ICONHAND); | 
 | 190 |     ADD_DEFINE(MB_ICONQUESTION); | 
| Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 191 | } |