victor_charlie has asked for the wisdom of the Perl Monks concerning the following question:

Hello, using a tutorial from Perl-gtk2 website, written by Ross McFarland -- basically the project is an alarm clock with a gui. At count 5 seconds it plays a .wav file, at 10 seconds another .wav. All the while we update text on a label showing the seconds counting up.

I get audio artifacts using SOX. WAVfile2 is faintly playing in the background of WAVfile1.

I changed over to use SDL v2.2.6; Because of SDL::Mixer I get much better audio fidelity. Much better. However, I have no synchronization. wav1.wav and then wav2.wav plays immediately as if one, single audio file. The code continues, then invokes the call for wav2 and it does play, but not on cue, Playback repeats for wav2 and is arbitrary, meaning any time before the 10-seconds test-condition.

I would post a code snippet, but current code is five files if we include the Glade<xml>, the .wav's, and the code.

Opinions wanted:

  1. Do I que functions in a thread list?
  2. Do I make Perl Modules to 'encapsulate' the audio files?
  3. I can't seem to preload the audio data, or can I?
  4. Is SDL:: trying to be a framework, much like java Swing, and I need to do all GUI, all audio, all functionality from that module?

Thanks, really, if you can give me insight. Sound functionality is the step-child of coders. I like exploring the audio capabilities when I begin a new language.

Replies are listed 'Best First'.
Re: Script with audio files
by Khen1950fx (Canon) on Jul 03, 2010 at 21:48 UTC
    First, take a look at these references:

    www.libsdl.org
    sdl.perl.org documentation

    Second, here's a little bundle to make sure that you have all the requirements
    #!/usr/bin/perl use strict; use warnings; use CPAN; CPAN::Shell->force('install', "PadWalker", "Data::Dumper::Names", "Filter::Util::Call", "ExtUtils::MakeMaker", "ExtUtils::CBuilder", "File::Spec", "Scalar::Util", "Test::Harness", "Pod::Man", "Pod::Simple", "Pod::Escapes", "Pod::Usage", "HTML::Entities", "Net::SMTP", "Net::SMTP_auth", "Test::Pod", "Test::Pod::Coverage", "Text::Trac", "Getopt::Long", "Text::Wrap", "Test", "YAML", "ExtUtils::CBuilder", "Test::Simple", "IO::CaptureOutput", "Test::Most", "Pod::ToDemo", "File::Temp", "File::ShareDir", "File::Path", "File::Fetch", "Digest::SHA", "Archive::Extract", "Archive::Tar", "Archive::Zip", "Module::Build", "Alien::SDL");
    Make sure you have the latest version, then try this script from sdl.perl:
    #!/usr/bin/perl use strict; use warnings; use SDL; use SDL::Audio; use SDL::AudioSpec; SDL::init(SDL_INIT_AUDIO); # Converting some WAV data to hardware format my $desired = SDL::AudioSpec->new(); my $obtained = SDL::AudioSpec->new(); # Set desired format $desired->freq(22050); $desired->channels(1); $desired->format(AUDIO_S16); $desired->samples(8192); # Open the audio device if( SDL::Audio::open($desired, $obtained) < 0 ) { printf( STDERR "Couldn't open audio: %s\n", SDL::get_error() ); exit(-1); } # Load the test.wav my $wav_ref = SDL::Audio::load_wav('../../test/data/sample.wav', $obta +ined); unless( $wav_ref ) { warn( "Could not open sample.wav: %s\n", SDL::get_error() ); SDL::Audio::close_audio(); SDL::quit; exit(-1); } my ( $wav_spec, $wav_buf, $wav_len ) = @{$wav_ref};

      Thanks for those scripts. However, I do note I cannot build SDL::Audio. I get most of the modules, but not the ::Audio or ::AudioSpecs. Test fails every time for an install.

      ALSA lib pcm.c:7234:(snd_pcm_recover) underrun occured

      ALSA lib pcm.c:7234:(snd_pcm_recover) underrun occured

      ... ad infintum

      I will seek the authors of SDL for more info, and I will add to this post with any new results

        I had to modify the Build.PL to get SDL to install. Here it is:
      why force the installs?
        You don't have to force the installs, but I wouldn't recommend it. Are you having any problems with it?