merrymonk has asked for the wisdom of the Perl Monks concerning the following question:
use Win32::Sound; Win32::Sound::Play($wav_file);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Playing wav files
by haukex (Archbishop) on Feb 08, 2019 at 14:57 UTC | |
You might be interested in the thread Playing sounds, maybe one of the methods described there helps. | [reply] |
|
Re: Playing wav files
by pryrt (Abbot) on Feb 08, 2019 at 15:20 UTC | |
Win32::Sound has direct access to the various Windows sounds: to test that everything's working okay, ... when I did that, all but SystemHand were the same -- probably because I don't have a lot of those sounds defined on my machine (I like a quiet machine). If you go to cpanm --look Win32::Sound, which (if you have cpanminus (*)) will put you into the distro-directory for Win32::Sound, then cd into the samples directory, you should be able to run sinus.pl and hear a tone (the note A). Then perl -MWin32::Sound -le "Win32::Sound::Play('welcome.wav')" should play a small sound. (*: if you don't have cpanminus, download the Win32::Sound tarball and untar it, then go into the samples subdirectory.) My guess is what's happening in your example is that it's not finding $wav_file. In the same directory as welcome.wav above, run . This should play the welcome sound twice, then play the system default (because it didn't find DNE.wav), then play nothing (because it didn't find DNE.wav and was set to no default) | [reply] [d/l] [select] |
|
Re: Playing wav files
by harangzsolt33 (Deacon) on Feb 08, 2019 at 14:41 UTC | |
Playing Sound in Perl script I'm trying to add sound to a Perl script to alert the user that the transaction was OK (user may not be looking at the screen all the time while working). I'd like to stay as portable as possible, as the script runs on Windows and Linux stations. I can
for Windows. But I'm not sure how to call a generic sound on Linux (Gnome). So far, I've come up with
But I'm not sure if I can count on that path being available. So, three questions: Is there a better way to call a default sound in Gnome Is that path pretty universal (at least among Debain/Ubuntu flavors) paplay takes a while to exit after playing a sound, is there a better way to call it? I'd rather stay away from beeping the system speaker, it sounds awful (this is going to get played a lot) and Ubuntu blacklists the PC Speaker anyway. Thanks! asked Oct 10 '12 at 19:11 1 Answer A more portable way to get the path to paplay (assuming it's there) might be to use File::Which. Then you could get the path like:
And to play the sound asynchronously, you can fork a subprocess:
Notice also that I've used the multi-argument form of system; doing so avoids the shell and runs the requested program directly. This avoids dangerous bugs (and is more efficient.) answered Oct 10 '12 at 20:27 Also, I'm writing this in Perl/Tk. fork has to explicitly call CORE::exit() or POSIX::_exit() or horrible things happen. – charlesbridge Oct 24 '12 at 11:23 --- END OF PAGE --- | [reply] [d/l] [select] |
by merrymonk (Hermit) on Feb 08, 2019 at 15:26 UTC | |
| [reply] [d/l] |
by pryrt (Abbot) on Feb 08, 2019 at 15:36 UTC | |
That seems more like a reply to mine rather than the one it actually replied to. if you agree, I will consider it for reparenting. If you really meant it where you put it, sorry for the misunderstanding. The goal of my listing of the various System sounds was to ensure that your soundcard and Win32::Sound installation were correct. Which seems to be the case based on this code. My other examples -- running sinus.pl, or directly accessing the welcome.wav, while in the Win32::Sound samples/ directory -- was an attempt to make sure that you're really in the directory you think you are, and to make sure you can really get to a local wav file. If you don't want to download the whole tarball, you could run the code from the EXAMPLE portion of Win32::Sound, which will create sinus.wav in your local directory; you can then run perl -MWin32::Sound -e "Win32::Sound::Play('sinus.wav', SND_NODEFAULT)" and it should play, then perl -MWin32::Sound -e "Win32::Sound::Play('sinus.wavx', SND_NODEFAULT)" and it should do no sound at all, because 'sinus.wavx' doesn't exist. | [reply] [d/l] [select] |
|
Re: Playing wav files
by harangzsolt33 (Deacon) on Feb 08, 2019 at 17:16 UTC | |
If you're wondering why this script creates a TEMP folder, it's because Windows 8 and higher will give an Access Denied error if you try to create a file in the root folder. So, we can't create a temp file like C:\TEMP.TXT or anything like that. Windows XP would handle it all right, but Windows 10 requires you to have admin privilege for that. But you don't need admin privilege to create a folder and put a file within that folder.)
| [reply] [d/l] |
by Your Mother (Archbishop) on Feb 08, 2019 at 18:20 UTC | |
FWIW, this is an option on OS X–
I use it for short scripts or command line stuff since the visual bell isn't always enough. Stuff like–
| [reply] [d/l] [select] |
by bliako (Abbot) on Feb 09, 2019 at 09:40 UTC | |
Computer says no | [reply] |
by IB2017 (Pilgrim) on Feb 08, 2019 at 18:36 UTC | |
Oh you are opening up new scenarios for me! Can you point me to some pages describing the VB commands (I have no clue about VB, but I guess it should be easy to adapt), for example I guess there are more languages installed, how to select the one desired? | [reply] |
by harangzsolt33 (Deacon) on Feb 09, 2019 at 02:02 UTC | |
The same thing is true about JavaScript and VBScript. Files that end with .js .vbs can be executed just like any binary. You can either double-click on them or invoke them from perl. But this probably doesn't work the same way on Mac or Android or Linux, so this solution is for Windows only. I am pretty sure there's a way to make Linux read something out loud, but I don't know how to do that. Oh wait, maybe it's through the PerlSpeak module. VBScript is kind of like the BASIC language. It's very similar. But if you are familiar with JavaScript, then that works as well. JavaScript's syntax is more like Perl's or C syntax. There are tons of VBScript example programs online and manuals and references. The above code I wrote that makes the computer talk I did it by looking up various things online. I mean I myself couldn't write it from memory, but if I have to write something, I can look it up online and put the program together using online references and articles. I rarely use VBScript, but it's a cool feature in Windows. | [reply] |
|
Re: Playing wav files
by merrymonk (Hermit) on Feb 08, 2019 at 21:58 UTC | |
| [reply] |
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.
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. | [reply] [d/l] [select] |
by merrymonk (Hermit) on Feb 09, 2019 at 14:11 UTC | |
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. | [reply] [d/l] |
by poj (Abbot) on Feb 09, 2019 at 15:04 UTC | |
by merrymonk (Hermit) on Feb 10, 2019 at 16:56 UTC | |
| |
by pryrt (Abbot) on Feb 09, 2019 at 17:14 UTC | |
by merrymonk (Hermit) on Feb 10, 2019 at 16:58 UTC | |
|
Re: Playing wav files
by karlgoethebier (Abbot) on Feb 08, 2019 at 20:18 UTC | |
May i kindly ask why you need to accomplish this mission? Just because i gave it up with my multimedia activities from the commandline decades ago for some good reasons. Best regards, Karl «The Crux of the Biscuit is the Apostrophe» perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help | [reply] [d/l] |