/t/core_audio.t

http://github.com/PerlGameDev/SDL · Perl · 141 lines · 118 code · 22 blank · 1 comment · 11 complexity · 1a6301036c59e9565796d45e0e3977b2 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. BEGIN { # http://wiki.cpantesters.org/wiki/CPANAuthorNotes
  3. use Config;
  4. if ( !$Config{'useithreads'} ) {
  5. print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
  6. exit(0);
  7. }
  8. }
  9. use strict;
  10. use warnings;
  11. use SDL;
  12. use SDL::Audio;
  13. use SDL::AudioSpec;
  14. use Test::More;
  15. use Devel::Peek;
  16. use lib 't/lib';
  17. use SDL::TestTool;
  18. my $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. } else {
  23. plan( tests => 45 );
  24. }
  25. my @done = qw/
  26. audio_spec
  27. open
  28. pause
  29. close
  30. get_status
  31. lock
  32. unlock
  33. /;
  34. is( AUDIO_S16, 32784, 'AUDIO_S16 should be imported' );
  35. is( AUDIO_S16(), 32784, 'AUDIO_S16() should also be available' );
  36. is( AUDIO_S16MSB, 36880, 'AUDIO_S16MSB should be imported' );
  37. is( AUDIO_S16MSB(), 36880, 'AUDIO_S16MSB() should also be available' );
  38. is( AUDIO_S16LSB, 0x8010, 'AUDIO_S16MSB should be imported' );
  39. is( AUDIO_S16LSB(), 0x8010, 'AUDIO_S16MSB() should also be available' );
  40. is( AUDIO_S8, 32776, 'AUDIO_S8 should be imported' );
  41. is( AUDIO_S8(), 32776, 'AUDIO_S8() should also be available' );
  42. is( AUDIO_U16, 16, 'AUDIO_U16 should be imported' );
  43. is( AUDIO_U16(), 16, 'AUDIO_U16() should also be available' );
  44. is( AUDIO_U16MSB, 4112, 'AUDIO_U16MSB should be imported' );
  45. is( AUDIO_U16MSB(), 4112, 'AUDIO_U16MSB() should also be available' );
  46. is( AUDIO_U16LSB, 0x0010, 'AUDIO_U16MSB should be imported' );
  47. is( AUDIO_U16LSB(), 0x0010, 'AUDIO_U16MSB() should also be available' );
  48. is( AUDIO_U8, 8, 'AUDIO_U8 should be imported' );
  49. is( AUDIO_U8(), 8, 'AUDIO_U8() should also be available' );
  50. ok( ( SDL::Audio::AUDIO_U16SYS == AUDIO_U16LSB ) || ( SDL::Audio::AUDIO_U16SYS == AUDIO_U16MSB ),
  51. 'AUDIO_U16SYS should be imported'
  52. );
  53. ok( ( SDL::Audio::AUDIO_U16SYS() == AUDIO_U16LSB() ) || ( SDL::Audio::AUDIO_U16SYS() == AUDIO_U16MSB() ),
  54. 'AUDIO_U16SYS() should also be available'
  55. );
  56. is( SDL_AUDIO_PAUSED, 2, 'SDL_AUDIO_PAUSED should be imported' );
  57. is( SDL_AUDIO_PAUSED(), 2, 'SDL_AUDIO_PAUSED() should also be available' );
  58. is( SDL_AUDIO_PLAYING, 1, 'SDL_AUDIO_PLAYING should be imported' );
  59. is( SDL_AUDIO_PLAYING(), 1, 'SDL_AUDIO_PLAYING() should also be available' );
  60. is( SDL_AUDIO_STOPPED, 0, 'SDL_AUDIO_STOPPED should be imported' );
  61. is( SDL_AUDIO_STOPPED(), 0, 'SDL_AUDIO_STOPPED() should also be available' );
  62. my $driver = SDL::Audio::audio_driver_name();
  63. pass "[audio_driver_name] using audio driver $driver";
  64. my $desired = SDL::AudioSpec->new;
  65. $desired->freq(44100);
  66. is( $desired->freq, 44100, '[audiospec] can set freq' );
  67. $desired->format(SDL::Audio::AUDIO_S16SYS);
  68. is( $desired->format, SDL::Audio::AUDIO_S16SYS, '[audiospec] can set format' );
  69. $desired->channels(2);
  70. is( $desired->channels, 2, '[audiospec] can set channels' );
  71. $desired->samples(4096);
  72. is( $desired->samples, 4096, '[audiospec] can set samples' );
  73. $desired->callback('main::audio_callback');
  74. is( SDL::Audio::get_status, SDL_AUDIO_STOPPED, '[get_status stopped]' );
  75. my $obtained = SDL::AudioSpec->new;
  76. is( SDL::Audio::open( $desired, $obtained ), 0, '[open returned success]' );
  77. isa_ok( $obtained, 'SDL::AudioSpec', 'Created a new AudioSpec' );
  78. my $wav_ref = SDL::Audio::load_wav( 'test/data/sample.wav', $obtained );
  79. isa_ok( $wav_ref, 'ARRAY', "Got and Array Out of load_wav. $wav_ref" );
  80. my ( $wav_spec, $audio_buf, $audio_len ) = @{$wav_ref};
  81. isa_ok( $wav_spec, 'SDL::AudioSpec', '[load_wav] got Audio::Spec back out ' );
  82. is( $audio_len, 481712, '[load_wav] length is correct' );
  83. SDL::Audio::free_wav($audio_buf);
  84. is( SDL::Audio::get_status, SDL_AUDIO_PAUSED, '[get_status paused]' );
  85. SDL::Audio::pause(0);
  86. is( SDL::Audio::get_status, SDL_AUDIO_PLAYING, '[get_status playing]' );
  87. SDL::Audio::lock();
  88. pass('Audio locked');
  89. SDL::Audio::unlock();
  90. pass('Audio unlocked');
  91. SDL::Audio::close();
  92. pass('Audio Closed');
  93. is( SDL::Audio::get_status, SDL_AUDIO_STOPPED, '[get_status stopped]' );
  94. my @left = qw/
  95. audio_cvt
  96. build_audio_cvt
  97. convert_audio
  98. mix_audio
  99. /;
  100. my $why =
  101. '[Percentage Completion] '
  102. . int( 100 * ( $#done + 1 ) / ( $#done + $#left + 2 ) )
  103. . "\% implementation. "
  104. . ( $#done + 1 ) . " / "
  105. . ( $#done + $#left + 2 );
  106. TODO:
  107. {
  108. local $TODO = $why;
  109. fail "Not Implmented $_" foreach (@left)
  110. }
  111. print "$why\n";
  112. if ($audiodriver) {
  113. $ENV{SDL_AUDIODRIVER} = $audiodriver;
  114. } else {
  115. delete $ENV{SDL_AUDIODRIVER};
  116. }
  117. sleep(1);
  118. sub audio_callback {
  119. }