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