Source

pygame / src / music.c

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 


pygame ae12316 
ahvezda 99d32cc 


gamedude 406c144 
pygame 18b5fa9 
ahvezda 99d32cc 


pygame 463003f 
pygame 7250e77 
pygame 86649d3 

pygame d83539e 


ahvezda 99d32cc 
illume 8749d7c 

pygame 463003f 
illume 8749d7c 




pygame 463003f 

illume 8749d7c 

pygame 7250e77 
illume 8749d7c 





















pygame 7250e77 

illume 8749d7c 






ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 


ahvezda 99d32cc 
illume 8749d7c 




pygame 8808eb0 
pygame 3ab3543 
illume 612c224 
illume 8749d7c 


illume 612c224 
pygame b1fa454 
illume 8749d7c 


illume 612c224 
illume 8749d7c 
illume 612c224 
pygame b1fa454 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 





ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 
illume 8749d7c 






ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 






ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 




ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 

illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 
pygame d83539e 
illume 8749d7c 

pygame d83539e 
illume 8749d7c 




pygame d83539e 
illume 8749d7c 
pygame d83539e 

illume 8749d7c 



pygame d83539e 
illume 8749d7c 



pygame 7250e77 

illume 8749d7c 

pygame 7250e77 
illume 8749d7c 
pygame 7250e77 

illume 8749d7c 


illume 612c224 




illume 8749d7c 
pygame 7250e77 
illume 8749d7c 
pygame 455843c 
illume ea7a6d4 
gamedude 406c144 
illume 612c224 










gamedude 406c144 











illume 612c224 

gamedude 406c144 
illume 612c224 







illume ea7a6d4 
illume 8749d7c 









illume ea7a6d4 

illume 612c224 
illume 8749d7c 
ahvezda 99d32cc 

illume 8749d7c 


illume 612c224 

illume 8749d7c 
illume 612c224 

illume 8749d7c 
ahvezda 99d32cc 
illume 8749d7c 
pygame 463003f 
illume ea7a6d4 
gamedude 406c144 
illume 612c224 










gamedude 406c144 











illume 612c224 

gamedude 406c144 
illume 612c224 



pygame 463003f 
illume 8749d7c 

pygame c2da2d7 
illume ea7a6d4 
illume 8749d7c 




illume ea7a6d4 

illume 8749d7c 

pygame 463003f 

gamedude 406c144 
ahvezda 99d32cc 
illume 8749d7c 



ahvezda 99d32cc 
illume 8749d7c 
















ahvezda 99d32cc 
illume 8749d7c 

ahvezda 99d32cc 
illume 8749d7c 
ahvezda 99d32cc 

gamedude 406c144 
illume 8749d7c 

gamedude 406c144 











ahvezda 99d32cc 
illume 8749d7c 
gamedude 29a7d65 





gamedude 406c144 
gamedude 29a7d65 


gamedude 406c144 
gamedude 29a7d65 

illume 8749d7c 
gamedude 406c144 



illume 8749d7c 
gamedude 406c144 


ahvezda 99d32cc 
gamedude 406c144 




















  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
  pygame - Python Game Library
  Copyright (C) 2000-2001  Pete Shinners

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Pete Shinners
  pete@shinners.org
*/

/*
 *  music module for pygame
 */
#define PYGAMEAPI_MUSIC_INTERNAL
#include "pygame.h"
#include "pgcompat.h"
#include "pygamedocs.h"
#include "mixer.h"

static Mix_Music* current_music = NULL;
static Mix_Music* queue_music = NULL;
static int endmusic_event = SDL_NOEVENT;
static Uint64 music_pos = 0;
static long music_pos_time = -1;
static int music_frequency = 0;
static Uint16 music_format = 0;
static int music_channels = 0;

static void
mixmusic_callback (void *udata, Uint8 *stream, int len)
{
    if (!Mix_PausedMusic ())
    {
        music_pos += len;
        music_pos_time = SDL_GetTicks ();
    }
}

static void
endmusic_callback (void)
{
    if (endmusic_event && SDL_WasInit (SDL_INIT_VIDEO))
    {
        SDL_Event e;
        memset (&e, 0, sizeof (e));
        e.type = endmusic_event;
        SDL_PushEvent (&e);
    }
    if (queue_music)
    {
        if (current_music)
            Mix_FreeMusic (current_music);
        current_music = queue_music;
        queue_music = NULL;
        Mix_HookMusicFinished (endmusic_callback);
        music_pos = 0;
        Mix_PlayMusic (current_music, 0);
    }
    else
    {
        music_pos_time = -1;
        Mix_SetPostMix (NULL, NULL);
    }
}

/*music module methods*/
static PyObject*
music_play (PyObject* self, PyObject* args)
{
    int loops = 0;
    float startpos = 0.0;
    int val, volume = 0;

    if (!PyArg_ParseTuple (args, "|if", &loops, &startpos))
        return NULL;

    MIXER_INIT_CHECK ();
    if (!current_music)
        return RAISE (PyExc_SDLError, "music not loaded");

    Mix_HookMusicFinished (endmusic_callback);
    Mix_SetPostMix (mixmusic_callback, NULL);
    Mix_QuerySpec (&music_frequency, &music_format, &music_channels);
    music_pos = 0;
    music_pos_time = SDL_GetTicks ();

#if MIX_MAJOR_VERSION>=1 && MIX_MINOR_VERSION>=2 && MIX_PATCHLEVEL>=3
    Py_BEGIN_ALLOW_THREADS
    volume = Mix_VolumeMusic (-1);
    val = Mix_FadeInMusicPos (current_music, loops, 0, startpos);
    Mix_VolumeMusic (volume);
    Py_END_ALLOW_THREADS
#else
    if (startpos)
        return RAISE (PyExc_NotImplementedError,
                      "music start position requires SDL_mixer-1.2.4");
    Py_BEGIN_ALLOW_THREADS
    val = Mix_PlayMusic (current_music, loops);
    Py_END_ALLOW_THREADS
#endif
    if (val == -1)
        return RAISE (PyExc_SDLError, SDL_GetError ());

    Py_RETURN_NONE;
}

static PyObject*
music_get_busy (PyObject* self)
{
    MIXER_INIT_CHECK ();
    return PyInt_FromLong (Mix_PlayingMusic ());
}

static PyObject*
music_fadeout (PyObject* self, PyObject* args)
{
    int _time;
    if (!PyArg_ParseTuple (args, "i", &_time))
        return NULL;

    MIXER_INIT_CHECK ();

    Mix_FadeOutMusic (_time);
    if (queue_music)
    {
        Mix_FreeMusic (queue_music);
        queue_music = NULL;
    }
    Py_RETURN_NONE;
}

static PyObject*
music_stop (PyObject* self)
{
    MIXER_INIT_CHECK ();

    Mix_HaltMusic ();
    if (queue_music)
    {
        Mix_FreeMusic (queue_music);
        queue_music = NULL;
    }
    Py_RETURN_NONE;
}

static PyObject*
music_pause (PyObject* self)
{
    MIXER_INIT_CHECK ();

    Mix_PauseMusic ();
    Py_RETURN_NONE;
}

static PyObject*
music_unpause (PyObject* self)
{
    MIXER_INIT_CHECK ();

    Mix_ResumeMusic ();
    Py_RETURN_NONE;
}

static PyObject*
music_rewind (PyObject* self)
{
    MIXER_INIT_CHECK ();

    Mix_RewindMusic ();
    Py_RETURN_NONE;
}

static PyObject*
music_set_volume (PyObject* self, PyObject* args)
{
    float volume;

    if (!PyArg_ParseTuple (args, "f", &volume))
        return NULL;

    MIXER_INIT_CHECK ();

    Mix_VolumeMusic ((int)(volume * 128));
    Py_RETURN_NONE;
}

static PyObject*
music_get_volume (PyObject* self)
{
    int volume;
    MIXER_INIT_CHECK ();

    volume = Mix_VolumeMusic (-1);
    return PyFloat_FromDouble (volume / 128.0);
}

static PyObject*
music_get_pos (PyObject* self)
{
    long ticks;

    MIXER_INIT_CHECK ();

    if (music_pos_time < 0)
        return PyLong_FromLong (-1);

    ticks = (long) (1000 * music_pos /
                    (music_channels * music_frequency *
                     ((music_format & 0xff) >> 3)));
    if (!Mix_PausedMusic ())
        ticks += SDL_GetTicks () - music_pos_time;

    return PyInt_FromLong ((long)ticks);
}

static PyObject*
music_set_endevent (PyObject* self, PyObject* args)
{
    int eventid = SDL_NOEVENT;

    if (!PyArg_ParseTuple (args, "|i", &eventid))
        return NULL;
    endmusic_event = eventid;
    Py_RETURN_NONE;
}

static PyObject*
music_get_endevent (PyObject* self)
{
    return PyInt_FromLong (endmusic_event);
}

static PyObject*
music_load (PyObject* self, PyObject* args)
{
    char* name = NULL;
    PyObject* file;
    Mix_Music* new_music;
    SDL_RWops *rw;
    if(!PyArg_ParseTuple(args, "O", &file))
        return NULL;

    MIXER_INIT_CHECK ();

    #if (MIX_MAJOR_VERSION*100*100 + MIX_MINOR_VERSION*100 + MIX_PATCHLEVEL) >= 10208
    if(!Bytes_Check(file) && !PyUnicode_Check(file))
    {
        rw = RWopsFromPythonThreaded(file);
        if(!rw)
            return NULL;
        Py_BEGIN_ALLOW_THREADS
        new_music = Mix_LoadMUS_RW(rw);
        Py_END_ALLOW_THREADS
    }
    else
    #endif
    {
#if PY3
        if (PyUnicode_Check(file)) {
            if (!PyArg_ParseTuple(args, "s", &name)) {
                return NULL;
            }
        }
        else {
            if (!PyArg_ParseTuple(args, "y", &name)) {
                return NULL;
            }
        }
#else
        if(!PyArg_ParseTuple(args, "s", &name))
            return NULL;
#endif
        Py_BEGIN_ALLOW_THREADS
        new_music = Mix_LoadMUS(name);
        Py_END_ALLOW_THREADS
    }

    if (!new_music)
        return RAISE (PyExc_SDLError, SDL_GetError ());

    Py_BEGIN_ALLOW_THREADS
    if (current_music)
    {
        Mix_FreeMusic (current_music);
        current_music = NULL;
    }
    if (queue_music)
    {
        Mix_FreeMusic (queue_music);
        queue_music = NULL;
    }
    Py_END_ALLOW_THREADS

    current_music = new_music;
    Py_RETURN_NONE;
}

static PyObject*
music_queue (PyObject* self, PyObject* args)
{
    char* name = NULL;
    PyObject* file;
    Mix_Music* new_music;
    SDL_RWops *rw;
    if (!PyArg_ParseTuple (args, "O", &file))
        return NULL;

    MIXER_INIT_CHECK ();

    #if MIX_MAJOR_VERSION*100*100 + MIX_MINOR_VERSION*100 + MIX_PATCHLEVEL >= 10208
    if(!Bytes_Check(file) && !PyUnicode_Check(file))
    {
        rw = RWopsFromPythonThreaded(file);
        if(!rw)
            return NULL;
        Py_BEGIN_ALLOW_THREADS
        new_music = Mix_LoadMUS_RW(rw);
        Py_END_ALLOW_THREADS
    }
    else
    #endif
    {
#if PY3
        if (PyUnicode_Check(file)) {
            if (!PyArg_ParseTuple(args, "s", &name)) {
                return NULL;
            }
        }
        else {
            if (!PyArg_ParseTuple(args, "y", &name)) {
                return NULL;
            }
        }
#else
        if(!PyArg_ParseTuple(args, "s", &name))
            return NULL;
#endif
        Py_BEGIN_ALLOW_THREADS
        new_music = Mix_LoadMUS(name);
        Py_END_ALLOW_THREADS
    }

    if (!new_music)
        return RAISE (PyExc_SDLError, SDL_GetError ());

    Py_BEGIN_ALLOW_THREADS
    if (queue_music)
    {
        Mix_FreeMusic (queue_music);
        queue_music = NULL;
    }
    Py_END_ALLOW_THREADS

    queue_music = new_music;
    Py_RETURN_NONE;
}

static PyMethodDef _music_methods[] =
{
    { "set_endevent", music_set_endevent, METH_VARARGS,
      DOC_PYGAMEMIXERMUSICSETENDEVENT },
    { "get_endevent", (PyCFunction) music_get_endevent, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICGETENDEVENT },

    { "play", music_play, METH_VARARGS, DOC_PYGAMEMIXERMUSICPLAY },
    { "get_busy", (PyCFunction) music_get_busy, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICGETBUSY },
    { "fadeout", music_fadeout, METH_VARARGS, DOC_PYGAMEMIXERMUSICFADEOUT },
    { "stop", (PyCFunction) music_stop, METH_NOARGS, DOC_PYGAMEMIXERMUSICSTOP },
    { "pause", (PyCFunction) music_pause, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICPAUSE },
    { "unpause", (PyCFunction) music_unpause, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICUNPAUSE },
    { "rewind", (PyCFunction) music_rewind, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICREWIND },
    { "set_volume", music_set_volume, METH_VARARGS,
      DOC_PYGAMEMIXERMUSICSETVOLUME },
    { "get_volume", (PyCFunction) music_get_volume, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICGETVOLUME },
    { "get_pos", (PyCFunction) music_get_pos, METH_NOARGS,
      DOC_PYGAMEMIXERMUSICGETPOS },

    { "load", music_load, METH_VARARGS, DOC_PYGAMEMIXERMUSICLOAD },
    { "queue", music_queue, METH_VARARGS, DOC_PYGAMEMIXERMUSICQUEUE },

    { NULL, NULL, 0, NULL }
};

MODINIT_DEFINE (mixer_music)
{
    PyObject *module;
    PyObject *cobj;

#if PY3
    static struct PyModuleDef _module = {
        PyModuleDef_HEAD_INIT,
        "mixer_music",
        DOC_PYGAMEMIXERMUSIC,
        -1,
        _music_methods,
        NULL, NULL, NULL, NULL
    };
#endif

    PyMIXER_C_API[0] = PyMIXER_C_API[0]; /*clean an unused warning*/

    /* imported needed apis; Do this first so if there is an error
       the module is not loaded.
    */
    import_pygame_base ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }
    import_pygame_rwobject ();
    if (PyErr_Occurred ()) {
	MODINIT_ERROR;
    }

    /* create the module */
#if PY3
    module = PyModule_Create (&_module);
#else
    module = Py_InitModule3 (MODPREFIX "mixer_music", _music_methods,
                             DOC_PYGAMEMIXERMUSIC);
#endif
    if (module == NULL) {
        MODINIT_ERROR;
}
    cobj = PyCObject_FromVoidPtr (&current_music, NULL);
    if (cobj == NULL) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    if (PyModule_AddObject(module, "_MUSIC_POINTER", cobj) < 0) {
        Py_DECREF (cobj);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    cobj = PyCObject_FromVoidPtr (&queue_music, NULL);
    if (cobj == NULL) {
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    if (PyModule_AddObject(module, "_QUEUE_POINTER", cobj) < 0) {
        Py_DECREF (cobj);
        DECREF_MOD (module);
        MODINIT_ERROR;
    }
    MODINIT_RETURN (module);
}