/t/mixer_music.t

http://github.com/PerlGameDev/SDL · Perl · 223 lines · 174 code · 46 blank · 3 comment · 8 complexity · 53ac28949d7d6d501a2c97db459d1040 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. my $audiodriver;
  5. BEGIN {
  6. use Config;
  7. if ( !$Config{'useithreads'} ) {
  8. print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
  9. exit(0);
  10. }
  11. use SDL;
  12. use SDL::Config;
  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::Music;
  28. use SDL::Mixer::Samples;
  29. use SDL::RWOps;
  30. use SDL::Version;
  31. my $v = SDL::Mixer::linked_version();
  32. is( SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 ),
  33. 0, '[open_audio] ran'
  34. );
  35. my $finished : shared = 0;
  36. my $mix_func_called : shared = 0;
  37. sub mix_func {
  38. my $position = shift; # position
  39. my $length = shift; # length of bytes we have to put in stream
  40. my @stream = '';
  41. $mix_func_called++;
  42. # printf("[hook_music] callback: position=%8s, stream length=%6s\n", $position, $length);
  43. for ( my $i = 0; $i < $length; $i++ ) {
  44. push( @stream, ( ( $i + $position ) & 0xFF ) );
  45. }
  46. return @stream;
  47. }
  48. my $delay = 100;
  49. my $audio_test_file = 'test/data/silence.wav';
  50. my $volume1 = 2;
  51. my $volume2 = 1;
  52. if ( $ENV{'SDL_RELEASE_TESTING'} ) {
  53. $delay = 2000;
  54. $audio_test_file = 'test/data/sample.wav';
  55. $volume1 = 20;
  56. $volume2 = 10;
  57. }
  58. SDL::Mixer::Music::volume_music($volume1);
  59. is( SDL::Mixer::Music::volume_music($volume2),
  60. $volume1, "[volume_music] was $volume1, now set to $volume2"
  61. );
  62. sub callback {
  63. # printf("[hook_music_finished] callback called\n", shift);
  64. $finished++;
  65. }
  66. SKIP:
  67. {
  68. skip( 'No sound unless SDL_RELEASE_TESTING', 5 )
  69. unless $ENV{'SDL_RELEASE_TESTING'};
  70. SDL::Mixer::Music::hook_music_finished('main::callback');
  71. pass '[hook_music_finished] registered callback';
  72. SDL::Mixer::Music::hook_music( 'main::mix_func', 0 );
  73. pass '[hook_music] registered custom music player';
  74. is( SDL::Mixer::Music::get_music_hook_data(),
  75. 0, "[get_music_hook_data] should return 0"
  76. );
  77. SDL::delay(1000);
  78. SDL::Mixer::Music::hook_music();
  79. pass '[hook_music] unregistered custom music player';
  80. SDL::delay($delay);
  81. is( $mix_func_called > 0, 1, "[hook_music] called $mix_func_called times" );
  82. }
  83. my $sample_music = SDL::Mixer::Music::load_MUS($audio_test_file);
  84. isa_ok( $sample_music, 'SDL::Mixer::MixMusic', '[load_MUS]' );
  85. is( SDL::Mixer::Music::play_music( $sample_music, 0 ),
  86. 0, "[play_music] plays $audio_test_file"
  87. );
  88. SKIP:
  89. {
  90. skip( 'Version 1.2.7 needed', 2 ) if $v < 1.2.7;
  91. my $rw = SDL::RWOps->new_file( $audio_test_file, "rb" );
  92. my $sample_music_rw = SDL::Mixer::Music::load_MUS_RW( $rw );
  93. isa_ok( $sample_music_rw, 'SDL::Mixer::MixMusic', '[load_MUS_RW]' );
  94. is( SDL::Mixer::Music::play_music( $sample_music_rw, 0 ),
  95. 0, "[play_music_rw] plays $audio_test_file"
  96. );
  97. }
  98. SKIP:
  99. {
  100. skip( 'Version 1.2.9 needed', 2 ) if $v < 1.2.9;
  101. my $num_decoders = SDL::Mixer::Music::get_num_music_decoders();
  102. is( $num_decoders >= 0,
  103. 1, "[get_num_music_decoders] $num_decoders decoders available"
  104. );
  105. my $decoder = SDL::Mixer::Music::get_music_decoder(0);
  106. isnt( $decoder, undef, "[get_music_decoder] got $decoder" );
  107. }
  108. SDL::delay($delay);
  109. is( SDL::Mixer::Music::playing_music(), 1, "[playing_music] music is playing" );
  110. is( SDL::Mixer::Music::get_music_type($sample_music),
  111. MUS_WAV, "[get_music_type] $audio_test_file is MUS_WAV"
  112. );
  113. is( SDL::Mixer::Music::get_music_type(),
  114. MUS_WAV, "[get_music_type] currently playing MUS_WAV"
  115. );
  116. SDL::delay($delay);
  117. is( SDL::Mixer::Music::pause_music(), undef, "[pause_music] ran" );
  118. is( SDL::Mixer::Music::paused_music(), 1, "[paused_music] music is paused" );
  119. SDL::delay($delay);
  120. is( SDL::Mixer::Music::resume_music(), undef, "[resume_music] ran" );
  121. is( SDL::Mixer::Music::playing_music(), 1, "[paused_music] music is playing" );
  122. is( SDL::Mixer::Music::fading_music(),
  123. MIX_NO_FADING, "[fading_music] music is not fading"
  124. );
  125. is( SDL::Mixer::Music::rewind_music(), undef, "[rewind_music] ran" );
  126. SDL::delay($delay);
  127. is( SDL::Mixer::Music::fade_out_music(2000), 1, "[fade_out_music] $delay ms" );
  128. is( SDL::Mixer::Music::fading_music(),
  129. MIX_FADING_OUT, "[fading_music] music is fading out"
  130. );
  131. SDL::delay(3000);
  132. is( SDL::Mixer::Music::halt_music(), 0, '[halt_music]' );
  133. is( SDL::Mixer::Music::set_music_cmd("mpeg123 -q"),
  134. 0, '[set_music_cmd] we can specify an external player'
  135. );
  136. is( SDL::Mixer::Music::set_music_cmd(),
  137. 0, '[set_music_cmd] return to the internal player'
  138. );
  139. is( SDL::Mixer::Music::fade_in_music( $sample_music, 0, 2000 ),
  140. 0, "[fade_in_music] $delay ms"
  141. );
  142. SDL::delay(100);
  143. is( SDL::Mixer::Music::fading_music(),
  144. MIX_FADING_IN, "[fading_music] music is fading in"
  145. );
  146. is( SDL::Mixer::Music::halt_music(), 0, '[halt_music]' );
  147. SKIP:
  148. {
  149. skip( 'We need an MOD/OGG/MP3 for positioning', 2 )
  150. unless $audio_test_file =~ /\.(ogg|mod|mp3)$/;
  151. is( SDL::Mixer::Music::fade_in_music_pos( $sample_music, 0, 2000, 2.5 ),
  152. 0, "[fade_in_music_pos] $delay ms, beginning at 2.5 ms"
  153. );
  154. is( SDL::Mixer::Music::set_music_position(2.5),
  155. 0, "[set_music_position] skipping 2.5 ms"
  156. );
  157. SDL::Mixer::Music::halt_music();
  158. }
  159. SKIP:
  160. {
  161. skip( 'No sound unless SDL_RELEASE_TESTING', 2 )
  162. unless $ENV{'SDL_RELEASE_TESTING'};
  163. is( $finished > 0,
  164. 1, "[hook_music_finished] called the callback $finished times"
  165. );
  166. SDL::Mixer::Music::hook_music_finished();
  167. pass '[hook_music_finished] unregistered callback';
  168. }
  169. SDL::delay($delay);
  170. SDL::Mixer::close_audio();
  171. pass '[close_audio] ran';
  172. if ($audiodriver) {
  173. $ENV{SDL_AUDIODRIVER} = $audiodriver;
  174. } else {
  175. delete $ENV{SDL_AUDIODRIVER};
  176. }
  177. done_testing();