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
        tt = tt2 * 65535#
    End If
    tt = tt And &HFFFF0000
    CurrentVolRight = tt
    i = waveOutSetVolume(0, CurrentVolRight Or CurrentVolLeft)
End Function

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
   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)

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.