winsound.c 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* Author: Toby Dickenson <htrd90@zepler.org>
 *
 * Copyright (c) 1999 Toby Dickenson
 *
 * Permission to use this software in any way is granted without
 * fee, provided that the copyright notice above appears in all
 * copies. This software is provided "as is" without any warranty.
 */

/* Modified by Guido van Rossum */
Guido van Rossum's avatar
Guido van Rossum committed
11
/* Beep added by Mark Hammond */
12
/* Win9X Beep and platform identification added by Uncle Timmy */
13 14 15 16 17 18

/* Example:

   import winsound
   import time

19
   # Play wav file
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
   winsound.PlaySound('c:/windows/media/Chord.wav', winsound.SND_FILENAME)

   # Play sound from control panel settings
   winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)

   # Play wav file from memory
   data=open('c:/windows/media/Chimes.wav',"rb").read()
   winsound.PlaySound(data, winsound.SND_MEMORY)

   # Start playing the first bit of wav file asynchronously
   winsound.PlaySound('c:/windows/media/Chord.wav',
                   winsound.SND_FILENAME|winsound.SND_ASYNC)
   # But dont let it go for too long...
   time.sleep(0.1)
   # ...Before stopping it
   winsound.PlaySound(None, 0)
*/

38
#include <Python.h>
39 40 41
#include <windows.h>
#include <mmsystem.h>

42
PyDoc_STRVAR(sound_module_doc,
43 44
"PlaySound(sound, flags) - play a sound\n"
"SND_FILENAME - sound is a wav file name\n"
45
"SND_ALIAS - sound is a registry sound association name\n"
46 47 48 49 50 51 52
"SND_LOOP - Play the sound repeatedly; must also specify SND_ASYNC\n"
"SND_MEMORY - sound is a memory image of a wav file\n"
"SND_PURGE - stop all instances of the specified sound\n"
"SND_ASYNC - PlaySound returns immediately\n"
"SND_NODEFAULT - Do not play a default beep if the sound can not be found\n"
"SND_NOSTOP - Do not interrupt any sounds currently playing\n"  // Raising RuntimeError if needed
"SND_NOWAIT - Return immediately if the sound driver is busy\n" // Without any errors
Guido van Rossum's avatar
Guido van Rossum committed
53
"\n"
54 55 56 57 58 59 60 61 62 63 64 65 66
"Beep(frequency, duration) - Make a beep through the PC speaker.\n"
"MessageBeep(x) - Call Windows MessageBeep.");

/*[clinic input]
module winsound
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a18401142d97b8d5]*/

#include "clinic/winsound.c.h"

/*[clinic input]
winsound.PlaySound

67
    sound: Py_UNICODE(accept={str, NoneType})
68 69 70 71 72 73 74
        The sound to play; a filename, data, or None.
    flags: int
        Flag values, ored together.  See module documentation.
    /

A wrapper around the Windows PlaySound API.
[clinic start generated code]*/
75

76
static PyObject *
77 78
winsound_PlaySound_impl(PyObject *module, Py_UNICODE *sound, int flags)
/*[clinic end generated code: output=ec24b3a2b4368378 input=3411b1b7c1f36d93]*/
79 80 81
{
    int ok;

82 83 84 85 86 87
    if (flags & SND_ASYNC && flags & SND_MEMORY) {
        /* Sidestep reference counting headache; unfortunately this also
            prevent SND_LOOP from memory. */
        PyErr_SetString(PyExc_RuntimeError,
                        "Cannot play asynchronously from memory");
        return NULL;
88
    }
89 90 91 92 93 94 95 96 97

    Py_BEGIN_ALLOW_THREADS
    ok = PlaySoundW(sound, NULL, flags);
    Py_END_ALLOW_THREADS
    if (!ok) {
        PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
        return NULL;
    }
    Py_RETURN_NONE;
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112
/*[clinic input]
winsound.Beep

    frequency: int
        Frequency of the sound in hertz.
        Must be in the range 37 through 32,767.
    duration: int
        How long the sound should play, in milliseconds.
    /

A wrapper around the Windows Beep API.
[clinic start generated code]*/

113
static PyObject *
114 115
winsound_Beep_impl(PyObject *module, int frequency, int duration)
/*[clinic end generated code: output=f32382e52ee9b2fb input=628a99d2ddf73798]*/
Guido van Rossum's avatar
Guido van Rossum committed
116
{
117 118
    BOOL ok;

119
    if (frequency < 37 || frequency > 32767) {
120 121 122 123 124 125
        PyErr_SetString(PyExc_ValueError,
                        "frequency must be in 37 thru 32767");
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
126
    ok = Beep(frequency, duration);
127 128 129 130 131 132
    Py_END_ALLOW_THREADS
    if (!ok) {
        PyErr_SetString(PyExc_RuntimeError,"Failed to beep");
        return NULL;
    }

133
    Py_RETURN_NONE;
Guido van Rossum's avatar
Guido van Rossum committed
134 135
}

136 137 138 139 140 141 142 143 144 145 146
/*[clinic input]
winsound.MessageBeep

    x: int(c_default="MB_OK") = MB_OK
    /

Call Windows MessageBeep(x).

x defaults to MB_OK.
[clinic start generated code]*/

147
static PyObject *
148 149
winsound_MessageBeep_impl(PyObject *module, int x)
/*[clinic end generated code: output=1ad89e4d8d30a957 input=a776c8a85c9853f6]*/
150
{
151
    MessageBeep(x);
152
    Py_RETURN_NONE;
153 154
}

155 156
static struct PyMethodDef sound_methods[] =
{
157 158 159
    WINSOUND_PLAYSOUND_METHODDEF
    WINSOUND_BEEP_METHODDEF
    WINSOUND_MESSAGEBEEP_METHODDEF
160 161 162
    {NULL,  NULL}
};

163 164
static void
add_define(PyObject *dict, const char *key, long value)
165
{
166 167 168
    PyObject *k = PyUnicode_FromString(key);
    PyObject *v = PyLong_FromLong(value);
    if (v && k) {
169
        PyDict_SetItem(dict, k, v);
170 171 172 173 174 175 176
    }
    Py_XDECREF(k);
    Py_XDECREF(v);
}

#define ADD_DEFINE(tok) add_define(dict,#tok,tok)

177 178

static struct PyModuleDef winsoundmodule = {
179 180 181 182 183 184 185 186 187
    PyModuleDef_HEAD_INIT,
    "winsound",
    sound_module_doc,
    -1,
    sound_methods,
    NULL,
    NULL,
    NULL,
    NULL
188 189
};

190
PyMODINIT_FUNC
191
PyInit_winsound(void)
192
{
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    PyObject *dict;
    PyObject *module = PyModule_Create(&winsoundmodule);
    if (module == NULL)
        return NULL;
    dict = PyModule_GetDict(module);

    ADD_DEFINE(SND_ASYNC);
    ADD_DEFINE(SND_NODEFAULT);
    ADD_DEFINE(SND_NOSTOP);
    ADD_DEFINE(SND_NOWAIT);
    ADD_DEFINE(SND_ALIAS);
    ADD_DEFINE(SND_FILENAME);
    ADD_DEFINE(SND_MEMORY);
    ADD_DEFINE(SND_PURGE);
    ADD_DEFINE(SND_LOOP);
    ADD_DEFINE(SND_APPLICATION);

    ADD_DEFINE(MB_OK);
    ADD_DEFINE(MB_ICONASTERISK);
    ADD_DEFINE(MB_ICONEXCLAMATION);
    ADD_DEFINE(MB_ICONHAND);
    ADD_DEFINE(MB_ICONQUESTION);
    return module;
216
}