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.
jtrue

In reply to Win32::Sound 16 bit example by true

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.