Aldo wrote a great module
Win32::Sound. I have been struggling with making an example using 16bit sound though and i would dig some help from any monk willing. Here's the example included with Win32::Sound distro. It generates a 8 bit sound in stereo.
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
Above example works great. My questoin is how to do this with 16bit sound. 16 bit sound has a range of 65335 values. It is stored different from 8bit as a signed integer value -32768 <-> 32767. Zero (no sound is 0). My best guess is to use pack i but i know it is not correct. The below example tries to make a 16 bit mono wav file the same way Aldo's 8bit example works. One 16bit sound is stored in 2 bytes while 8bit sound is stored in one. The pack i line is the line that needs correcting. WAV files are stored in little endian order.
use Win32::Sound;
# Create the object
$WAV = new Win32::Sound::WaveOut(44100, 16, 1);
$data = "";
$counter = 0;
$increment = 440/44100;
# Generate 44100 samples ( = 1 second)
for $i (1..44100) {
# Calculate the pitch
# (range 0..65335 for 16 bits)
$v = int(sin($counter/2*3.14) * (65335/2));
#signed integer (v) range -32768 <-> 32767
$data .= pack("i", $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
Thanks for reading.