slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Fello Monks, I am stumped on the output of the waveOutGetVolume Win32 API. Can anyone help me getting the proper output from this API call? I want to convert it to a number from 1-100 representing the volume percantage.
my $hwnd = shift || 0; my $vol=" "x10; $waveOutGetVolume ||= new Win32::API("Winmm", "waveOutGetVolume", ['N' +,'P'], 'N') || return; my $ck = $waveOutGetVolume->Call($hwnd, $vol);

Replies are listed 'Best First'.
Re: Output syntax for waveOutGetVolume API
by BrowserUk (Patriarch) on Jul 14, 2004 at 03:55 UTC

    Derived from waveOutGetVolume

    ## following on from your code ## If the values are wacky try 'nn' instead of 'vv' my( $leftVol, $rightVol ) = unpack 'vv', $vol; my $leftPercent = $leftVol * 100 / 0xffff; my $rightPercent = $rightVol * 100 / 0xffff; ## Numerically correct if not to a sound engineer my $totalVolPercent = ( $leftPercent + $rightPercent ) / 2;

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      Thanks a lot. That worked great!

      For the record, here is the final code wrapped into a subroutine.

      sub getSoundVolume { #usage: $tpcnt=getSoundVolume($hwnd) or ($tpcnt,$lpcnt,$rpcnt)=get +SoundVolume($hwnd); #info: gets sound volume - $tpcnt is a number from 0 to 100. my $hwnd = shift || 0; my $vol=" "x10; $waveOutGetVolume ||= new Win32::API("Winmm", "waveOutGetVolume", +['N','P'], 'N') || return; my $ck = $waveOutGetVolume->Call($hwnd, $vol); my ($leftVol,$rightVol) = unpack 'vv', $vol; my $leftPercent = int($leftVol * 100 / 0xffff); my $rightPercent = int($rightVol * 100 / 0xffff); my $totalVolPercent = int(($leftPercent+$rightPercent)/2); if(wantarray){return ($totalVolPercent,$leftPercent,$rightPercent) +;} return $totalVolPercent; }