1 | n/a | /* Author: Toby Dickenson <htrd90@zepler.org> |
---|
2 | n/a | * |
---|
3 | n/a | * Copyright (c) 1999 Toby Dickenson |
---|
4 | n/a | * |
---|
5 | n/a | * Permission to use this software in any way is granted without |
---|
6 | n/a | * fee, provided that the copyright notice above appears in all |
---|
7 | n/a | * copies. This software is provided "as is" without any warranty. |
---|
8 | n/a | */ |
---|
9 | n/a | |
---|
10 | n/a | /* Modified by Guido van Rossum */ |
---|
11 | n/a | /* Beep added by Mark Hammond */ |
---|
12 | n/a | /* Win9X Beep and platform identification added by Uncle Timmy */ |
---|
13 | n/a | |
---|
14 | n/a | /* Example: |
---|
15 | n/a | |
---|
16 | n/a | import winsound |
---|
17 | n/a | import time |
---|
18 | n/a | |
---|
19 | n/a | # Play wav file |
---|
20 | n/a | winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME) |
---|
21 | n/a | |
---|
22 | n/a | # Play sound from control panel settings |
---|
23 | n/a | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) |
---|
24 | n/a | |
---|
25 | n/a | # Play wav file from memory |
---|
26 | n/a | data=open('c:/windows/media/Chimes.wav',"rb").read() |
---|
27 | n/a | winsound.PlaySound(data, winsound.SND_MEMORY) |
---|
28 | n/a | |
---|
29 | n/a | # Start playing the first bit of wav file asynchronously |
---|
30 | n/a | winsound.PlaySound('c:/windows/media/Chord.wav', |
---|
31 | n/a | winsound.SND_FILENAME|winsound.SND_ASYNC) |
---|
32 | n/a | # But dont let it go for too long... |
---|
33 | n/a | time.sleep(0.1) |
---|
34 | n/a | # ...Before stopping it |
---|
35 | n/a | winsound.PlaySound(None, 0) |
---|
36 | n/a | */ |
---|
37 | n/a | |
---|
38 | n/a | #include <Python.h> |
---|
39 | n/a | #include <windows.h> |
---|
40 | n/a | #include <mmsystem.h> |
---|
41 | n/a | |
---|
42 | n/a | PyDoc_STRVAR(sound_module_doc, |
---|
43 | n/a | "PlaySound(sound, flags) - play a sound\n" |
---|
44 | n/a | "SND_FILENAME - sound is a wav file name\n" |
---|
45 | n/a | "SND_ALIAS - sound is a registry sound association name\n" |
---|
46 | n/a | "SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n" |
---|
47 | n/a | "SND_MEMORY - sound is a memory image of a wav file\n" |
---|
48 | n/a | "SND_PURGE - stop all instances of the specified sound\n" |
---|
49 | n/a | "SND_ASYNC - PlaySound returns immediately\n" |
---|
50 | n/a | "SND_NODEFAULT - Do not play a default beep if the sound can not be found\n" |
---|
51 | n/a | "SND_NOSTOP - Do not interrupt any sounds currently playing\n" // Raising RuntimeError if needed |
---|
52 | n/a | "SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors |
---|
53 | n/a | "\n" |
---|
54 | n/a | "Beep(frequency, duration) - Make a beep through the PC speaker.\n" |
---|
55 | n/a | "MessageBeep(type) - Call Windows MessageBeep."); |
---|
56 | n/a | |
---|
57 | n/a | /*[clinic input] |
---|
58 | n/a | module winsound |
---|
59 | n/a | [clinic start generated code]*/ |
---|
60 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a18401142d97b8d5]*/ |
---|
61 | n/a | |
---|
62 | n/a | #include "clinic/winsound.c.h" |
---|
63 | n/a | |
---|
64 | n/a | /*[clinic input] |
---|
65 | n/a | winsound.PlaySound |
---|
66 | n/a | |
---|
67 | n/a | sound: object |
---|
68 | n/a | The sound to play; a filename, data, or None. |
---|
69 | n/a | flags: int |
---|
70 | n/a | Flag values, ored together. See module documentation. |
---|
71 | n/a | |
---|
72 | n/a | A wrapper around the Windows PlaySound API. |
---|
73 | n/a | [clinic start generated code]*/ |
---|
74 | n/a | |
---|
75 | n/a | static PyObject * |
---|
76 | n/a | winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags) |
---|
77 | n/a | /*[clinic end generated code: output=49a0fd16a372ebeb input=c63e1f2d848da2f2]*/ |
---|
78 | n/a | { |
---|
79 | n/a | int ok; |
---|
80 | n/a | wchar_t *wsound; |
---|
81 | n/a | Py_buffer view = {NULL, NULL}; |
---|
82 | n/a | |
---|
83 | n/a | if (sound == Py_None) { |
---|
84 | n/a | wsound = NULL; |
---|
85 | n/a | } else if (flags & SND_MEMORY) { |
---|
86 | n/a | if (flags & SND_ASYNC) { |
---|
87 | n/a | /* Sidestep reference counting headache; unfortunately this also |
---|
88 | n/a | prevent SND_LOOP from memory. */ |
---|
89 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
90 | n/a | "Cannot play asynchronously from memory"); |
---|
91 | n/a | return NULL; |
---|
92 | n/a | } |
---|
93 | n/a | if (PyObject_GetBuffer(sound, &view, PyBUF_SIMPLE) < 0) { |
---|
94 | n/a | return NULL; |
---|
95 | n/a | } |
---|
96 | n/a | wsound = (wchar_t *)view.buf; |
---|
97 | n/a | } else { |
---|
98 | n/a | if (!PyUnicode_Check(sound)) { |
---|
99 | n/a | PyErr_Format(PyExc_TypeError, |
---|
100 | n/a | "'sound' must be str or None, not '%s'", |
---|
101 | n/a | Py_TYPE(sound)->tp_name); |
---|
102 | n/a | return NULL; |
---|
103 | n/a | } |
---|
104 | n/a | wsound = PyUnicode_AsWideCharString(sound, NULL); |
---|
105 | n/a | if (wsound == NULL) { |
---|
106 | n/a | return NULL; |
---|
107 | n/a | } |
---|
108 | n/a | } |
---|
109 | n/a | |
---|
110 | n/a | |
---|
111 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
112 | n/a | ok = PlaySoundW(wsound, NULL, flags); |
---|
113 | n/a | Py_END_ALLOW_THREADS |
---|
114 | n/a | if (view.obj) { |
---|
115 | n/a | PyBuffer_Release(&view); |
---|
116 | n/a | } else if (sound != Py_None) { |
---|
117 | n/a | PyMem_Free(wsound); |
---|
118 | n/a | } |
---|
119 | n/a | if (!ok) { |
---|
120 | n/a | PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); |
---|
121 | n/a | return NULL; |
---|
122 | n/a | } |
---|
123 | n/a | Py_RETURN_NONE; |
---|
124 | n/a | } |
---|
125 | n/a | |
---|
126 | n/a | /*[clinic input] |
---|
127 | n/a | winsound.Beep |
---|
128 | n/a | |
---|
129 | n/a | frequency: int |
---|
130 | n/a | Frequency of the sound in hertz. |
---|
131 | n/a | Must be in the range 37 through 32,767. |
---|
132 | n/a | duration: int |
---|
133 | n/a | How long the sound should play, in milliseconds. |
---|
134 | n/a | |
---|
135 | n/a | A wrapper around the Windows Beep API. |
---|
136 | n/a | [clinic start generated code]*/ |
---|
137 | n/a | |
---|
138 | n/a | static PyObject * |
---|
139 | n/a | winsound_Beep_impl(PyObject *module, int frequency, int duration) |
---|
140 | n/a | /*[clinic end generated code: output=f32382e52ee9b2fb input=40e360cfa00a5cf0]*/ |
---|
141 | n/a | { |
---|
142 | n/a | BOOL ok; |
---|
143 | n/a | |
---|
144 | n/a | if (frequency < 37 || frequency > 32767) { |
---|
145 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
146 | n/a | "frequency must be in 37 thru 32767"); |
---|
147 | n/a | return NULL; |
---|
148 | n/a | } |
---|
149 | n/a | |
---|
150 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
151 | n/a | ok = Beep(frequency, duration); |
---|
152 | n/a | Py_END_ALLOW_THREADS |
---|
153 | n/a | if (!ok) { |
---|
154 | n/a | PyErr_SetString(PyExc_RuntimeError,"Failed to beep"); |
---|
155 | n/a | return NULL; |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | Py_RETURN_NONE; |
---|
159 | n/a | } |
---|
160 | n/a | |
---|
161 | n/a | /*[clinic input] |
---|
162 | n/a | winsound.MessageBeep |
---|
163 | n/a | |
---|
164 | n/a | type: int(c_default="MB_OK") = MB_OK |
---|
165 | n/a | |
---|
166 | n/a | Call Windows MessageBeep(x). |
---|
167 | n/a | |
---|
168 | n/a | x defaults to MB_OK. |
---|
169 | n/a | [clinic start generated code]*/ |
---|
170 | n/a | |
---|
171 | n/a | static PyObject * |
---|
172 | n/a | winsound_MessageBeep_impl(PyObject *module, int type) |
---|
173 | n/a | /*[clinic end generated code: output=120875455121121f input=db185f741ae21401]*/ |
---|
174 | n/a | { |
---|
175 | n/a | BOOL ok; |
---|
176 | n/a | |
---|
177 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
178 | n/a | ok = MessageBeep(type); |
---|
179 | n/a | Py_END_ALLOW_THREADS |
---|
180 | n/a | |
---|
181 | n/a | if (!ok) { |
---|
182 | n/a | PyErr_SetExcFromWindowsErr(PyExc_RuntimeError, 0); |
---|
183 | n/a | return NULL; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | Py_RETURN_NONE; |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | static struct PyMethodDef sound_methods[] = |
---|
190 | n/a | { |
---|
191 | n/a | WINSOUND_PLAYSOUND_METHODDEF |
---|
192 | n/a | WINSOUND_BEEP_METHODDEF |
---|
193 | n/a | WINSOUND_MESSAGEBEEP_METHODDEF |
---|
194 | n/a | {NULL, NULL} |
---|
195 | n/a | }; |
---|
196 | n/a | |
---|
197 | n/a | static void |
---|
198 | n/a | add_define(PyObject *dict, const char *key, long value) |
---|
199 | n/a | { |
---|
200 | n/a | PyObject *k = PyUnicode_FromString(key); |
---|
201 | n/a | PyObject *v = PyLong_FromLong(value); |
---|
202 | n/a | if (v && k) { |
---|
203 | n/a | PyDict_SetItem(dict, k, v); |
---|
204 | n/a | } |
---|
205 | n/a | Py_XDECREF(k); |
---|
206 | n/a | Py_XDECREF(v); |
---|
207 | n/a | } |
---|
208 | n/a | |
---|
209 | n/a | #define ADD_DEFINE(tok) add_define(dict,#tok,tok) |
---|
210 | n/a | |
---|
211 | n/a | |
---|
212 | n/a | static struct PyModuleDef winsoundmodule = { |
---|
213 | n/a | PyModuleDef_HEAD_INIT, |
---|
214 | n/a | "winsound", |
---|
215 | n/a | sound_module_doc, |
---|
216 | n/a | -1, |
---|
217 | n/a | sound_methods, |
---|
218 | n/a | NULL, |
---|
219 | n/a | NULL, |
---|
220 | n/a | NULL, |
---|
221 | n/a | NULL |
---|
222 | n/a | }; |
---|
223 | n/a | |
---|
224 | n/a | PyMODINIT_FUNC |
---|
225 | n/a | PyInit_winsound(void) |
---|
226 | n/a | { |
---|
227 | n/a | PyObject *dict; |
---|
228 | n/a | PyObject *module = PyModule_Create(&winsoundmodule); |
---|
229 | n/a | if (module == NULL) |
---|
230 | n/a | return NULL; |
---|
231 | n/a | dict = PyModule_GetDict(module); |
---|
232 | n/a | |
---|
233 | n/a | ADD_DEFINE(SND_ASYNC); |
---|
234 | n/a | ADD_DEFINE(SND_NODEFAULT); |
---|
235 | n/a | ADD_DEFINE(SND_NOSTOP); |
---|
236 | n/a | ADD_DEFINE(SND_NOWAIT); |
---|
237 | n/a | ADD_DEFINE(SND_ALIAS); |
---|
238 | n/a | ADD_DEFINE(SND_FILENAME); |
---|
239 | n/a | ADD_DEFINE(SND_MEMORY); |
---|
240 | n/a | ADD_DEFINE(SND_PURGE); |
---|
241 | n/a | ADD_DEFINE(SND_LOOP); |
---|
242 | n/a | ADD_DEFINE(SND_APPLICATION); |
---|
243 | n/a | |
---|
244 | n/a | ADD_DEFINE(MB_OK); |
---|
245 | n/a | ADD_DEFINE(MB_ICONASTERISK); |
---|
246 | n/a | ADD_DEFINE(MB_ICONEXCLAMATION); |
---|
247 | n/a | ADD_DEFINE(MB_ICONHAND); |
---|
248 | n/a | ADD_DEFINE(MB_ICONQUESTION); |
---|
249 | n/a | return module; |
---|
250 | n/a | } |
---|