/t/mixer_channels.t

http://github.com/PerlGameDev/SDL · Perl · 213 lines · 172 code · 40 blank · 1 comment · 7 complexity · c625a943de8fcefa34b5948a50a95b88 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use SDL;
  5. use SDL::Config;
  6. my $audiodriver;
  7. BEGIN {
  8. use Config;
  9. if ( !$Config{'useithreads'} ) {
  10. print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
  11. exit(0);
  12. }
  13. require threads;
  14. require threads::shared;
  15. use Test::More;
  16. use lib 't/lib';
  17. use SDL::TestTool;
  18. $audiodriver = $ENV{SDL_AUDIODRIVER};
  19. $ENV{SDL_AUDIODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
  20. if ( !SDL::TestTool->init(SDL_INIT_AUDIO) ) {
  21. plan( skip_all => 'Failed to init sound' );
  22. } elsif ( !SDL::Config->has('SDL_mixer') ) {
  23. plan( skip_all => 'SDL_mixer support not compiled' );
  24. }
  25. }
  26. use SDL::Mixer;
  27. use SDL::Mixer::Channels;
  28. use SDL::Mixer::Samples;
  29. use SDL::Mixer::MixChunk;
  30. my $can_open = SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 );
  31. unless($can_open == 0)
  32. {
  33. plan( skip_all => 'Cannot open audio :'.SDL::get_error() );
  34. }
  35. is( $can_open ,
  36. 0, '[open_audio] ran'
  37. );
  38. is( SDL::Mixer::Channels::allocate_channels(4),
  39. 4, "[allocate_channels] 4 channels allocated"
  40. );
  41. my $finished :shared = 0;
  42. my $callback = sub {
  43. my ($channel) = shift;
  44. printf( "[channel_finished] callback called for channel %d\n", $channel);
  45. $finished++;
  46. };
  47. SKIP:
  48. {
  49. skip( 'No callbacks unless SDL_RELEASE_TESTING', 1 )
  50. unless $ENV{'SDL_RELEASE_TESTING'};
  51. SDL::Mixer::Channels::channel_finished($callback);
  52. pass '[channel_finished] registered callback';
  53. }
  54. my $delay = 500;
  55. my $audio_test_file = 'test/data/silence.wav';
  56. if ( $ENV{'SDL_RELEASE_TESTING'} ) {
  57. SDL::Mixer::Channels::volume( -1, 10 );
  58. is( SDL::Mixer::Channels::volume( -1, 20 ),
  59. 10, "[volume] set to 20, previously was 10"
  60. );
  61. $delay = 2000;
  62. $audio_test_file = 'test/data/sample.wav';
  63. } else {
  64. SDL::Mixer::Channels::volume( -1, 10 );
  65. is( SDL::Mixer::Channels::volume( -1, 1 ),
  66. 10, "[volume] set to 1, previously was 10"
  67. );
  68. }
  69. my $sample_chunk = SDL::Mixer::Samples::load_WAV($audio_test_file);
  70. my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
  71. isnt(
  72. $playing_channel, -1,
  73. "[play_channel] plays $audio_test_file on channel " . $playing_channel
  74. );
  75. is( SDL::Mixer::Channels::fading_channel($playing_channel),
  76. MIX_NO_FADING, "[fading_channel] channel $playing_channel is not fading"
  77. );
  78. is( SDL::Mixer::Channels::playing($playing_channel),
  79. 1, "[playing] channel $playing_channel is playing"
  80. );
  81. is( SDL::Mixer::Channels::paused($playing_channel),
  82. 0, "[paused] channel $playing_channel is not paused"
  83. );
  84. ok( $delay, 'delay definedness madness test #1' );
  85. my $fading_channels = SDL::Mixer::Channels::fade_out_channel( $playing_channel, $delay );
  86. is( $fading_channels > 0,
  87. 1, "[fade_out_channel] $delay ms for $fading_channels channel(s)"
  88. );
  89. is( SDL::Mixer::Channels::fading_channel($playing_channel),
  90. MIX_FADING_OUT, "[fading_channel] channel $playing_channel is fading out"
  91. );
  92. ok( $delay, 'delay definedness madness test #2' );
  93. SDL::delay($delay);
  94. ok( $delay, 'delay definedness madness test #3' );
  95. $playing_channel = SDL::Mixer::Channels::fade_in_channel( -1, $sample_chunk, 0, $delay );
  96. ok( $delay, 'delay definedness madness test #4' );
  97. isnt(
  98. $playing_channel, -1,
  99. "[fade_in_channel] $delay ms for channel $playing_channel"
  100. );
  101. is( SDL::Mixer::Channels::fading_channel($playing_channel),
  102. MIX_FADING_IN, "[fading_channel] channel $playing_channel is fading in"
  103. );
  104. ok( $delay, 'delay definedness madness test #5' );
  105. SDL::delay($delay);
  106. ok( $delay, 'delay definedness madness test #6' );
  107. SDL::Mixer::Channels::pause(-1);
  108. pass '[pause] ran';
  109. is( SDL::Mixer::Channels::paused($playing_channel),
  110. 1, "[paused] channel $playing_channel is paused"
  111. );
  112. SDL::delay( $delay / 4 );
  113. ok( $delay, 'delay definedness madness test #7' );
  114. SDL::Mixer::Channels::resume(-1);
  115. pass '[resume] ran';
  116. SDL::delay($delay);
  117. ok( $delay, 'delay definedness madness test #8' );
  118. is( SDL::Mixer::Channels::halt_channel($playing_channel),
  119. 0, "[halt_channel] stop channel $playing_channel"
  120. );
  121. is( SDL::Mixer::Channels::playing($playing_channel),
  122. 0, "[playing] channel $playing_channel is not playing"
  123. );
  124. SDL::delay($delay);
  125. ok( $delay, 'delay definedness madness test #9' );
  126. $playing_channel = SDL::Mixer::Channels::play_channel_timed( -1, $sample_chunk, 0, $delay );
  127. ok( $delay, 'delay definedness madness test #10' );
  128. isnt(
  129. $playing_channel, -1,
  130. "[play_channel_timed] play $delay ms for channel $playing_channel"
  131. );
  132. SDL::delay( $delay / 4 );
  133. ok( $delay, 'delay definedness madness test #11' );
  134. my $expire_channel = SDL::Mixer::Channels::expire_channel( $playing_channel, $delay );
  135. ok( $delay, 'delay definedness madness test #12' );
  136. is( $expire_channel > 0,
  137. 1,
  138. "[expire_channel] stops after $delay ms for $expire_channel channel(s)"
  139. );
  140. SDL::delay($delay);
  141. ok( $delay, 'delay definedness madness test #13' );
  142. $playing_channel = SDL::Mixer::Channels::fade_in_channel_timed(
  143. -1, $sample_chunk, 0, $delay,
  144. $delay * 2
  145. );
  146. ok( $delay, 'delay definedness madness test #14' );
  147. isnt(
  148. $playing_channel, -1,
  149. "[fade_in_channel_timed] play " . ( $delay * 2 ) . " ms after $delay ms fade in for channel $playing_channel"
  150. );
  151. isa_ok(
  152. SDL::Mixer::Channels::get_chunk($playing_channel),
  153. 'SDL::Mixer::MixChunk', '[get_chunk]'
  154. );
  155. SDL::delay(1000);
  156. ok( $delay, 'delay definedness madness test #15' );
  157. SDL::Mixer::close_audio();
  158. pass '[close_audio] ran';
  159. SKIP:
  160. {
  161. skip( 'No callbacks unless SDL_RELEASE_TESTING', 1 )
  162. unless $ENV{'SDL_RELEASE_TESTING'};
  163. is( $finished > 0,
  164. 1, '[callback_finished] called the callback got ' . $finished
  165. );
  166. }
  167. if ($audiodriver) {
  168. $ENV{SDL_AUDIODRIVER} = $audiodriver;
  169. } else {
  170. delete $ENV{SDL_AUDIODRIVER};
  171. }
  172. sleep(1);
  173. done_testing();