/t/mixer_groups.t

http://github.com/PerlGameDev/SDL · Perl · 132 lines · 112 code · 19 blank · 1 comment · 7 complexity · 5d705dde46e215d23e75f362d33ddf59 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. use Test::More;
  14. use lib 't/lib';
  15. use SDL::TestTool;
  16. $audiodriver = $ENV{SDL_AUDIODRIVER};
  17. $ENV{SDL_AUDIODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
  18. if ( !SDL::TestTool->init(SDL_INIT_AUDIO) ) {
  19. plan( skip_all => 'Failed to init sound' );
  20. } elsif ( !SDL::Config->has('SDL_mixer') ) {
  21. plan( skip_all => 'SDL_mixer support not compiled' );
  22. }
  23. }
  24. use SDL::Mixer;
  25. use SDL::Mixer::Channels;
  26. use SDL::Mixer::Groups;
  27. use SDL::Mixer::Samples;
  28. my $can_open = SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 );
  29. unless($can_open == 0)
  30. {
  31. plan( skip_all => 'Cannot open audio :'.SDL::get_error() );
  32. }
  33. is( $can_open ,
  34. 0, '[open_audio] ran'
  35. );
  36. is( SDL::Mixer::Channels::allocate_channels(8),
  37. 8, "[allocate_channels] 8 channels allocated"
  38. );
  39. is( SDL::Mixer::Groups::reserve_channels(4),
  40. 4, "[reserve_channels] 4 channels reserved"
  41. );
  42. my $delay = 100;
  43. my $audio_test_file = 'test/data/silence.wav';
  44. if ( $ENV{'SDL_RELEASE_TESTING'} ) {
  45. SDL::Mixer::Channels::volume( -1, 10 );
  46. is( SDL::Mixer::Channels::volume( -1, 20 ),
  47. 10, "[volume] set to 20, previously was 10"
  48. );
  49. $delay = 2000;
  50. $audio_test_file = 'test/data/sample.wav';
  51. } else {
  52. SDL::Mixer::Channels::volume( -1, 10 );
  53. is( SDL::Mixer::Channels::volume( -1, 1 ),
  54. 10, "[volume] set to 1, previously was 10"
  55. );
  56. }
  57. my $sample_chunk = SDL::Mixer::Samples::load_WAV($audio_test_file);
  58. my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
  59. is( $playing_channel > 3,
  60. 1, "[play_channel] plays on channel $playing_channel"
  61. );
  62. SDL::Mixer::Channels::halt_channel(-1);
  63. is( SDL::Mixer::Groups::group_channel( 0, 0 ),
  64. 1, "[group_channel] channel 0 to group 0"
  65. );
  66. is( SDL::Mixer::Groups::group_channels( 1, 3, 1 ),
  67. 3, "[group_channels] channel 1-3 to group 1"
  68. );
  69. is( SDL::Mixer::Groups::group_channel( 3, -1 ),
  70. 1, "[group_channel] channel 0 ungrouped"
  71. );
  72. is( SDL::Mixer::Groups::group_channels( 3, 3, 2 ),
  73. 1, "[group_channels] channel 3-3 to group 2"
  74. );
  75. is( SDL::Mixer::Groups::group_count(0), 1, "[group_count] for group 0 is 1" );
  76. is( SDL::Mixer::Groups::group_count(1), 2, "[group_count] for group 1 is 2" );
  77. is( SDL::Mixer::Groups::group_count(2), 1, "[group_count] for group 2 is 1" );
  78. is( SDL::Mixer::Groups::group_available(0),
  79. 0, "[group_available] first channel for group 0 is 0"
  80. );
  81. is( SDL::Mixer::Groups::group_available(1),
  82. 1, "[group_available] first channel for group 1 is 1"
  83. );
  84. is( SDL::Mixer::Groups::group_available(2),
  85. 3, "[group_available] first channel for group 2 is 3"
  86. );
  87. is( SDL::Mixer::Groups::group_oldest(1),
  88. -1, "[group_oldest] group 1 does not play something"
  89. );
  90. SDL::Mixer::Channels::play_channel( 2, $sample_chunk, -1 );
  91. SDL::delay( $delay / 4 );
  92. SDL::Mixer::Channels::play_channel( 1, $sample_chunk, -1 );
  93. SDL::delay(100);
  94. is( SDL::Mixer::Groups::group_oldest(1),
  95. 2, "[group_oldest] channel 2 started first"
  96. );
  97. is( SDL::Mixer::Groups::group_newer(1),
  98. 1, "[group_newer] channel 1 started at last"
  99. );
  100. SDL::delay(100);
  101. is( SDL::Mixer::Groups::fade_out_group( 1, $delay * 2 ),
  102. 2, "[fade_out_group] $delay ms for group 1"
  103. );
  104. SDL::delay($delay);
  105. is( SDL::Mixer::Groups::halt_group(1), 0, "[halt_group] group 1 halted" );
  106. SDL::Mixer::close_audio();
  107. pass '[close_audio] ran';
  108. if ($audiodriver) {
  109. $ENV{SDL_AUDIODRIVER} = $audiodriver;
  110. } else {
  111. delete $ENV{SDL_AUDIODRIVER};
  112. }
  113. done_testing();