Results 1 to 10 of 10

Thread: [VB6, Vista+] Core Audio - Peak Meter

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    [VB6, Vista+] Core Audio - Peak Meter

    Core Audio - Peak Meter
    Name:  capeaks.jpg
Views: 1873
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 Files Attached Files
    Last edited by fafalone; Apr 28th, 2021 at 07:35 PM.

  2. #2
    New Member
    Join Date
    Apr 2021
    Posts
    11

    Re: [VB6, Vista+] Core Audio - Peak Meter

    Hi fafalone,

    this is great, thanks.

    Regards
    Peter

  3. #3
    Junior Member SaschaT's Avatar
    Join Date
    Mar 2017
    Location
    Berlin, Germany
    Posts
    23

    Re: [VB6, Vista+] Core Audio - Peak Meter

    As an addition this one here for the capture (recording) device...

    MS states, that getting the AudioMeter for the capture device just requires to change the Endpoint device from eRender to eCapture. Ok, that works, but you won't get any values...
    They missed to note that you have also to activate the corresponding IAudioClient on the device before you request the meter.
    The attached demo shows how to achieve this (actual oleexp.tlb required!).

    Name:  capturemeter.png
Views: 849
Size:  3.4 KB

    @Fafalone:
    As you can see I directly use WAVEFORMATEXTENSIBLE for the FormatSupported and Initialize methods. Because to my observations FormatTag is always -2 (WAVE_FORMAT_EXTENSIBLE) and cbSize is 22. This has to be changed to WAVE_FORMAT_PCM (1) and cbSize = 0 before setting it in Initialize. Otherwise you get the "not supported format" error.
    Attached Files Attached Files

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6, Vista+] Core Audio - Peak Meter

    Interesting, thanks for sharing!

    (PS - So every time you need an IID, you need to declare a local variable and call CLSIDFromString? Yeah, I'd much rather just autogenerate my iids and include mIID lol )

  5. #5
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: [VB6, Vista+] Core Audio - Peak Meter

    There appears to be a missing BAS file from within that original Post's zipped file CoreAudioWatchPeaks.zip, Faf old boy...

    it states mCoreAudio.bas is not present
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  6. #6
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: [VB6, Vista+] Core Audio - Peak Meter

    Quote Originally Posted by yereverluvinuncleber View Post
    There appears to be a missing BAS file from within that original Post's zipped file CoreAudioWatchPeaks.zip, Faf old boy...

    it states mCoreAudio.bas is not present
    this file is in oleexp64.zip
    https://www.vbforums.com/showthread....ary-oleexp-tlb

  7. #7
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: [VB6, Vista+] Core Audio - Peak Meter

    [Well, I am sure I have that installed and registered, it is certainly within WoW64 already. How can we ascertain which version we currently have installed? Isn't that just a type library? mCoreAudio.bas looks like a file containing code rather than just enums and type definitions.] - all sorted.
    Last edited by yereverluvinuncleber; Aug 15th, 2023 at 07:12 PM.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  8. #8
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: [VB6, Vista+] Core Audio - Peak Meter

    > OK, now I have it, it is within the same zipfile that contains the TLB.<
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6, Vista+] Core Audio - Peak Meter

    Yes unfortunately a TLB can't contain GUIDs (besides as strings) or functions, so there's addon modules for those things, with separate ones for particular feature sets.

    A good thing about the twinBASIC package format is that it can include those, so tbShellLib (the x64-compatible successor to oleexp) is just a single package without additional modules.

  10. #10
    Junior Member SaschaT's Avatar
    Join Date
    Mar 2017
    Location
    Berlin, Germany
    Posts
    23

    Re: [VB6, Vista+] Core Audio - Peak Meter

    Quote Originally Posted by fafalone View Post
    (PS - So every time you need an IID, you need to declare a local variable and call CLSIDFromString? Yeah, I'd much rather just autogenerate my iids and include mIID lol )
    No. You see the reason I did it here in the above posts. I want a demo to be as minimalistic as possible.
    I attached one of my solutions there.
    Otherwise I store the IID strings in a resource file. Loading and immediately converting them to an UUID array on Initialize()/Form_Load. (Over the years I assembled an Access database containing >60.000 GUIDs, 70% of them MS GUIDs. I filter the table e.g. by column "source=mmreg.h" and export this to a text file that I'll include in the resource.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width