true has asked for the wisdom of the Perl Monks concerning the following question:
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, 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
Thanks for reading.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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32::Sound 16 bit example
by BrowserUk (Patriarch) on Jul 20, 2005 at 22:56 UTC | |
by true (Pilgrim) on Jul 20, 2005 at 23:18 UTC | |
|
Unpacking 16bit sound
by true (Pilgrim) on Jul 22, 2005 at 19:44 UTC | |
by BrowserUk (Patriarch) on Jul 22, 2005 at 20:20 UTC |