ยปCore Development>Code coverage>PC/winsound.c

Python code coverage for PC/winsound.c

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