forked from ohai/ruby-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixer.c.m4
More file actions
1387 lines (1264 loc) · 40.4 KB
/
mixer.c.m4
File metadata and controls
1387 lines (1264 loc) · 40.4 KB
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
/* -*- mode: C -*- */
#ifdef HAVE_SDL_MIXER_H
#include "rubysdl2_internal.h"
#include <SDL_mixer.h>
static VALUE mMixer;
static VALUE cChunk;
static VALUE cMusic;
static VALUE mChannels;
static VALUE cGroup;
static VALUE mMusicChannel;
static VALUE playing_chunks = Qnil;
static VALUE playing_music = Qnil;
#define MIX_ERROR() do { HANDLE_ERROR(SDL_SetError("%s", Mix_GetError())); } while(0)
#define HANDLE_MIX_ERROR(code) \
do { if ((code) < 0) { MIX_ERROR(); } } while (0)
typedef struct Chunk {
Mix_Chunk* chunk;
} Chunk;
typedef struct Music {
Mix_Music* music;
} Music;
static void Chunk_free(Chunk* c)
{
if (rubysdl2_is_active() && c->chunk)
Mix_FreeChunk(c->chunk);
free(c);
}
DEFINE_DATA_TYPE(Chunk, Chunk_free);
static VALUE Chunk_new(Mix_Chunk* chunk)
{
Chunk* c;
VALUE obj = TypedData_Make_Struct(cChunk, Chunk, &Chunk_data_type, c);
c->chunk = chunk;
return obj;
}
DEFINE_WRAPPER(Mix_Chunk, Chunk, chunk, cChunk, "SDL2::Mixer::Chunk");
static void Music_free(Music* m)
{
if (rubysdl2_is_active() && m->music)
Mix_FreeMusic(m->music);
free(m);
}
DEFINE_DATA_TYPE(Music, Music_free);
static VALUE Music_new(Mix_Music* music)
{
Music* c;
VALUE obj = TypedData_Make_Struct(cMusic, Music, &Music_data_type, c);
c->music = music;
return obj;
}
DEFINE_WRAPPER(Mix_Music, Music, music, cMusic, "SDL2::Mixer::Music");
/*
* Document-module: SDL2::Mixer
*
* Sound mixing module.
*
* With this module, you can play many kinds of sound files such as:
*
* * WAVE/RIFF (.wav)
* * AIFF (.aiff)
* * VOC (.voc)
* * MOD (.mod .xm .s3m .669 .it .med etc.)
* * MIDI (.mid)
* * OggVorbis (.ogg)
* * MP3 (.mp3)
* * FLAC (.flac)
*
* Before playing sounds,
* you need to initialize this module by {.init} and
* open a sound device by {.open}.
*
* This module mixes multiple sound sources in parallel.
* To play a sound source, you assign the source to a "channel"
* and this module mixes all sound sources assigned to the channels.
*
* In this module, there are two types of sound sources:
* {SDL2::Mixer::Chunk} and {SDL2::Mixer::Music}.
* And there are two corresponding types of channels:
* {SDL2::Mixer::Channels} and {SDL2::Mixer::MusicChannel}.
*
* {SDL2::Mixer::Channels} module plays {SDL2::Mixer::Chunk} objects,
* through multiple (default eight) channels. This module is suitable
* for the sound effects.
* The number of channels is variable with {SDL2::Mixer::Channels.allocate}.
*
* {SDL2::Mixer::MusicChannel} module plays {SDL2::Mixer::Music} objects.
* This module has only one playing channel, and you cannot play
* multiple music in parallel. However an {SDL2::Mixer::Music} object
* is more efficient for memory, and this module supports more file formats
* than {SDL2::Mixer::Channels}.
* This module is suitable for playing "BGMs" of your application.
*
*/
/*
* @overload init(flags)
* Initialize the mixer library.
*
* This module function load dynamically-linked libraries for sound file
* formats such as ogg and flac.
*
* You can give the initialized libraries (file formats) with OR'd bits of the
* following constants:
*
* * SDL2::Mixer::INIT_FLAC
* * SDL2::Mixer::INIT_MOD
* * SDL2::Mixer::INIT_MODPLUG
* * SDL2::Mixer::INIT_MP3
* * SDL2::Mixer::INIT_OGG
* * SDL2::Mixer::INIT_FLUIDSYNTH
*
* @param flags [Integer] intialized sublibraries
* @return [nil]
*
*/
static VALUE Mixer_s_init(VALUE self, VALUE f)
{
int flags = NUM2INT(f);
if ((Mix_Init(flags) & flags) != flags)
rb_raise(eSDL2Error, "Couldn't initialize SDL_mixer");
return Qnil;
}
static void check_channel(VALUE ch, int allow_minus_1)
{
int channel = NUM2INT(ch);
if (channel >= Mix_AllocateChannels(-1))
rb_raise(rb_eArgError, "too large number of channel (%d)", channel);
if ((channel == -1 && !allow_minus_1) || channel < -1)
rb_raise(rb_eArgError, "negative number of channel is not allowed");
}
/*
* @overload open(freq=22050, format=SDL2::Mixer::DEFAULT_FORMAT, channels=2, chunksize=1024)
* Open a sound device.
*
* Before calling loading/playing methods in the mixer module,
* this method must be called.
* Before calling this method,
* {SDL2.init} must be called with SDL2::INIT_AUDIO.
*
* @param freq [Integer] output sampling frequency in Hz.
* Normally 22050 or 44100 is used.
* 44100 is CD audio rate. SDL2::Mixer::DEFAULT_FREQUENCY(22050) is best for
* many kinds of game because 44100 requires too much CPU power on older computers.
* @param format [Integer] output sample format
* @param channels 1 is for mono, and 2 is for stereo.
* @param chunksize bytes used per output sample
*
* @return [nil]
*
* @raise [SDL2::Error] raised when a device cannot be opened
*
* @see .init
* @see .close
* @see .query
*/
static VALUE Mixer_s_open(int argc, VALUE* argv, VALUE self)
{
VALUE freq, format, channels, chunksize;
rb_scan_args(argc, argv, "04", &freq, &format, &channels, &chunksize);
HANDLE_MIX_ERROR(Mix_OpenAudio((freq == Qnil) ? MIX_DEFAULT_FREQUENCY : NUM2INT(freq),
(format == Qnil) ? MIX_DEFAULT_FORMAT : NUM2UINT(format),
(channels == Qnil) ? 2 : NUM2INT(channels),
(chunksize == Qnil) ? 1024 : NUM2INT(chunksize)));
playing_chunks = rb_ary_new();
return Qnil;
}
/*
* Close the audio device.
*
* @return [nil]
*/
static VALUE Mixer_s_close(VALUE self)
{
Mix_CloseAudio();
return Qnil;
}
/*
* Query a sound device spec.
*
* This method returns the most suitable setting for {.open} the device.
*
* @return [Array(Integer, Integer, Integer, Integer)]
* the suitable frequency in Hz, the suitable format,
* the suitable number of channels (1 for mono, 2 for stereo),
* and the number of call of {.open}.
*
*/
static VALUE Mixer_s_query(VALUE self)
{
int frequency = 0, channels = 0, num_opened;
Uint16 format = 0;
num_opened = Mix_QuerySpec(&frequency, &format, &channels);
return rb_ary_new3(4, INT2NUM(frequency), UINT2NUM(format),
INT2NUM(channels), INT2NUM(num_opened));
}
/*
* Document-module: SDL2::Mixer::Channels
*
* This module plays {SDL2::Mixer::Chunk} objects in parallel.
*
* Each virtual sound output device is called channel, and
* the number of channels determines the f
*/
/*
* @overload allocate(num_channels)
* Set the number of channels being mixed.
*
* @param num_channels [Integer] Number of channels prepared for mixing.
*
* @return [Integer] the number of prepared channels.
*/
static VALUE Channels_s_allocate(VALUE self, VALUE num_channels)
{
return INT2NUM(Mix_AllocateChannels(NUM2INT(num_channels)));
}
/*
* @overload reserve(num)
* Reserve channel from 0 to num-1 and reserved channels are not used by
* {Channels.play} and {Channels.fade_in} with **channels**==-1.
*
* @param num [Integer]
* @return [Integer]
*/
static VALUE Channels_s_reserve(VALUE self, VALUE num)
{
return INT2NUM(Mix_ReserveChannels(NUM2INT(num)));
}
/*
* @overload volume(channel)
* Get the volume of specified channel.
*
* @param channel [Integer] the channel to get volume for.
* If the specified channel is -1, this method returns
* the average volume of all channels.
* @return [Integer] the volume, 0-128
*
* @see .set_volume
*/
static VALUE Channels_s_volume(VALUE self, VALUE channel)
{
return INT2NUM(Mix_Volume(NUM2INT(channel), -1));
}
/*
* @overload set_volume(channel, volume)
* Set the volume of specified channel.
*
* The volume should be from 0 to {SDL2::Mixer::MAX_VOLUME}(128).
* If the specified channel is -1, set volume for all channels.
*
* @param channel [Integer] the channel to set volume for.
* @param volume [Integer] the volume to use
* @return [void]
*
* @see .volume
*/
static VALUE Channels_s_set_volume(VALUE self, VALUE channel, VALUE volume)
{
return INT2NUM(Mix_Volume(NUM2INT(channel), NUM2INT(volume)));
}
static void protect_playing_chunk_from_gc(int channel, VALUE chunk)
{
rb_ary_store(playing_chunks, channel, chunk);
}
/*
* @overload play(channel, chunk, loops, ticks = -1)
* Play a {SDL2::Mixer::Chunk} on **channel**.
*
* @param channel [Integer] the channel to play, or -1 for the first free unreserved
* channel
* @param chunk [SDL2::Mixer::Chunk] the chunk to play
* @param loops [Integer] the number of loops, or -1 for infite loops.
* passing 1 plays the sample twice (1 loop).
* @param ticks [Integer] milliseconds limit to play, at most.
* If the chunk is long enough and **loops** is large enough,
* the play will stop after **ticks** milliseconds.
* Otherwise, the play will stop when the loop ends.
* -1 means infinity.
* @return [Integer] the channel that plays the chunk.
*
* @raise [SDL2::Error] raised on a playing error. For example,
* **channel** is out of the allocated channels, or
* there is no free channels when **channel** is -1.
*
* @see .fade_in
*/
static VALUE Channels_s_play(int argc, VALUE* argv, VALUE self)
{
VALUE channel, chunk, loops, ticks;
int ch;
rb_scan_args(argc, argv, "31", &channel, &chunk, &loops, &ticks);
if (ticks == Qnil)
ticks = INT2FIX(-1);
check_channel(channel, 1);
ch = Mix_PlayChannelTimed(NUM2INT(channel), Get_Mix_Chunk(chunk),
NUM2INT(loops), NUM2INT(ticks));
HANDLE_MIX_ERROR(ch);
protect_playing_chunk_from_gc(ch, chunk);
return INT2FIX(ch);
}
/*
* @overload fade_in(channel, chunk, loops, ms, ticks = -1)
* Play a {SDL2::Mixer::Chunk} on **channel** with fading in.
*
* @param channel [Integer] the channel to play, or -1 for the first free unreserved
* channel
* @param chunk [SDL2::Mixer::Chunk] the chunk to play
* @param loops [Integer] the number of loops, or -1 for infite loops.
* passing 1 plays the sample twice (1 loop).
* @param ms [Integer] milliseconds of time of fade-in effect.
* @param ticks [Integer] milliseconds limit to play, at most.
* If the chunk is long enough and **loops** is large enough,
* the play will stop after **ticks** milliseconds.
* Otherwise, the play will stop when the loop ends.
* -1 means infinity.
* @return [Integer] the channel that plays the chunk.
*
* @raise [SDL2::Error] raised on a playing error. For example,
* **channel** is out of the allocated channels, or
* there is no free channels when **channel** is -1.
*
* @see .play
* @see .fade_out
*/
static VALUE Channels_s_fade_in(int argc, VALUE* argv, VALUE self)
{
VALUE channel, chunk, loops, ms, ticks;
int ch;
rb_scan_args(argc, argv, "41", &channel, &chunk, &loops, &ms, &ticks);
if (ticks == Qnil)
ticks = INT2FIX(-1);
check_channel(channel, 1);
ch = Mix_FadeInChannelTimed(NUM2INT(channel), Get_Mix_Chunk(chunk),
NUM2INT(loops), NUM2INT(ms), NUM2INT(ticks));
HANDLE_MIX_ERROR(ch);
protect_playing_chunk_from_gc(ch, chunk);
return INT2FIX(ch);
}
/*
* @overload pause(channel)
* Pause a specified channel.
*
* @param channel [Integer] the channel to pause, or -1 for all channels.
* @return [nil]
*
* @see .resume
* @see .pause?
*/
static VALUE Channels_s_pause(VALUE self, VALUE channel)
{
check_channel(channel, 1);
Mix_Pause(NUM2INT(channel));
return Qnil;
}
/*
* @overload resume(channel)
* Resume a specified channel that already pauses.
*
* @note This method has no effect to unpaused channels.
* @param channel [Integer] the channel to be resumed, or -1 for all channels.
* @return [nil]
*
* @see .pause
* @see .pause?
*/
static VALUE Channels_s_resume(VALUE self, VALUE channel)
{
check_channel(channel, 1);
Mix_Resume(NUM2INT(channel));
return Qnil;
}
/*
* @overload halt(channel)
* Halt playing of a specified channel.
*
* @param channel [Integer] the channel to be halted, or -1 for all channels.
* @return [nil]
*
* @see .expire
* @see .fade_out
* @see .play?
*/
static VALUE Channels_s_halt(VALUE self, VALUE channel)
{
check_channel(channel, 1);
Mix_HaltChannel(NUM2INT(channel));
return Qnil;
}
/*
* @overload expire(channel, ticks)
* Halt playing of a specified channel after **ticks** milliseconds.
*
* @param channel [Integer] the channel to be halted, or -1 for all channels.
* @param ticks [Integer] milliseconds untils the channel halts playback.
* @return [nil]
*
* @see .halt
* @see .fade_out
* @see .play?
*/
static VALUE Channels_s_expire(VALUE self, VALUE channel, VALUE ticks)
{
check_channel(channel, 1);
Mix_ExpireChannel(NUM2INT(channel), NUM2INT(ticks));
return Qnil;
}
/* --- Pitch shift effect using Mix_RegisterEffect --- */
#define MAX_PITCH_CHANNELS 32
typedef struct {
double speed; /* playback speed (pitch ratio), 1.0 = normal */
double position; /* fractional sample position in source chunk */
Sint16 *chunk_data; /* pointer to the chunk's decoded PCM data */
int chunk_frames; /* total frames in the chunk */
int chunk_channels; /* channels in chunk (1 or 2) */
int done; /* 1 if playback reached end */
} PitchShiftData;
static PitchShiftData pitch_data[MAX_PITCH_CHANNELS];
static void pitch_effect_callback(int channel, void *stream, int len, void *udata)
{
PitchShiftData *data = (PitchShiftData *)udata;
if (!data || data->speed == 1.0 || !data->chunk_data) {
return;
}
int out_channels = data->chunk_channels;
Sint16 *buf = (Sint16 *)stream;
int out_frames = len / (sizeof(Sint16) * out_channels);
/* If already done, silence the entire buffer to prevent original chunk
data from leaking through at normal pitch */
if (data->done) {
memset(stream, 0, len);
return;
}
double pos = data->position;
double speed = data->speed;
int src_frames = data->chunk_frames;
Sint16 *src = data->chunk_data;
int i;
for (i = 0; i < out_frames; i++) {
int src_idx = (int)pos;
double frac = pos - src_idx;
if (src_idx >= src_frames - 1) {
/* Past end of chunk: silence the rest and mark done */
int j, c;
for (j = i; j < out_frames; j++) {
for (c = 0; c < out_channels; c++) {
buf[j * out_channels + c] = 0;
}
}
data->done = 1;
break;
} else {
/* Linear interpolation from chunk's PCM data */
int c;
for (c = 0; c < out_channels; c++) {
Sint16 s1 = src[src_idx * out_channels + c];
Sint16 s2 = src[(src_idx + 1) * out_channels + c];
buf[i * out_channels + c] = (Sint16)(s1 + frac * (s2 - s1));
}
}
pos += speed;
}
data->position = pos;
}
static void pitch_effect_done(int channel, void *udata)
{
PitchShiftData *data = (PitchShiftData *)udata;
if (data) {
data->position = 0;
data->done = 0;
if (data->chunk_data) {
free(data->chunk_data);
data->chunk_data = NULL;
}
}
}
/*
* @overload set_pitch(channel, pitch)
* Set the pitch (playback speed) of a channel using Mix_RegisterEffect.
*
* A pitch of 1.0 is normal speed, 2.0 is double speed (one octave up),
* 0.5 is half speed (one octave down).
*
* @param channel [Integer] the channel to set pitch for (0-31)
* @param pitch [Float] the pitch ratio (0.1 to 4.0)
* @return [nil]
*/
/*
* @overload play_pitched(channel, chunk, pitch, loops=0)
* Play a Chunk on a channel with pitch shifting.
*
* Instead of setting pitch separately, this method combines play + pitch
* because the effect callback needs access to the chunk's PCM data.
*
* @param channel [Integer] the channel to play on (0-31)
* @param chunk [SDL2::Mixer::Chunk] the chunk to play
* @param pitch [Float] pitch ratio (1.0 = normal, 2.0 = octave up)
* @param loops [Integer] number of loops (0 = play once, -1 = infinite)
* @return [Integer] the channel used
*/
static VALUE Channels_s_play_pitched(int argc, VALUE *argv, VALUE self)
{
VALUE channel, chunk, pitch, loops;
rb_scan_args(argc, argv, "31", &channel, &chunk, &pitch, &loops);
int ch = NUM2INT(channel);
double p = NUM2DBL(pitch);
int lp = (loops == Qnil) ? 0 : NUM2INT(loops);
if (ch < 0 || ch >= MAX_PITCH_CHANNELS)
rb_raise(rb_eArgError, "channel %d out of range (0-%d)", ch, MAX_PITCH_CHANNELS - 1);
Mix_Chunk *mc = Get_Mix_Chunk(chunk);
/* Unregister previous effect if any */
Mix_UnregisterEffect(ch, pitch_effect_callback);
/* Free previous chunk_data copy */
if (pitch_data[ch].chunk_data) {
free(pitch_data[ch].chunk_data);
pitch_data[ch].chunk_data = NULL;
}
/* Protect chunk from GC while playing */
protect_playing_chunk_from_gc(ch, chunk);
if (p == 1.0) {
/* Normal playback, no effect needed */
int result = Mix_PlayChannel(ch, mc, lp);
HANDLE_MIX_ERROR(result);
return INT2NUM(result);
}
/* Query audio format to determine channels */
int frequency, mix_channels;
Uint16 format;
Mix_QuerySpec(&frequency, &format, &mix_channels);
/* Copy chunk PCM data */
int bytes_per_sample = sizeof(Sint16) * mix_channels;
int total_frames = mc->alen / bytes_per_sample;
pitch_data[ch].speed = p;
pitch_data[ch].position = 0;
pitch_data[ch].done = 0;
pitch_data[ch].chunk_channels = mix_channels;
pitch_data[ch].chunk_frames = total_frames;
pitch_data[ch].chunk_data = (Sint16 *)malloc(mc->alen);
if (pitch_data[ch].chunk_data) {
memcpy(pitch_data[ch].chunk_data, mc->abuf, mc->alen);
}
/* Register effect and play */
HANDLE_MIX_ERROR(Mix_RegisterEffect(ch, pitch_effect_callback,
pitch_effect_done, &pitch_data[ch]));
int result = Mix_PlayChannel(ch, mc, lp);
HANDLE_MIX_ERROR(result);
return INT2NUM(result);
}
/*
* @overload fade_out(channel, ms)
* Halt playing of a specified channel with fade-out effect.
*
* @param channel [Integer] the channel to be halted, or -1 for all channels.
* @param ms [Integer] milliseconds of fade-out effect
* @return [nil]
*
* @see .halt
* @see .expire
* @see .play?
* @see .fade_in
*/
static VALUE Channels_s_fade_out(VALUE self, VALUE channel, VALUE ms)
{
check_channel(channel, 1);
Mix_FadeOutChannel(NUM2INT(channel), NUM2INT(ms));
return Qnil;
}
/*
* @overload play?(channel)
* Return true if a specified channel is playing.
*
* @param channel [Integer] channel to test
* @return [Boolean]
*
* @see .pause?
* @see .fading
*/
static VALUE Channels_s_play_p(VALUE self, VALUE channel)
{
check_channel(channel, 0);
return INT2BOOL(Mix_Playing(NUM2INT(channel)));
}
/*
* @overload pause?(channel)
* Return true if a specified channel is paused.
*
* @note This method returns true if a paused channel is halted by {.halt}, or any
* other halting methods.
*
* @param channel [Integer] channel to test
* @return [Boolean]
*
* @see .play?
* @see .fading
*/
static VALUE Channels_s_pause_p(VALUE self, VALUE channel)
{
check_channel(channel, 0);
return INT2BOOL(Mix_Paused(NUM2INT(channel)));
}
/*
* @overload fading(channel)
* Return the fading state of a specified channel.
*
* The return value is one of the following:
*
* * {SDL2::Mixer::NO_FADING} - **channel** is not fading in, and fading out
* * {SDL2::Mixer::FADING_IN} - **channel** is fading in
* * {SDL2::Mixer::FADING_OUT} - **channel** is fading out
*
* @param channel [Integer] channel to test
*
* @return [Integer]
*
* @see .play?
* @see .pause?
* @see .fade_in
* @see .fade_out
*/
static VALUE Channels_s_fading(VALUE self, VALUE which)
{
check_channel(which, 0);
return INT2FIX(Mix_FadingChannel(NUM2INT(which)));
}
/*
* @overload playing_chunk(channel)
* Get the {SDL2::Mixer::Chunk} object most recently playing on **channel**.
*
* If **channel** is out of allocated channels, or
* no chunk is played yet on **channel**, this method returns nil.
*
* @param channel [Integer] the channel to get the chunk object
* @return [SDL2::Mixer::Chunk,nil]
*/
static VALUE Channels_s_playing_chunk(VALUE self, VALUE channel)
{
check_channel(channel, 0);
return rb_ary_entry(playing_chunks, NUM2INT(channel));
}
/*
* Document-class: SDL2::Mixer::Channels::Group
*
* This class represents a channel group. A channel group is
* a set of channels and you can stop playing and fade out playing
* channels of an group at the same time.
*
* Each channel group is identified by an integer called tag.
*/
/*
* Initialize the channel with given **tag**.
*
* @param tag [Integer] channel indentifier
*
* Groups with a common tag are identified.
*/
static VALUE Group_initialize(VALUE self, VALUE tag)
{
rb_iv_set(self, "@tag", tag);
return Qnil;
}
/*
* Get the default channel group.
*
* The default channel group refers all channels in the mixer system.
*
* @return [SDL2::Mixer::Channels::Group]
*/
static VALUE Group_s_default(VALUE self)
{
VALUE tag = INT2FIX(-1);
return rb_class_new_instance(1, &tag, self);
}
/*
* Get the tag of the group.
*
* @return [Integer]
*/
inline static int Group_tag(VALUE group)
{
return NUM2INT(rb_iv_get(group, "@tag"));
}
/*
* @overload ==(other)
* Return true if **self** and **other** are same.
*
* **self** and **other** are considered to be same
* if they have the same tag.
*
* @param other [SDL2::Mixer::Channels::Group] a compared object
* @return [Boolean]
*/
static VALUE Group_eq(VALUE self, VALUE other)
{
return INT2BOOL(rb_obj_is_instance_of(other, cGroup) &&
Group_tag(self) == Group_tag(other));
}
/*
* @overload add(which)
* Add a channel to the group.
*
* @param which [Integer] a channel id
* @return [nil]
*/
static VALUE Group_add(VALUE self, VALUE which)
{
if (!Mix_GroupChannel(NUM2INT(which), Group_tag(self))) {
SDL_SetError("Cannot add channel %d", NUM2INT(which));
SDL_ERROR();
}
return Qnil;
}
/*
* Get the number of channels belong to the group.
*
* @return [Integer]
*/
static VALUE Group_count(VALUE self)
{
return INT2NUM(Mix_GroupCount(Group_tag(self)));
}
/*
* Return the first available channel in the group.
*
* Return -1 if no channel is available.
*
* @return [Integer]
*/
static VALUE Group_available(VALUE self)
{
return INT2NUM(Mix_GroupAvailable(Group_tag(self)));
}
/*
* Return the oldest cahnnel in the group.
*
* Return -1 if no channel is available.
*
* @return [Integer]
*/
static VALUE Group_oldest(VALUE self)
{
return INT2NUM(Mix_GroupOldest(Group_tag(self)));
}
/*
* Return the newer cahnnel in the group.
*
* Return -1 if no channel is available.
*
* @return [Integer]
*/
static VALUE Group_newer(VALUE self)
{
return INT2NUM(Mix_GroupNewer(Group_tag(self)));
}
/*
* @overload fade_out(ms)
* Halt playing of all channels in the group with fade-out effect.
*
* @param ms [Integer] milliseconds of fade-out effect
* @return [Integer] the number of channels affected by this method
* @see Channels.fade_out
* @see .halt
*/
static VALUE Group_fade_out(VALUE self, VALUE ms)
{
return INT2NUM(Mix_FadeOutGroup(Group_tag(self), NUM2INT(ms)));
}
/*
* Halt playing of all channels in the group.
*
* @return [nil]
* @see Channels.halt
* @see .fade_out
*/
static VALUE Group_halt(VALUE self)
{
Mix_HaltGroup(Group_tag(self));
return Qnil;
}
/*
* Document-module: SDL2::Mixer::MusicChannel
*
* This module provides the functions to play {SDL2::Mixer::Music}.
*/
/*
* @overload play(music, loops)
* Play **music** **loops** times.
*
* @note the meaning of **loop** is different from {SDL2::Mixer::Channels.play}.
*
* @param music [SDL2::Mixer::Music] music to play
* @param loops [Integer] number of times to play the music.
* 0 plays the music zero times.
* -1 plays the music forever.
*
* @return [nil]
*
* @see .fade_in
*
*/
static VALUE MusicChannel_s_play(VALUE self, VALUE music, VALUE loops)
{
HANDLE_MIX_ERROR(Mix_PlayMusic(Get_Mix_Music(music), NUM2INT(loops)));
playing_music = music;
return Qnil;
}
/*
* @overload fade_in(music, loops, ms, pos=0)
* Play **music** **loops** times with fade-in effect.
*
* @note the meaning of **loop** is different from {SDL2::Mixer::Channels.play}.
*
* @param music [SDL2::Mixer::Music] music to play
* @param loops [Integer] number of times to play the music.
* 0 plays the music zero times.
* -1 plays the music forever.
* @param ms [Integer] milliseconds for the fade-in effect
* @param pos [Float] the position to play from.
* The meaning of "position" is different for the type of music sources.
*
* @return [nil]
*
* @see .play
*/
static VALUE MusicChannel_s_fade_in(int argc, VALUE* argv, VALUE self)
{
VALUE music, loops, fade_in_ms, pos;
rb_scan_args(argc, argv, "31", &music, &loops, &fade_in_ms, &pos);
HANDLE_MIX_ERROR(Mix_FadeInMusicPos(Get_Mix_Music(music), NUM2INT(loops),
NUM2INT(fade_in_ms),
pos == Qnil ? 0 : NUM2DBL(pos)));
playing_music = music;
return Qnil;
}
/*
* Get the volume of the music channel.
*
* @return [Integer]
*
* @see .volume=
*/
static VALUE MusicChannel_s_volume(VALUE self)
{
return INT2FIX(Mix_VolumeMusic(-1));
}
/*
* @overload volume=(vol)
* Set the volume of the music channel.
*
* @param vol [Integer] the volume for mixing,
* from 0 to {SDL2::Mixer::MAX_VOLUME}(128).
* @return [void]
*
* @see .volume
*/
static VALUE MusicChannel_s_set_volume(VALUE self, VALUE volume)
{
Mix_VolumeMusic(NUM2INT(volume));
return volume;
}
/*
* Pause the playback of the music channel.
*
* @return [nil]
*
* @see .resume
* @see .pause?
*/
static VALUE MusicChannel_s_pause(VALUE self)
{
Mix_PauseMusic(); return Qnil;
}
/*
* Resume the playback of the music channel.
*
* @return [nil]
*
* @see .pause
* @see .pause?
*/
static VALUE MusicChannel_s_resume(VALUE self)
{
Mix_ResumeMusic(); return Qnil;
}
/*
* Rewind the music to the start.
*
* @return [nil]
*/
static VALUE MusicChannel_s_rewind(VALUE self)
{
Mix_RewindMusic(); return Qnil;
}
/*
* @overload set_position(position)
* Set the position of the currently playing music.
*
* @param position [Float] the position to play from.
* @return [nil]
*/
static VALUE MusicChannel_s_set_position(VALUE self, VALUE position)
{
HANDLE_MIX_ERROR(Mix_SetMusicPosition(NUM2DBL(position)));
return Qnil;
}
/*
* Halt the music playback.
*
* @return [nil]
*/
static VALUE MusicChannel_s_halt(VALUE self)
{
Mix_HaltMusic(); return Qnil;
}
/*
* @overload fade_out(ms)
* Halt the music playback with fade-out effect.
*