We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

waveOutSetVolume()

coq
coq asked
on
Medium Priority
3,169 Views
Last Modified: 2008-03-03
I've got a problem when setting the volume of the wave output. waveOutSetVolume takes 2 arguments; the first is the  id of the sounddevice and the second is the volume. The problem is that the second argument should be a unsigned long, but VB can just handle signed longs.
Anyone got a solution of how to solve this problem?

The code i have today (that doesnt fully work) is:
(left & right are the volume in percent)

Function SetVolume(left As Integer, right As Integer)
    Dim i As Integer
    Dim BothVolumes As Long
    Dim tt As Currency
    Dim tt2 As Currency
   
    CurrentVolLeft = (left * 65535) / 100
    CurrentVolRight = ((right * 65535) / 100) And &HFFFF&
    tt2 = CurrentVolRight
    If tt2 > 32767 Then
        tt = tt2 * 32768#
        tt = -tt
        tt = tt + 32767 * 65535
    Else
        tt = tt2 * 65535#
    End If
    tt = tt And &HFFFF0000
    CurrentVolRight = tt
    i = waveOutSetVolume(0, CurrentVolRight Or CurrentVolLeft)
End Function

/johan
Comment
Watch Question

Commented:
Hi !

Long and Unsigned Long are same 32 bit data types.
the difference is only in that how Visual Basic Or C++ Interpritates (Convert) it into a number.

So If you write in Visual Basic:

dim a as long
a = &HFFFFFFFF  
msgbox a

So In Visual Basic You`ll Get -1
But In C++ When you Print it Using Printf you'll get
Number: 2^32 -1 (  4294967295 )


Function that Inputs ( L as Long) and Returns (L*2 as Long)
( So Windows function ( Not VB function) would get it as   UnsignedLong)

(0 <= L < 2^31)

Function L2UL(L As long) As Long
 Dim TL As Long
  If L < &H40000000 Then
   L2UL = L * 2
  Else
   TL = L Xor &H40000000
   L2UL = TL * 2 - 2 ^ 31
  End If
 End Function

Now You Can make Your Sub:

' Inputs Volume as long ( 0 <= Valume < 2^31)
Sub MywaveOutSetVolume ( Volume as long )
 dim NewValue as long
 NewValue = L2UL(Volume)
 waveOutSetVolume(0, NewValue)
End Sub

so MywaveOutSetVolume (17) same to waveOutSetVolume(0,34)
and MywaveOutSetVolume(2^31-1)same to waveOutSetVolume(0,2^32-2)

PS:
   I think that waveOutSetVolume is not Linear Function
   ( Volume of waveOutSetVolume(2*k) is not twice than  waveOutSetVolume(k), I think that the function is proportional to Log)
 !!! I haven't read the Help its only depends of my
     Physics Knowledge !!!!
             
     
I Hope it's will help you !!!




 









 
When I have worked with setting the wave volume, I used the following function to combine the left and right volumes into the DoubleWord that is required.

Function MakeDWord(wHi As Long, wLo As Long) As Long
    If wHi And &H8000& Then
        MakeDWord = (((wHi And &H7FFF&) * 65536) Or (wLo And &HFFFF&)) Or &H80000000
    Else
        MakeDWord = (wHi * &H10000) + (wLo And &HFFFF&)
    End If
End Function

With this your function could be coded as follows:

Function SetVolume(left As Integer, right As Integer)
  Dim i As Integer
  Dim BothVolumes As Long
  Dim CurrentVolLeft As Long
  Dim CurrentVolRight As Long
  Dim tt As Currency
  Dim tt2 As Currency
                         
  CurrentVolLeft = (left * 65535) / 100
  CurrentVolRight = (right * 65535) / 100
  BothVolumes = MakeDWord(CurrentVolLeft, CurrentVolRight)
  i = waveOutSetVolume(0, BothVolumes)
End Function

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.