PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/t/core_audiospec.t

http://github.com/PerlGameDev/SDL
Perl | 79 lines | 60 code | 18 blank | 1 comment | 10 complexity | a9e06e82d280bcb3474ea900805b6c60 MD5 | raw file
 1#!/usr/bin/perl -w
 2BEGIN { # 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}
 9use strict;
10use warnings;
11use threads;
12use threads::shared;
13use SDL;
14use SDL::Audio;
15use SDL::AudioSpec;
16use Test::More;
17use Devel::Peek;
18use Config;
19
20use lib 't/lib';
21use SDL::TestTool;
22
23plan( skip_all => "author tests not required for installation" )
24	unless ( $ENV{AUTOMATED_TESTING} or $ENV{SDL_RELEASE_TESTING} );
25
26my $audiodriver = $ENV{SDL_AUDIODRIVER};
27$ENV{SDL_AUDIODRIVER} = 'dummy'; # unless $ENV{SDL_RELEASE_TESTING};
28
29plan( skip_all => 'Failed to init sound' )
30	unless SDL::TestTool->init(SDL_INIT_AUDIO);
31
32my $obtained   = SDL::AudioSpec->new;
33my $p : shared = 0;
34my $f : shared = 0;
35
36my $desired = SDL::AudioSpec->new;
37$desired->freq(44100);
38$desired->format(AUDIO_S8);
39$desired->channels(1);
40$desired->samples(4096);
41$desired->callback('main::callback');
42
43sub callback {
44	my ( $int_size, $len, $streamref ) = @_;
45	my $chr = chr(0);
46	$chr = chr($p) if $p; #Windows is delaying the thread update for some reason
47	for ( my $i = 0; $i < $len; $i++ ) {
48		use bytes;
49		substr( $$streamref, $i, 1, $chr );
50
51		if ( $f && $p++ > 200 ) {
52			$f = 0;
53		} elsif ( !$f && $p-- < 0 ) {
54			$f = 1;
55		}
56	}
57	isnt $p, 0, '[callback] tested $p = ' . $p;
58
59}
60die 'AudioMixer, Unable to open audio: ' . SDL::get_error()
61	if ( SDL::Audio::open( $desired, $obtained ) < 0 );
62
63SDL::Audio::pause(0);
64
65sleep(1);
66
67SDL::Audio::close();
68
69if ($audiodriver) {
70	$ENV{SDL_AUDIODRIVER} = $audiodriver;
71} else {
72	delete $ENV{SDL_AUDIODRIVER};
73}
74
75
76
77done_testing();
78
79