Hack55.Build a Simple Sound Cart for Windows


Hack 55. 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.

8.7.1. 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; 

8.7.2. Running the Hack

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

        C:\> perl perlcart.pl  

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 8-16.

Figure 8-16. 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.

8.7.3. See Also

  • "Choose the Right Audio Tools" [Hack #50]

  • "Build a Simple Sound Cart for Macintosh" [Hack #54]



    Podcasting Hacks
    Podcasting Hacks: Tips and Tools for Blogging Out Loud
    ISBN: 0596100663
    EAN: 2147483647
    Year: 2003
    Pages: 144

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net