Build a Simple Sound Cart for Windows

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

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


Running the Hack

Add a couple of .wav files to the directory where you placed Then run this command:

       C:\> perl 

It 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 way of doing layout. Then I run in the infinite MainLoop subroutine.

The result is Figure.

The Perl cart

I could have added a Quit button, but I figure the Close button on the window is just as good.

Where you go from here with this little script is up to you. Tk has the ability to show HTML in windows, so you can display your show notes along with the cart buttons. If you find a recording application that supports automation through COM, you can even drive that from the script using Win32::OLE and create a complete podcasting application.

See Also

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows