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

hello guys...I am very new to Perl. I did some search on the internet and got some samples for playing wave file. Here is what I got
#.....PlayWave.pl use Audio::Wav; my $wav = new Audio::Wav; my $read = $wav -> read( 'Test.wav' ); my $details = $read -> details();
Now first thing I want to know is; how do I know that I have this library Audio::Wav. Second: I moved to the directory where I saved this file using terminal and run the command perl ./PlayWave.pl but this does not do anything. Whats wrong with this sample? thnx

Replies are listed 'Best First'.
Re: Playing wave file in Ubuntu 10.10
by fidesachates (Monk) on Mar 30, 2011 at 18:45 UTC
    First off, make sure you  use strict; use warnings;

    Secondly to figure out if you have that module run the command perl -MAudio::Wav -e 1 If you see output, it means you do NOT have it installed.

    Your shebang line looks messed up, but if you're in windows, it doesn't matter.Edit: and I just noticed the title of your post says you're in ubuntu. Make sure your shebang lines points to the perl binary.

    Update
    I installed the module on my windows machine and tested your code. Nothing printed because your program doesn't say to print anything. See my code below for an output.
    #!perl use warnings; use strict; use Audio::Wav; my $wav = new Audio::Wav; my $read = $wav -> read( 'test_tone.wav' ); my $details = $read -> details(); use Data::Dumper; print Dumper($details)."\n"
    It would appear that read() returns a reference to a hash. Since you're new, you're going to want to read up on hashes in perl and hash references.

    To help you out a bit, the two lines I added to your program, use Data::Dumper is a module which will show you the internals of variables. The second line passes the variable details into Dumper() which exposes for us the internals.
Re: Playing wave file in Ubuntu 10.10
by Eliya (Vicar) on Mar 30, 2011 at 19:16 UTC
    ...but this does not do anything.

    The snippet you have doesn't generate any output, so how do you tell?

    That said, it certainly won't play the wave file... because the module is solely for reading/writing (manipulating) wave files.

Re: Playing wave file in Ubuntu 10.10
by zentara (Cardinal) on Mar 30, 2011 at 23:00 UTC
    Here is a simple Audio::DSP script for wave playback. Remember, you need to initialize the soundcard, to accept the audio data you pump in. The program Sox will help you detecting wav information.

    On Ubuntu, with the Alsa sound system, you have a great utility called aplay. It will auto-detect the soundfile stats, for proper playing, so all you need to play a wav is

    #!/usr/bin/perl # fork off, so the playback is non-blocking if(fork() == 0){exec("aplay $my_sound_file")}

    Or with Perl......

    #!/usr/bin/perl use warnings; use strict; use Audio::DSP; #alsamixer must be setup right, just turn everything up :-) # use the channel count, bitrate, and samplerate of the wave # you want to play my ($buf, $chan, $fmt, $rate) = (4096, 1, 8, 22050); my $dsp = new Audio::DSP(buffer => $buf, channels => $chan, format => $fmt, rate => $rate); $dsp->init() || die $dsp->errstr(); my $file = shift || 'Om.wav'; open(IN, "<$file") or warn $!; while ( 4096 == read( IN, my $buffer, 4096 ) ) { $dsp->dwrite($buffer); } $dsp->close();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Playing wave file in Ubuntu 10.10
by chrestomanci (Priest) on Mar 30, 2011 at 20:17 UTC

    That library is for reading raw data from wav files. It is not for playing them.

    In theory you could probably use it and another module that interfaces with your sound card such as Audio::DSP, but unless this is some sort of learning project or your needs are complex or unusual, then that would probably be to much work.

    Instead I suggest you check out the many Linux applications out there for playing multimedia. If it where me I would start with mplayer or perhaps ffplay (part of ffmpeg), as they both have simple command line interfaces.