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