Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sound in perl?

Status
Not open for further replies.

tekpchak

Technical User
Jan 5, 2005
17
US
I need to make a timer. I need to create a loop that runs every 4 minutes. When the 4 minutes is up play a sound. Just a simple ding. I was wondering how this could be done in perl. Would this be better suited in Java? It will be run on a windows xp system. If this were linux/unix I can do this with a shell script. I however am just starting to dabble in perl. Thanks for the help.
 
This works on WinXP with last ActiveState(5.10.0, binary build 1002) that I installed:

Code:
use strict;
use Win32::Sound;
while()
  {
  Win32::Sound::Volume('100%');
  Win32::Sound::Play("tada.wav");
  Win32::Sound::Stop();
  sleep 240; 
  }

Kordaff
 
If you remember that Tk is not spelled 'use TK;' then you can use Win32::Sound in a not really less trivial example like this:

Code:
use strict;
use Tk;
use Win32::Sound;

my $sound="tada.wav";
my $timer_id;
my $mw=new MainWindow();

my %row1=(-side=>'left',-pady=>10,-padx=>10);
my %row2=(-side=>'bottom',-pady=>10,-padx=>10);

my $b5=$mw->Button(-text => "EXIT", -command => sub {exit})->pack( %row2 );
my $b1=$mw->Button(-text => "play", -command => \&start_sound)->pack( %row1 );
my $b2=$mw->Button(-text => "stop", -command => \&stop_sound)->pack( %row1 );
my $b3=$mw->Button(-text => "start timer", -command => \&timer_on)->pack( %row1 );
my $b4=$mw->Button(-text => "stop timer", -command => \&timer_off)->pack( %row1 );

MainLoop();

sub start_sound()
  {
  Win32::Sound::Volume('100%');
  Win32::Sound::Play("tada.wav",SND_ASYNC);
  }
sub stop_sound()
  {
  Win32::Sound::Stop();
  }
sub timer_on()
  {
  if (! defined $timer_id)
    {
    $timer_id=$mw->repeat(1200,\&start_sound);
    }
  }
sub timer_off()
  {
  if (defined $timer_id)
    {
    $timer_id->cancel;
    undef($timer_id);
    }
  }

Not sure why a perl -w on script with 'use TK;' just shows lots of subs being renamed and no real error... ???

Kordaff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top