Brought all packages under eye of strict, warnings and love of Carp, For
[sdlgit/SDL_perl.git] / test / loopwave.pl
1 #!/usr/bin/env perl
2
3 use SDL;
4 use Carp;
5
6 croak "Could not initialize SDL: ", SDL::GetError()
7         if ( 0 > SDL::Init(SDL_INIT_AUDIO()));
8
9 $ARGV[0] ||= 'data/sample.wav';
10
11 croak "usage: $0 [wavefile]\n"
12         if ( in $ARGV[0], qw/ -h --help -? /);
13
14 my ($wav_spec,$wav_buffer,$wav_len,$wav_pos) = (0,0,0,0);
15
16 my $done = 0;
17
18 $fillerup  = sub {
19         my ($data,$len) = @_;
20
21         $wav_ptr = $wav_buffer + $wav_pos;
22         $wav_remainder = $wav_len - $wav_pos;
23
24         while ( $wav_remainder <= $len ) {
25                 SDL::MixAudio($data,$wav_ptr,$wav_remainder,SDL_MIX_MAXVOLUME);
26                 $data += $wav_remainder;
27                 $len -= $wav_remainder;
28                 $wav_ptr = $wav_buffer; 
29                 $wav_remainder = $wav_len;
30                 $wav_pos = 0;
31         }
32         SDL::MixAudio($data,$wav_ptr,$len,SDL_MIX_MAXVOLUME);
33         $wav_pos += $len;
34 };
35
36 $poked = sub {
37         $done = 1;
38 };
39
40 $SIG{HUP} = $poked;
41 $SIG{INT} = $poked;
42 $SIG{QUIT} = $poked;
43 $SIG{TERM} = $poked;
44
45 $spec = SDL::NewAudioSpec(44100,AUDIO_S16,2,4096);
46
47 $wave = SDL::LoadWAV($ARGV[0],$spec);
48
49 ($wav_spec,$wav_buffer,$wav_len) = @$wave;
50
51 croak "Could not load wav file $ARGV[0], ", SDL::GetError(), "\n" unless ( $wav_len );
52
53 croak "Could not open audio ", SDL::GetError()
54         if (0 > SDL::OpenAudio($wav_spec,$fillerup));
55
56 SDL::PauseAudio(0);
57
58 print "Using audio driver: ", SDL::AudioDriverName(), "\n";
59         
60 while (! $done && ( SDL::GetAudioStatus() == SDL_AUDIO_PLAYING())) {
61         SDL::Delay(1000);
62 }
63
64