Build a Simple Sound Cart for Windows
Using ActiveState Perl on Windows, you can build a quick cart program to play WAV files on demand.
ActiveState Perl comes with Tk (see
http://www.tcl.tk/) support built in for creating reasonably good-looking Windows interfaces quickly. It also comes with the
Win32::Sound module
[Hack #7]
, which makes it simple to play WAV files with a single command. So, I figured I would take these two packages and slap them together to make a simple Windows sound cart program. The result is this script.
The Code
Save this file as perlcart.pl:
use Tk;
use Tk::Button;
use DirHandle;
use Win32::Sound;
use strict;
sub play($) { Win32::Sound::Play( $_[0] ); }
my $main = new MainWindow();
my $dh = new DirHandle( "." );
while( my $filename = $dh->read() )
{
next unless $filename =~ /[.]wav$/i;
my $buttonname = $filename;
$buttonname =~ s/[.]wav$//i;
my $button = $main->Button( -text => $buttonname,
-command => sub { play( $filename ); } );
$button->pack( -fill => "x", -padx => 2, -pady => 2 );
}
MainLoop;Running the Hack
Add a couple of .wav files to the directory where you placed perlcart.pl. Then run this command:
C:\> perl perlcart.plIt doesn’t come a whole lot simpler than this. I create the main window, then iterate through the files in the current directory looking for .wav files. Each time I find one, I add a new button with a command handler that will play the file using the play subroutine. I then pack the button into the main window, which is Tk’s simplest ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access