Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1512

[VB6, Vista+] Core Audio - Peak Meter

$
0
0
Core Audio - Peak Meter
Name:  capeaks.jpg
Views: 13
Size:  24.8 KB


This demo is in a response to a question by Peterd51, asking if there was a way to detect if audio was playing. CoreAudio provides an easy way to watch peaks for a peak meter, so obviously if that's 0 no audio is playing, and non-zero if audio is playing.

Here we display a Audio detected/No audio label for the yes/no answer, then also a peak meter using a ProgressBar, and a list of the raw values the program is receiving. This is basically a VB version of Microsoft's Peak Meter Example.

The code is pretty simple,
Code:

Option Explicit
Private pDevice As IMMDevice
Private pEnum As MMDeviceEnumerator
Private pMeterInfo As IAudioMeterInformation
Private nCount As Long
Private Sub Command1_Click()
Timer1.Interval = CLng(Text1.Text)
If (pDevice Is Nothing) Then
    Set pEnum = New MMDeviceEnumerator
    pEnum.GetDefaultAudioEndpoint eRender, eConsole, pDevice
    If (pDevice Is Nothing) = False Then
        pDevice.Activate IID_IAudioMeterInformation, CLSCTX_INPROC_SERVER Or CLSCTX_INPROC_HANDLER Or CLSCTX_LOCAL_SERVER Or CLSCTX_REMOTE_SERVER, 0&, pMeterInfo
        If (pMeterInfo Is Nothing) = False Then
            Timer1.Enabled = True
        Else
            Debug.Print "Failed to activate meter."
        End If
    Else
        Debug.Print "Failed to get default endpoint."
    End If
Else
    Timer1.Enabled = True
End If
End Sub

Private Sub Timer1_Timer()
Dim snValue As Single
If (pMeterInfo Is Nothing) = False Then
    pMeterInfo.GetPeakValue snValue
    List1.AddItem CStr(snValue * 100), 0
    ProgressBar1.Value = snValue * 100
    If snValue = 0 Then
        Label4.Caption = "No audio."
        nCount = nCount + 1
        If nCount > 5 Then
            'definitely not playing
        End If
    Else
        nCount = 0
        Label4.Caption = "Audio detected"
    End If
End If
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set pMeterInfo = Nothing
Set pDevice = Nothing
Set pEnum = Nothing
End Sub

Requirements
-Core Audio is only available on Windows Vista and newer.
-oleexp.tlb v4.7 or higher
-oleexp addon modules mIID.bas and mCoreAudio.bas (included in the oleexp download)

Core Audio in VB6
If you're not already familiar with using Core Audio in VB6, you can check out my earlier projects:
[VB6, Vista+] Core Audio Basics
[VB6, Vista+] Core Audio - Change the system default audio device
[VB6, Vista+] Core Audio - Monitor for disabled/active, default, and property changes
Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 1512

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>