Skip to content
This repository
Browse code

more subs implemented and tested, callbacks are working

  • Loading branch information...
commit fc83c1294d15816c6e00651d0607c973080af998 1 parent 1770630
Tobias Leich FROGGS authored

Showing 2 changed files with 143 additions and 25 deletions. Show diff stats Hide diff stats

  1. +9 7 lib/SDL/Mixer/Channels.pm6
  2. +134 18 t/mixer-channels.t
16 lib/SDL/Mixer/Channels.pm6
@@ -3,12 +3,14 @@ module SDL::Mixer::Channels;
3 3
4 4 use NativeCall;
5 5
6   -our sub volume( int, int ) returns Int is native('libSDL_mixer') is symbol('Mix_Volume') { * }
7   -our sub allocate_channels( int ) returns Int is native('libSDL_mixer') is symbol('Mix_AllocateChannels') { * }
8   -our sub channel_finished( &callback(int) ) returns Int is native('libSDL_mixer') is symbol('Mix_ChannelFinished') { * }
9   -our sub play_channel_timed( int, OpaquePointer, int, int ) returns Int is native('libSDL_mixer') is symbol('Mix_PlayChannelTimed') { * }
10   -our sub play_channel( int $channel, OpaquePointer $sample, int $loops ) returns Int {
11   - return play_channel_timed( $channel, $sample, $loops, -1 );
12   -}
  6 +our sub volume( int, int ) returns Int is native('libSDL_mixer') is symbol('Mix_Volume') { * }
  7 +our sub allocate( int ) returns Int is native('libSDL_mixer') is symbol('Mix_AllocateChannels') { * }
  8 +our sub finished( Code &callback(int) ) returns Int is native('libSDL_mixer') is symbol('Mix_ChannelFinished') { * }
  9 +our sub play( $channel, $sample, $loops, $time = -1 ) returns Int { return _play( $channel, $sample, $loops, $time ); }
  10 +our sub _play( int, OpaquePointer, int, int ) returns Int is native('libSDL_mixer') is symbol('Mix_PlayChannelTimed') { * }
  11 +our sub halt( int ) returns Int is native('libSDL_mixer') is symbol('Mix_HaltChannel') { * }
  12 +our sub playing( int ) returns Int is native('libSDL_mixer') is symbol('Mix_Playing') { * }
  13 +our sub paused( int ) returns Int is native('libSDL_mixer') is symbol('Mix_Paused') { * }
  14 +our sub fading( int ) returns Int is native('libSDL_mixer') is symbol('Mix_FadingChannel') { * }
13 15
14 16 1;
152 t/mixer-channels.t
@@ -7,36 +7,152 @@ use SDL::Mixer::Channels;
7 7 use SDL::Mixer::Samples;
8 8 use Test;
9 9
10   -plan 7;
  10 +plan *;
11 11
12   -is SDL::init( 255 ), 0, "SDL::init";
  12 +# Setting up audio. You will hear nothing unless SDL_RELEASE_TESTING is set.
  13 +my $delay = 50;
  14 +my $audio_test_file = 'silence.wav';
  15 +my $volume = 1;
13 16
14   -is( SDL::Mixer::open_audio( 44100, 36880, 2, 4096 ), 0, '[open_audio] ran');
  17 +if %*ENV{'SDL_RELEASE_TESTING'} {
  18 + $delay = 300;
  19 + $audio_test_file = 'sample.wav';
  20 + $volume = 20;
  21 +}
  22 +
  23 +# Try to init audio. If it fails its not our fault, maybe the machine has no sound card.
  24 +if 0 != SDL::init( 16 ) {
  25 + skip_rest( 'Failed to initialize audio with reason: "' ~ SDL::get_error() ~ '"' );
  26 + exit;
  27 +}
15 28
16   -is( SDL::Mixer::Channels::allocate_channels(4), 4, "[allocate_channels] 4 channels allocated"
17   -);
  29 +# Basically the same
  30 +if 0 != SDL::Mixer::open_audio( 44100, 36880, 2, 4096 ) {
  31 + skip_rest( 'Failed to open audio: "' ~ SDL::get_error() ~ '"' );
  32 + exit;
  33 +}
18 34
19   -is SDL::Mixer::Channels::volume( -1, 20 ), 128, 'SDL::Mixer::Channels::volume';
20   -my $delay = 5000;
21   -my $audio_test_file = 'sample.wav';
  35 +# Hooray! So far...
  36 +pass '[open_audio] ran';
22 37
23   -my $sample_chunk = SDL::Mixer::Samples::load_WAV( $audio_test_file );
24   -isa_ok( $sample_chunk, 'OpaquePointer', 'SDL::Mixer::Samples::load_WAV' );
  38 +# We request 4 channels, but we will actually just need one.
  39 +is SDL::Mixer::Channels::allocate( 4 ), 4, '[allocate] allocated 4 channels';
  40 +
  41 +# Changing the volume.
  42 +is SDL::Mixer::Channels::volume( -1, 10 ), 128, '[volume] set to 10, previously was 128';
  43 +is SDL::Mixer::Channels::volume( -1, $volume ), 10, "[volume] set to $volume, previously was 10";
25 44
26   -# the following fucks up:
27   -#my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
  45 +# Loading audio file.
  46 +my $sample_chunk = SDL::Mixer::Samples::load_WAV( $audio_test_file );
  47 +isa_ok( $sample_chunk, 'OpaquePointer', "[load_WAV] loaded audio sample $audio_test_file" );
28 48
29   -# but this works:
30   -my $playing_channel = SDL::Mixer::Channels::play_channel_timed( -1, $sample_chunk, -1, -1 );
  49 +# Play that audio file.
  50 +my $playing_channel = SDL::Mixer::Channels::play( -1, $sample_chunk, -1, -1 );
  51 +ok $playing_channel >= 0, '[play] plays audio sample';
  52 +SDL::delay( $delay );
31 53
32   -ok $playing_channel >= 0, 'SDL::Mixer::Channels::play_channel';
  54 +# Request the current states.
  55 +is SDL::Mixer::Channels::playing( $playing_channel ), 1, "[playing] channel $playing_channel is playing";
  56 +is SDL::Mixer::Channels::paused( $playing_channel ), 0, "[paused] channel $playing_channel is not paused";
  57 +is SDL::Mixer::Channels::fading( $playing_channel ), 0, "[fading] channel $playing_channel is not fading";
33 58
34   -sub callback( int $channel ) {
35   - say "hello $channel"
  59 +# Preparing first callback test. NativeCall++, its pretty easy to do.
  60 +my $callback_finished = 0;
  61 +sub callback_finished( int $channel ) {
  62 + pass "[finished] called callback type: &callback for channel $channel";
  63 + $callback_finished++;
36 64 }
  65 +SDL::Mixer::Channels::finished( &callback_finished );
  66 +pass '[finished] registered callback type: &callback';
  67 +
  68 +# Stopping the music, the callback should be called then.
  69 +is SDL::Mixer::Channels::halt( $playing_channel ), 0, "[halt_channel] stopped channel $playing_channel";
  70 +is $callback_finished, 1, "[finished] callback was called once";
  71 +
  72 +# Checking states again.
  73 +is SDL::Mixer::Channels::playing( $playing_channel ), 0, "[playing] channel $playing_channel is not playing";
  74 +is SDL::Mixer::Channels::paused( $playing_channel ), 0, "[paused] channel $playing_channel is not paused";
  75 +is SDL::Mixer::Channels::fading( $playing_channel ), 0, "[fading] channel $playing_channel is not fading";
  76 +
  77 +# Playing and stopping again, the callback should get called again.
  78 +$playing_channel = SDL::Mixer::Channels::play( -1, $sample_chunk, -1, -1 );
  79 +SDL::delay( $delay );
  80 +is SDL::Mixer::Channels::halt( $playing_channel ), 0, "[halt_channel] stopped channel $playing_channel";
  81 +is $callback_finished, 2, "[finished] callback was called twice";
  82 +
  83 +# Unregistering callback and checking that it doesn't get called.
  84 +SDL::Mixer::Channels::finished( Code );
  85 +pass '[finished] unregistered callback type: &callback';
  86 +$playing_channel = SDL::Mixer::Channels::play( -1, $sample_chunk, -1, -1 );
  87 +SDL::delay( $delay );
  88 +is SDL::Mixer::Channels::halt( $playing_channel ), 0, "[halt_channel] stopped channel $playing_channel";
  89 +is $callback_finished, 2, "[finished] callback still was called twice";
  90 +
  91 +
  92 +
  93 +
  94 +
  95 +
  96 +#is( SDL::Mixer::Channels::fading_channel($playing_channel), MIX_NO_FADING, "[fading_channel] channel $playing_channel is not fading");
  97 +#is( SDL::Mixer::Channels::playing($playing_channel), 1, "[playing] channel $playing_channel is playing");
  98 +#is( SDL::Mixer::Channels::paused($playing_channel), 0, "[paused] channel $playing_channel is not paused");
  99 +
  100 +#my $fading_channels = SDL::Mixer::Channels::fade_out_channel( $playing_channel, $delay );
  101 +#is( $fading_channels > 0, 1, "[fade_out_channel] $delay ms for $fading_channels channel(s)");
  102 +#is( SDL::Mixer::Channels::fading_channel($playing_channel), MIX_FADING_OUT, "[fading_channel] channel $playing_channel is fading out");
  103 +
  104 +#SDL::delay($delay);
  105 +
  106 +#$playing_channel = SDL::Mixer::Channels::fade_in_channel( -1, $sample_chunk, 0, $delay );
  107 +
  108 +#isnt( $playing_channel, -1,"[fade_in_channel] $delay ms for channel $playing_channel");
  109 +#is( SDL::Mixer::Channels::fading_channel($playing_channel), MIX_FADING_IN, "[fading_channel] channel $playing_channel is fading in");
  110 +#SDL::delay($delay);
  111 +
  112 +#SDL::Mixer::Channels::pause(-1);
  113 +#pass '[pause] ran';
  114 +#is( SDL::Mixer::Channels::paused($playing_channel), 1, "[paused] channel $playing_channel is paused");
  115 +#SDL::delay( $delay / 4 );
  116 +
  117 +#SDL::Mixer::Channels::resume(-1);
  118 +#pass '[resume] ran';
  119 +
  120 +#SDL::delay($delay);
  121 +
  122 +#is( SDL::Mixer::Channels::halt_channel($playing_channel), 0, "[halt_channel] stop channel $playing_channel");
  123 +#is( SDL::Mixer::Channels::playing($playing_channel), 0, "[playing] channel $playing_channel is not playing");
  124 +
  125 +#SDL::delay($delay);
  126 +
  127 +#$playing_channel = SDL::Mixer::Channels::play_channel_timed( -1, $sample_chunk, 0, $delay );
  128 +
  129 +#isnt( $playing_channel, -1, "[play_channel_timed] play $delay ms for channel $playing_channel");
  130 +#SDL::delay( $delay / 4 );
  131 +
  132 +#my $expire_channel = SDL::Mixer::Channels::expire_channel( $playing_channel, $delay );
  133 +
  134 +#is( $expire_channel > 0, 1, "[expire_channel] stops after $delay ms for $expire_channel channel(s)");
  135 +
  136 +#SDL::delay($delay);
  137 +
  138 +#$playing_channel = SDL::Mixer::Channels::fade_in_channel_timed( -1, $sample_chunk, 0, $delay, $delay * 2);
  139 +
  140 +#isnt( $playing_channel, -1, "[fade_in_channel_timed] play " . ( $delay * 2 ) . " ms after $delay ms fade in for channel $playing_channel");
  141 +
  142 +#isa_ok(SDL::Mixer::Channels::get_chunk($playing_channel),'SDL::Mixer::MixChunk', '[get_chunk]');
  143 +#is( $finished > 0, 1, '[callback_finished] called the callback got ' . $finished);
  144 +
  145 +#sleep(1);
  146 +
  147 +
  148 +
  149 +
  150 +
  151 +
37 152
38   -SDL::Mixer::Channels::channel_finished( &callback );
39 153
40 154 SDL::delay( $delay );
41 155 SDL::Mixer::close_audio();
42 156 pass '[close_audio] ran';
  157 +
  158 +done;

0 comments on commit fc83c12

Please sign in to comment.
Something went wrong with that request. Please try again.