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