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 */ |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 12 | |
| 13 | /* Example: |
| 14 | |
| 15 | import winsound |
| 16 | import time |
| 17 | |
| 18 | # Play wav file |
| 19 | winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME) |
| 20 | |
| 21 | # Play sound from control panel settings |
| 22 | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) |
| 23 | |
| 24 | # Play wav file from memory |
| 25 | data=open('c:/windows/media/Chimes.wav',"rb").read() |
| 26 | winsound.PlaySound(data, winsound.SND_MEMORY) |
| 27 | |
| 28 | # Start playing the first bit of wav file asynchronously |
| 29 | winsound.PlaySound('c:/windows/media/Chord.wav', |
| 30 | winsound.SND_FILENAME|winsound.SND_ASYNC) |
| 31 | # But dont let it go for too long... |
| 32 | time.sleep(0.1) |
| 33 | # ...Before stopping it |
| 34 | winsound.PlaySound(None, 0) |
| 35 | */ |
| 36 | |
| 37 | #include <windows.h> |
| 38 | #include <mmsystem.h> |
| 39 | #include <Python.h> |
| 40 | |
| 41 | static char sound_playsound_doc[] = |
| 42 | "PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n" |
| 43 | "\n" |
| 44 | "The sound argument can be a filename, data, or None.\n" |
| 45 | "For flag values, ored together, see module documentation.\n"; |
| 46 | |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 47 | static char sound_beep_doc[] = |
| 48 | "Beep(frequency, duration) - a wrapper around the Windows Beep API\n" |
| 49 | "\n" |
| 50 | "The frequency argument specifies frequency, in hertz, of the sound.\n" |
| 51 | "This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).\n" |
| 52 | "The duration argument specifies the number of milli-seconds.\n"; |
| 53 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 54 | static char sound_module_doc[] = |
| 55 | "PlaySound(sound, flags) - play a sound\n" |
| 56 | "SND_FILENAME - sound is a wav file name\n" |
| 57 | "SND_ALIAS - sound is a control panel sound association name\n" |
| 58 | "SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n" |
| 59 | "SND_MEMORY - sound is a memory image of a wav file\n" |
| 60 | "SND_PURGE - stop all instances of the specified sound\n" |
| 61 | "SND_ASYNC - PlaySound returns immediately\n" |
| 62 | "SND_NODEFAULT - Do not play a default beep if the sound can not be found\n" |
| 63 | "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed |
| 64 | "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] | 65 | "\n" |
| 66 | "Beep(frequency, duration) - Make a beep through the PC speaker.\n"; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 67 | |
| 68 | PyObject *sound_playsound(PyObject *s, PyObject *args) |
| 69 | { |
| 70 | const char *sound; |
| 71 | int flags; |
| 72 | int length; |
| 73 | int ok; |
| 74 | |
| 75 | if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags)) |
| 76 | { |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | if(flags&SND_ASYNC && flags &SND_MEMORY) |
| 81 | { |
| 82 | /* Sidestep reference counting headache; unfortunately this also |
| 83 | prevent SND_LOOP from memory. */ |
| 84 | PyErr_SetString(PyExc_RuntimeError,"Cannot play asynchronously from memory"); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | Py_BEGIN_ALLOW_THREADS |
| 89 | ok = PlaySound(sound,NULL,flags); |
| 90 | Py_END_ALLOW_THREADS |
| 91 | if(!ok) |
| 92 | { |
| 93 | PyErr_SetString(PyExc_RuntimeError,"Failed to play sound"); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | Py_INCREF(Py_None); |
| 98 | return Py_None; |
| 99 | } |
| 100 | |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 101 | static PyObject *sound_beep( PyObject *self, PyObject *args ) |
| 102 | { |
| 103 | int freq; |
| 104 | int dur; |
| 105 | BOOL ok; |
| 106 | |
| 107 | if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) |
| 108 | return NULL; |
| 109 | Py_BEGIN_ALLOW_THREADS |
| 110 | ok = Beep(freq,dur); |
| 111 | Py_END_ALLOW_THREADS |
| 112 | if(!ok) |
| 113 | { |
| 114 | PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
| 115 | return NULL; |
| 116 | } |
| 117 | Py_INCREF(Py_None); |
| 118 | return Py_None; |
| 119 | } |
| 120 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 121 | static struct PyMethodDef sound_methods[] = |
| 122 | { |
| 123 | {"PlaySound", sound_playsound, 1, sound_playsound_doc}, |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 124 | {"Beep", sound_beep, 1, sound_beep_doc}, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 125 | {NULL, NULL} |
| 126 | }; |
| 127 | |
| 128 | static void add_define(PyObject *dict, const char *key, long value) |
| 129 | { |
| 130 | PyObject *k=PyString_FromString(key); |
| 131 | PyObject *v=PyLong_FromLong(value); |
| 132 | if(v&&k) |
| 133 | { |
| 134 | PyDict_SetItem(dict,k,v); |
| 135 | } |
| 136 | Py_XDECREF(k); |
| 137 | Py_XDECREF(v); |
| 138 | } |
| 139 | |
| 140 | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
| 141 | |
| 142 | DL_EXPORT(void) |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 143 | initwinsound(void) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 144 | { |
| 145 | PyObject *module=Py_InitModule3("winsound", sound_methods, sound_module_doc); |
| 146 | PyObject *dict=PyModule_GetDict(module); |
| 147 | |
| 148 | ADD_DEFINE(SND_ASYNC); |
| 149 | ADD_DEFINE(SND_NODEFAULT); |
| 150 | ADD_DEFINE(SND_NOSTOP); |
| 151 | ADD_DEFINE(SND_NOWAIT); |
| 152 | ADD_DEFINE(SND_ALIAS); |
| 153 | ADD_DEFINE(SND_FILENAME); |
| 154 | ADD_DEFINE(SND_MEMORY); |
| 155 | ADD_DEFINE(SND_PURGE); |
| 156 | ADD_DEFINE(SND_LOOP); |
| 157 | ADD_DEFINE(SND_APPLICATION); |
| 158 | } |