Solved

waveOutSetVolume()

Posted on 1997-04-10
2
2,560 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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