PageRenderTime 69ms CodeModel.GetById 21ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 0ms

/t/mixer_channels.t

http://github.com/PerlGameDev/SDL
Perl | 213 lines | 172 code | 40 blank | 1 comment | 13 complexity | c625a943de8fcefa34b5948a50a95b88 MD5 | raw file
  1#!/usr/bin/perl -w
  2use strict;
  3use warnings;
  4use SDL;
  5use SDL::Config;
  6
  7my $audiodriver;
  8
  9BEGIN {
 10	use Config;
 11	if ( !$Config{'useithreads'} ) {
 12		print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
 13		exit(0);
 14	}
 15	require threads;
 16	require threads::shared;
 17
 18
 19	use Test::More;
 20	use lib 't/lib';
 21	use SDL::TestTool;
 22
 23	$audiodriver = $ENV{SDL_AUDIODRIVER};
 24	$ENV{SDL_AUDIODRIVER} = 'dummy' unless $ENV{SDL_RELEASE_TESTING};
 25
 26	if ( !SDL::TestTool->init(SDL_INIT_AUDIO) ) {
 27		plan( skip_all => 'Failed to init sound' );
 28	} elsif ( !SDL::Config->has('SDL_mixer') ) {
 29		plan( skip_all => 'SDL_mixer support not compiled' );
 30	}
 31}
 32
 33use SDL::Mixer;
 34use SDL::Mixer::Channels;
 35use SDL::Mixer::Samples;
 36use SDL::Mixer::MixChunk;
 37
 38my $can_open = SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 );
 39
 40unless($can_open == 0)
 41{
 42	plan( skip_all => 'Cannot open audio :'.SDL::get_error() );
 43}
 44
 45is( $can_open ,
 46		0, '[open_audio] ran'
 47  );
 48
 49is( SDL::Mixer::Channels::allocate_channels(4),
 50		4, "[allocate_channels] 4 channels allocated"
 51  );
 52
 53my $finished :shared = 0;
 54my $callback = sub {
 55	my ($channel) = shift;
 56	printf( "[channel_finished] callback called for channel %d\n", $channel);
 57	$finished++;
 58};
 59
 60SKIP:
 61{
 62	skip( 'No callbacks unless SDL_RELEASE_TESTING', 1 )
 63		unless $ENV{'SDL_RELEASE_TESTING'};
 64	SDL::Mixer::Channels::channel_finished($callback);
 65	pass '[channel_finished] registered callback';
 66}
 67
 68my $delay           = 500;
 69my $audio_test_file = 'test/data/silence.wav';
 70
 71if ( $ENV{'SDL_RELEASE_TESTING'} ) {
 72	SDL::Mixer::Channels::volume( -1, 10 );
 73	is( SDL::Mixer::Channels::volume( -1, 20 ),
 74			10, "[volume] set to 20, previously was 10"
 75	  );
 76	$delay           = 2000;
 77	$audio_test_file = 'test/data/sample.wav';
 78} else {
 79	SDL::Mixer::Channels::volume( -1, 10 );
 80	is( SDL::Mixer::Channels::volume( -1, 1 ),
 81			10, "[volume] set to 1, previously was 10"
 82	  );
 83}
 84
 85my $sample_chunk = SDL::Mixer::Samples::load_WAV($audio_test_file);
 86my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
 87isnt(
 88		$playing_channel, -1,
 89		"[play_channel] plays $audio_test_file on channel " . $playing_channel
 90	);
 91is( SDL::Mixer::Channels::fading_channel($playing_channel),
 92		MIX_NO_FADING, "[fading_channel] channel $playing_channel is not fading"
 93  );
 94is( SDL::Mixer::Channels::playing($playing_channel),
 95		1, "[playing] channel $playing_channel is playing"
 96  );
 97is( SDL::Mixer::Channels::paused($playing_channel),
 98		0, "[paused] channel $playing_channel is not paused"
 99  );
100ok( $delay, 'delay definedness madness test #1' );
101my $fading_channels = SDL::Mixer::Channels::fade_out_channel( $playing_channel, $delay );
102is( $fading_channels > 0,
103		1, "[fade_out_channel] $delay ms for $fading_channels channel(s)"
104  );
105is( SDL::Mixer::Channels::fading_channel($playing_channel),
106		MIX_FADING_OUT, "[fading_channel] channel $playing_channel is fading out"
107  );
108ok( $delay, 'delay definedness madness test #2' );
109SDL::delay($delay);
110ok( $delay, 'delay definedness madness test #3' );
111
112$playing_channel = SDL::Mixer::Channels::fade_in_channel( -1, $sample_chunk, 0, $delay );
113ok( $delay, 'delay definedness madness test #4' );
114
115isnt(
116		$playing_channel, -1,
117		"[fade_in_channel] $delay ms for channel $playing_channel"
118	);
119is( SDL::Mixer::Channels::fading_channel($playing_channel),
120		MIX_FADING_IN, "[fading_channel] channel $playing_channel is fading in"
121  );
122ok( $delay, 'delay definedness madness test #5' );
123
124SDL::delay($delay);
125ok( $delay, 'delay definedness madness test #6' );
126
127SDL::Mixer::Channels::pause(-1);
128pass '[pause] ran';
129is( SDL::Mixer::Channels::paused($playing_channel),
130		1, "[paused] channel $playing_channel is paused"
131  );
132
133
134SDL::delay( $delay / 4 );
135ok( $delay, 'delay definedness madness test #7' );
136
137SDL::Mixer::Channels::resume(-1);
138pass '[resume] ran';
139
140SDL::delay($delay);
141ok( $delay, 'delay definedness madness test #8' );
142
143is( SDL::Mixer::Channels::halt_channel($playing_channel),
144		0, "[halt_channel] stop channel $playing_channel"
145  );
146is( SDL::Mixer::Channels::playing($playing_channel),
147		0, "[playing] channel $playing_channel is not playing"
148  );
149
150SDL::delay($delay);
151ok( $delay, 'delay definedness madness test #9' );
152
153$playing_channel = SDL::Mixer::Channels::play_channel_timed( -1, $sample_chunk, 0, $delay );
154ok( $delay, 'delay definedness madness test #10' );
155
156isnt(
157		$playing_channel, -1,
158		"[play_channel_timed] play $delay ms for channel $playing_channel"
159	);
160SDL::delay( $delay / 4 );
161ok( $delay, 'delay definedness madness test #11' );
162
163my $expire_channel = SDL::Mixer::Channels::expire_channel( $playing_channel, $delay );
164ok( $delay, 'delay definedness madness test #12' );
165
166is( $expire_channel > 0,
167		1,
168		"[expire_channel] stops after $delay ms for $expire_channel channel(s)"
169  );
170
171SDL::delay($delay);
172ok( $delay, 'delay definedness madness test #13' );
173
174$playing_channel = SDL::Mixer::Channels::fade_in_channel_timed(
175		-1, $sample_chunk, 0, $delay,
176		$delay * 2
177		);
178ok( $delay, 'delay definedness madness test #14' );
179
180isnt(
181		$playing_channel, -1,
182		"[fade_in_channel_timed] play " . ( $delay * 2 ) . " ms after $delay ms fade in for channel $playing_channel"
183	);
184
185isa_ok(
186		SDL::Mixer::Channels::get_chunk($playing_channel),
187		'SDL::Mixer::MixChunk', '[get_chunk]'
188	  );
189
190SDL::delay(1000);
191ok( $delay, 'delay definedness madness test #15' );
192
193SDL::Mixer::close_audio();
194pass '[close_audio] ran';
195
196SKIP:
197{
198	skip( 'No callbacks unless SDL_RELEASE_TESTING', 1 )
199		unless $ENV{'SDL_RELEASE_TESTING'};
200	is( $finished > 0,
201		1, '[callback_finished] called the callback got ' . $finished
202	);
203}
204
205if ($audiodriver) {
206	$ENV{SDL_AUDIODRIVER} = $audiodriver;
207} else {
208	delete $ENV{SDL_AUDIODRIVER};
209}
210
211sleep(1);
212
213done_testing();