1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
Public Module Sound
#Region "MIXERLINE_COMPONENTTYPE"
Public Enum MIXERLINE_COMPONENTTYPE
DST_FIRST = &H0
DST_SPEAKERS = (DST_FIRST + 4)
SRC_FIRST = &H00001000
SRC_UNDEFINED = (SRC_FIRST + 0)
SRC_DIGITAL = (SRC_FIRST + 1)
SRC_LINE = (SRC_FIRST + 2)
SRC_MICROPHONE = (SRC_FIRST + 3)
SRC_SYNTHESIZER = (SRC_FIRST + 4)
SRC_COMPACTDISC = (SRC_FIRST + 5)
SRC_TELEPHONE = (SRC_FIRST + 6)
SRC_PCSPEAKER = (SRC_FIRST + 7)
SRC_WAVEOUT = (SRC_FIRST + 8)
SRC_AUXILIARY = (SRC_FIRST + 9)
SRC_ANALOG = (SRC_FIRST + 10)
SRC_LAST = (SRC_FIRST + 10)
End Enum
#End Region
#Region "Constants"
Friend Const MMSYSERR_NOERROR As Integer = 0
Friend Const MMSYSERR_BASE = 0
Friend Const MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2)
Friend Const MMSYSERR_INVALFLAG = (MMSYSERR_BASE + 10)
Friend Const MMSYSERR_INVALHANDLE = (MMSYSERR_BASE + 5)
Friend Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11)
Friend Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6)
Friend Const MAXPNAMELEN As Integer = 32
Friend Const MIXER_LONG_NAME_CHARS As Integer = 64
Friend Const MIXER_SHORT_NAME_CHARS As Integer = 16
Friend Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
Friend Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
Friend Const MIXERCONTROL_CT_UNITS_BOOLEAN As Integer = &H10000
Friend Const MIXERCONTROL_CT_CLASS_SWITCH As Integer = &H20000000
Friend Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = (MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED)
Friend Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 1)
Friend Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3&
Friend Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
Friend Const MIXERCONTROL_CONTROLTYPE_BASS As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 2)
Friend Const MIXERCONTROL_CONTROLTYPE_TREBLE As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 3)
Friend Const MIXERCONTROL_CONTROLTYPE_EQUALIZER As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 4)
Friend Const MIXERCONTROL_CONTROLTYPE_BOOLEAN As Integer = (MIXERCONTROL_CT_CLASS_SWITCH Or MIXERCONTROL_CT_UNITS_BOOLEAN)
Friend Const MIXERCONTROL_CONTROLTYPE_MUTE As Integer = (MIXERCONTROL_CONTROLTYPE_BOOLEAN + 2)
#End Region
#Region "Structs"
Friend Structure MIXERCAPS
Dim wMid As Integer
Dim wPid As Integer
Dim vDriverVersion As Long
Dim szPname As String
Dim fdwSupport As Long
Dim cDestinations As Long
End Structure
<StructLayout(LayoutKind.Sequential)> _
Friend Structure MIXERCONTROL
<FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERCONTROL
<FieldOffset(4)> Public dwControlID As Integer ' unique control id for mixer device
<FieldOffset(8)> Public dwControlType As Integer ' MIXERCONTROL_CONTROLTYPE_xxx
<FieldOffset(12)> Public fdwControl As Integer ' MIXERCONTROL_CONTROLF_xxx
<FieldOffset(16)> Public cMultipleItems As Integer ' if MIXERCONTROL_CONTROLF_MULTIPLE set
<FieldOffset(20), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String ' * MIXER_SHORT_NAME_CHARS ' short name of control
<FieldOffset(36), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String ' * MIXER_LONG_NAME_CHARS ' Integer name of control
<FieldOffset(100)> Public lMinimum As Integer ' Minimum value
<FieldOffset(104)> Public lMaximum As Integer ' Maximum value
<FieldOffset(108), MarshalAs(UnmanagedType.ByValArray, SizeConst:=11, ArraySubType:=UnmanagedType.AsAny)> Public reserved() As Integer ' reserved structure space
End Structure
<StructLayout(LayoutKind.Sequential)> _
Friend Structure MIXERCONTROLDETAILS
<FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERCONTROLDETAILS
<FieldOffset(4)> Public dwControlID As Integer ' control id to get/set details on
<FieldOffset(8)> Public cChannels As Integer ' number of channels in paDetails array
<FieldOffset(12)> Public item As Integer ' hwndOwner or cMultipleItems
<FieldOffset(16)> Public cbDetails As Integer ' size of _one_ details_XX struct
<FieldOffset(20)> Public paDetails As IntPtr ' pointer to array of details_XX structs
End Structure
<StructLayout(LayoutKind.Sequential)> _
Friend Structure MIXERCONTROLDETAILS_UNSIGNED
<FieldOffset(0)> Public dwValue As Integer ' value of the control
End Structure
<StructLayout(LayoutKind.Sequential)> _
Friend Structure MIXERLINECONTROLS
<FieldOffset(0)> Public cbStruct As Integer ' size in Byte of MIXERLINECONTROLS
<FieldOffset(4)> Public dwLineID As Integer ' line id (from MIXERLINE.dwLineID)
<FieldOffset(8)> Public dwControl As Integer ' MIXER_GETLINECONTROLSF_ONEBYTYPE
<FieldOffset(12)> Public cControls As Integer ' count of controls pmxctrl points to
<FieldOffset(16)> Public cbmxctrl As Integer ' size in Byte of _one_ MIXERCONTROL
<FieldOffset(20)> Public pamxctrl As IntPtr ' pointer to first MIXERCONTROL array
End Structure
<StructLayout(LayoutKind.Sequential)> _
Friend Structure MIXERLINE
<FieldOffset(0)> Public cbStruct As Integer ' size of MIXERLINE structure
<FieldOffset(4)> Public dwDestination As Integer ' zero based destination index
<FieldOffset(8)> Public dwSource As Integer ' zero based source index (if source)
<FieldOffset(12)> Public dwLineID As Integer ' unique line id for mixer device
<FieldOffset(16)> Public fdwLine As Integer ' state/information about line
<FieldOffset(20)> Public dwUser As Integer ' driver specific information
<FieldOffset(24)> Public dwComponentType As Integer ' component type line connects to
<FieldOffset(28)> Public cChannels As Integer ' number of channels line supports
<FieldOffset(32)> Public cConnections As Integer ' number of connections (possible)
<FieldOffset(36)> Public cControls As Integer ' number of controls at this line
<FieldOffset(40), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String ' * MIXER_SHORT_NAME_CHARS
<FieldOffset(56), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String ' * MIXER_LONG_NAME_CHARS
<FieldOffset(120)> Public dwType As Integer
<FieldOffset(124)> Public dwDeviceID As Integer
<FieldOffset(128)> Public wMid As Integer
<FieldOffset(132)> Public wPid As Integer
<FieldOffset(136)> Public vDriverVersion As Integer
<FieldOffset(135), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String ' * MAXPNAMELEN
End Structure
#End Region
#Region "API Declarations"
Friend Declare Function mixerGetNumDevs Lib "winmm.dll" () As Integer
Friend Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
Friend Declare Function mixerSetControlDetails Lib "winmm.dll" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer,ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
Friend Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
Friend Declare Function mixerOpen Lib "winmm.dll" (ByRef phmx As Integer, <MarshalAs(UnmanagedType.U4)> ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
Friend Declare Function mixerClose Lib "winmm.dll" (hm As Integer) As Integer
Friend Declare Function mixerGetControlDetails Lib "winmm.dll" Alias "mixerGetControlDetailsA" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
#End Region
Public Class Device
Public Dim Handle As Integer
Public Dim ID As Integer
Public Dim Name As String
Private Sub CreateDevice(ByVal Identity As String)
'Cette partie sert à Trouver la carte son correspondant à l'identity demandé, et à enregistrer son handle dans Handle
'L'Identity peut être un string (le nom de carte audio) ou le DeviceID de la carte son.
'Le DeviceID part de 0 (0 étant la carte son configuré actuellement dasn windows XP) et va jusqu'à votre derniere carte son
Dim lngReturn As Integer
Dim i As Integer
Dim rc As Integer
Dim DeviceID As Integer
Dim DeviceName As String = ""
Dim MixerLine As MIXERLINE
Try
If CInt(Identity) >= 0 Then
DeviceID = CInt(Identity) + 1
Else
Exit Sub
End If
Catch
DeviceName = Identity
Finally
End Try
Do
mixerClose(Handle)
MixerLine = New MIXERLINE
' Obtain the Handle struct
lngReturn = mixerOpen(Handle, i, 0, 0, 0)
i=i+1
' Error check
If lngReturn <> 0 And i > mixerGetNumDevs()+7 Then
mixerClose(Handle)
Dim ex As New Exception("No one SoundDevice corresponds to DeviceID/DeviceName indicated!")
Throw ex
Exit Sub
End If
MixerLine.cbStruct = Marshal.SizeOf(MixerLine)
MixerLine.dwComponentType = MIXERLINE_COMPONENTTYPE.DST_SPEAKERS
' Obtain a line corresponding to the component type
rc = mixerGetLineInfo(Handle, MixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE)
Loop Until MMSYSERR_NOERROR = rc and (DeviceID = MixerLine.dwDeviceID+1 Or DeviceName Like "*" & MixerLine.szPname & "*")
ID = MixerLine.dwDeviceID
Name = MixerLine.szPname
End Sub
Public Sub New(ByVal DeviceID As Integer)
Me.CreateDevice(DeviceID)
End Sub
Public Sub New(ByVal DeviceName As String)
Me.CreateDevice(DeviceName)
End Sub
Private Function GetControl(ByVal MixerLine As MIXERLINE ,ByVal ControlType As Integer) As MIXERCONTROL
Dim rc As Integer
Dim mxlc As New MIXERLINECONTROLS
Dim Control As New MIXERCONTROL
Dim pmem As IntPtr
mxlc.cbStruct = Marshal.SizeOf(mxlc)
mxlc.dwLineID = MixerLine.dwLineID
mxlc.dwControl = ControlType
mxlc.cControls = 0
mxlc.cbmxctrl = Marshal.SizeOf(Control)
' Allocate a buffer for the control
pmem = Marshal.AllocHGlobal(Marshal.SizeOf(Control))
mxlc.pamxctrl = pmem
Control.cbStruct = Marshal.SizeOf(Control)
' Get the control
rc = mixerGetLineControls(Handle, mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE)
If (MMSYSERR_NOERROR = rc) Then
Control = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
Else
Dim ex As New Exception("No one Control corresponds to ControlIndex indicated!")
Throw ex
Exit Function
End If
Marshal.FreeHGlobal(pmem)
Return Control
End Function
Public Function GetLine(ByVal iComponentType As Integer) As Line
Dim MixerLine As New MIXERLINE
Dim rc As Integer
MixerLine.cbStruct = Marshal.SizeOf(MixerLine)
MixerLine.dwComponentType = iComponentType
' Obtain a line corresponding to the component type
rc = mixerGetLineInfo(Handle, MixerLine, MIXER_GETLINEINFOF_COMPONENTTYPE)
If (MMSYSERR_NOERROR = rc) Then
Dim LineMx As New Line
LineMx.VolCtrl = GetControl(MixerLine, MIXERCONTROL_CONTROLTYPE_VOLUME)
LineMx.MuteCtrl = GetControl(MixerLine, MIXERCONTROL_CONTROLTYPE_MUTE)
LineMx.DeviceHandle = Handle
LineMx.Name = MixerLine.szName
Return LineMx
Else
Dim ex As New Exception("No one Line corresponds with Component Type indicated!")
Throw ex
End If
End Function
End Class
Public Class Line
Public Dim DeviceHandle As Integer
Public Dim Name As String
Friend Dim MuteCtrl As MIXERCONTROL
Friend Dim VolCtrl As MIXERCONTROL
Public Sub SetVolume(ByVal Level As Integer)
If not (Level >= 0 And Level <= 100) Then
Dim ex As New Exception("Level is not between 0 and 100")
Throw ex
End If
' Then determine the value of the volume and Set the volume!
Call SetParameter(VolCtrl,CType(volCtrl.lMaximum * (Level / 100), Integer))
End Sub
Public Sub SetMute(ByVal boolMute As Boolean)
' Then determine the value of the volume
If boolMute Then
' Mute
Call SetParameter(MuteCtrl,1)
Else
' Turn the sound on
Call SetParameter(MuteCtrl,0)
End If
End Sub
Private Sub SetParameter(ByVal Control As MIXERCONTROL,ByVal Parameter As Integer)
Dim rc As Integer
Dim mxcd As MIXERCONTROLDETAILS
Dim vol As MIXERCONTROLDETAILS_UNSIGNED
Dim hptr As IntPtr
mxcd.item = 0
mxcd.dwControlID = Control.dwControlID
mxcd.cbStruct = Marshal.SizeOf(mxcd)
mxcd.cbDetails = Marshal.SizeOf(vol)
hptr = Marshal.AllocHGlobal(Marshal.SizeOf(vol))
' Allocate a buffer for the control value buffer
mxcd.paDetails = hptr
mxcd.cChannels = 1
vol.dwValue = Parameter
Marshal.StructureToPtr(vol, hptr, False)
' Set the control value
rc = mixerSetControlDetails(DeviceHandle,mxcd, 0)
Marshal.FreeHGlobal(hptr)
End Sub
Private Function GetParameter(ByVal Control As MIXERCONTROL) As Integer
Dim rc As Integer
Dim mxcd As MIXERCONTROLDETAILS
Dim vol As MIXERCONTROLDETAILS_UNSIGNED
Dim hptr As IntPtr
mxcd.item = 0
mxcd.dwControlID = Control.dwControlID
mxcd.cbStruct = Marshal.SizeOf(mxcd)
mxcd.cbDetails = Marshal.SizeOf(vol)
hptr = Marshal.AllocHGlobal(Marshal.SizeOf(vol))
' Allocate a buffer for the control value buffer
mxcd.paDetails = hptr
mxcd.cChannels = 1
Marshal.StructureToPtr(vol, hptr, False)
' Obtenir les détails du controle
rc = mixerGetControlDetails(DeviceHandle,mxcd, 0)
vol = CType(Marshal.PtrToStructure(mxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED)), MIXERCONTROLDETAILS_UNSIGNED)
Marshal.FreeHGlobal(hptr)
' Retour du paramètre!
Return vol.dwValue
End Function
Public Function GetVolume() As Integer
' Retour du volume en cours!
Return CType((GetParameter(VolCtrl)*100)/volCtrl.lMaximum, Integer)
End Function
Public Function GetMute() As Boolean
' Retour de la Valeur du Mute en cours!
If GetParameter(MuteCtrl) = 1 Then
Return True
Else
Return False
End If
End Function
End Class
End Module |
Partager