blob: bc96387a6b8bf62748d92bad8ad9343d3a877b71 [file] [log] [blame]
Guido van Rossume6005781999-02-04 22:40:42 +00001/* 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 Rossum99eb7a11999-10-01 14:29:17 +000011/* Beep added by Mark Hammond */
Guido van Rossume6005781999-02-04 22:40:42 +000012
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
41static 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 Rossum99eb7a11999-10-01 14:29:17 +000047static 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"
Tim Petersbe30c6b2001-01-25 20:40:28 +000052"The duration argument specifies the number of milli-seconds.\n"
53"Note: Under Windows 95 and 98, the arguments are ignored; if the system\n"
54"has a sound card, the system default sound is played; else (no sound card)\n"
55"the standard system beep.\n";
Guido van Rossum99eb7a11999-10-01 14:29:17 +000056
Guido van Rossume6005781999-02-04 22:40:42 +000057static char sound_module_doc[] =
58"PlaySound(sound, flags) - play a sound\n"
59"SND_FILENAME - sound is a wav file name\n"
60"SND_ALIAS - sound is a control panel sound association name\n"
61"SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n"
62"SND_MEMORY - sound is a memory image of a wav file\n"
63"SND_PURGE - stop all instances of the specified sound\n"
64"SND_ASYNC - PlaySound returns immediately\n"
65"SND_NODEFAULT - Do not play a default beep if the sound can not be found\n"
66"SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed
67"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
Guido van Rossum99eb7a11999-10-01 14:29:17 +000068"\n"
69"Beep(frequency, duration) - Make a beep through the PC speaker.\n";
Guido van Rossume6005781999-02-04 22:40:42 +000070
71PyObject *sound_playsound(PyObject *s, PyObject *args)
72{
73 const char *sound;
74 int flags;
75 int length;
76 int ok;
77
78 if(!PyArg_ParseTuple(args,"z#i:PlaySound",&sound,&length,&flags))
79 {
80 return NULL;
81 }
82
83 if(flags&SND_ASYNC && flags &SND_MEMORY)
84 {
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;
89 }
90
91 Py_BEGIN_ALLOW_THREADS
92 ok = PlaySound(sound,NULL,flags);
93 Py_END_ALLOW_THREADS
94 if(!ok)
95 {
96 PyErr_SetString(PyExc_RuntimeError,"Failed to play sound");
97 return NULL;
98 }
99
100 Py_INCREF(Py_None);
101 return Py_None;
102}
103
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000104static PyObject *sound_beep( PyObject *self, PyObject *args )
105{
106 int freq;
107 int dur;
108 BOOL ok;
109
110 if (!PyArg_ParseTuple(args, "ii:Beep", &freq, &dur))
111 return NULL;
112 Py_BEGIN_ALLOW_THREADS
113 ok = Beep(freq,dur);
114 Py_END_ALLOW_THREADS
115 if(!ok)
116 {
117 PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
118 return NULL;
119 }
120 Py_INCREF(Py_None);
121 return Py_None;
122}
123
Guido van Rossume6005781999-02-04 22:40:42 +0000124static struct PyMethodDef sound_methods[] =
125{
126 {"PlaySound", sound_playsound, 1, sound_playsound_doc},
Guido van Rossum99eb7a11999-10-01 14:29:17 +0000127 {"Beep", sound_beep, 1, sound_beep_doc},
Guido van Rossume6005781999-02-04 22:40:42 +0000128 {NULL, NULL}
129};
130
131static void add_define(PyObject *dict, const char *key, long value)
132{
133 PyObject *k=PyString_FromString(key);
134 PyObject *v=PyLong_FromLong(value);
135 if(v&&k)
136 {
137 PyDict_SetItem(dict,k,v);
138 }
139 Py_XDECREF(k);
140 Py_XDECREF(v);
141}
142
143#define ADD_DEFINE(tok) add_define(dict,#tok,tok)
144
145DL_EXPORT(void)
Thomas Wouters78890102000-07-22 19:25:51 +0000146initwinsound(void)
Guido van Rossume6005781999-02-04 22:40:42 +0000147{
148 PyObject *module=Py_InitModule3("winsound", sound_methods, sound_module_doc);
149 PyObject *dict=PyModule_GetDict(module);
150
151 ADD_DEFINE(SND_ASYNC);
152 ADD_DEFINE(SND_NODEFAULT);
153 ADD_DEFINE(SND_NOSTOP);
154 ADD_DEFINE(SND_NOWAIT);
155 ADD_DEFINE(SND_ALIAS);
156 ADD_DEFINE(SND_FILENAME);
157 ADD_DEFINE(SND_MEMORY);
158 ADD_DEFINE(SND_PURGE);
159 ADD_DEFINE(SND_LOOP);
160 ADD_DEFINE(SND_APPLICATION);
161}