#!/usr/bin/env perl # # loopwave.pl # # Copyright (C) 2005 David J. Goehrig # # ------------------------------------------------------------------------------ # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # ------------------------------------------------------------------------------ # # Please feel free to send questions, suggestions or improvements to: # # David J. Goehrig # dgoehrig@cpan.org # use SDL; use MIME::Base64 qw(decode_base64); die "Could not initialize SDL: ", SDL::GetError() if ( 0 > SDL::Init(SDL_INIT_AUDIO())); my $filename = '/tmp/sample.wav'; 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); write_wav(); $wave = SDL::LoadWAV($filename,$spec); ($wav_spec,$wav_buffer,$wav_len) = @$wave; die "Could not load wav file $filename, ", 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 (10 > $done && ( SDL::GetAudioStatus() == SDL_AUDIO_PLAYING())) { SDL::Delay(1000); ++$done; } sub write_wav { $wav = decode_base64 < $filename" or die "Failed to write to $!"; print FP $wav; close FP; }