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 | |
| 38 | #include <windows.h> |
| 39 | #include <mmsystem.h> |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 40 | #include <conio.h> /* port functions on Win9x */ |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 41 | #include <Python.h> |
| 42 | |
| 43 | static char sound_playsound_doc[] = |
| 44 | "PlaySound(sound, flags) - a wrapper around the Windows PlaySound API\n" |
| 45 | "\n" |
| 46 | "The sound argument can be a filename, data, or None.\n" |
| 47 | "For flag values, ored together, see module documentation.\n"; |
| 48 | |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 49 | static char sound_beep_doc[] = |
| 50 | "Beep(frequency, duration) - a wrapper around the Windows Beep API\n" |
| 51 | "\n" |
| 52 | "The frequency argument specifies frequency, in hertz, of the sound.\n" |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 53 | "This parameter must be in the range 37 through 32,767.\n" |
| 54 | "The duration argument specifies the number of milliseconds.\n" |
| 55 | "On WinNT and 2000, the platform Beep API is used directly. Else funky\n" |
| 56 | "code doing direct port manipulation is used; it's unknown whether that\n" |
| 57 | "will work on all systems.\n"; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 59 | static char sound_module_doc[] = |
| 60 | "PlaySound(sound, flags) - play a sound\n" |
| 61 | "SND_FILENAME - sound is a wav file name\n" |
Tim Peters | e79af27 | 2001-02-20 10:02:21 +0000 | [diff] [blame] | 62 | "SND_ALIAS - sound is a registry sound association name\n" |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 63 | "SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n" |
| 64 | "SND_MEMORY - sound is a memory image of a wav file\n" |
| 65 | "SND_PURGE - stop all instances of the specified sound\n" |
| 66 | "SND_ASYNC - PlaySound returns immediately\n" |
| 67 | "SND_NODEFAULT - Do not play a default beep if the sound can not be found\n" |
| 68 | "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed |
| 69 | "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] | 70 | "\n" |
| 71 | "Beep(frequency, duration) - Make a beep through the PC speaker.\n"; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 72 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 73 | PyObject * |
| 74 | sound_playsound(PyObject *s, PyObject *args) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 75 | { |
| 76 | const char *sound; |
| 77 | int flags; |
| 78 | int length; |
| 79 | int ok; |
| 80 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 81 | if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags)) { |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 85 | if(flags&SND_ASYNC && flags &SND_MEMORY) { |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 86 | /* Sidestep reference counting headache; unfortunately this also |
| 87 | prevent SND_LOOP from memory. */ |
| 88 | PyErr_SetString(PyExc_RuntimeError,"Cannot play asynchronously from memory"); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | Py_BEGIN_ALLOW_THREADS |
| 93 | ok = PlaySound(sound,NULL,flags); |
| 94 | Py_END_ALLOW_THREADS |
| 95 | if(!ok) |
| 96 | { |
| 97 | PyErr_SetString(PyExc_RuntimeError,"Failed to play sound"); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | Py_INCREF(Py_None); |
| 102 | return Py_None; |
| 103 | } |
| 104 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 105 | enum OSType {Win9X, WinNT2000}; |
| 106 | static enum OSType whichOS; /* set by module init */ |
| 107 | |
| 108 | static PyObject * |
| 109 | sound_beep(PyObject *self, PyObject *args) |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 110 | { |
| 111 | int freq; |
| 112 | int dur; |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 113 | |
| 114 | if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur)) |
| 115 | return NULL; |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 116 | |
| 117 | if (freq < 37 || freq > 32767) { |
| 118 | PyErr_SetString(PyExc_ValueError, |
| 119 | "frequency must be in 37 thru 32767"); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /* On NT and 2000, the SDK Beep() function does the whole job. |
| 124 | * But while Beep() exists before NT, it ignores its arguments and |
| 125 | * plays the system default sound. Sheesh ... |
| 126 | * The Win9X code is mondo bizarre. I (Tim) pieced it together from |
| 127 | * crap all over the web. The original IBM PC used some particular |
| 128 | * pieces of hardware (Intel 8255 and 8254 chips) hardwired to |
| 129 | * particular port addresses and running at particular clock speeds, |
| 130 | * and the poor sound card folks have been forced to emulate that in |
| 131 | * all particulars ever since. But NT and 2000 don't support port |
Tim Peters | 373d151 | 2001-02-19 08:36:41 +0000 | [diff] [blame] | 132 | * manipulation. Don't know about WinME; guessing it's like 98. |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 133 | */ |
| 134 | |
| 135 | if (whichOS == WinNT2000) { |
| 136 | BOOL ok; |
| 137 | Py_BEGIN_ALLOW_THREADS |
| 138 | ok = Beep(freq, dur); |
| 139 | Py_END_ALLOW_THREADS |
| 140 | if (!ok) { |
| 141 | PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
| 142 | return NULL; |
| 143 | } |
| 144 | } |
| 145 | else if (whichOS == Win9X) { |
| 146 | int speaker_state; |
| 147 | /* Force timer into oscillator mode via timer control port. */ |
| 148 | _outp(0x43, 0xb6); |
| 149 | /* Compute ratio of ancient hardcoded timer frequency to |
| 150 | * frequency we want. Then feed that ratio (lowest byte |
| 151 | * first) into timer data port. |
| 152 | */ |
| 153 | freq = 1193180 / freq; |
| 154 | _outp(0x42, freq & 0xff); |
| 155 | _outp(0x42, (freq >> 8) & 0xff); |
| 156 | /* Get speaker control state. */ |
| 157 | speaker_state = _inp(0x61); |
| 158 | /* Turn the speaker on (bit 1) |
| 159 | * and drive speaker from timer (bit 0). |
| 160 | */ |
| 161 | _outp(0x61, speaker_state | 0x3); |
| 162 | /* Let it blast in peace for the duration. */ |
| 163 | Py_BEGIN_ALLOW_THREADS |
| 164 | Sleep(dur); |
| 165 | Py_END_ALLOW_THREADS |
| 166 | /* Restore speaker control to original state. */ |
| 167 | _outp(0x61, speaker_state); |
| 168 | } |
| 169 | else { |
| 170 | assert(!"winsound's whichOS has insane value"); |
| 171 | } |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 172 | Py_INCREF(Py_None); |
| 173 | return Py_None; |
| 174 | } |
| 175 | |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 176 | static struct PyMethodDef sound_methods[] = |
| 177 | { |
| 178 | {"PlaySound", sound_playsound, 1, sound_playsound_doc}, |
Guido van Rossum | 99eb7a1 | 1999-10-01 14:29:17 +0000 | [diff] [blame] | 179 | {"Beep", sound_beep, 1, sound_beep_doc}, |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 180 | {NULL, NULL} |
| 181 | }; |
| 182 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 183 | static void |
| 184 | add_define(PyObject *dict, const char *key, long value) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 185 | { |
| 186 | PyObject *k=PyString_FromString(key); |
| 187 | PyObject *v=PyLong_FromLong(value); |
| 188 | if(v&&k) |
| 189 | { |
| 190 | PyDict_SetItem(dict,k,v); |
| 191 | } |
| 192 | Py_XDECREF(k); |
| 193 | Py_XDECREF(v); |
| 194 | } |
| 195 | |
| 196 | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
| 197 | |
| 198 | DL_EXPORT(void) |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 199 | initwinsound(void) |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 200 | { |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 201 | OSVERSIONINFO version; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 202 | |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 203 | PyObject *module = Py_InitModule3("winsound", |
| 204 | sound_methods, |
| 205 | sound_module_doc); |
| 206 | PyObject *dict = PyModule_GetDict(module); |
| 207 | |
| 208 | ADD_DEFINE(SND_ASYNC); |
| 209 | ADD_DEFINE(SND_NODEFAULT); |
| 210 | ADD_DEFINE(SND_NOSTOP); |
| 211 | ADD_DEFINE(SND_NOWAIT); |
| 212 | ADD_DEFINE(SND_ALIAS); |
| 213 | ADD_DEFINE(SND_FILENAME); |
| 214 | ADD_DEFINE(SND_MEMORY); |
| 215 | ADD_DEFINE(SND_PURGE); |
| 216 | ADD_DEFINE(SND_LOOP); |
| 217 | ADD_DEFINE(SND_APPLICATION); |
| 218 | |
| 219 | version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 220 | GetVersionEx(&version); |
Tim Peters | 3e50242 | 2001-02-19 07:33:23 +0000 | [diff] [blame] | 221 | whichOS = Win9X; |
Tim Peters | 25a9ce3 | 2001-02-19 07:06:36 +0000 | [diff] [blame] | 222 | if (version.dwPlatformId != VER_PLATFORM_WIN32s && |
| 223 | version.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) |
| 224 | whichOS = WinNT2000; |
Guido van Rossum | e600578 | 1999-02-04 22:40:42 +0000 | [diff] [blame] | 225 | } |