Newbie: Win32:Sound:WaveOut problem

D

DG

Hello and thanks for the help,

I would like to play a *portion* of a wav file (say from sample 400 to
sample 9000) on a Windows machine.
Unfortunately, I'm unable to get my code to work. Below are my tests.

I tried the following code:

use Win32::Sound;
$WAV = new Win32::Sound::WaveOut("klaxton.wav"); #open "klaxton.wav"
located in the current directory
$WAV->Play(400,9000); #play from sample 400 to
sample 9000


When I run it, I get the following error messages:
Use of uninitialized value at C:/Perl/site/lib/Win32/Sound.pm line 109.
Argument "klaxton.wav" isn't numeric in entersub at
C:/Perl/site/lib/Win32/Sound.pm line 109.


So I tried the modified code below:

use Win32::Sound;
$WAV = new Win32::Sound::WaveOut(); #use defaults
$WAV->Open("klaxton.wav"); #open "klaxton.wav" located in the
current directory
$WAV->Play(400,9000); #play from sample 400 to sample 9000

When I run it, I get the following error messages:
Use of uninitialized value at C:/Perl/site/lib/Win32/Sound.pm line 109.
Use of uninitialized value at C:/Perl/site/lib/Win32/Sound.pm line 109.
Use of uninitialized value at C:/Perl/site/lib/Win32/Sound.pm line 109.
Use of uninitialized value at x.bat line 28.
Argument "klaxton.wav" isn't numeric in entersub at x.bat line 28.


To test the Sound package, I ran the following code (it worked fine):
use Win32::Sound;
Win32::Sound::play("klaxton.wav");


To test the WaveOut package, I ran the example code given in the
documentation (shown below). It worked fine.


use Win32::Sound;

# Create the object
$WAV = new Win32::Sound::WaveOut(44100, 8, 2);

$data = "";
$counter = 0;
$increment = 440/44100;

# Generate 44100 samples ( = 1 second)
for $i (1..44100) {

# Calculate the pitch
# (range 0..255 for 8 bits)
$v = sin($counter/2*3.14) * 128 + 128;

# "pack" it twice for left and right
$data .= pack("cc", $v, $v);

$counter += $increment;
}

$WAV->Load($data); # get it
$WAV->Write(); # hear it
1 until $WAV->Status(); # wait for completion
$WAV->Save("sinus.wav"); # write to disk
$WAV->Unload(); # drop it


So the packages are working. The wav file is present. Yet I can't get my
original code to work. What am I doing wrong?

thanks for the help,
DG
 
D

DG

I found a solution at:
http://aspn.activestate.com/ASPN/Mail/Message/activeperl/1520559
The "Win32:Sound:WaveOut" module seems to have a bug when opening files.
The link above shows how to fix this module.

Also, there seems to be a documentation error. "Play([FROM, TO])" should be
"Play([FROM, DURATION])". For example, if you want to play from sample
#1200 to sample #1800 (duration of 600 samples) the documentation says that
you would use "$WAV->Play(1200,1800)". But my tests show that you should
use "$WAV->Play(1200,600)". So the second term is NOT the "TO" location, it
is the "DURATION" or number of samples to play starting after "FROM".

Hope this info helps someone else.
DG
 
D

DG

I found a solution to this problem at:
http://aspn.activestate.com/ASPN/Mail/Message/activeperl/1520559

It seems that the Win32::Sound:WaveOut module has a bug. It won't open
files. The above link tells how to fix this module.

Also, I discovered that the documentation for "Play([FROM, TO])" is also
incorrect. It should be "Play([FROM, LENGTH])". For example: if you want
to play starting from sample #1200 to sample #1800 (LENGTH = 1800 - 1200 =
600), the documentation leads you to believe that you should use
"$WAV->Play(1200,1800)". My tests show that statement to be incorrect. The
correct statement should be "$WAV->Play(1200,600)". In other words
"$WAV->Play([FROM, LENGTH])".

I hope this info helps someone else.
DG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top