-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathmusic.c
630 lines (526 loc) · 16.4 KB
/
music.c
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
pygame-ce - 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 "doc/music_doc.h"
#include "mixer.h"
static Mix_Music *current_music = NULL;
static Mix_Music *queue_music = NULL;
static int queue_music_loops = 0;
static int endmusic_event = SDL_NOEVENT;
static Uint64 music_pos = 0;
static Uint64 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 = PG_GetTicks();
}
}
static void
endmusic_callback(void)
{
if (endmusic_event && SDL_WasInit(SDL_INIT_VIDEO)) {
pg_post_event(endmusic_event, NULL);
}
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, queue_music_loops);
queue_music_loops = 0;
}
else {
music_pos_time = -1;
Mix_SetPostMix(NULL, NULL);
}
}
/*music module methods*/
static PyObject *
music_play(PyObject *self, PyObject *args, PyObject *keywds)
{
int loops = 0;
float startpos = 0.0;
int val, volume = 0, fade_ms = 0;
static char *kwids[] = {"loops", "start", "fade_ms", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|ifi", kwids, &loops,
&startpos, &fade_ms))
return NULL;
MIXER_INIT_CHECK();
if (!current_music)
return RAISE(pgExc_SDLError, "music not loaded");
Py_BEGIN_ALLOW_THREADS;
Mix_HookMusicFinished(endmusic_callback);
Mix_SetPostMix(mixmusic_callback, NULL);
Mix_QuerySpec(&music_frequency, &music_format, &music_channels);
music_pos = 0;
music_pos_time = PG_GetTicks();
volume = Mix_VolumeMusic(-1);
val = Mix_FadeInMusicPos(current_music, loops, fade_ms, startpos);
Mix_VolumeMusic(volume);
Py_END_ALLOW_THREADS;
if (val == -1)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_NONE;
}
static PyObject *
music_get_busy(PyObject *self, PyObject *_null)
{
int playing;
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
playing = (Mix_PlayingMusic() && !Mix_PausedMusic());
Py_END_ALLOW_THREADS;
return PyBool_FromLong(playing);
}
static PyObject *
music_fadeout(PyObject *self, PyObject *args)
{
int _time;
if (!PyArg_ParseTuple(args, "i", &_time))
return NULL;
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
/* To prevent the queue_music from playing, free it before fading. */
if (queue_music) {
Mix_FreeMusic(queue_music);
queue_music = NULL;
queue_music_loops = 0;
}
Mix_FadeOutMusic(_time);
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
music_stop(PyObject *self, PyObject *_null)
{
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
/* To prevent the queue_music from playing, free it before stopping. */
if (queue_music) {
Mix_FreeMusic(queue_music);
queue_music = NULL;
queue_music_loops = 0;
}
Mix_HaltMusic();
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
music_pause(PyObject *self, PyObject *_null)
{
MIXER_INIT_CHECK();
Mix_PauseMusic();
Py_RETURN_NONE;
}
static PyObject *
music_unpause(PyObject *self, PyObject *_null)
{
MIXER_INIT_CHECK();
Mix_ResumeMusic();
/* need to set pos_time for the adjusted time spent paused*/
music_pos_time = PG_GetTicks();
Py_RETURN_NONE;
}
static PyObject *
music_rewind(PyObject *self, PyObject *_null)
{
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
Mix_RewindMusic();
Py_END_ALLOW_THREADS;
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();
Py_BEGIN_ALLOW_THREADS;
Mix_VolumeMusic((int)(volume * 128));
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
music_get_volume(PyObject *self, PyObject *_null)
{
int volume;
MIXER_INIT_CHECK();
volume = Mix_VolumeMusic(-1);
return PyFloat_FromDouble(volume / 128.0);
}
static PyObject *
music_set_pos(PyObject *self, PyObject *arg)
{
int position_set;
double pos = PyFloat_AsDouble(arg);
if (pos == -1 && PyErr_Occurred()) {
PyErr_Clear();
return RAISE(PyExc_TypeError, "set_pos expects 1 float argument");
}
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
position_set = Mix_SetMusicPosition(pos);
Py_END_ALLOW_THREADS;
if (position_set == -1)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_NONE;
}
static PyObject *
music_get_pos(PyObject *self, PyObject *_null)
{
Uint64 ticks;
MIXER_INIT_CHECK();
Uint16 intermediate_step = (music_format & 0xff) >> 3;
long denominator = music_channels * music_frequency * intermediate_step;
if (music_pos_time < 0 || denominator == 0) {
return PyLong_FromLong(-1);
}
ticks = (long)(1000 * music_pos / denominator);
if (!Mix_PausedMusic())
ticks += PG_GetTicks() - music_pos_time;
return PyLong_FromUnsignedLongLong(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, PyObject *_null)
{
return PyLong_FromLong(endmusic_event);
}
Mix_MusicType
_get_type_from_hint(char *namehint)
{
Mix_MusicType type = MUS_NONE;
char *dot;
// Adjusts namehint into a mere file extension component
if (namehint != NULL) {
dot = strrchr(namehint, '.');
if (dot != NULL) {
namehint = dot + 1;
}
}
else {
return type;
}
/* Copied almost directly from SDL_mixer. Originally meant to check file
* extensions to get a hint of what music type it should be.
* https://github.com/libsdl-org/SDL_mixer/blob/master/src/music.c#L586-L631
*/
if (SDL_strcasecmp(namehint, "WAV") == 0) {
type = MUS_WAV;
}
else if (SDL_strcasecmp(namehint, "MID") == 0 ||
SDL_strcasecmp(namehint, "MIDI") == 0 ||
SDL_strcasecmp(namehint, "KAR") == 0) {
type = MUS_MID;
}
else if (SDL_strcasecmp(namehint, "OGG") == 0) {
type = MUS_OGG;
}
else if (SDL_strcasecmp(namehint, "OPUS") == 0) {
type = MUS_OPUS;
}
else if (SDL_strcasecmp(namehint, "FLAC") == 0) {
type = MUS_FLAC;
}
#if SDL_MIXER_VERSION_ATLEAST(2, 8, 0)
else if (SDL_strcasecmp(namehint, "WV") == 0) {
type = MUS_WAVPACK;
}
#endif
else if (SDL_strcasecmp(namehint, "MPG") == 0 ||
SDL_strcasecmp(namehint, "MPEG") == 0 ||
SDL_strcasecmp(namehint, "MP3") == 0 ||
SDL_strcasecmp(namehint, "MAD") == 0) {
type = MUS_MP3;
}
else if (SDL_strcasecmp(namehint, "669") == 0 ||
SDL_strcasecmp(namehint, "AMF") == 0 ||
SDL_strcasecmp(namehint, "AMS") == 0 ||
SDL_strcasecmp(namehint, "DBM") == 0 ||
SDL_strcasecmp(namehint, "DSM") == 0 ||
SDL_strcasecmp(namehint, "FAR") == 0 ||
SDL_strcasecmp(namehint, "GDM") == 0 ||
SDL_strcasecmp(namehint, "IT") == 0 ||
SDL_strcasecmp(namehint, "MED") == 0 ||
SDL_strcasecmp(namehint, "MDL") == 0 ||
SDL_strcasecmp(namehint, "MOD") == 0 ||
SDL_strcasecmp(namehint, "MOL") == 0 ||
SDL_strcasecmp(namehint, "MTM") == 0 ||
SDL_strcasecmp(namehint, "NST") == 0 ||
SDL_strcasecmp(namehint, "OKT") == 0 ||
SDL_strcasecmp(namehint, "PTM") == 0 ||
SDL_strcasecmp(namehint, "S3M") == 0 ||
SDL_strcasecmp(namehint, "STM") == 0 ||
SDL_strcasecmp(namehint, "ULT") == 0 ||
SDL_strcasecmp(namehint, "UMX") == 0 ||
SDL_strcasecmp(namehint, "WOW") == 0 ||
SDL_strcasecmp(namehint, "XM") == 0) {
type = MUS_MOD;
}
#if SDL_MIXER_VERSION_ATLEAST(2, 8, 0)
else if (SDL_strcasecmp(namehint, "GBS") == 0 ||
SDL_strcasecmp(namehint, "M3U") == 0 ||
SDL_strcasecmp(namehint, "NSF") == 0 ||
SDL_strcasecmp(namehint, "SPC") == 0 ||
SDL_strcasecmp(namehint, "VGM") == 0) {
type = MUS_GME;
}
#endif
return type;
}
Mix_Music *
_load_music(PyObject *obj, char *namehint)
{
Mix_Music *new_music = NULL;
char *ext = NULL, *type = NULL;
SDL_RWops *rw = NULL;
MIXER_INIT_CHECK();
rw = pgRWops_FromObject(obj, &ext);
if (rw == NULL) {
return NULL;
}
if (namehint) {
type = namehint;
}
else {
type = ext;
}
Py_BEGIN_ALLOW_THREADS;
#if SDL_VERSION_ATLEAST(3, 0, 0)
new_music = Mix_LoadMUSType_IO(rw, _get_type_from_hint(type), SDL_TRUE);
#else
new_music = Mix_LoadMUSType_RW(rw, _get_type_from_hint(type), SDL_TRUE);
#endif
Py_END_ALLOW_THREADS;
if (ext) {
free(ext);
}
if (!new_music) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
return new_music;
}
static PyObject *
music_load(PyObject *self, PyObject *args, PyObject *keywds)
{
Mix_Music *new_music = NULL;
PyObject *obj;
char *namehint = NULL;
static char *kwids[] = {"filename", "namehint", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|s", kwids, &obj,
&namehint))
return NULL;
MIXER_INIT_CHECK();
new_music = _load_music(obj, namehint);
if (new_music == NULL) // meaning it has an error to return
return NULL;
Py_BEGIN_ALLOW_THREADS;
if (current_music != NULL) {
Mix_FreeMusic(current_music);
current_music = NULL;
}
if (queue_music != NULL) {
Mix_FreeMusic(queue_music);
queue_music = NULL;
queue_music_loops = 0;
}
Py_END_ALLOW_THREADS;
current_music = new_music;
Py_RETURN_NONE;
}
static PyObject *
music_unload(PyObject *self, PyObject *_null)
{
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
if (current_music) {
Mix_FreeMusic(current_music);
current_music = NULL;
}
if (queue_music) {
Mix_FreeMusic(queue_music);
queue_music = NULL;
queue_music_loops = 0;
}
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
music_queue(PyObject *self, PyObject *args, PyObject *keywds)
{
Mix_Music *local_queue_music = NULL;
PyObject *obj;
int loops = 0;
char *namehint = NULL;
static char *kwids[] = {"filename", "namehint", "loops", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|si", kwids, &obj,
&namehint, &loops))
return NULL;
MIXER_INIT_CHECK();
queue_music_loops = loops;
local_queue_music = _load_music(obj, namehint);
if (local_queue_music == NULL) // meaning it has an error to return
return NULL;
Py_BEGIN_ALLOW_THREADS;
/* Free any existing queued music. */
if (queue_music != NULL) {
Mix_FreeMusic(queue_music);
}
Py_END_ALLOW_THREADS;
queue_music = local_queue_music;
Py_RETURN_NONE;
}
static PyObject *
music_get_metadata(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *meta_dict;
Mix_Music *music = current_music;
PyObject *obj = NULL;
char *namehint = NULL;
static char *kwids[] = {"filename", "namehint", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|Os", kwids, &obj,
&namehint))
return NULL;
MIXER_INIT_CHECK();
if (obj) {
music = _load_music(obj, namehint);
if (!music) {
return NULL;
}
}
else if (namehint) {
return RAISE(
pgExc_SDLError,
"'namehint' specified without specifying 'filename' or 'fileobj'");
}
const char *title = "";
const char *album = "";
const char *artist = "";
const char *copyright = "";
#if SDL_MIXER_VERSION_ATLEAST(2, 6, 0)
title = Mix_GetMusicTitleTag(music);
album = Mix_GetMusicAlbumTag(music);
artist = Mix_GetMusicArtistTag(music);
copyright = Mix_GetMusicCopyrightTag(music);
#endif
if (!music) {
return RAISE(pgExc_SDLError, "music not loaded");
}
meta_dict = Py_BuildValue("{ss ss ss ss}", "title", title, "album", album,
"artist", artist, "copyright", copyright);
if (obj) {
Mix_FreeMusic(music);
}
return meta_dict;
}
static PyMethodDef _music_methods[] = {
{"set_endevent", music_set_endevent, METH_VARARGS,
DOC_MIXER_MUSIC_SETENDEVENT},
{"get_endevent", music_get_endevent, METH_NOARGS,
DOC_MIXER_MUSIC_GETENDEVENT},
{"play", (PyCFunction)music_play, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_MUSIC_PLAY},
{"get_busy", music_get_busy, METH_NOARGS, DOC_MIXER_MUSIC_GETBUSY},
{"fadeout", music_fadeout, METH_VARARGS, DOC_MIXER_MUSIC_FADEOUT},
{"stop", music_stop, METH_NOARGS, DOC_MIXER_MUSIC_STOP},
{"pause", music_pause, METH_NOARGS, DOC_MIXER_MUSIC_PAUSE},
{"unpause", music_unpause, METH_NOARGS, DOC_MIXER_MUSIC_UNPAUSE},
{"rewind", music_rewind, METH_NOARGS, DOC_MIXER_MUSIC_REWIND},
{"set_volume", music_set_volume, METH_VARARGS, DOC_MIXER_MUSIC_SETVOLUME},
{"get_volume", music_get_volume, METH_NOARGS, DOC_MIXER_MUSIC_GETVOLUME},
{"set_pos", music_set_pos, METH_O, DOC_MIXER_MUSIC_SETPOS},
{"get_pos", music_get_pos, METH_NOARGS, DOC_MIXER_MUSIC_GETPOS},
{"get_metadata", (PyCFunction)music_get_metadata,
METH_VARARGS | METH_KEYWORDS, DOC_MIXER_MUSIC_GETMETADATA},
{"load", (PyCFunction)music_load, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_MUSIC_LOAD},
{"unload", music_unload, METH_NOARGS, DOC_MIXER_MUSIC_UNLOAD},
{"queue", (PyCFunction)music_queue, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_MUSIC_QUEUE},
{NULL, NULL, 0, NULL}};
MODINIT_DEFINE(mixer_music)
{
PyObject *module;
PyObject *cobj;
static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
"mixer_music",
DOC_MIXER_MUSIC,
-1,
_music_methods,
NULL,
NULL,
NULL,
NULL};
/* imported needed apis; Do this first so if there is an error
the module is not loaded.
*/
import_pygame_base();
if (PyErr_Occurred()) {
return NULL;
}
import_pygame_rwobject();
if (PyErr_Occurred()) {
return NULL;
}
import_pygame_event();
if (PyErr_Occurred()) {
return NULL;
}
/* create the module */
module = PyModule_Create(&_module);
if (module == NULL) {
return NULL;
}
cobj = PyCapsule_New(¤t_music, "pygame.music_mixer._MUSIC_POINTER",
NULL);
if (PyModule_AddObject(module, "_MUSIC_POINTER", cobj)) {
Py_XDECREF(cobj);
Py_DECREF(module);
return NULL;
}
cobj =
PyCapsule_New(&queue_music, "pygame.music_mixer._QUEUE_POINTER", NULL);
if (PyModule_AddObject(module, "_QUEUE_POINTER", cobj)) {
Py_XDECREF(cobj);
Py_DECREF(module);
return NULL;
}
return module;
}