PageRenderTime 49ms CodeModel.GetById 20ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

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