Re: master volume control in winmm using namespaces
- From: "Rich Milburn [MVP]" <richdotmilburnatapplebeesdotcom>
- Date: Fri, 10 Feb 2006 14:36:31 -0600
You Rock! it works great! I'll post the code here in case anyone is
interested. I have a command button called SetVol, and a text box called
Text1. Enter the percentage in the box and it sets both the master and wave
vol levels together.
it's mostly from that code at devx...
Rich
--
Rich Milburn
[MVP - Directory Services]
Const MMSYSERR_NOERROR = 0
Const MAXPNAMELEN = 32
Const MIXER_LONG_NAME_CHARS = 64
Const MIXER_SHORT_NAME_CHARS = 16
Const MIXER_GETLINEINFOF_COMPONENTTYPE = &H3&
Const MIXER_GETLINECONTROLSF_ONEBYTYPE = &H2&
Const MIXER_SETCONTROLDETAILSF_VALUE = &H0&
Const MIXERLINE_COMPONENTTYPE_DST_FIRST = &H0&
Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = &H4
Const MIXERCONTROL_CONTROLTYPE_VOLUME = &H50030001
Private Declare Function mixerOpen Lib "WINMM.DLL" (phmx As Long, _
ByVal uMxId As Long, ByVal dwCallback As Long, ByVal dwInstance As Long,
_
ByVal fdwOpen As Long) As Long
Private Declare Function mixerGetLineInfo Lib "WINMM.DLL" Alias _
"mixerGetLineInfoA" (ByVal hmxobj As Long, pmxl As MIXERLINE, _
ByVal fdwInfo As Long) As Long
Private Declare Function mixerGetLineControls Lib "WINMM.DLL" Alias _
"mixerGetLineControlsA" (ByVal hmxobj As Long, pmxlc As
MIXERLINECONTROLS, _
ByVal fdwControls As Long) As Long
Private Declare Function mixerSetControlDetails Lib "WINMM.DLL" (ByVal
hmxobj _
As Long, pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Long) As Long
Private Declare Function mixerClose Lib "WINMM.DLL" (ByVal hmx As Long) As
Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hmem As Long) As
Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hmem As Long) As
Long
Private Declare Function waveOutSetVolume Lib "WINMM.DLL" (ByVal wDeviceID
As Integer, ByVal dwVolume As Long) As Integer
Private Type MIXERCONTROL
cbStruct As Long
dwControlID As Long
dwControlType As Long
fdwControl As Long
cMultipleItems As Long
szShortName As String * MIXER_SHORT_NAME_CHARS
szName As String * MIXER_LONG_NAME_CHARS
lMinimum As Long
lMaximum As Long
reserved(10) As Long
End Type
Private Type MIXERCONTROLDETAILS
cbStruct As Long
dwControlID As Long
cChannels As Long
item As Long
cbDetails As Long
paDetails As Long
End Type
Private Type MIXERCONTROLDETAILS_UNSIGNED
dwValue As Long
End Type
Private Type MIXERLINE
cbStruct As Long
dwDestination As Long
dwSource As Long
dwLineID As Long
fdwLine As Long
dwUser As Long
dwComponentType As Long
cChannels As Long
cConnections As Long
cControls As Long
szShortName As String * MIXER_SHORT_NAME_CHARS
szName As String * MIXER_LONG_NAME_CHARS
dwType As Long
dwDeviceID As Long
wMid As Integer
wPid As Integer
vDriverVersion As Long
szPname As String * MAXPNAMELEN
End Type
Private Type MIXERLINECONTROLS
cbStruct As Long
dwLineID As Long
dwControl As Long
cControls As Long
cbmxctrl As Long
pamxctrl As Long
End Type
' Set the master volume level.
'
' VolumeLevel is the level value in percentage (0 = min, 100 = max)
' Returns True if successful
Function SetVolume(VolumeLevel As Long) As Boolean
Dim hmx As Long
Dim uMixerLine As MIXERLINE
Dim uMixerControl As MIXERCONTROL
Dim uMixerLineControls As MIXERLINECONTROLS
Dim uDetails As MIXERCONTROLDETAILS
Dim uUnsigned As MIXERCONTROLDETAILS_UNSIGNED
Dim RetValue As Long
Dim hmem As Long
' VolumeLevel value must be between 0 and 100
If VolumeLevel < 0 Or VolumeLevel > 100 Then GoTo error
' Open the mixer
RetValue = mixerOpen(hmx, 0, 0, 0, 0)
If RetValue <> MMSYSERR_NOERROR Then GoTo error
' Initialize MIXERLINE structure and call mixerGetLineInfo
uMixerLine.cbStruct = Len(uMixerLine)
uMixerLine.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
RetValue = mixerGetLineInfo(hmx, uMixerLine, _
MIXER_GETLINEINFOF_COMPONENTTYPE)
If RetValue <> MMSYSERR_NOERROR Then GoTo error
' Initialize MIXERLINECONTROLS strucure and
' call mixerGetLineControls
uMixerLineControls.cbStruct = Len(uMixerLineControls)
uMixerLineControls.dwLineID = uMixerLine.dwLineID
uMixerLineControls.dwControl = MIXERCONTROL_CONTROLTYPE_VOLUME
uMixerLineControls.cControls = 1
uMixerLineControls.cbmxctrl = Len(uMixerControl)
' Allocate a buffer to receive the properties of the master volume
control
' and put his address into uMixerLineControls.pamxctrl
hmem = GlobalAlloc(&H40, Len(uMixerControl))
uMixerLineControls.pamxctrl = GlobalLock(hmem)
uMixerControl.cbStruct = Len(uMixerControl)
RetValue = mixerGetLineControls(hmx, uMixerLineControls, _
MIXER_GETLINECONTROLSF_ONEBYTYPE)
If RetValue <> MMSYSERR_NOERROR Then GoTo error
' Copy data buffer into the uMixerControl structure
CopyMemory uMixerControl, ByVal uMixerLineControls.pamxctrl, _
Len(uMixerControl)
GlobalFree hmem
hmem = 0
uDetails.item = 0
uDetails.dwControlID = uMixerControl.dwControlID
uDetails.cbStruct = Len(uDetails)
uDetails.cbDetails = Len(uUnsigned)
' Allocate a buffer in which properties for the volume control are set
' and put his address into uDetails.paDetails
hmem = GlobalAlloc(&H40, Len(uUnsigned))
uDetails.paDetails = GlobalLock(hmem)
uDetails.cChannels = 1
uUnsigned.dwValue = CLng((VolumeLevel * uMixerControl.lMaximum) / 100)
CopyMemory ByVal uDetails.paDetails, uUnsigned, Len(uUnsigned)
' Set new volume level
RetValue = mixerSetControlDetails(hmx, uDetails, _
MIXER_SETCONTROLDETAILSF_VALUE)
GlobalFree hmem
hmem = 0
If RetValue <> MMSYSERR_NOERROR Then GoTo error
mixerClose hmx
' signal success
SetVolume = True
Exit Function
error:
' An error occurred
' Release resources
If hmx <> 0 Then mixerClose hmx
If hmem Then GlobalFree hmem
' signal failure
SetVolume = False
End Function
Private Sub SetWaveVol(VolumeLevel As Long)
Dim a As Long
Dim tmp, vol As String
vol = VolumeLevel / 100 * 65535
tmp = Right((Hex$(vol + 65536)), 4)
vol = CLng("&H" & tmp & tmp)
a = waveOutSetVolume(0, vol)
End Sub
Private Sub SetVol_Click()
SetVolume (Text1.Text)
SetWaveVol (Text1.Text)
End Sub
"The Grim Reaper" <grim_reaper@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:dsirui$d2b$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Right.. after over 10 minutes of digging through my 500 gig hard drive....
lol... found my old code!
I used the MixerLine and Mixer structures and some different APIs to the
WaveOutSetVolume thingy.
A quick search on MSDN found the link below, which is a bit clearer than
my old code!
http://www.devx.com/vb2themax/Tip/19040?type=kbArticle&trk=MSCP
Hope that helps get your master volume under control!
______________________________________________
The Grim Reaper
"Rich Milburn [MVP]" <richdotmilburnatapplebeesdotcom> wrote in message
news:uLPVv$mLGHA.208@xxxxxxxxxxxxxxxxxxxxxxx
I downloaded the source code you referred to, and tried to wade through
it... it sure does a lot of stuff... I did indeed already find the MSDN
link on waveOutSetVolume, and I have indeed gotten that one working. What
I could not figure out is what is the comparable API for
MasterOutSetVolume - not that it is called that, it's not - if it was, I
wouldn't have to ask :)
I did post this to directx.audio also, but that group is pretty slow...
I cut my vb6 code down to:
Private Declare Function waveOutSetVolume Lib "WINMM.DLL" (ByVal
wDeviceID
As Integer, ByVal dwVolume As Long) As Integer
Private Sub SetVol_Click()
Dim a As Long
Dim tmp, vol As String
vol = Text1 / 100 * 65535
tmp = Right((Hex$(vol + 65536)), 4)
vol = CLng("&H" & tmp & tmp)
a = waveOutSetVolume(0, vol)
End Sub
any ideas what the master volume set function is called, or how I can
find out? Did you figure that out in VB6?
thanks again -
Rich
--
Rich Milburn
[MVP - Directory Services]
"The Grim Reaper" <grim_reaper@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:dsgkbe$mtj$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I managed to write a class to control Wave, Master, CD player, Line In
and Microphone volumes in the windows mixer many years ago.
It was in VB6, and took me several months of research to build it all!
I upgraded it to VB2003 a few years ago, but I don't think I ever got it
working like it used to.
This article is the most useful one I've found so far - it's C#, but I
find that easier to translate to VB.NET than VB6 sometimes!
http://www.codeproject.com/useritems/AudioLib.asp
As for your code below, here's a few pointers;
a) Change your As Short declarations to As Integer (a Long in VB6
translates to a Short in .NET, you want an Integer (32 bits))
b) Dim 'tmp' as a string, and 'a' as integer, not objects. That clears
off the first 3 upgrade warnings.
c) Switch Option Strict On - it's there to help - not just fill up the
task list/error list!!
This is the MSDN entry for WaveOutSetVolume. I'm suprised you couldn't
find it in your local MSDN(??).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_waveoutsetvolume.asp
Start there, and go through each of the "See Also" items to get your
head round how the API call works.
This post sets the WaveOutVolume to 0. Note the different API
declaration to your "old" method.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=178167&SiteId=1
Hope that gets you started.
_____________________________________________________
The Grim Reaper
"Rich Milburn [MVP]" <richdotmilburnatapplebeesdotcom> wrote in message
news:eZ2D$pcLGHA.1180@xxxxxxxxxxxxxxxxxxxxxxx
Ok I am not a programmer, I copied some code and very painfully got it
working in VB6. I can adjust the volume with waveOutSetVolume on
winmm.dll. But I could not seem to be able to figure out how to adjust
the master volume. I thought maybe if I upgraded this into VS2005
VB.Net then it might be a little easier. But now I am getting
PInvokeStackImbalance messages on this, and it doesn't even run. We're
talking a very little program... A programmer-friend of mine told me
this might be related to me calling dlls instead of using namespaces,
he thinks something.computer.soundcard.volume.set or some such thing.
My experience is vbScript so I am lost - I read you can't do this in
vbScript, there are lots of questions out there about how do you
control the master volume, lots of answers that dodge that question...
anyone want to take a stab at fixing this, or pointing me to a good
help-myself point? I do have MSDN Library but I have not made it
through the whole DVD yet :op I'm not sure what to even search for. I
suspect there is something like waveOutSetVolume but not starting with
wave....
Anyway first is the VB.Net conversion (this is all in the form code),
complete with errors, then is the vb6 code that works - type in a
percent and it sets the wave volume.
TIA if anyone wants to take a stab at it...
--
Rich Milburn
[MVP - Directory Services]
'----------------vbNet code-----------------------------
' This gets several errors from the conversion from vb6, most of it is
copied and modified from vb6 (I think) examples
Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function waveOutSetVolume Lib "WINMM.DLL" (ByVal
wDeviceID As Short, ByVal dwVolume As Integer) As Short
Private Declare Function waveOutGetVolume Lib "WINMM.DLL" (ByVal
wDeviceID As Short, ByRef lpdwVolume As Integer) As Short
Const SND_ASYNC As Short = &H1s
Const SND_NODEFAULT As Short = &H2s
Dim CurrentVolLeft As Integer
Dim CurrentVolRight As Integer
Private Sub SetMaxVol_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles SetMaxVol.Click
Dim x As Short
Dim a As Object
Dim i As Integer
Dim tmp As Object
Dim vol As String
vol = CStr(CDbl(Text1.Text) / 100 * 65535)
'UPGRADE_WARNING: Couldn't resolve default property of object tmp.
Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
tmp = VB.Right(Hex(CDbl(vol) + 65536), 4)
'UPGRADE_WARNING: Couldn't resolve default property of object tmp.
Click for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
vol = CStr(CInt("&H" & tmp & tmp))
'UPGRADE_WARNING: Couldn't resolve default property of object a. Click
for more:
'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
a = waveOutSetVolume(0, CInt(vol))
' x = waveOutSetVolume(0, &HFFFFFFFF)
End Sub
Private Sub cmdRefresh_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdRefresh.Click
Dim x As Short
Dim BothVolumes As Integer
Dim tmp As String
x = waveOutGetVolume(0, BothVolumes)
CurrentVolLeft = BothVolumes And &HFFFF
CurrentVolRight = (CShort(BothVolumes And &HFFFF0000) / &H10000) And
&HFFFF
LeftVol.Text = Hex(CurrentVolLeft)
RightVol.Text = Hex(CurrentVolRight)
tmp = "&h" & VB.Right(Hex(BothVolumes), 4)
Text1.Text = CStr((CInt(tmp) / 65535) * 100)
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles MyBase.Load
cmdRefresh_Click(cmdRefresh, New System.EventArgs())
End Sub
End Class
'------------------ end vbNet code--------------------------
'vb6 code -----------------------------------------------
'this works for wave volume, but could not figure out master volume
Option Explicit
Private Declare Function waveOutSetVolume Lib "WINMM.DLL" (ByVal
wDeviceID As Integer, ByVal dwVolume As Long) As Integer
Private Declare Function waveOutGetVolume Lib "WINMM.DLL" (ByVal
wDeviceID As Integer, lpdwVolume As Long) As Integer
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Dim CurrentVolLeft As Long
Dim CurrentVolRight As Long
Private Sub SetMaxVol_Click()
Dim x As Integer
Dim a, i As Long
Dim tmp, vol As String
vol = Text1 / 100 * 65535
tmp = Right((Hex$(vol + 65536)), 4)
vol = CLng("&H" & tmp & tmp)
a = waveOutSetVolume(0, vol)
' x = waveOutSetVolume(0, &HFFFFFFFF)
End Sub
Private Sub cmdRefresh_Click()
Dim x As Integer
Dim BothVolumes As Long
Dim tmp As String
x = waveOutGetVolume(0, BothVolumes)
CurrentVolLeft = BothVolumes And &HFFFF&
CurrentVolRight = ((BothVolumes And &HFFFF0000) / &H10000) And
&HFFFF&
LeftVol.Caption = Hex$(CurrentVolLeft)
RightVol.Caption = Hex$(CurrentVolRight)
tmp = "&h" & Right(Hex$(BothVolumes), 4)
Text1 = (CLng(tmp) / 65535) * 100
End Sub
Private Sub Form_Load()
cmdRefresh_Click
End Sub
'--------------------- end vb6
code -----------------------------------------------
.
- Follow-Ups:
- Re: master volume control in winmm using namespaces
- From: The Grim Reaper
- Re: master volume control in winmm using namespaces
- References:
- master volume control in winmm using namespaces
- From: Rich Milburn [MVP]
- Re: master volume control in winmm using namespaces
- From: The Grim Reaper
- Re: master volume control in winmm using namespaces
- From: Rich Milburn [MVP]
- Re: master volume control in winmm using namespaces
- From: The Grim Reaper
- master volume control in winmm using namespaces
- Prev by Date: Set PasswordExpired
- Next by Date: Just-In-Time Debugging?????
- Previous by thread: Re: master volume control in winmm using namespaces
- Next by thread: Re: master volume control in winmm using namespaces
- Index(es):
Loading