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