Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Playing wav files

by merrymonk (Hermit)
on Feb 08, 2019 at 21:58 UTC ( [id://1229650]=note: print w/replies, xml ) Need Help??


in reply to Playing wav files

This is an update to my question. One of the suggestions was that it could not find the .wav file. Therefore I created a directory which had both the Perl to play the sound and the .wav files as well. This worked. As in the case where it did not work, I gave the full path to the .wav file (therefore this was the same in both cases). Therefore the suggestion that it could not find the file was correct but why it works when the wav files are in the same directory is a mystery to me. It would be nice to know.

Replies are listed 'Best First'.
Re^2: Playing wav files
by pryrt (Abbot) on Feb 08, 2019 at 23:11 UTC

    Making progress. I just ran the following on my computer: It played the wave once when it created it, once each for 'sinus.wav', './sinus.wav', and for the tempdir version, then finally played the WindowsDefault / error chime when it could not find 'DoesNotExist'. So, that means as long as the file exists and is readable (and is a valid wave file, presumably), Win32::Sound::Play() does what I expect.

    Please add a fourth and a fifth entry to the for-loop: once for a local copy of your .wav, and once for the copy that's in some other directory. Then run the following script, and report the results.

    use warnings; use strict; use Win32::Sound; # Create the object my $WAV = new Win32::Sound::WaveOut(44100, 8, 2); my $data = ""; my $counter = 0; my $increment = 440/44100; # Generate 44100 samples ( = 1 second) for my $i (1..44100) { # Calculate the pitch # (range 0..255 for 8 bits) my $v = sin($counter*2*3.14) * 127 + 128; # "pack" it twice for left and right $data .= pack("CC", $v, $v); $counter += $increment; } $WAV->Load($data); # get it $WAV->Write(); # hear it 1 until $WAV->Status(); # wait for completion $WAV->Save("sinus.wav"); # write to disk my $otherFile = $ENV{TEMP} . '/sinus.wav'; $WAV->Save($otherFile); # write to disk elsewhere $WAV->Unload(); # drop it ############ $|++; for('sinus.wav', './sinus.wav', $otherFile, 'DoesNotExist') { sleep(1); printf "'%s' does%s exist and is%s readable\n", $_, (-f $_ ? '' : +' not'), (-r $_ ? '' : ' not'); Win32::Sound::Play($_); (my $f = $_) =~ s{/}{\\}; print qx(cmd.exe /c dir /N "$f"); print "\n" x 4; } __END__ 'sinus.wav' does exist and is readable Volume in drive C is Windows Volume Serial Number is XXXX-XXXX Directory of C:\usr\local\share\PassThru\perl\perlmonks 02/08/2019 03:08 PM 96,278 sinus.wav 1 File(s) 96,278 bytes 0 Dir(s) 113,186,381,824 bytes free './sinus.wav' does exist and is readable Volume in drive C is Windows Volume Serial Number is XXXX-XXXX Directory of C:\usr\local\share\PassThru\perl\perlmonks 02/08/2019 03:08 PM 96,278 sinus.wav 1 File(s) 96,278 bytes 0 Dir(s) 113,186,381,824 bytes free 'C:\Users\pryrt\AppData\Local\Temp/sinus.wav' does exist and is readab +le Volume in drive C is Windows Volume Serial Number is XXXX-XXXX Directory of C:\Users\pryrt\AppData\Local\Temp 02/08/2019 03:08 PM 96,278 sinus.wav 1 File(s) 96,278 bytes 0 Dir(s) 113,186,381,824 bytes free 'DoesNotExist' does not exist and is not readable File Not Found Volume in drive C is Windows Volume Serial Number is XXXX-XXXX Directory of C:\usr\local\share\PassThru\perl\perlmonks

    As you might be able to guess, since I am able to access a .wav in my local directory and in a different directory, I see no reason why it should not work for you; thus, I added the file-exists / file-readable test and dir output to give some clue as to what's going wrong for you.

      I tried the Perl but found a problem which causes a message saying "perl.exe has stopped working".
      I am using Perl version 5.028000
      The area where it failed is
      $WAV->Load($data); # get it $WAV->Write(); # hear it 1 until $WAV->Status(); # wait for completion
      Specifically the line ...Load was ok
      ..but I did not get a print statement working after the row ...Write
      I did try removing the '1 until' because it did not look like proper Perl
      Any clues about how to make this work would be appreciated.

        This works for me on Windows 10 Perl v5.16.1 Win32::Sound 0.51

        #!perl use strict; use Win32::Sound; printf "%s %s\n",$^V,$Win32::Sound::VERSION; Win32::Sound::Volume('100%'); for my $n ('01'..'10'){ my $wav = 'C:/Windows/media/Alarm'.$n.'.wav'; print "Playing $wav\n"; Win32::Sound::Play($wav); }
        poj

        1 until $WAV->Status(); is perfectly-valid perl; it's using the Statement Modifiers syntax rather than the Compound Statements syntax. I didn't come up with most of that example: except for the for loop at the end, it's verbatim from the Win32::Sound documentation, specifically their EXAMPLE at the end: everything until the ############ was theirs, not mine, and should work. That line, specifically, is saying "loop doing nothing (1 is a noop, in that case) until $WAV->Status() is true"; that status will go true when the wav is done playing.

        If it's not working, there's something wrong with your perl installation or Win32::Sound installation on your machine.

        Are using Strawberry Perl, ActiveState, cygwin, or WSL (Windows Subsystem for Linux)? The full output of perl -V (that's a capital V) might help us debug what's going wrong, along with perl -MWin32::Sound -le "print $INC{'Win32/Sound.pm'}; print $Win32::Sound::VERSION".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found