forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer.c
2127 lines (1843 loc) · 58 KB
/
mixer.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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
*/
/*
* mixer module for pygame
*/
#define PYGAMEAPI_MIXER_INTERNAL
#include "pygame.h"
#include "pgcompat.h"
#include "doc/mixer_doc.h"
#include "mixer.h"
#define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F))
#define CHECK_CHUNK_VALID(CHUNK, RET) \
if ((CHUNK) == NULL) { \
PyErr_SetString(PyExc_RuntimeError, \
"__init__() was not called on Sound object so it " \
"failed to setup correctly."); \
return (RET); \
}
/* The SDL audio format constants are not defined for anything larger
than 2 byte samples. Define our own. Low two bytes gives sample
size in bytes. Higher bytes are flags.
*/
typedef Uint32 PG_sample_format_t;
const PG_sample_format_t PG_SAMPLE_SIGNED = 0x10000u;
const PG_sample_format_t PG_SAMPLE_NATIVE_ENDIAN = 0x20000u;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
const PG_sample_format_t PG_SAMPLE_LITTLE_ENDIAN = 0x20000u;
const PG_sample_format_t PG_SAMPLE_BIG_ENDIAN = 0;
#else
const PG_sample_format_t PG_SAMPLE_LITTLE_ENDIAN = 0;
const PG_sample_format_t PG_SAMPLE_BIG_ENDIAN = 0x20000u;
#endif
const PG_sample_format_t PG_SAMPLE_CHAR_SIGN = (char)0xff > 0 ? 0 : 0x10000u;
#define PG_SAMPLE_SIZE(sf) ((sf) & 0x0ffffu)
#define PG_IS_SAMPLE_SIGNED(sf) ((sf) & PG_SAMPLE_SIGNED != 0)
#define PG_IS_SAMPLE_NATIVE_ENDIAN(sf) ((sf) & PG_SAMPLE_NATIVE_ENDIAN != 0)
#define PG_IS_SAMPLE_LITTLE_ENDIAN(sf) \
((sf) & PG_SAMPLE_LITTLE_ENDIAN == PG_SAMPLE_LITTLE_ENDIAN)
#define PG_IS_SAMPLE_BIG_ENDIAN(sf) \
((sf) & PG_SAMPLE_BIG_ENDIAN == PG_SAMPLE_BIG_ENDIAN)
/* Since they are documented, the default init values are defined here
rather than taken from SDL_mixer. It also means that the default
size is defined in Pygame, rather than SDL AUDIO_xxx, terms.
*/
#define PYGAME_MIXER_DEFAULT_FREQUENCY 44100
#define PYGAME_MIXER_DEFAULT_SIZE -16
#define PYGAME_MIXER_DEFAULT_CHANNELS 2
#define PYGAME_MIXER_DEFAULT_CHUNKSIZE 512
#define PYGAME_MIXER_DEFAULT_ALLOWEDCHANGES \
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE
static int
sound_init(PyObject *, PyObject *, PyObject *);
static PyTypeObject pgSound_Type;
static PyTypeObject pgChannel_Type;
static PyObject *
pgSound_New(Mix_Chunk *);
static PyObject *
pgChannel_New(int);
#define pgSound_Check(x) (PyObject_IsInstance(x, (PyObject *)&pgSound_Type))
#define pgChannel_Check(x) \
(PyObject_IsInstance(x, (PyObject *)&pgChannel_Type))
static int
snd_getbuffer(PyObject *, Py_buffer *, int);
static void
snd_releasebuffer(PyObject *, Py_buffer *);
static int request_frequency = PYGAME_MIXER_DEFAULT_FREQUENCY;
static int request_size = PYGAME_MIXER_DEFAULT_SIZE;
static int request_channels = PYGAME_MIXER_DEFAULT_CHANNELS;
static int request_chunksize = PYGAME_MIXER_DEFAULT_CHUNKSIZE;
static int request_allowedchanges = PYGAME_MIXER_DEFAULT_ALLOWEDCHANGES;
static char *request_devicename = NULL;
struct ChannelData {
PyObject *sound;
PyObject *queue;
int endevent;
};
static struct ChannelData *channeldata = NULL;
static int numchanneldata = 0;
Mix_Music **mx_current_music;
Mix_Music **mx_queue_music;
static int
_format_itemsize(Uint16 format)
{
int size = -1;
switch (format) {
case AUDIO_U8:
case AUDIO_S8:
size = 1;
break;
case AUDIO_U16LSB:
case AUDIO_U16MSB:
case AUDIO_S16LSB:
case AUDIO_S16MSB:
size = 2;
break;
case AUDIO_S32LSB:
case AUDIO_S32MSB:
case AUDIO_F32LSB:
case AUDIO_F32MSB:
size = 4;
break;
default:
PyErr_Format(PyExc_SystemError,
"Pygame bug (mixer.Sound): unknown mixer format %d",
(int)format);
}
return size;
}
static PG_sample_format_t
_format_view_to_audio(Py_buffer *view)
{
size_t fstr_len;
int native_size = 0;
int index = 0;
PG_sample_format_t format = 0;
if (!view->format) {
/* Assume unsigned byte */
return (PG_sample_format_t)sizeof(unsigned char);
}
fstr_len = strlen(view->format);
if (fstr_len < 1 || fstr_len > 2) {
PyErr_SetString(PyExc_ValueError, "Array has unsupported item format");
return 0;
}
if (fstr_len == 1) {
format |= PG_SAMPLE_NATIVE_ENDIAN;
native_size = 1;
}
else {
switch (view->format[index]) {
case '@':
native_size = 1;
format |= PG_SAMPLE_NATIVE_ENDIAN;
break;
case '=':
format |= PG_SAMPLE_NATIVE_ENDIAN;
break;
case '<':
format |= PG_SAMPLE_LITTLE_ENDIAN;
break;
case '>':
case '!':
format |= PG_SAMPLE_BIG_ENDIAN;
break;
default:
PyErr_SetString(PyExc_ValueError,
"Array has unsupported item format");
return 0;
}
++index;
}
switch (view->format[index]) {
case 'c':
format |= PG_SAMPLE_CHAR_SIGN;
format += native_size ? sizeof(char) : 1;
break;
case 'b':
format |= PG_SAMPLE_SIGNED;
format += native_size ? sizeof(signed char) : 1;
break;
case 'B':
format += native_size ? sizeof(unsigned char) : 1;
break;
case 'h':
format |= PG_SAMPLE_SIGNED;
format += native_size ? sizeof(short int) : 2;
break;
case 'H':
format += native_size ? sizeof(unsigned short int) : 2;
break;
case 'i':
format |= PG_SAMPLE_SIGNED;
format += native_size ? sizeof(int) : 4;
break;
case 'I':
format += native_size ? sizeof(unsigned int) : 4;
break;
case 'l':
format |= PG_SAMPLE_SIGNED;
format += native_size ? sizeof(long int) : 4;
break;
case 'L':
format += native_size ? sizeof(unsigned long int) : 4;
break;
case 'f':
format += native_size ? sizeof(float) : 4;
break;
case 'd':
format += native_size ? sizeof(double) : 8;
break;
case 'q':
format |= PG_SAMPLE_SIGNED;
format += native_size ? sizeof(long long int) : 8;
break;
case 'Q':
format += native_size ? sizeof(unsigned long long int) : 8;
break;
default:
PyErr_Format(PyExc_ValueError,
"Array has unsupported item format '%s'",
view->format);
return 0;
}
if (view->itemsize && PG_SAMPLE_SIZE(format) != view->itemsize) {
PyErr_Format(PyExc_ValueError,
"Array item size %d does not match format '%s'",
(int)view->itemsize, view->format);
return 0;
}
return format;
}
static void
_pg_push_mixer_event(int type, int code)
{
PyObject *dict, *dictcode;
PyGILState_STATE gstate = PyGILState_Ensure();
dict = PyDict_New();
if (dict) {
if (type >= PGE_USEREVENT && type < PG_NUMEVENTS) {
dictcode = PyLong_FromLong(code);
if (dictcode) {
PyDict_SetItemString(dict, "code", dictcode);
Py_DECREF(dictcode);
}
}
pg_post_event(type, dict);
Py_DECREF(dict);
}
PyGILState_Release(gstate);
}
static void
endsound_callback(int channel)
{
if (channeldata) {
if (channeldata[channel].endevent && SDL_WasInit(SDL_INIT_VIDEO))
_pg_push_mixer_event(channeldata[channel].endevent, channel);
if (channeldata[channel].queue) {
PyGILState_STATE gstate = PyGILState_Ensure();
int channelnum;
Mix_Chunk *sound = pgSound_AsChunk(channeldata[channel].queue);
Py_XDECREF(channeldata[channel].sound);
channeldata[channel].sound = channeldata[channel].queue;
channeldata[channel].queue = NULL;
PyGILState_Release(gstate);
channelnum = Mix_PlayChannelTimed(channel, sound, 0, -1);
if (channelnum != -1)
Mix_GroupChannel(channelnum, (int)(intptr_t)sound);
}
else {
PyGILState_STATE gstate = PyGILState_Ensure();
Py_XDECREF(channeldata[channel].sound);
channeldata[channel].sound = NULL;
PyGILState_Release(gstate);
Mix_GroupChannel(channel, -1);
}
}
}
static PyObject *
import_music(void)
{
PyObject *music = PyImport_ImportModule(IMPPREFIX "mixer_music");
if (music == NULL) {
PyErr_Clear();
music = PyImport_ImportModule(RELATIVE_MODULE("mixer_music"));
}
return music;
}
static PyObject *
_init(int freq, int size, int channels, int chunk, char *devicename,
int allowedchanges)
{
Uint16 fmt = 0;
int i;
PyObject *music;
char *drivername;
if (!freq) {
freq = request_frequency;
}
if (!size) {
size = request_size;
}
if (allowedchanges == -1) {
allowedchanges = request_allowedchanges;
}
if (!channels) {
channels = request_channels;
}
if (allowedchanges & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
if (channels <= 1)
channels = 1;
else if (channels <= 3)
channels = 2;
else if (channels <= 5)
channels = 4;
else
channels = 6;
}
else {
switch (channels) {
case 1:
case 2:
case 4:
case 6:
break;
default:
return RAISE(PyExc_ValueError,
"'channels' must be 1, 2, 4, or 6");
}
}
if (!chunk) {
chunk = request_chunksize;
}
if (!devicename) {
devicename = request_devicename;
}
/* printf("size:%d:\n", size); */
switch (size) {
case 8:
fmt = AUDIO_U8;
break;
case -8:
fmt = AUDIO_S8;
break;
case 16:
fmt = AUDIO_U16SYS;
break;
case -16:
fmt = AUDIO_S16SYS;
break;
case 32:
fmt = AUDIO_F32SYS;
break;
default:
PyErr_Format(PyExc_ValueError, "unsupported size %i", size);
return NULL;
}
/* printf("size:%d:\n", size); */
/*make chunk a power of 2*/
for (i = 0; 1 << i < chunk; ++i)
; /*yes, semicolon on for loop*/
chunk = MAX(1 << i, 256); /*do this after for loop exits*/
if (!SDL_WasInit(SDL_INIT_AUDIO)) {
if (!channeldata) { /*should always be null*/
channeldata = (struct ChannelData *)malloc(
sizeof(struct ChannelData) * MIX_CHANNELS);
if (!channeldata) {
return PyErr_NoMemory();
}
numchanneldata = MIX_CHANNELS;
for (i = 0; i < numchanneldata; ++i) {
channeldata[i].sound = NULL;
channeldata[i].queue = NULL;
channeldata[i].endevent = 0;
}
}
/* Compatibility:
pulse and dsound audio drivers were renamed in SDL2,
and we don't want it to fail.
*/
drivername = SDL_getenv("SDL_AUDIODRIVER");
if (drivername && SDL_strncasecmp("pulse", drivername,
SDL_strlen(drivername)) == 0) {
SDL_setenv("SDL_AUDIODRIVER", "pulseaudio", 1);
}
else if (drivername && SDL_strncasecmp("dsound", drivername,
SDL_strlen(drivername)) == 0) {
SDL_setenv("SDL_AUDIODRIVER", "directsound", 1);
}
if (SDL_InitSubSystem(SDL_INIT_AUDIO))
return RAISE(pgExc_SDLError, SDL_GetError());
if (Mix_OpenAudioDevice(freq, fmt, channels, chunk, devicename,
allowedchanges) == -1) {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return RAISE(pgExc_SDLError, SDL_GetError());
}
Mix_ChannelFinished(endsound_callback);
Mix_VolumeMusic(127);
}
mx_current_music = NULL;
mx_queue_music = NULL;
music = import_music();
if (music) {
PyObject *ptr;
ptr = PyObject_GetAttrString(music, "_MUSIC_POINTER");
if (ptr) {
mx_current_music =
(Mix_Music **)PyCapsule_GetPointer(ptr,
"pygame.music_mixer."
"_MUSIC_POINTER");
if (!mx_current_music) {
PyErr_Clear();
}
}
else {
PyErr_Clear();
}
ptr = PyObject_GetAttrString(music, "_QUEUE_POINTER");
if (ptr) {
mx_queue_music =
(Mix_Music **)PyCapsule_GetPointer(ptr,
"pygame.music_mixer."
"_QUEUE_POINTER");
if (!mx_queue_music) {
PyErr_Clear();
}
}
else {
PyErr_Clear();
}
Py_DECREF(music);
}
else {
PyErr_Clear();
}
Py_RETURN_NONE;
}
static PyObject *
pgMixer_AutoInit(PyObject *self, PyObject *_null)
{
/* Return init with defaults */
return _init(0, 0, 0, 0, NULL, -1);
}
static PyObject *
mixer_quit(PyObject *self, PyObject *_null)
{
int i;
if (SDL_WasInit(SDL_INIT_AUDIO)) {
Py_BEGIN_ALLOW_THREADS;
Mix_HaltMusic();
Py_END_ALLOW_THREADS;
if (channeldata) {
for (i = 0; i < numchanneldata; ++i) {
Py_XDECREF(channeldata[i].sound);
Py_XDECREF(channeldata[i].queue);
}
free(channeldata);
channeldata = NULL;
numchanneldata = 0;
}
if (mx_current_music) {
if (*mx_current_music) {
Py_BEGIN_ALLOW_THREADS;
Mix_FreeMusic(*mx_current_music);
Py_END_ALLOW_THREADS;
*mx_current_music = NULL;
}
mx_current_music = NULL;
}
if (mx_queue_music) {
if (*mx_queue_music) {
Py_BEGIN_ALLOW_THREADS;
Mix_FreeMusic(*mx_queue_music);
Py_END_ALLOW_THREADS;
*mx_queue_music = NULL;
}
mx_queue_music = NULL;
}
Py_BEGIN_ALLOW_THREADS;
Mix_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
Py_END_ALLOW_THREADS;
}
Py_RETURN_NONE;
}
static PyObject *
pg_mixer_init(PyObject *self, PyObject *args, PyObject *keywds)
{
int freq = 0, size = 0, channels = 0, chunk = 0, allowedchanges = -1;
char *devicename = NULL;
static char *kwids[] = {"frequency", "size", "channels",
"buffer", "devicename", "allowedchanges",
NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iiiizi", kwids, &freq,
&size, &channels, &chunk, &devicename,
&allowedchanges)) {
return NULL;
}
return _init(freq, size, channels, chunk, devicename, allowedchanges);
}
static PyObject *
pg_mixer_get_init(PyObject *self, PyObject *_null)
{
int freq, channels, realform;
Uint16 format;
if (!SDL_WasInit(SDL_INIT_AUDIO))
Py_RETURN_NONE;
if (!Mix_QuerySpec(&freq, &format, &channels))
Py_RETURN_NONE;
// create a signed or unsigned number of bits per sample
realform = SDL_AUDIO_BITSIZE(format);
if (SDL_AUDIO_ISSIGNED(format)) {
realform = -realform;
}
return Py_BuildValue("(iii)", freq, realform, channels);
}
static PyObject *
pg_mixer_get_driver(PyObject *self, PyObject *_null)
{
const char *name = NULL;
MIXER_INIT_CHECK();
name = SDL_GetCurrentAudioDriver();
if (!name) {
name = "unknown";
}
return PyUnicode_FromString(name);
}
static PyObject *
pre_init(PyObject *self, PyObject *args, PyObject *keywds)
{
static char *kwids[] = {"frequency", "size", "channels",
"buffer", "devicename", "allowedchanges",
NULL};
request_frequency = 0;
request_size = 0;
request_channels = 0;
request_chunksize = 0;
request_devicename = NULL;
request_allowedchanges = -1;
if (!PyArg_ParseTupleAndKeywords(
args, keywds, "|iiiizi", kwids, &request_frequency, &request_size,
&request_channels, &request_chunksize, &request_devicename,
&request_allowedchanges))
return NULL;
if (!request_frequency) {
request_frequency = PYGAME_MIXER_DEFAULT_FREQUENCY;
}
if (!request_size) {
request_size = PYGAME_MIXER_DEFAULT_SIZE;
}
if (!request_channels) {
request_channels = PYGAME_MIXER_DEFAULT_CHANNELS;
}
if (!request_chunksize) {
request_chunksize = PYGAME_MIXER_DEFAULT_CHUNKSIZE;
}
if (request_allowedchanges == -1) {
request_allowedchanges = PYGAME_MIXER_DEFAULT_ALLOWEDCHANGES;
}
Py_RETURN_NONE;
}
/* sound object methods */
static PyObject *
pgSound_Play(PyObject *self, PyObject *args, PyObject *kwargs)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
int channelnum = -1;
int loops = 0, playtime = -1, fade_ms = 0;
CHECK_CHUNK_VALID(chunk, NULL);
char *kwids[] = {"loops", "maxtime", "fade_ms", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", kwids, &loops,
&playtime, &fade_ms))
return NULL;
Py_BEGIN_ALLOW_THREADS;
if (fade_ms > 0) {
channelnum =
Mix_FadeInChannelTimed(-1, chunk, loops, fade_ms, playtime);
}
else {
channelnum = Mix_PlayChannelTimed(-1, chunk, loops, playtime);
}
Py_END_ALLOW_THREADS;
if (channelnum == -1)
Py_RETURN_NONE;
Py_XDECREF(channeldata[channelnum].sound);
Py_XDECREF(channeldata[channelnum].queue);
channeldata[channelnum].queue = NULL;
channeldata[channelnum].sound = self;
Py_INCREF(self);
// make sure volume on this arbitrary channel is set to full
Mix_Volume(channelnum, 128);
Py_BEGIN_ALLOW_THREADS;
Mix_GroupChannel(channelnum, (int)(intptr_t)chunk);
Py_END_ALLOW_THREADS;
return pgChannel_New(channelnum);
}
static PyObject *
snd_get_num_channels(PyObject *self, PyObject *_null)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
MIXER_INIT_CHECK();
return PyLong_FromLong(Mix_GroupCount((int)(intptr_t)chunk));
}
static PyObject *
snd_fadeout(PyObject *self, PyObject *args)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
int _time;
CHECK_CHUNK_VALID(chunk, NULL);
if (!PyArg_ParseTuple(args, "i", &_time))
return NULL;
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
Mix_FadeOutGroup((int)(intptr_t)chunk, _time);
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
snd_stop(PyObject *self, PyObject *_null)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
MIXER_INIT_CHECK();
Py_BEGIN_ALLOW_THREADS;
Mix_HaltGroup((int)(intptr_t)chunk);
Py_END_ALLOW_THREADS;
Py_RETURN_NONE;
}
static PyObject *
snd_set_volume(PyObject *self, PyObject *args)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
float volume;
CHECK_CHUNK_VALID(chunk, NULL);
if (!PyArg_ParseTuple(args, "f", &volume))
return NULL;
MIXER_INIT_CHECK();
Mix_VolumeChunk(chunk, (int)(volume * 128));
Py_RETURN_NONE;
}
static PyObject *
snd_get_volume(PyObject *self, PyObject *_null)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
int volume;
MIXER_INIT_CHECK();
volume = Mix_VolumeChunk(chunk, -1);
return PyFloat_FromDouble(volume / 128.0);
}
static PyObject *
snd_get_length(PyObject *self, PyObject *_null)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
int freq, channels, mixerbytes, numsamples;
Uint16 format;
MIXER_INIT_CHECK();
Mix_QuerySpec(&freq, &format, &channels);
if (format == AUDIO_S8 || format == AUDIO_U8)
mixerbytes = 1;
else if (format == AUDIO_F32LSB || format == AUDIO_F32MSB) {
mixerbytes = 4;
}
else
mixerbytes = 2;
numsamples = chunk->alen / mixerbytes / channels;
return PyFloat_FromDouble((float)numsamples / (float)freq);
}
static PyObject *
snd_get_raw(PyObject *self, PyObject *_null)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
MIXER_INIT_CHECK();
return PyBytes_FromStringAndSize((const char *)chunk->abuf,
(Py_ssize_t)chunk->alen);
}
static PyObject *
snd_get_arraystruct(PyObject *self, void *closure)
{
Py_buffer view;
PyObject *cobj;
if (snd_getbuffer(self, &view, PyBUF_RECORDS)) {
return 0;
}
cobj = pgBuffer_AsArrayStruct(&view);
snd_releasebuffer(view.obj, &view);
Py_XDECREF(view.obj);
return cobj;
}
static PyObject *
snd_get_arrayinterface(PyObject *self, void *closure)
{
Py_buffer view;
PyObject *dict;
if (snd_getbuffer(self, &view, PyBUF_RECORDS)) {
return 0;
}
dict = pgBuffer_AsArrayInterface(&view);
snd_releasebuffer(self, &view);
Py_DECREF(self);
return dict;
}
static PyObject *
snd_get_samples_address(PyObject *self, PyObject *closure)
{
Mix_Chunk *chunk = pgSound_AsChunk(self);
CHECK_CHUNK_VALID(chunk, NULL);
MIXER_INIT_CHECK();
#if SIZEOF_VOID_P > SIZEOF_LONG
return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)chunk->abuf);
#else
return PyLong_FromUnsignedLong((unsigned long)chunk->abuf);
#endif
}
PyMethodDef sound_methods[] = {
{"play", (PyCFunction)pgSound_Play, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_SOUND_PLAY},
{"get_num_channels", snd_get_num_channels, METH_NOARGS,
DOC_MIXER_SOUND_GETNUMCHANNELS},
{"fadeout", snd_fadeout, METH_VARARGS, DOC_MIXER_SOUND_FADEOUT},
{"stop", snd_stop, METH_NOARGS, DOC_MIXER_SOUND_STOP},
{"set_volume", snd_set_volume, METH_VARARGS, DOC_MIXER_SOUND_SETVOLUME},
{"get_volume", snd_get_volume, METH_NOARGS, DOC_MIXER_SOUND_GETVOLUME},
{"get_length", snd_get_length, METH_NOARGS, DOC_MIXER_SOUND_GETLENGTH},
{"get_raw", snd_get_raw, METH_NOARGS, DOC_MIXER_SOUND_GETRAW},
{NULL, NULL, 0, NULL}};
static PyGetSetDef sound_getset[] = {
{"__array_struct__", snd_get_arraystruct, NULL, "Version 3", NULL},
{"__array_interface__", snd_get_arrayinterface, NULL, "Version 3", NULL},
{"_samples_address", (getter)snd_get_samples_address, NULL,
"samples buffer address (readonly)", NULL},
{NULL, NULL, NULL, NULL, NULL}};
/*buffer protocol*/
/*
snd_buffer_iteminfo converts between SDL and python format constants.
https://wiki.libsdl.org/SDL_AudioSpec
https://docs.python.org/3/library/struct.html#format-characters
returns:
-1 on error, else 0.
format: buffer string showing the format.
itemsize: bytes for each item.
*/
static int
snd_buffer_iteminfo(char **format, Py_ssize_t *itemsize, int *channels)
{
static char fmt_AUDIO_U8[] = "B";
static char fmt_AUDIO_S8[] = "b";
static char fmt_AUDIO_U16SYS[] = "=H";
static char fmt_AUDIO_S16SYS[] = "=h";
static char fmt_AUDIO_S32LSB[] = "<i";
static char fmt_AUDIO_S32MSB[] = ">i";
static char fmt_AUDIO_F32LSB[] = "<f";
static char fmt_AUDIO_F32MSB[] = ">f";
int freq = 0;
Uint16 mixer_format = 0;
Mix_QuerySpec(&freq, &mixer_format, channels);
switch (mixer_format) {
case AUDIO_U8:
*format = fmt_AUDIO_U8;
*itemsize = 1;
return 0;
case AUDIO_S8:
*format = fmt_AUDIO_S8;
*itemsize = 1;
return 0;
case AUDIO_U16SYS:
*format = fmt_AUDIO_U16SYS;
*itemsize = 2;
return 0;
case AUDIO_S16SYS:
*format = fmt_AUDIO_S16SYS;
*itemsize = 2;
return 0;
case AUDIO_S32LSB:
*format = fmt_AUDIO_S32LSB;
*itemsize = 4;
return 0;
case AUDIO_S32MSB:
*format = fmt_AUDIO_S32MSB;
*itemsize = 4;
return 0;
case AUDIO_F32LSB:
*format = fmt_AUDIO_F32LSB;
*itemsize = 4;
return 0;
case AUDIO_F32MSB:
*format = fmt_AUDIO_F32MSB;
*itemsize = 4;
return 0;
}
PyErr_Format(PyExc_SystemError,
"Pygame bug (mixer.Sound): unknown mixer format %d",
(int)mixer_format);
return -1;
}
static int
snd_getbuffer(PyObject *obj, Py_buffer *view, int flags)
{
Mix_Chunk *chunk = pgSound_AsChunk(obj);
int channels;
char *format;
int ndim = 0;
Py_ssize_t *shape = 0;
Py_ssize_t *strides = 0;
Py_ssize_t itemsize;
Py_ssize_t samples;
CHECK_CHUNK_VALID(chunk, -1);
view->obj = 0;
if (snd_buffer_iteminfo(&format, &itemsize, &channels)) {
return -1;
}
if (channels != 1 && PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) {
PyErr_SetString(pgExc_BufferError,
"polyphonic sound is not Fortran contiguous");
return -1;
}
if (PyBUF_HAS_FLAG(flags, PyBUF_ND)) {
ndim = channels > 1 ? 2 : 1;
samples = chunk->alen / (itemsize * channels);
shape = PyMem_New(Py_ssize_t, 2 * ndim);
if (!shape) {
PyErr_NoMemory();
return -1;
}
shape[ndim - 1] = channels;
shape[0] = samples;
if (PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) {
strides = shape + ndim;
strides[0] = itemsize * channels;
strides[ndim - 1] = itemsize;
}
}
Py_INCREF(obj);
view->obj = obj;
view->buf = chunk->abuf;
view->len = (Py_ssize_t)chunk->alen;
view->readonly = 0;
view->itemsize = itemsize;
view->format = PyBUF_HAS_FLAG(flags, PyBUF_FORMAT) ? format : 0;
view->ndim = ndim;
view->shape = shape;
view->strides = strides;
view->suboffsets = 0;
view->internal = shape;
return 0;
}
static void
snd_releasebuffer(PyObject *obj, Py_buffer *view)
{
if (view->internal) {
PyMem_Free(view->internal);
view->internal = 0;
}
}
static PyBufferProcs sound_as_buffer[] = {{snd_getbuffer, snd_releasebuffer}};
/*sound object internals*/
static void
sound_dealloc(pgSoundObject *self)