PageRenderTime 36ms CodeModel.GetById 24ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/loopwave.pl

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