Solved

waveOutSetVolume()

Posted on 1997-04-10
2
2,458 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
0
Comment
Question by:coq
2 Comments
 

Expert Comment

by:Msoft
ID: 1425199
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 !!!




 









 
0
 

Accepted Solution

by:
syberdyne earned 70 total points
ID: 1425200
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

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel VBA combine two working workbooks 8 60
VBA saving file message display 5 72
passing parameter in sql procedure 9 65
fso.FolderExists("\\server\HiddenFolder$") 4 76
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question