The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / PC / winsound.c
blob63906c99dac0c44779d883d8075168c7104f0694
1 /* Author: Toby Dickenson <htrd90@zepler.org>
3 * Copyright (c) 1999 Toby Dickenson
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 */
10 /* Modified by Guido van Rossum */
11 /* Beep added by Mark Hammond */
13 /* Example:
15 import winsound
16 import time
18 # Play wav file
19 winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME)
21 # Play sound from control panel settings
22 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
24 # Play wav file from memory
25 data=open('c:/windows/media/Chimes.wav',"rb").read()
26 winsound.PlaySound(data, winsound.SND_MEMORY)
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)
37 #include <windows.h>
38 #include <mmsystem.h>
39 #include <Python.h>
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";
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";
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
65 "\n"
66 "Beep(frequency, duration) - Make a beep through the PC speaker.\n";
68 PyObject *sound_playsound(PyObject *s, PyObject *args)
70 const char *sound;
71 int flags;
72 int length;
73 int ok;
75 if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags))
77 return NULL;
80 if(flags&SND_ASYNC && flags &SND_MEMORY)
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;
88 Py_BEGIN_ALLOW_THREADS
89 ok = PlaySound(sound,NULL,flags);
90 Py_END_ALLOW_THREADS
91 if(!ok)
93 PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
94 return NULL;
97 Py_INCREF(Py_None);
98 return Py_None;
101 static PyObject *sound_beep( PyObject *self, PyObject *args )
103 int freq;
104 int dur;
105 BOOL ok;
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)
114 PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
115 return NULL;
117 Py_INCREF(Py_None);
118 return Py_None;
121 static struct PyMethodDef sound_methods[] =
123 {"PlaySound", sound_playsound, 1, sound_playsound_doc},
124 {"Beep", sound_beep, 1, sound_beep_doc},
125 {NULL, NULL}
128 static void add_define(PyObject *dict, const char *key, long value)
130 PyObject *k=PyString_FromString(key);
131 PyObject *v=PyLong_FromLong(value);
132 if(v&&k)
134 PyDict_SetItem(dict,k,v);
136 Py_XDECREF(k);
137 Py_XDECREF(v);
140 #define ADD_DEFINE(tok) add_define(dict,#tok,tok)
142 DL_EXPORT(void)
143 initwinsound()
145 PyObject *module=Py_InitModule3("winsound", sound_methods, sound_module_doc);
146 PyObject *dict=PyModule_GetDict(module);
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);