Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Wave Playback Program Using Win32::Sound and Tk

by lofichurch (Beadle)
on Jun 20, 2002 at 16:24 UTC ( [id://176077]=perlcraft: print w/replies, xml ) Need Help??

   1: # WavePlayBack.pl (c) 2002 C. Church
   2: # This program is distributed under the same terms
   3: # as perl its self.
   4: #----------------------------------------
   5: 
   6: use strict;
   7: use warnings;
   8: 
   9: use Win32::Sound;
  10: use Audio::Wav;
  11: 
  12: use Tk;
  13: use Tk::Toplevel;
  14: use Tk::Frame;
  15: use Tk::Label;
  16: use Tk::Button;
  17: 
  18: my $wave_file = shift;
  19: 
  20: if(!defined($wave_file)) {
  21:   die("Usage: $0 <wave file to play>\n");
  22:   }
  23: 
  24:      # our main body
  25:      
  26: my $main_window = Tk::MainWindow->new( -title => 'PlayBack Controls');
  27: 
  28: my $is_paused = 0;
  29: 
  30: my $lbl = $main_window->Label(-text => "Please Wait While $wave_file is Read...")->pack(-side => 'top');
  31: 
  32: $main_window->update();
  33: 
  34: my $datRef = read_file($wave_file);
  35: 
  36: $lbl->destroy();
  37: $main_window->update();
  38: 
  39: play_back($datRef,$main_window);
  40: 
  41: MainLoop();
  42: 
  43: exit(0);
  44: 
  45:      # subroutines
  46:      
  47: sub read_file {
  48: 
  49:  my $file = shift;
  50:  
  51:  my $wav = Audio::Wav->new();
  52:  my $readF = $wav->read($file);
  53:  
  54:  die("Unable to read $file\n") if(!defined($readF));
  55:  
  56:  my($tmpf,$data);
  57:  
  58:  $data .= $tmpf while(defined($tmpf = $readF->read_raw(4096)));
  59:  
  60:  my $hash = $readF->details();
  61:  my %tmpH = ();
  62:  
  63:  $tmpH{'data'} = $data;
  64:  $tmpH{'srate'} = $hash->{'sample_rate'};
  65:  $tmpH{'bits'} = $hash->{'bits_sample'};
  66:  $tmpH{'channels'} = $hash->{'channels'};
  67:  
  68:  return(\%tmpH);
  69: }
  70:  
  71: sub play_back {
  72: 
  73:  my $hRef = shift;
  74:  my $mw = shift;
  75: 
  76:  my $object;
  77: 
  78:  my $dat = $hRef->{'data'};
  79:  my $srate = $hRef->{'srate'};
  80:  my $bits = $hRef->{'bits'};
  81:  my $chan = $hRef->{'channels'};
  82:  
  83:  if(!defined($dat) || !defined($srate) || !defined($bits) || !defined($chan)) {
  84:      die("ERROR: [play_back] Not enough data to playback.\n");
  85:      }
  86:      
  87:  eval { $object = new Win32::Sound::WaveOut($srate,$bits,$chan); };
  88: 
  89:  if($@) {
  90:     die("WARNING: WaveOut Returned: $@\n");
  91:     }
  92: 
  93: 
  94:      $is_paused = 0;
  95: 
  96:      my $frame1 = $mw->Frame()->pack(-side => 'top');
  97:      my $frame2 = $mw->Frame()->pack(-side => 'top');
  98:      my $frame3 = $mw->Frame()->pack(-side => 'top');
  99: 
 100:      $frame1->Label(-text => "Playback Controls")->pack();
 101: 
 102:      $frame2->Button(-text => "Play", -command => sub { if($is_paused == 0) { $object->Load($dat); $object->Write(); } else { $object->Restart(); $is_paused = 1; } })->pack(-side => "left");
 103: 
 104:      $frame2->Button(-text => "Pause", -command => sub { $object->Pause(); $is_paused = 1; })->pack(-side => "left");
 105: 
 106:      $frame2->Button(-text => "Stop", -command => sub { $object->Reset(); } )->pack(-side => 'left');
 107: 
 108:      $frame2->Button(-text => "Rew", -command => sub { $object->Reset(); })->pack(-side => 'left');
 109: 
 110:      $frame3->Button(-text => "Close", -command => sub { $object->Reset(); $object->Unload(); $object->CloseDevice(); exit(0); })->pack(-side => 'top');
 111: 
 112:  return(1);
 113: }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found