PageRenderTime 36ms CodeModel.GetById 2ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/t/mixer_effects.t

http://github.com/PerlGameDev/SDL
Perl | 240 lines | 208 code | 31 blank | 1 comment | 11 complexity | f789954287712521c2024dd9cb8c8be9 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}
 32use SDL::Mixer;
 33use SDL::Mixer::Channels;
 34use SDL::Mixer::Effects;
 35use SDL::Mixer::Samples;
 36
 37my $can_open = SDL::Mixer::open_audio( 44100, SDL::Audio::AUDIO_S16SYS, 2, 4096 );
 38
 39unless($can_open == 0) 
 40{
 41	plan( skip_all => 'Cannot open audio :'.SDL::get_error() );
 42}
 43
 44is( $can_open ,
 45		0, '[open_audio] ran'
 46  );
 47
 48my $delay           = 500;
 49my $audio_test_file = 'test/data/silence.wav';
 50SDL::Mixer::Channels::volume( -1, 1 );
 51
 52if ( $ENV{'SDL_RELEASE_TESTING'} ) {
 53	SDL::Mixer::Channels::volume( -1, 20 );
 54	$delay           = 1000;
 55	$audio_test_file = 'test/data/sample.wav';
 56}
 57
 58my $effect_func_called : shared = 0;
 59my $effect_done_called : shared = 0;
 60my @last_stream                 = ();
 61
 62sub echo_effect_func {
 63	my $channel  = shift;
 64	my $samples  = shift;
 65	my $position = shift;
 66	my @stream   = @_;
 67
 68	$effect_func_called++;
 69	printf(
 70			"[effect_func] callback: channel=%2s, position=%8s, samples=%6s\n",
 71			$channel, $position, scalar(@stream)
 72		  );
 73
 74	my @stream2 = @stream;
 75	my $offset  = $samples / 2;
 76	for ( my $i = 0; $i < $samples; $i += 2 ) {
 77		if ( $i < $offset ) {
 78			if ( scalar(@last_stream) == $samples ) {
 79				$stream2[$i] = $stream[$i] * 0.6 + $last_stream[ $samples + $i - $offset ] * 0.4; # left
 80					$stream2[ $i + 1 ] =
 81					$stream[ $i + 1 ] * 0.6 + $last_stream[ $samples + $i - $offset + 1 ] * 0.4;  # right
 82			}
 83		} else {
 84			$stream2[$i] = $stream[$i] * 0.6 + $stream[ $i - $offset ] * 0.4;                     # left
 85				$stream2[ $i + 1 ] = $stream[ $i + 1 ] * 0.6 + $stream[ $i - $offset + 1 ] * 0.4;     # right
 86		}
 87	}
 88
 89	@last_stream = @stream;
 90	push( @stream2, $position + $samples );
 91	return @stream2;
 92}
 93
 94sub echo_effect_func2 {
 95	my $channel  = shift;
 96	my $samples  = shift;
 97	my $position = shift;
 98	my @stream   = @_;
 99
100	$effect_func_called++;
101	printf(
102			"[effect_func2] callback: channel=%2s, position=%8s, samples=%6s\n",
103			$channel, $position, scalar(@stream)
104		  );
105	push( @stream, $position + $samples );
106	return @stream;
107
108}
109
110sub effect_done2 {
111	printf("[effect_done2] called\n");
112	$effect_done_called++;
113}
114
115sub effect_done {
116	printf("[effect_done] called\n");
117	$effect_done_called++;
118}
119
120my $sample_chunk = SDL::Mixer::Samples::load_WAV($audio_test_file);
121my $playing_channel = SDL::Mixer::Channels::play_channel( -1, $sample_chunk, -1 );
122is( $playing_channel >= 0, 1, "[play_channel] playing $audio_test_file" );
123
124SDL::delay($delay);
125my $effect_id = SDL::Mixer::Effects::register(
126		$playing_channel,     "main::echo_effect_func2",
127		"main::effect_done2", 0
128		);
129isnt( $effect_id, -1, '[register] registerering echo effect callback' );
130SDL::delay($delay);
131my $check = SDL::Mixer::Effects::unregister( $playing_channel, $effect_id );
132isnt(
133		$check, 0,
134		'[unregister] unregistering effect_func will call effect_done'
135	);
136SDL::delay(200);
137is( $effect_func_called > 0,
138		1, "[effect_func] called $effect_func_called times"
139  );
140is( $effect_done_called > 0,
141		1, "[effect_done] called $effect_done_called times"
142  );
143
144SDL::delay($delay);
145$effect_func_called = 0;
146$effect_done_called = 0;
147
148$effect_id = SDL::Mixer::Effects::register(
149		$playing_channel,     "main::echo_effect_func2",
150		"main::effect_done2", 0
151		);
152isnt( $effect_id, -1, '[register] registerering echo effect callback' );
153SDL::delay($delay);
154$check = SDL::Mixer::Effects::unregister( $playing_channel, $effect_id );
155isnt(
156		$check, 0,
157		'[unregister] unregistering effect_func will call effect_done'
158	);
159SDL::delay(200);
160is( $effect_func_called > 0,
161		1, "[effect_func] called $effect_func_called times"
162  );
163is( $effect_done_called > 0,
164		1, "[effect_done] called $effect_done_called times"
165  );
166
167$effect_func_called = 0;
168$effect_done_called = 0;
169my $effect_id_all = SDL::Mixer::Effects::register( MIX_CHANNEL_POST, "main::echo_effect_func",
170		"main::effect_done", 0
171		);
172isnt( $effect_id_all, -1, '[register] registerering echo effect callback' );
173SDL::delay($delay);
174isnt(
175		SDL::Mixer::Effects::unregister_all(MIX_CHANNEL_POST),
176		0, '[unregister_all] unregistering all will call effect_done'
177	);
178SDL::delay(200);
179is( $effect_func_called > 0,
180		1, "[effect_func] called $effect_func_called times"
181  );
182is( $effect_done_called > 0,
183		1, "[effect_done] called $effect_done_called times"
184  );
185
186$effect_func_called = 0;
187is( SDL::Mixer::Effects::set_post_mix( "main::echo_effect_func", 0 ),
188		undef, '[set_post_mix] registering echo effect callback'
189  );
190SDL::delay($delay);
191is( SDL::Mixer::Effects::set_post_mix(),
192		undef, '[set_post_mix] unregistering echo effect callback'
193  );
194SDL::delay(200);
195is( $effect_func_called > 0,
196		1, "[effect_func] called $effect_func_called times"
197  );
198SDL::delay($delay);
199
200isnt(
201		SDL::Mixer::Effects::set_panning( $playing_channel, 128, 255 ),
202		0, '[set_panning]  50% left, 100% right'
203	);
204SDL::delay($delay);
205isnt(
206		SDL::Mixer::Effects::set_position( $playing_channel, 225, 80 ),
207		0, '[set_position] left-behind, 33% away'
208	);
209SDL::delay($delay);
210isnt(
211		SDL::Mixer::Effects::set_distance( $playing_channel, 160 ),
212		0, '[set_distance] 66% away'
213	);
214SDL::delay($delay);
215isnt(
216		SDL::Mixer::Effects::set_position( $playing_channel, 0, 0 ),
217		0, '[set_position] front, 0% away'
218	);
219SDL::delay($delay);
220isnt(
221		SDL::Mixer::Effects::set_reverse_stereo( $playing_channel, 1 ),
222		0, '[set_reverse_stereo] on'
223	);
224SDL::delay($delay);
225isnt(
226		SDL::Mixer::Effects::set_reverse_stereo( $playing_channel, 0 ),
227		0, '[set_reverse_stereo] off'
228	);
229SDL::delay($delay);
230
231SDL::Mixer::close_audio();
232pass '[close_audio] ran';
233
234if ($audiodriver) {
235	$ENV{SDL_AUDIODRIVER} = $audiodriver;
236} else {
237	delete $ENV{SDL_AUDIODRIVER};
238}
239
240done_testing();