-
Notifications
You must be signed in to change notification settings - Fork 0
/
rudl_audio.c
1110 lines (935 loc) · 30.6 KB
/
rudl_audio.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
/* RUDL - a C library wrapping SDL for use in Ruby. Copyright (C) 2001 Danny van Bruggen */
#include "rudl_audio.h"
VALUE classSound;
VALUE classChannel;
VALUE classMixer;
VALUE classMusic;
VALUE classEndOfMusicEvent;
#ifdef HAVE_SDL_MIXER_H
#include "SDL_mixer.h"
#ifndef MIX_MAJOR_VERSION
Your SDL_mixer lib is too old
#endif
// Thanks to Ruby-SDL
#define MAX_CHANNELS 256 /* should be more than enough */
static VALUE playing_wave[MAX_CHANNELS];
static VALUE playing_music=Qnil;
void clearGCHack()
{
int i;
for(i=0; i<MAX_CHANNELS; i++){
playing_wave[i]=Qnil;
rb_global_variable(&(playing_wave[i]));
}
rb_global_variable( &playing_music );
}
static VALUE mixer_get_format(VALUE self);
//////////////////
Mix_Chunk* retrieveMixChunk(VALUE self);
void initAudio()
{
if(!SDL_WasInit(SDL_INIT_AUDIO)){
DEBUG_S("Starting audio subsystem");
initSDL();
if(!SDL_WasInit(SDL_INIT_AUDIO)){
SDL_VERIFY(!SDL_InitSubSystem(SDL_INIT_AUDIO));
}
if(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1){
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_RAISE;
}
}
}
void quitAudio()
{
// It's a HACK! For some reason some or all Musics can't be freed after
// the mixer was closed. This will give some ugly bogus Music objects
// though, but hey, it wasn't me who closed and opened the mixer and reused
// some Music objects!
rb_eval_string("ObjectSpace.each_object(RUDL::Music) {|x| x.destroy}");
if(SDL_WasInit(SDL_INIT_AUDIO)){
Mix_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
DEBUG_S("Stopping audio subsystem");
}
}
/////////////// CHANNEL
__inline__ VALUE createChannelObject(int number)
{
return rb_funcall(classChannel, id_new, 1, INT2NUM(number));
}
__inline__ int retrieveChannelNumber(VALUE channel)
{
VALUE tmp;
tmp=rb_iv_get(channel, "@number");
return NUM2INT(tmp);
}
/**
@file Audio
On this page, there are various classes for sound output.
The system consists of a mixer that will mix several sounds,
and one piece of music.
The @Sound class represents a particular sound sample. It can
be played on any Channel of the Mixer.
The Mixer has a certain amount of Channels.
Channels can be told to play a certain Sound.
It is not possible to play more Sounds at once than there are Channels.
A separate Channel is kept by the Mixer for playing Music files.
Since it's only one channel, only one song can play at a time.
This is a wrapper around <a href='http://www.libsdl.org/projects/SDL_mixer/'>SDL_mixer</a>.
*/
/**
@class Channel
A Channel is an interface object to one of the mixer's channels.
It can be used for manipulating sound on a particular channel.
*/
/**
@section Class Methods
@method new( number )
Creates a Channel interface object for mixer channel @number.
*/
// Replaced by some evalled stuff
/**
@section Volume methods
@method fade_out( milliseconds )
fade_out fades the channel to silence in @milliseconds milliseconds.
*/
/**
@method volume
@method volume=( loudness )
Get and set the volume of a @RUDL::Channel.
Volumes range from 0.0 to 1.0.
*/
static VALUE channel_fade_out(VALUE self, VALUE milliseconds)
{
Mix_FadeOutChannel(retrieveChannelNumber(self), NUM2UINT(milliseconds));
return self;
}
static VALUE channel_get_volume(VALUE self, VALUE volume)
{
return DBL2NUM((Mix_Volume(retrieveChannelNumber(self), -1))/128.0);
}
static VALUE channel_set_volume(VALUE self, VALUE volume)
{
Mix_Volume(retrieveChannelNumber(self), (int)(NUM2DBL(volume)*128));
return self;
}
/**
@section Play control
@method busy => boolean
@method pause => self
@method unpause => self
@method play( sound, loops, maxtime ) => self
@method play( sound, loops ) => self
@method play( sound ) => self
@method stop => self
*/
/**
@section Play control
These are pretty self-explanatory.
@play plays @Sound @sound 1 + @loops times, for a maximum time of @maxtime milliseconds.
*/
static VALUE channel_busy(VALUE self)
{
return Mix_Playing(retrieveChannelNumber(self));
}
static VALUE channel_pause(VALUE self)
{
Mix_Pause(retrieveChannelNumber(self));
return self;
}
static VALUE channel_play(int argc, VALUE* argv, VALUE self)
{
int loops = 0, maxtime = -1, channelnum;
VALUE sound=Qnil, loopsValue, maxtimeValue;
Mix_Chunk* chunk=retrieveMixChunk(argv[0]);
switch(rb_scan_args(argc, argv, "12", &sound, &loopsValue, &maxtimeValue)){
case 3: maxtime=NUM2INT(maxtimeValue);
case 2: loops=NUM2INT(loopsValue);
}
channelnum = Mix_PlayChannelTimed(retrieveChannelNumber(self), chunk, loops, maxtime);
if(channelnum != -1){
Mix_GroupChannel(channelnum, (int)chunk);
}
return self;
}
static VALUE channel_stop(VALUE self)
{
Mix_HaltChannel(retrieveChannelNumber(self));
return self;
}
static VALUE channel_unpause(VALUE self)
{
Mix_Resume(retrieveChannelNumber(self));
return self;
}
/**
@section Positioning
@method set_panning( left, right ) => self
Set the panning of a channel. The left and right channels are specified
as numbers between 0.0 and 1.0, quietest to loudest, respectively.
Technically, this is just individual volume control for a sample with
two (stereo) channels, so it can be used for more than just panning.
If you want real panning, call it like this:
<p class='example'>channel.set_panning(left, 1.0-left);</p>
...which isn't so hard.
*/
static VALUE channel_set_panning(VALUE self, VALUE left, VALUE right)
{
double l=NUM2DBL(left);
double r=NUM2DBL(right);
RUDL_ASSERT(l<=1.0, "left volume too high");
RUDL_ASSERT(l>=0.0, "left volume too low");
RUDL_ASSERT(r<=1.0, "right volume too high");
RUDL_ASSERT(r>=0.0, "right volume too low");
SDL_VERIFY(Mix_SetPanning(retrieveChannelNumber(self), (Uint8)(l*255), (Uint8)(r*255)));
return self;
}
/**
@method set_position( angle, distance ) => self
Set the position of a channel. @angle is an number from 0 to 360, that
specifies the location of the sound in relation to the listener. @angle
will be reduced as neccesary (540 becomes 180 degrees, -100 becomes 260).
@Angle 0 is due north, and rotates clockwise as the value increases.
For efficiency, the precision of this effect may be limited (angles 1
through 7 might all produce the same effect, 8 through 15 are equal, etc).
@distance is a number between 0.0 and 1.0 that specifies the space
between the sound and the listener. The larger the number, the further
away the sound is. Using 1.0 does not guarantee that the channel will be
culled from the mixing process or be completely silent. For efficiency,
the precision of this effect may be limited (@distance 0 through 0.1 might
all produce the same effect, 0.1 through 0.2 are equal, etc). Setting @angle
and @distance to 0 unregisters this effect, since the data would be
unchanged.
*/
static VALUE channel_set_position(VALUE self, VALUE angle, VALUE distance)
{
SDL_VERIFY(Mix_SetPosition(retrieveChannelNumber(self), (Sint16)NUM2INT(angle), (Sint8)(NUM2DBL(distance)*255)));
return self;
}
/**
@method set_distance( distance ) => self
@Distance is a number between 0.0 and 1.0 that specifies the space
between the sound and the listener. The larger the number, the further
away the sound is. Using 1.0 does not guarantee that the channel will be
culled from the mixing process or be completely silent. For efficiency,
the precision of this effect may be limited (distance 0 through 0.1 might
all produce the same effect, 0.1 through 0.2 are equal, etc). Setting
@distance to 0 unregisters this effect, since the data would be
unchanged.
*/
static VALUE channel_set_distance(VALUE self, VALUE distance)
{
SDL_VERIFY(Mix_SetDistance(retrieveChannelNumber(self), (Uint8)(NUM2DBL(distance)*255)));
return self;
}
/**
@method reverse_stereo( reverse ) => self
Causes a channel to reverse its stereo. This is handy if the user has his
speakers hooked up backwards, or you would like to have a minor bit of
psychedelia in your sound code. :) Calling this function with @reverse
set to true reverses the chunks's usual channels. If @reverse is false,
the effect is unregistered.
*/
static VALUE channel_reverse_stereo(VALUE self, VALUE reverse)
{
SDL_VERIFY(Mix_SetReverseStereo(retrieveChannelNumber(self), NUM2BOOL(reverse)));
return self;
}
/////////////// SOUND
Mix_Chunk* retrieveMixChunk(VALUE self)
{
Mix_Chunk* chunk;
Data_Get_Struct(self, Mix_Chunk, chunk);
RUDL_ASSERT(chunk, "Retrieved NULL chunk");
return chunk;
}
/**
@class Sound
Sound is a single sample.
It is loaded from a WAV file.
*/
/**@section Initializers
@method new
@method new( filename )
@method load_new( filename )
@section Initializers
Creates a new @Sound object with the sound in file @filename.
*/
static VALUE sound_new(VALUE self, VALUE filename)
{
Mix_Chunk* chunk;
VALUE newObject;
initAudio();
chunk=Mix_LoadWAV(STR2CSTR(filename));
SDL_VERIFY(chunk);
newObject=Data_Wrap_Struct(classSound, 0, SDL_FreeWAV, chunk);
RUDL_VERIFY(newObject, "Sound.new misbehaved");
rb_obj_call_init(newObject, 0, NULL);
return newObject;
}
/**
@method import( sampledata )
This method imports raw sampledata.
If it is not in the Mixer's format, use @Mixer@convert first.
*/
static VALUE sound_import(VALUE self, VALUE sampledata)
{
Mix_Chunk* chunk=(Mix_Chunk *)malloc(sizeof(Mix_Chunk));
VALUE newObject;
initAudio();
Check_Type(sampledata, T_STRING);
chunk->allocated = 0;
chunk->alen = RSTRING(sampledata)->len;
chunk->abuf = malloc(chunk->alen);
chunk->volume = MIX_MAX_VOLUME;
memcpy(chunk->abuf, RSTRING(sampledata)->ptr, chunk->alen);
newObject=Data_Wrap_Struct(classSound, 0, SDL_FreeWAV, chunk);
RUDL_VERIFY(newObject, "Sound.load_raw misbehaved");
rb_obj_call_init(newObject, 0, NULL);
return newObject;
}
/* NOT IMPLEMENTED
@method convert( sample, source_format, destination_format )
Returns a string with the string @sample with sampledata in it,
assumed to be in @source_format,
converted to the @destination_format.
A format is an array with these contents: [frequency, format (like AUDIO_S8), channels].
*/
static __inline__ void unpack_audio_format_array(VALUE array, int* rate, Uint16* format, int* channels)
{
*rate=NUM2INT(rb_ary_entry(array, 0));
*format=NUM2Uint16(rb_ary_entry(array, 1));
*channels=NUM2INT(rb_ary_entry(array, 2));
DEBUG_I(*format);
DEBUG_I(*channels);
DEBUG_I(*rate);
}
static __inline__ VALUE pack_audio_format_array(int rate, Uint16 format, int channels)
{
return rb_ary_new3(3, INT2NUM(rate), INT2NUM(format), INT2NUM(channels));
}
static VALUE sound_convert(int argc, VALUE* argv, VALUE self)
{
int dest_rate;
Uint16 dest_format;
int dest_channels;
Uint8* dest_memory;
int dest_length;
int src_rate;
Uint16 src_format;
int src_channels;
VALUE src;
Uint8* src_memory;
int src_length;
VALUE src_array, dest_array;
switch(rb_scan_args(argc, argv, "21", &src, &src_array, &dest_array)){
case 2: {
initAudio();
dest_array=mixer_get_format(0);
break;
}
}
src_memory=RSTRING(src)->ptr;
src_length=RSTRING(src)->len;
unpack_audio_format_array(src_array, &src_rate, &src_format, &src_channels);
unpack_audio_format_array(dest_array, &dest_rate, &dest_format, &dest_channels);
// call converter
SDL_VERIFY( rudl_convert_audio(
src_memory, src_length,
&dest_memory, &dest_length,
src_format, src_channels, src_rate,
dest_format, dest_channels, dest_rate) !=-1);
// wrap result in a string
return rb_str_new(dest_memory, dest_length);
}
/* NOT IMPLEMENTED
@method mix( destination, source )
@method mix( destination, source, max_volume )
*/
/*
__inline__ char* sound_pad(VALUE input, int to_length)
{
char* new_sound=(char*)calloc(to_length, sizeof(char));
memcpy(new_sound, RSTRING(input)->ptr, RSTRING(input)->len);
return new_sound;
}
static VALUE sound_mix(int argc, VALUE* argv, VALUE self)
{
VALUE v_data1, v_data2, v_volume;
double volume=1.0;
char *data1, *data2;
switch(rb_scan_args(argc, argv, "21", &v_data1, &v_data2, &v_volume)){
case 3: volume=NUM2DBL(v_volume);
}
Check_Type(v_data1, T_STRING);
Check_Type(v_data2, T_STRING);
*data1=RSTRING(v_data1)->len;
*data2=RSTRING(v_data2)->len;
if(RSTRING(v_data1)->len > RSTRING(v_data2)->len){
data2=sound_pad(v_data2, RSTRING(v_data1)->len);
}
if(RSTRING(v_data1)->len < RSTRING(v_data2)->len){
data1=sound_pad(v_data1, RSTRING(v_data2)->len);
}
Kopieer sdl_mixer.c
SDL_MixAudio(data, Uint32 len, int volume);
}
*/
/**
@method String.to_sound
Creates a new @RUDL::Sound object with the sound (in .WAV format) in the string.
*/
static VALUE string_to_sound(VALUE self)
{
Mix_Chunk* chunk;
VALUE newObject;
SDL_RWops* rwops;
initAudio();
rwops=SDL_RWFromMem(RSTRING(self)->ptr, RSTRING(self)->len);
chunk=Mix_LoadWAV_RW(rwops, 0);
SDL_FreeRW(rwops);
SDL_VERIFY(chunk);
newObject=Data_Wrap_Struct(classSound, 0, SDL_FreeWAV, chunk);
RUDL_VERIFY(newObject, "String.to_sound misbehaved");
rb_obj_call_init(newObject, 0, NULL);
return newObject;
}
/**
@section Volume methods
@method fade_out( milliseconds )
@method volume
@method volume=( loudness )
These are volume methods.
*/
/**
@fade_out fades all instances of this sound that are playing to silence in @milliseconds milliseconds.
@volume returns the current volume.
@volume= sets the volume to loudness.
Volumes range from 0.0 to 1.0.
*/
static VALUE sound_fade_out(VALUE self, VALUE time)
{
Mix_FadeOutGroup((int)retrieveMixChunk(self), NUM2UINT(time));
return self;
}
static VALUE sound_get_volume(VALUE self)
{
return DBL2NUM((Mix_VolumeChunk(retrieveMixChunk(self), -1))/128.0);
}
static VALUE sound_set_volume(VALUE self, VALUE volume)
{
Mix_VolumeChunk(retrieveMixChunk(self), (int)(NUM2DBL(volume)*128));
return self;
}
/**
@method play( loops, maxtime ) => Channel or nil
Starts playing a song on an available channel.
If no channels are available, it will not play and return @nil.
@Loops controls how many extra times the sound will play, a negative loop will play
indefinitely, it defaults to 0.
@Maxtime is the number of total milliseconds that the sound will play.
It defaults to forever (-1).
Returns a channel object for the channel that is selected to play the sound.
Returns nil if for some reason no channel is found.
*/
static VALUE sound_play(int argc, VALUE* argv, VALUE self)
{
VALUE loopsValue, timeValue;
int loops=0;
int time=-1;
int channelnum=-1;
Mix_Chunk* chunk=retrieveMixChunk(self);
switch(rb_scan_args(argc, argv, "02", &loopsValue, &timeValue)){
case 2: time=NUM2INT(timeValue);
case 1: loops=NUM2INT(loopsValue);
}
channelnum = Mix_PlayChannelTimed(-1, chunk, loops, time);
if(channelnum == -1) return Qnil;
//make sure volume on this arbitrary channel is set to full
Mix_Volume(channelnum, 128);
Mix_GroupChannel(channelnum, (int)chunk);
playing_wave[channelnum]=self; // to avoid gc problem
return createChannelObject(channelnum);
}
/**
@method stop => self
Stops all channels playing this @Sound.
*/
static VALUE sound_stop(VALUE self)
{
Mix_HaltGroup((int)retrieveMixChunk(self));
return self;
}
static VALUE sound_get_num_channels(VALUE self)
{
return UINT2NUM(Mix_GroupCount((int)retrieveMixChunk(self)));
}
/**
@method to_s => String
Returns a string with the sampledata.
*/
static VALUE sound_export(VALUE self)
{
Mix_Chunk* chunk=retrieveMixChunk(self);
return rb_str_new(chunk->abuf, chunk->alen);
}
/**
@method format
Returns the format of the @Sound, which is always the same as the Mixer's format.
See @Mixer.format
*/
// This method is actually an alias for Mixer.format.
/////////////// MIXER
/**
@class Mixer
Mixer is the main sound class.
All methods are available as class and instance methods.
*/
/**
@section Initializers
@method new => Mixer
@method new( frequency ) => Mixer
@method new( frequency, format ) => Mixer
@method new( frequency, format, stereo ) => Mixer
@method new( frequency, format, stereo, buffersize ) => Mixer
Initializes the sound system.
This call is not neccesary, the mixer will call @Mixer.new when it is needed.
When you disagree with the defaults (at the time of writing: 16 bit, 22kHz, stereo, 4096)
you can set them yourself, but do this <em>before</em> using any sound related method!
<ul>
<li>@frequency is a number like 11025, 22050 or 44100.
<li>@format is AUDIO_S8 for 8 bit samples or AUDIO_S16SYS for 16 bit samples.
Other possibilities are listed at the bottom of this page.
<li>@channels is 1 (mono) or 2 (stereo.)
<li>@buffersize is how many samples are calculated in one go.
4096 by default.
Set higher if your sound stutters.
</ul>
@Mixer.new will return a @Mixer object, but if you don't want it, you can
discard it and use class methods instead.
*/
static VALUE mixer_initialize(int argc, VALUE* argv, VALUE self)
{
int frequency = MIX_DEFAULT_FREQUENCY;
Uint16 format = MIX_DEFAULT_FORMAT;
int channels = MIX_DEFAULT_CHANNELS;
int buffersize=4096;
VALUE frequencyValue, sizeValue, stereoValue, bufferValue;
switch(rb_scan_args(argc, argv, "04", &frequencyValue, &sizeValue, &stereoValue, &bufferValue)){
case 4: buffersize=NUM2INT(bufferValue);
case 3: channels=NUM2INT(stereoValue);
case 2: format=NUM2Uint16(sizeValue);
case 1: frequency=NUM2INT(frequencyValue);
}
initSDL();
if(!SDL_WasInit(SDL_INIT_AUDIO)){
DEBUG_S("Starting audio subsystem");
SDL_VERIFY(!SDL_InitSubSystem(SDL_INIT_AUDIO));
}
if(Mix_OpenAudio(frequency, format, channels, buffersize) == -1){
DEBUG_S("Stopping audio subsystem");
SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_RAISE;
}
return self;
}
/**
@method destroy => self
(was Mixer.quit)
Uninitializes the Mixer.
If you really want to do this, then be warned that all Music objects will be destroyed too!
*/
static VALUE mixer_destroy(VALUE self)
{
quitAudio();
return self;
}
/**
@section Methods
@method fade_out( millisecs )
Fades away all sound to silence in millisecs milliseconds.
*/
static VALUE mixer_fade_out(VALUE self, VALUE time)
{
initAudio();
Mix_FadeOutChannel(-1, NUM2INT(time));
return self;
}
/**
@method find_free_channel => Channel or nil
@method find_oldest_channel => Channel or nil
Both functions search for a channel that can be used to play new sounds on.
@find_free_channel finds a channel that is not playing anything and returns nil
if there is no such channel.
@find_oldest_channel finds the channel that has been playing for the longest time.
*/
static VALUE mixer_find_free_channel(VALUE self)
{
int channel;
initAudio();
channel=Mix_GroupAvailable(-1);
if(channel==-1){
return Qnil;
}
return createChannelObject(channel);
}
static VALUE mixer_find_oldest_channel(VALUE self)
{
initAudio();
return createChannelObject(Mix_GroupOldest(-1));
}
/**
@method busy? => boolean
Returns the number of current active channels.
This is not the total channels,
but the number of channels that are currently playing sound.
*/
static VALUE mixer_get_busy(VALUE self)
{
if(!SDL_WasInit(SDL_INIT_AUDIO)){
return INT2NUM(0);
}
return INT2NUM(Mix_Playing(-1));
}
/*
@method num_channels => number_of_channels
@method num_channels=( amount ) => number_of_channels
Gets or sets the current number of channels available for the mixer.
This value defaults to 8 when the mixer is first initialized.
RUDL imposes a channel limit of 256.
These channels are not the same thing as in @Mixer.init.
*/
static VALUE mixer_get_num_channels(VALUE self)
{
initAudio();
return INT2NUM(Mix_GroupCount(-1));
}
static VALUE mixer_set_num_channels(VALUE self, VALUE channelsValue)
{
int channels=NUM2INT(channelsValue);
initAudio();
RUDL_VERIFY(channels<=MAX_CHANNELS, "256 channels ought to be enough for anyone");
Mix_AllocateChannels(channels);
return self;
}
/**
@section Mass Channel Operations
These work on all channels at once.
*/
/**
@method pause
@method unpause
@method stop
@section Methods
*/
static VALUE mixer_pause(VALUE self)
{
initAudio();
Mix_Pause(-1);
return self;
}
static VALUE mixer_stop(VALUE self)
{
initAudio();
Mix_HaltChannel(-1);
return self;
}
static VALUE mixer_unpause(VALUE self)
{
initAudio();
Mix_Resume(-1);
return self;
}
/**
@method reserved=( amount ) => self
This sets aside the first ((|amount|)) channels.
They can only be played on by directly creating a Channel object and playing on it.
No other function will take these channels for playing.
*/
static VALUE mixer_set_reserved(VALUE self, VALUE amount)
{
initAudio();
Mix_ReserveChannels(NUM2INT(amount));
return self;
}
/**
@method driver => String
Returns the name of the driver doing the sound output.
*/
static VALUE mixer_driver(VALUE self)
{
int bufsize = 1024;
char* name = ALLOCA_N(char, bufsize);
char* result;
initAudio();
result = SDL_AudioDriverName(name, bufsize);
RUDL_ASSERT(result, "Audio not initialized yet");
return CSTR2STR(name);
}
/**
@method format
These get the parameters that Mixer is playing at.
It returns an array of [format (like AUDIO_S8), channels, frequency].
See also @Mixer@init.
*/
static VALUE mixer_get_format(VALUE self)
{
int rate;
Uint16 format;
int channels;
SDL_VERIFY( Mix_QuerySpec(&rate, &format, &channels) ==1);
return pack_audio_format_array(rate, format, channels);
}
/////////////// MUSIC
static bool endmusic_event=false;
static void endmusic_callback(void)
{
if(endmusic_event && SDL_WasInit(SDL_INIT_VIDEO)){
SDL_Event e;
memset(&e, 0, sizeof(e));
e.type = RUDL_ENDMUSICEVENT;
SDL_PushEvent(&e);
}
}
Mix_Music* retrieveMusicPointer(VALUE self)
{
Mix_Music* music;
Data_Get_Struct(self, Mix_Music, music);
return music;
}
void freemusic(Mix_Music* m)
{
if(m){
Mix_FreeMusic(m); // This is called multiple times, but it seems to work :)
}
}
/**
@class Music
This encapsulates a song.
For some reason, SDL will only play one song at a time, and even though most of the
Music methods are instance methods, some will act on the playing Music only.
*/
/**
@section Initializers
@method new( filename )
Creates a new Music object from a MOD, XM, MIDI, MP3 or OGG file (I think.)
*/
static VALUE music_new(VALUE self, VALUE filename)
{
VALUE newObject;
initAudio();
newObject=Data_Wrap_Struct(classMusic, 0, freemusic, Mix_LoadMUS(STR2CSTR(filename)));
rb_obj_call_init(newObject, 0, NULL);
return newObject;
}
/**
@section Volume methods
These methods work on the currently playing music.
*/
/**
@method volume => Number
@method volume=( loudness ) => self
volume and volume= get and set the volume.
Volume ranges from 0.0 to 1.0.
*/
/**
@method fade_out( milliseconds )
fades out <em>the currently playing music</em> to silence in @milliseconds.
*/
static VALUE music_fade_out(VALUE self, VALUE time)
{
Mix_FadeOutMusic(NUM2INT(time));
return self;
}
static VALUE music_get_volume(VALUE self)
{
return DBL2NUM(Mix_VolumeMusic(-1)/127.0);
}
static VALUE music_set_volume(VALUE self, VALUE volume)
{
Mix_VolumeMusic((int)(NUM2DBL(volume)*127));
return self;
}
/**
@section Play control
All pretty straight forward, except that they all act on the playing music, not the current one.
*/
/**
@method play
@method play( loops )
Plays this piece of music, stopping the previously playing one.
Plays the music one time, or @loops + 1 times if you pass @loops.
*/
static VALUE music_play(int argc, VALUE* argv, VALUE self)
{
int loops = 0;
VALUE loopsValue;
switch(rb_scan_args(argc, argv, "01", &loopsValue)){
case 1: loops=NUM2INT(loopsValue);
}
Mix_HookMusicFinished(endmusic_callback);
playing_music=self; /* to avoid gc problem */
SDL_VERIFY(!(Mix_PlayMusic(retrieveMusicPointer(self), loops)==-1));
return self;
}
/**
@method stop
@method pause
@method unpause
@method busy?
@method restart
*/
static VALUE music_stop(VALUE self)
{
Mix_HaltMusic();
return self;
}
static VALUE music_pause(VALUE self)
{
Mix_PauseMusic();
return self;
}
static VALUE music_unpause(VALUE self)
{
Mix_ResumeMusic();
return self;
}
static VALUE music_get_busy(VALUE self)
{
return INT2BOOL(Mix_PlayingMusic());
}
static VALUE music_restart(VALUE self)
{
Mix_RewindMusic();
return self;
}
/**
@section Methods
@method post_end_event => boolean
@method post_end_event=( on_or_off ) => self
Returns, or sets whether an @EndOfMusicEvent will be posted when the current music stops playing.
@on_or_off is true or false.
*/
static VALUE music_set_post_end_event(VALUE self, VALUE onOff)
{
endmusic_event=NUM2BOOL(onOff);
return self;
}
static VALUE music_get_post_end_event(VALUE self)
{
return INT2BOOL(endmusic_event);
}
/**
@section Initializers
@method destroy
Frees the music from memory,
thereby rendering the music instance useless.
Ruby will crash if you use it anyway.
*/
static VALUE music_destroy(VALUE self)
{
freemusic(retrieveMusicPointer(self));
DATA_PTR(self)=NULL;
return self;
}
#endif
/////////////// INIT
void initAudioClasses()
{
#ifdef HAVE_SDL_MIXER_H
classChannel=rb_define_class_under(moduleRUDL, "Channel", rb_cObject);
//rb_define_singleton_method(classChannel, "new", channel_new, 1);
rb_eval_string(
"module RUDL class Channel \n"
" def initialize(nr) \n"