in reply to Playing wav files

Also related: If you just want your computer to read something to you out loud, you can do that too. This code works on Windows XP and higher. This code creates a VBScript file on the hard drive and executes it, so it's not the most ideal solution, but the job gets done! Lol

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.)

#!/usr/bin/perl -w use strict; use warnings; SAY("Hello World!!!"); exit; # Usage: STATUS = SAY(TEXT, VOLUME) - Reads a text in computer voice o +n Windows computers (tested on Windows XP) Returns 1 if something was + read or 0 if nothing was read sub SAY { @_ or return 0; my $TEXT = shift; defined $TEXT or return 0; length($TEXT) or return 0; my $VOLUME = @_ ? shift : 100; defined $VOLUME or $VOLUME = 100; length($VOLUME) or $VOLUME = 100; $TEXT =~ tr|\"| |; # Remove all quotes print "\n\n$TEXT\n\n"; # For those who are deaf, it's important to a +lso print the same message. my $TEMP_VBS_FILE = 'C:\\TEMP\\SAY.VBS'; my $TEMPDIR = substr($TEMP_VBS_FILE, 0, rindex($TEMP_VBS_FILE, "\\") + + 1); my $VB_SCRIPT = "IF Wscript.Arguments.length > 1 THEN\nSET VOICE = C +reateObject(\"SAPI.SpVoice\")\nVOICE.Volume = Wscript.Arguments(0)\nV +OICE.Speak Wscript.Arguments(1)\nELSE\nWscript.echo \"Usage: say.vbs +<VOLUME> <TEXT>\"\nEND IF\n"; unless (-f $TEMP_VBS_FILE && -s $TEMP_VBS_FILE == length($VB_SCRIPT) +) { mkdir $TEMPDIR; open(SCRIPTFILE, "> $TEMP_VBS_FILE") or return 0; print SCRIPTFILE $VB_SCRIPT; close SCRIPTFILE; } `$TEMP_VBS_FILE $VOLUME \"$TEXT\"`; #unlink $TEMP_VBS_FILE; #rmdir $TEMPDIR; return 1; }

Replies are listed 'Best First'.
Re^2: Playing wav files
by Your Mother (Archbishop) on Feb 08, 2019 at 18:20 UTC

    FWIW, this is an option on OS X–

    moo@cowi>perl -e 'system say => qw/ Ohai der /' moo@cow>which say /usr/bin/say # say - Convert text to audible speech

    I use it for short scripts or command line stuff since the visual bell isn't always enough. Stuff like–

    run-my-thing.pl || say "Epic fail, hire a real programmer" # or echo "Long running script." && say "Done. Finally. Jeeze."

      Computer says no

Re^2: Playing wav files
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?

      Well, the good thing about VBScript is that it's included in Windows XP and all versions of Windows after that. So, if you create a file with .vbs extension and execute it, it will run. There's no need to install anything.

      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.