Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Win32::Sound 16 bit example

by true (Pilgrim)
on Jul 20, 2005 at 23:18 UTC ( [id://476655]=note: print w/replies, xml ) Need Help??


in reply to Re: Win32::Sound 16 bit example
in thread Win32::Sound 16 bit example

By george i think that's the answer. Sound generated with your fix is the exact same tone as the 8bit one!!! Thanks so much for your help BrowserUK! I tried to do this over a year ago and was stuck then too. I have read the pack docs so many times its crazy but their brevity times my ignorance about value types still confuses me. Here is a working example with your fix for a 16bit mono sound wave...
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("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 again, hopefully someone else can use this too. BTW if anyone wants to do this in stereo here's the code for 16bit stereo sound too. Just as above, this generates a 1 second sinusoidal wave at 440Hz but in both channels.
use Win32::Sound; # Create the object $WAV = new Win32::Sound::WaveOut(44100, 16, 2); $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("v", $v); $data .= pack("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 exit;
Thanks again all!
jtrue

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found