btwhite
asked on
How to set microphone recording levels
My VB app uses the MCI control and a microphone to record wave files. But on some PC's, the microphone level may be turned way down, or the microphone is deselected. In these cases, the recording doesn't work.
How can I control the record level and the microphone select from inside my program?
Thanks.
How can I control the record level and the microphone select from inside my program?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmmm. Isn't it kind of obscene that it requires 260+ lines of code to change the microphone volume? I haven't found any other way of doing it, however.
Force accepting proposed answer.
costello
Community Support Moderator @ Experts-Exchange
costello
Community Support Moderator @ Experts-Exchange
Here is the VB.NET code
Option Strict Off
Option Explicit On
Module TEST
Private Const MMSYSERR_NOERROR As Short = 0
Private Const MAXPNAMELEN As Short = 32
Private Const MIXER_LONG_NAME_CHARS As Short = 64
Private Const MIXER_SHORT_NAME_CHARS As Short = 16
Private Const MIXER_GETLINEINFOF_COMPONE NTTYPE As Integer = &H3
Private Const MIXER_GETLINEINFOF_SOURCE As Integer = &H1
Private Const MIXER_GETCONTROLDETAILSF_V ALUE As Integer = &H0
Private Const MIXER_GETLINECONTROLSF_ONE BYTYPE As Integer = &H2
Private Const MIXERLINE_COMPONENTTYPE_DS T_FIRST As Integer = &H0
Private Const MIXERLINE_COMPONENTTYPE_SR C_FIRST As Integer = &H1000
Private Const MIXERLINE_COMPONENTTYPE_DS T_SPEAKERS As Decimal = (MIXERLINE_COMPONENTTYPE_D ST_FIRST + 4)
Private Const MIXERLINE_COMPONENTTYPE_SR C_MICROPHO NE As Decimal = (MIXERLINE_COMPONENTTYPE_S RC_FIRST + 3)
Private Const MIXERLINE_COMPONENTTYPE_SR C_LINE As Decimal = (MIXERLINE_COMPONENTTYPE_S RC_FIRST + 2)
Private Const MIXERCONTROL_CT_CLASS_FADE R As Integer = &H50000000
Private Const MIXERCONTROL_CT_UNITS_UNSI GNED As Integer = &H30000
Private Const MIXERCONTROL_CONTROLTYPE_F ADER As Boolean = (MIXERCONTROL_CT_CLASS_FAD ER Or MIXERCONTROL_CT_UNITS_UNSI GNED)
Private Const MIXERCONTROL_CONTROLTYPE_V OLUME As Boolean = (CShort(MIXERCONTROL_CONTR OLTYPE_FAD ER) + 1)
Private Const MIXERLINE_COMPONENTTYPE_DS T_WAVEIN As Decimal = (MIXERLINE_COMPONENTTYPE_D ST_FIRST + 7)
Private Const MIXER_SETCONTROLDETAILSF_V ALUE As Integer = &H0
Private Structure MIXERCAPS
Dim wMid As Short ' manufacturer id
Dim wPid As Short ' product id
Dim vDriverVersion As Integer ' version of the driver
<VBFixedString(MAXPNAMELEN ),System.R untime.Int eropServic es.Marshal As(System. Runtime.In teropServi ces.Unmana gedType.By ValTStr,Si zeConst:=M AXPNAMELEN )> Public szPname As String ' product name
Dim fdwSupport As Integer ' misc. support bits
Dim cDestinations As Integer ' count of destinations
End Structure
Private Structure MIXERCONTROL
Dim cbStruct As Integer ' size in Byte of MIXERCONTROL
Dim dwControlID As Integer ' unique control id for mixer device
Dim dwControlType As Integer ' MIXERCONTROL_CONTROLTYPE_x xx
Dim fdwControl As Integer ' MIXERCONTROL_CONTROLF_xxx
Dim cMultipleItems As Integer ' if MIXERCONTROL_CONTROLF_MULT IPLE set
<VBFixedString(MIXER_SHORT _NAME_CHAR S),System. Runtime.In teropServi ces.Marsha lAs(System .Runtime.I nteropServ ices.Unman agedType.B yValTStr,S izeConst:= MIXER_SHOR T_NAME_CHA RS)> Public szShortName As String ' short name of control
<VBFixedString(MIXER_LONG_ NAME_CHARS ),System.R untime.Int eropServic es.Marshal As(System. Runtime.In teropServi ces.Unmana gedType.By ValTStr,Si zeConst:=M IXER_LONG_ NAME_CHARS )> Public szName As String ' long name of control
Dim lMinimum As Integer ' Minimum value
Dim lMaximum As Integer ' Maximum value
<VBFixedArray(10)> Dim reserved() As Integer ' reserved structure space
'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 026"'
Public Sub Initialize()
ReDim reserved(10)
End Sub
End Structure
Private Structure MIXERCONTROLDETAILS
Dim cbStruct As Integer ' size in Byte of MIXERCONTROLDETAILS
Dim dwControlID As Integer ' control id to get/set details on
Dim cChannels As Integer ' number of channels in paDetails array
Dim item As Integer ' hwndOwner or cMultipleItems
Dim cbDetails As Integer ' size of _one_ details_XX struct
Dim paDetails As Integer ' pointer to array of details_XX structs
End Structure
Private Structure MIXERCONTROLDETAILS_UNSIGN ED
Dim dwValue As Integer ' value of the control
End Structure
Private Structure MIXERLINE
Dim cbStruct As Integer ' size of MIXERLINE structure
Dim dwDestination As Integer ' zero based destination index
Dim dwSource As Integer ' zero based source index (if source)
Dim dwLineID As Integer ' unique line id for mixer device
Dim fdwLine As Integer ' state/information about line
Dim dwUser As Integer ' driver specific information
Dim dwComponentType As Integer ' component type line connects to
Dim cChannels As Integer ' number of channels line supports
Dim cConnections As Integer ' number of connections (possible)
Dim cControls As Integer ' number of controls at this line
<VBFixedString(MIXER_SHORT _NAME_CHAR S),System. Runtime.In teropServi ces.Marsha lAs(System .Runtime.I nteropServ ices.Unman agedType.B yValTStr,S izeConst:= MIXER_SHOR T_NAME_CHA RS)> Public szShortName As String
<VBFixedString(MIXER_LONG_ NAME_CHARS ),System.R untime.Int eropServic es.Marshal As(System. Runtime.In teropServi ces.Unmana gedType.By ValTStr,Si zeConst:=M IXER_LONG_ NAME_CHARS )> Public szName As String
Dim dwType As Integer
Dim dwDeviceID As Integer
Dim wMid As Short
Dim wPid As Short
Dim vDriverVersion As Integer
<VBFixedString(MAXPNAMELEN ),System.R untime.Int eropServic es.Marshal As(System. Runtime.In teropServi ces.Unmana gedType.By ValTStr,Si zeConst:=M AXPNAMELEN )> Public szPname As String
End Structure
Private Structure MIXERLINECONTROLS
Dim cbStruct As Integer ' size in Byte of MIXERLINECONTROLS
Dim dwLineID As Integer ' line id (from MIXERLINE.dwLineID)
' MIXER_GETLINECONTROLSF_ONE BYID or
Dim dwControl As Integer ' MIXER_GETLINECONTROLSF_ONE BYTYPE
Dim cControls As Integer ' count of controls pmxctrl points to
Dim cbmxctrl As Integer ' size in Byte of _one_ MIXERCONTROL
Dim pamxctrl As Integer ' pointer to first MIXERCONTROL array
End Structure
Private Declare Function mixerClose Lib "winmm.dll" (ByVal hmx As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERCONTROLDETAILS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
Private Declare Function mixerGetControlDetails Lib "winmm.dll" Alias "mixerGetControlDetailsA"( ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERCAPS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
Private Declare Function mixerGetDevCaps Lib "winmm.dll" Alias "mixerGetDevCapsA"(ByVal uMxId As Integer, ByVal pmxcaps As MIXERCAPS, ByVal cbmxcaps As Integer) As Integer
Private Declare Function mixerGetID Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pumxID As Integer, ByVal fdwId As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERLINECONTROLS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
Private Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA"(By Val hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERLINE may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
Private Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA"(ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
Private Declare Function mixerGetNumDevs Lib "winmm.dll" () As Integer
Private Declare Function mixerMessage Lib "winmm.dll" (ByVal hmx As Integer, ByVal uMsg As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
Private Declare Function mixerOpen Lib "winmm.dll" (ByRef phmx As Integer, ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERCONTROLDETAILS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 050"'
Private Declare Function mixerSetControlDetails Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 016"'
Private Declare Sub CopyStructFromPtr Lib "kernel32" Alias "RtlMoveMemory"(ByRef struct As Any, ByVal ptr As Integer, ByVal cb As Integer)
'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 016"'
Private Declare Sub CopyPtrFromStruct Lib "kernel32" Alias "RtlMoveMemory"(ByVal ptr As Integer, ByRef struct As Any, ByVal cb As Integer)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Integer, ByVal dwBytes As Integer) As Integer
Private Declare Function GlobalLock Lib "kernel32" (ByVal hmem As Integer) As Integer
Private Declare Function GlobalFree Lib "kernel32" (ByVal hmem As Integer) As Integer
Private lMixerHnd As Integer
'UPGRADE_WARNING: Arrays in structure tMIC may need to be initialized before they can be used. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 063"'
Private tMIC As MIXERCONTROL
Private bHasMic As Boolean
Private Function GetMixer() As Boolean
Dim lReturn As Object
' Get a Handle to the Mixer
' If we've already got one, return
If lMixerHnd Then
GetMixer = True
Exit Function
End If
' Get a new handle to the Mizer
'UPGRADE_WARNING: Couldn't resolve default property of object lReturn. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 037"'
lReturn = mixerOpen(lMixerHnd, 0, 0, 0, 0)
'UPGRADE_WARNING: Couldn't resolve default property of object lReturn. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 037"'
If lReturn = MMSYSERR_NOERROR Then
GetMixer = True
Else
' Problems opening the Mixer
MsgBox("Unable to open mixer.")
End If
End Function
Public Function GetMicrophoneRecordVolume( ) As Integer
' Retreives the current volume of the Microphone control (Recording)
Dim lResult, lMemHnd As Integer
Dim tMIXERCONTROLDETAILS As MIXERCONTROLDETAILS
Dim tVOLUME As MIXERCONTROLDETAILS_UNSIGN ED
' If we haven't gotten the Microphone Control yet, do so.
If Not bHasMic Then bHasMic = GetMicControl()
If Not bHasMic Then
' Unable to get the Microphone
MsgBox("Unable to open Microphone Control")
Exit Function
End If
' Prep a MICERCONTROLDETAILS structure for retreiving info. about a specific control
tMIXERCONTROLDETAILS.item = 0
tMIXERCONTROLDETAILS.dwCon trolID = tMIC.dwControlID
tMIXERCONTROLDETAILS.cbStr uct = Len(tMIXERCONTROLDETAILS)
tMIXERCONTROLDETAILS.cbDet ails = Len(tVOLUME)
' Allocate a buffer for the control's value
lMemHnd = GlobalAlloc(&H40s, Len(tVOLUME))
tMIXERCONTROLDETAILS.paDet ails = GlobalLock(lMemHnd)
tMIXERCONTROLDETAILS.cChan nels = 1
' Get the controls details
Call mixerGetControlDetails(lMi xerHnd, tMIXERCONTROLDETAILS, MIXER_GETCONTROLDETAILSF_V ALUE)
' Copy the data into the control's VOLUME struct
'UPGRADE_WARNING: Couldn't resolve default property of object tVOLUME. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 037"'
CopyStructFromPtr(tVOLUME, tMIXERCONTROLDETAILS.paDet ails, Len(tVOLUME))
' Release the memory buffer
Call GlobalFree(lMemHnd)
' Return the current value
GetMicrophoneRecordVolume = tVOLUME.dwValue
End Function
Public Sub SetMicrophoneRecordVolume( ByVal lVolume As Integer)
' Set the Microphone volume used for recording
Dim lResult, lMemHnd As Integer
Dim tMIXERCONTROLDETAILS As MIXERCONTROLDETAILS
Dim tVOLUME As MIXERCONTROLDETAILS_UNSIGN ED
' If we haven't got the Microphone yet, do so..
If Not bHasMic Then bHasMic = GetMicControl()
If Not bHasMic Then
MsgBox("Unable to open Microphone Control")
Exit Sub
End If
' Prep the MIXERCONTROLDETAILS struct to set info. about this control
tMIXERCONTROLDETAILS.item = 0
tMIXERCONTROLDETAILS.dwCon trolID = tMIC.dwControlID
tMIXERCONTROLDETAILS.cbStr uct = Len(tMIXERCONTROLDETAILS)
tMIXERCONTROLDETAILS.cbDet ails = Len(tVOLUME)
' Allocate a buffer for the control's volume value
lMemHnd = GlobalAlloc(&H40s, Len(tVOLUME))
tMIXERCONTROLDETAILS.paDet ails = GlobalLock(lMemHnd)
tMIXERCONTROLDETAILS.cChan nels = 1
tVOLUME.dwValue = lVolume
' Copy the data from the VOLUME struct into the memory buffer
'UPGRADE_WARNING: Couldn't resolve default property of object tVOLUME. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 037"'
CopyPtrFromStruct(tMIXERCO NTROLDETAI LS.paDetai ls, tVOLUME, Len(tVOLUME))
' Set the new volume
Call mixerSetControlDetails(lMi xerHnd, tMIXERCONTROLDETAILS, MIXER_SETCONTROLDETAILSF_V ALUE)
' Release the memory buffer
Call GlobalFree(lMemHnd)
End Sub
Private Function GetMicControl() As Boolean
' Get the Microphone Control (from the Recording Line)
Dim tMIXERLINECONTROLS As MIXERLINECONTROLS
Dim tMIXERLINE As MIXERLINE
Dim lMemHnd As Integer
Dim lReturn As Integer
Dim lConnections As Integer
Dim lIndex As Integer
' Get a handle to the Mixer
If Not GetMixer() Then Exit Function
' First find the WAVEIN Line
tMIXERLINE.cbStruct = Len(tMIXERLINE)
tMIXERLINE.dwComponentType = MIXERLINE_COMPONENTTYPE_DS T_WAVEIN
lReturn = mixerGetLineInfo(lMixerHnd , tMIXERLINE, MIXER_GETLINEINFOF_COMPONE NTTYPE)
If lReturn <> MMSYSERR_NOERROR Then Exit Function
' Next enumerate the connections for this line, checking for the Microphone
lConnections = tMIXERLINE.cConnections - 1
For lIndex = 0 To lConnections
tMIXERLINE.dwSource = lIndex
Call mixerGetLineInfo(lMixerHnd , tMIXERLINE, MIXER_GETLINEINFOF_SOURCE)
If tMIXERLINE.dwComponentType = MIXERLINE_COMPONENTTYPE_SR C_MICROPHO NE Then
Exit For
End If
Next
' If no microphone was found, exit
If lIndex > lConnections Then Exit Function
' Extract the control for the Microphone from the line
tMIXERLINECONTROLS.cbStruc t = Len(tMIXERLINECONTROLS)
tMIXERLINECONTROLS.dwLineI D = tMIXERLINE.dwLineID
tMIXERLINECONTROLS.dwContr ol = MIXERCONTROL_CONTROLTYPE_V OLUME
tMIXERLINECONTROLS.cContro ls = 1
tMIXERLINECONTROLS.cbmxctr l = Len(tMIC)
' Create a buffer for the Microphone
lMemHnd = GlobalAlloc(&H40s, Len(tMIC))
tMIXERLINECONTROLS.pamxctr l = GlobalLock(lMemHnd)
tMIC.cbStruct = Len(tMIC)
' Get the Microphone
lReturn = mixerGetLineControls(lMixe rHnd, tMIXERLINECONTROLS, MIXER_GETLINECONTROLSF_ONE BYTYPE)
If (MMSYSERR_NOERROR = lReturn) Then
GetMicControl = True
' Copy the Microphone control into the tMIC structure
'UPGRADE_WARNING: Couldn't resolve default property of object tMIC. Click for more: 'ms-help://MS.VSCC.2003/co mmoner/red ir/redirec t.htm?keyw ord="vbup1 037"'
CopyStructFromPtr(tMIC, tMIXERLINECONTROLS.pamxctr l, Len(tMIC))
End If
' Release the buffer
Call GlobalFree(lMemHnd)
End Function
End Module
Hope this is useful
Option Strict Off
Option Explicit On
Module TEST
Private Const MMSYSERR_NOERROR As Short = 0
Private Const MAXPNAMELEN As Short = 32
Private Const MIXER_LONG_NAME_CHARS As Short = 64
Private Const MIXER_SHORT_NAME_CHARS As Short = 16
Private Const MIXER_GETLINEINFOF_COMPONE
Private Const MIXER_GETLINEINFOF_SOURCE As Integer = &H1
Private Const MIXER_GETCONTROLDETAILSF_V
Private Const MIXER_GETLINECONTROLSF_ONE
Private Const MIXERLINE_COMPONENTTYPE_DS
Private Const MIXERLINE_COMPONENTTYPE_SR
Private Const MIXERLINE_COMPONENTTYPE_DS
Private Const MIXERLINE_COMPONENTTYPE_SR
Private Const MIXERLINE_COMPONENTTYPE_SR
Private Const MIXERCONTROL_CT_CLASS_FADE
Private Const MIXERCONTROL_CT_UNITS_UNSI
Private Const MIXERCONTROL_CONTROLTYPE_F
Private Const MIXERCONTROL_CONTROLTYPE_V
Private Const MIXERLINE_COMPONENTTYPE_DS
Private Const MIXER_SETCONTROLDETAILSF_V
Private Structure MIXERCAPS
Dim wMid As Short ' manufacturer id
Dim wPid As Short ' product id
Dim vDriverVersion As Integer ' version of the driver
<VBFixedString(MAXPNAMELEN
Dim fdwSupport As Integer ' misc. support bits
Dim cDestinations As Integer ' count of destinations
End Structure
Private Structure MIXERCONTROL
Dim cbStruct As Integer ' size in Byte of MIXERCONTROL
Dim dwControlID As Integer ' unique control id for mixer device
Dim dwControlType As Integer ' MIXERCONTROL_CONTROLTYPE_x
Dim fdwControl As Integer ' MIXERCONTROL_CONTROLF_xxx
Dim cMultipleItems As Integer ' if MIXERCONTROL_CONTROLF_MULT
<VBFixedString(MIXER_SHORT
<VBFixedString(MIXER_LONG_
Dim lMinimum As Integer ' Minimum value
Dim lMaximum As Integer ' Maximum value
<VBFixedArray(10)> Dim reserved() As Integer ' reserved structure space
'UPGRADE_TODO: "Initialize" must be called to initialize instances of this structure. Click for more: 'ms-help://MS.VSCC.2003/co
Public Sub Initialize()
ReDim reserved(10)
End Sub
End Structure
Private Structure MIXERCONTROLDETAILS
Dim cbStruct As Integer ' size in Byte of MIXERCONTROLDETAILS
Dim dwControlID As Integer ' control id to get/set details on
Dim cChannels As Integer ' number of channels in paDetails array
Dim item As Integer ' hwndOwner or cMultipleItems
Dim cbDetails As Integer ' size of _one_ details_XX struct
Dim paDetails As Integer ' pointer to array of details_XX structs
End Structure
Private Structure MIXERCONTROLDETAILS_UNSIGN
Dim dwValue As Integer ' value of the control
End Structure
Private Structure MIXERLINE
Dim cbStruct As Integer ' size of MIXERLINE structure
Dim dwDestination As Integer ' zero based destination index
Dim dwSource As Integer ' zero based source index (if source)
Dim dwLineID As Integer ' unique line id for mixer device
Dim fdwLine As Integer ' state/information about line
Dim dwUser As Integer ' driver specific information
Dim dwComponentType As Integer ' component type line connects to
Dim cChannels As Integer ' number of channels line supports
Dim cConnections As Integer ' number of connections (possible)
Dim cControls As Integer ' number of controls at this line
<VBFixedString(MIXER_SHORT
<VBFixedString(MIXER_LONG_
Dim dwType As Integer
Dim dwDeviceID As Integer
Dim wMid As Short
Dim wPid As Short
Dim vDriverVersion As Integer
<VBFixedString(MAXPNAMELEN
End Structure
Private Structure MIXERLINECONTROLS
Dim cbStruct As Integer ' size in Byte of MIXERLINECONTROLS
Dim dwLineID As Integer ' line id (from MIXERLINE.dwLineID)
' MIXER_GETLINECONTROLSF_ONE
Dim dwControl As Integer ' MIXER_GETLINECONTROLSF_ONE
Dim cControls As Integer ' count of controls pmxctrl points to
Dim cbmxctrl As Integer ' size in Byte of _one_ MIXERCONTROL
Dim pamxctrl As Integer ' pointer to first MIXERCONTROL array
End Structure
Private Declare Function mixerClose Lib "winmm.dll" (ByVal hmx As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERCONTROLDETAILS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Function mixerGetControlDetails Lib "winmm.dll" Alias "mixerGetControlDetailsA"(
'UPGRADE_WARNING: Structure MIXERCAPS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Function mixerGetDevCaps Lib "winmm.dll" Alias "mixerGetDevCapsA"(ByVal uMxId As Integer, ByVal pmxcaps As MIXERCAPS, ByVal cbmxcaps As Integer) As Integer
Private Declare Function mixerGetID Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pumxID As Integer, ByVal fdwId As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERLINECONTROLS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA"(By
'UPGRADE_WARNING: Structure MIXERLINE may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA"(ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
Private Declare Function mixerGetNumDevs Lib "winmm.dll" () As Integer
Private Declare Function mixerMessage Lib "winmm.dll" (ByVal hmx As Integer, ByVal uMsg As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
Private Declare Function mixerOpen Lib "winmm.dll" (ByRef phmx As Integer, ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
'UPGRADE_WARNING: Structure MIXERCONTROLDETAILS may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Function mixerSetControlDetails Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Sub CopyStructFromPtr Lib "kernel32" Alias "RtlMoveMemory"(ByRef struct As Any, ByVal ptr As Integer, ByVal cb As Integer)
'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.2003/co
Private Declare Sub CopyPtrFromStruct Lib "kernel32" Alias "RtlMoveMemory"(ByVal ptr As Integer, ByRef struct As Any, ByVal cb As Integer)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Integer, ByVal dwBytes As Integer) As Integer
Private Declare Function GlobalLock Lib "kernel32" (ByVal hmem As Integer) As Integer
Private Declare Function GlobalFree Lib "kernel32" (ByVal hmem As Integer) As Integer
Private lMixerHnd As Integer
'UPGRADE_WARNING: Arrays in structure tMIC may need to be initialized before they can be used. Click for more: 'ms-help://MS.VSCC.2003/co
Private tMIC As MIXERCONTROL
Private bHasMic As Boolean
Private Function GetMixer() As Boolean
Dim lReturn As Object
' Get a Handle to the Mixer
' If we've already got one, return
If lMixerHnd Then
GetMixer = True
Exit Function
End If
' Get a new handle to the Mizer
'UPGRADE_WARNING: Couldn't resolve default property of object lReturn. Click for more: 'ms-help://MS.VSCC.2003/co
lReturn = mixerOpen(lMixerHnd, 0, 0, 0, 0)
'UPGRADE_WARNING: Couldn't resolve default property of object lReturn. Click for more: 'ms-help://MS.VSCC.2003/co
If lReturn = MMSYSERR_NOERROR Then
GetMixer = True
Else
' Problems opening the Mixer
MsgBox("Unable to open mixer.")
End If
End Function
Public Function GetMicrophoneRecordVolume(
' Retreives the current volume of the Microphone control (Recording)
Dim lResult, lMemHnd As Integer
Dim tMIXERCONTROLDETAILS As MIXERCONTROLDETAILS
Dim tVOLUME As MIXERCONTROLDETAILS_UNSIGN
' If we haven't gotten the Microphone Control yet, do so.
If Not bHasMic Then bHasMic = GetMicControl()
If Not bHasMic Then
' Unable to get the Microphone
MsgBox("Unable to open Microphone Control")
Exit Function
End If
' Prep a MICERCONTROLDETAILS structure for retreiving info. about a specific control
tMIXERCONTROLDETAILS.item = 0
tMIXERCONTROLDETAILS.dwCon
tMIXERCONTROLDETAILS.cbStr
tMIXERCONTROLDETAILS.cbDet
' Allocate a buffer for the control's value
lMemHnd = GlobalAlloc(&H40s, Len(tVOLUME))
tMIXERCONTROLDETAILS.paDet
tMIXERCONTROLDETAILS.cChan
' Get the controls details
Call mixerGetControlDetails(lMi
' Copy the data into the control's VOLUME struct
'UPGRADE_WARNING: Couldn't resolve default property of object tVOLUME. Click for more: 'ms-help://MS.VSCC.2003/co
CopyStructFromPtr(tVOLUME,
' Release the memory buffer
Call GlobalFree(lMemHnd)
' Return the current value
GetMicrophoneRecordVolume = tVOLUME.dwValue
End Function
Public Sub SetMicrophoneRecordVolume(
' Set the Microphone volume used for recording
Dim lResult, lMemHnd As Integer
Dim tMIXERCONTROLDETAILS As MIXERCONTROLDETAILS
Dim tVOLUME As MIXERCONTROLDETAILS_UNSIGN
' If we haven't got the Microphone yet, do so..
If Not bHasMic Then bHasMic = GetMicControl()
If Not bHasMic Then
MsgBox("Unable to open Microphone Control")
Exit Sub
End If
' Prep the MIXERCONTROLDETAILS struct to set info. about this control
tMIXERCONTROLDETAILS.item = 0
tMIXERCONTROLDETAILS.dwCon
tMIXERCONTROLDETAILS.cbStr
tMIXERCONTROLDETAILS.cbDet
' Allocate a buffer for the control's volume value
lMemHnd = GlobalAlloc(&H40s, Len(tVOLUME))
tMIXERCONTROLDETAILS.paDet
tMIXERCONTROLDETAILS.cChan
tVOLUME.dwValue = lVolume
' Copy the data from the VOLUME struct into the memory buffer
'UPGRADE_WARNING: Couldn't resolve default property of object tVOLUME. Click for more: 'ms-help://MS.VSCC.2003/co
CopyPtrFromStruct(tMIXERCO
' Set the new volume
Call mixerSetControlDetails(lMi
' Release the memory buffer
Call GlobalFree(lMemHnd)
End Sub
Private Function GetMicControl() As Boolean
' Get the Microphone Control (from the Recording Line)
Dim tMIXERLINECONTROLS As MIXERLINECONTROLS
Dim tMIXERLINE As MIXERLINE
Dim lMemHnd As Integer
Dim lReturn As Integer
Dim lConnections As Integer
Dim lIndex As Integer
' Get a handle to the Mixer
If Not GetMixer() Then Exit Function
' First find the WAVEIN Line
tMIXERLINE.cbStruct = Len(tMIXERLINE)
tMIXERLINE.dwComponentType
lReturn = mixerGetLineInfo(lMixerHnd
If lReturn <> MMSYSERR_NOERROR Then Exit Function
' Next enumerate the connections for this line, checking for the Microphone
lConnections = tMIXERLINE.cConnections - 1
For lIndex = 0 To lConnections
tMIXERLINE.dwSource = lIndex
Call mixerGetLineInfo(lMixerHnd
If tMIXERLINE.dwComponentType
Exit For
End If
Next
' If no microphone was found, exit
If lIndex > lConnections Then Exit Function
' Extract the control for the Microphone from the line
tMIXERLINECONTROLS.cbStruc
tMIXERLINECONTROLS.dwLineI
tMIXERLINECONTROLS.dwContr
tMIXERLINECONTROLS.cContro
tMIXERLINECONTROLS.cbmxctr
' Create a buffer for the Microphone
lMemHnd = GlobalAlloc(&H40s, Len(tMIC))
tMIXERLINECONTROLS.pamxctr
tMIC.cbStruct = Len(tMIC)
' Get the Microphone
lReturn = mixerGetLineControls(lMixe
If (MMSYSERR_NOERROR = lReturn) Then
GetMicControl = True
' Copy the Microphone control into the tMIC structure
'UPGRADE_WARNING: Couldn't resolve default property of object tMIC. Click for more: 'ms-help://MS.VSCC.2003/co
CopyStructFromPtr(tMIC, tMIXERLINECONTROLS.pamxctr
End If
' Release the buffer
Call GlobalFree(lMemHnd)
End Function
End Module
Hope this is useful