dcsimg
Results 1 to 5 of 5

Thread: VB6 how to capture Sound, using CoreAudio (Vista and onwards)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    VB6 how to capture Sound, using CoreAudio (Vista and onwards)

    Here's a simple Demo, which is using the CoreAudio-Interface wrapper-classes of the vbRichClient5-framework,
    to select a Capture-Device - followed by entering "Stream-Capture-Mode".

    Whilst the Stream_Data is rolling in continously - we use an Event of the Class cAudioCaptureClient,
    to retrieve Peak- and FFT-Data only... (no MP3-conversion of the SoundData, nor any FileWriting is done,
    only visualizing takes place)

    Here's what's needed in a normal VB-Form:

    Code:
    Option Explicit
     
    Private CapDev As cMMDevice, WithEvents ACC As cAudioCaptureClient, PeakAndFFT As New cDrawPeakAndFFT
    
    Private Sub Form_Load()
      ScaleMode = vbPixels
      
      Set CapDev = New_c.MMDeviceEnumerator.GetDefaultAudioEndpoint(eCapture, eCommunications)
      Caption = CapDev.OpenPropertyStore.GetDevice_FriendlyName & " (" & CapDev.GetStateName & ")"
     
      Set ACC = New_c.AudioCaptureClient
          ACC.InitOn CapDev, 44100
          ACC.RaiseMeterEvents = True
          ACC.RaiseFFTEvents = True
          ACC.FFTInputLevelFac = 0.8
          ACC.StartStream
    End Sub
    
    Private Sub ACC_MeterInfo(ByVal LeftPeak As Single, ByVal RightPeak As Single, LeftFFT() As Single, RightFFT() As Single)
      'let's give the Peak-Values a logarithmic scaling (also between 0 and 1), before passing them on...
      LeftPeak = Log(1.00000000000001 + LeftPeak * 9) / 2.303
      RightPeak = Log(1.00000000000001 + RightPeak * 9) / 2.303
    
      PeakAndFFT.Draw LeftPeak, RightPeak, LeftFFT, RightFFT
      PeakAndFFT.Srf.DrawToDC hDC, 0, 0, ScaleWidth, ScaleHeight
    End Sub
     
    Private Sub Form_Unload(Cancel As Integer)
      If Not ACC Is Nothing Then ACC.StopStream: Set ACC = Nothing
      Set CapDev = Nothing
      Set PeakAndFFT = Nothing
    End Sub
    
    Private Sub Form_Terminate()
      If Forms.Count = 0 Then New_c.CleanupRichClientDll
    End Sub
    In Form_Load above, the line:
    ... MMDeviceEnumerator.GetDefaultAudioEndpoint(eCapture, eCommunications)

    will usually resolve to the Microphone-Device which is built into your NoteBook or Tablet
    (or connected over Mike-In).

    There's different combinations of the Enums (the first one responsible for the direction, the second for the "Role") as e.g.:
    - (eCapture, eMultimedia) ... will typically resolve to a device on your LineIn-port, if connected
    - (eRender, eConsole) typically resolves to the Default-SoundOutput (Speakers or Phones)
    - (eRender, eMultimedia) often resolving to (digital) 5.1 Output-Ports e.g. when watching Video over HDMI

    The Roles are (since Win7) supported in a way, that the User can define different devices for
    those different "Role-Tasks".

    However, it's also possible to enumerate Devices, to select one later - e.g. using this code:
    Code:
     Dim i As Long, Dev As cMMDevice
     Debug.Print "Capture-Devices"
     With New_c.MMDeviceEnumerator.EnumAudioEndpoints(eCapture, DEVICE_STATEMASK_ALL)
       For i = 0 To .GetCount - 1
         Set Dev = .Item(i)
         Debug.Print , Dev.GetStateName, Dev.OpenPropertyStore.GetDevice_FriendlyName
       Next
     End With
     Debug.Print vbLf; "Render-Devices"
     With New_c.MMDeviceEnumerator.EnumAudioEndpoints(eRender, DEVICE_STATEMASK_ALL)
       For i = 0 To .GetCount - 1
         Set Dev = .Item(i)
         Debug.Print , Dev.GetStateName, Dev.OpenPropertyStore.GetDevice_FriendlyName
       Next
     End With
    I got the following output here on my Machine:
    Code:
    Capture-Devices
                  ACTIVE        Mikrofon (High Definition Audio-Gerät)
                  NOTPRESENT    CD-Audio (2- High Definition Audio-Gerät)
                  NOTPRESENT    Mikrofon (Sennheiser USB Headset)
                  NOTPRESENT    Mikrofon (2- High Definition Audio-Gerät)
                  NOTPRESENT    Mikrofon (Sennheiser USB Headset)
    
    Render-Devices
                  NOTPRESENT    Lautsprecher (Sennheiser USB Headset)
                  ACTIVE        Lautsprecher (High Definition Audio-Gerät)
                  NOTPRESENT    Kopfhörer (2- High Definition Audio-Gerät)
                  NOTPRESENT    Lautsprecher (2- High Definition Audio-Gerät)
                  NOTPRESENT    Lautsprecher (2- Logitech USB Speaker)
    The interfaces are (in comparison to the old Mixer/WaveIn/WaveOut-APIs) quite clear -
    but nevertheless would require some learning on your part, here's the MSDN-link to the
    CoreAudio-Interface-List in all its glory - you will find the most important ones in the RC5,
    prefixed with a small 'c' instead of an 'I': http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Here's the complete Demo, which visualizes Peak- as well as "FFTed"-Spectrum-Values (containing,
    in addition to the Form-Code above only one additional small Drawing-Class, cDrawPeakAndFFT...

    AudioCapture.zip

    Here's a screenshot, what the Demo will put out with the help of the Drawing-Class:


    Olaf

  2. #2
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: VB6 how to capture Sound, using CoreAudio (Vista and onwards)

    Dear Olaf

    I've heard of vbRichClient even in vb communities in my own country, that it is powerful and you are the author, right?
    I was so intrigued to delve deeper but it turned out I couldn't find any detailed (or step by step...) documentation. Maybe I was looking in the wrong place?

    I'm a vb6 newbie. And I think your vbRichClient should be a great tool.
    Thank you for all the efforts you've made for us.

    Kind regards
    Bill Price

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    Re: VB6 how to capture Sound, using CoreAudio (Vista and onwards)

    Thanks for the kind words Bill,

    as far as documentation goes - there's a quite recent *.chm compilation (thankfully provided by Dr. Unicode),
    which you can download here: http://cyberactivex.com/Download/CairoDocs.zip.

    But that's not (yet) the "comprehensive documentation in MSDN-style" you're probably after...
    (which would contain also a 'Remarks-Section', which describes the purpose and "typical use-cases"
    of certain Class-methods - and not only their Return-Types or Parameter-Types, as currently contained
    in Dr. Unicodes *.chm).

    But the *.chms are a good start (and I appreciate the efforts Dr. Unicode put into that contribution).

    In case you don't plan to use the chms - the VB-ObjectExplorer (reachable per <F2>) can be helpful
    looking up certain Classes, Methods (and their Types) too.

    As for learning certain areas of the RC5 "step by step" - here at least exists good enough coverage
    I think, especially for the "heavier" parts of the RC5 (the Cairo-Drawing and the SQLite-DB-Classes),
    by really comprehensive and well-commented VB6-Demos in appropriate Tutorial-Zips.

    For example in the Demo-Section on my site: http://vbrichclient.com/#/en/Demos/GUI/
    you will find 3 important Tutorial-Zips (in the first 3 Links on that page), which when you
    really study them - step-by-step - should leave not many questions open with regards
    to the Drawing-Calls I've made to produce e.g. the Output which is showing in this Demo here.

    There is surely a certain hurdle you will have to take first - but this is not really dependent on RC5,
    but "useful knowledge" in either case:

    Learn how to use (especially writing your own) VB6-Classes!

    I mean, not only "how to call methods on them" ... any VBScripting-Guy has learned how to do that
    (e.g. when using the FSO - or when using ADO-Recordsets to talk to DBs).

    What I mean is, that you learn to use Classes you wrote on your own, for your own purposes -
    including Events in some of them - or also how to use VBs Implements Keyword in others)...

    Anything in the RC5 is solved over Classes - and some of the mechanics, as in: how these Classes
    interoperate and *play-together* (sometimes over Events, sometimes over "Implements-Callbacks") -
    are really important things, which (after some time) should come "more and more naturally" to you...

    A better understanding of these mechanisms (because you're using them also in your own scenarios) -
    that's what will finally "switch the lights on" I'd guess - to better comprehend, how some of the more
    advanced RC5-examples accomplish the Output you see in "just a few lines".

    Well, other than "diving into it, studying concrete examples, demos and tutorials", there's not
    much else one can do, to learn "so far unknown API-interfaces".

    What's also useful is a Google-Search against this site here (since it contains quite a lot of
    examples I posted here over the last 1.5 years).

    E.g. in another recent thread you asked, if "counting *.jpg and *.png Files in Folders and SubFolders"
    could have been done more efficiently.

    Yes, there's a decent Class for that in the RC5, named cDirList (which should help to reduce
    the code you were using in your other example).

    Now (coming back to my suggestion to use a google-search against this site) - in case you
    want to see code-examples, how to use this Class in recursive scenarios - you could google with:
    [site:vbForums.com cDirList]
    or to suppress some more Google-noise:
    [site:vbForums.com cDirList Schmidt]

    Just start playing with the RC5-Classes - they don't bite - the worst they'll do to you is a thrown
    COM-error...

    Otherwise - I'm a member of this Forum, so you can always ask concrete questions about
    RC5-Classes in the normal VB6-Section of this site - there's other experienced Developers
    here, who know quite a lot about RC5-usage and could help out as well, even when I'm not around.

    Olaf
    Last edited by Schmidt; Dec 15th, 2014 at 10:38 PM.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    341

    Re: VB6 how to capture Sound, using CoreAudio (Vista and onwards)

    Quote Originally Posted by Schmidt View Post
    What I mean is, that you learn to use Classes you wrote on your own, for your own purposes... which (after some time) should come "more and more naturally" to you...
    It's true. After a year or so writing my own classes in VB6, now RC5 classes just look friendly, although there are some of the methods, i.e. cCommand.save, still troubling me. I can't really just read the method name and get the idea of how to use it. I guess it's because I still lack proper background knowledge, or programming experience.

    By the way, any further development on the RC5 documentation? Right... I think it might sound quite selfish to raise such a question, because writing documentation needs time and efforts; however I am not the one who's made any contribution to it. But anyway, it's just a problem

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,345

    Re: VB6 how to capture Sound, using CoreAudio (Vista and onwards)

    Quote Originally Posted by bPrice View Post
    It's true. After a year or so writing my own classes in VB6, now RC5 classes just look friendly,...
    Glad to hear that...

    Quote Originally Posted by bPrice View Post
    ...although there are some of the methods, i.e. cCommand.save, still troubling me. I can't really just read the method name and get the idea of how to use it. I guess it's because I still lack proper background knowledge, or programming experience.
    cCommand.Save (as well as cSelectCommand.Save) have a Parameter named: 'CommandKey As String'
    So, it obviously saves the Command(Definition) "in some place" under a given StringKey ... well,
    the obvious place to store Command-Defintions ("Stored Procedures" if you like),
    is the DB-File itself - so it is indeed persisted there under the given 'CommandKey'.

    That means, that you don't have to "ship your executable" with the concrete SQL-Definitions
    of certain CommandObjects, but can retrieve them from the DB per CommandKey instead.

    In case the DB is encrypted, that might help to ensure a little bit more security.

    Here's an example:
    Code:
    Option Explicit
    
    Private Cnn As cConnection
    
    Const CmdKey_T1_InsertNameAndDescription$ = "T1_InsertNameAndDescription"
    
    Private Sub Form_Load()
      Set Cnn = New_c.Connection(, DBCreateInMemory)
          Cnn.Execute "Create Table T1(ID Integer Primary Key, Name Text, Description Text)"
      
      With Cnn.CreateCommand("Insert Into T1(Name, Description) Values(?,?)")
        .Save CmdKey_T1_InsertNameAndDescription$
      End With
    End Sub
    
    Private Sub Form_Click()
      Caption = T1_InsertNameAndDescription("foo", "bar")
    End Sub
    
    Function T1_InsertNameAndDescription(Name As String, Description As String) As Currency
    Static Cmd As cCommand
        If Cmd Is Nothing Then Set Cmd = Cnn.CreateCommand(CmdKey_T1_InsertNameAndDescription)
           Cmd.SetText 1, Name
           Cmd.SetText 2, Description
           Cmd.Execute
        T1_InsertNameAndDescription = Cnn.LastInsertAutoID
    End Function
    Quote Originally Posted by bPrice View Post
    By the way, any further development on the RC5 documentation? Right... I think it might sound quite selfish to raise such a question, because writing documentation needs time and efforts; however I am not the one who's made any contribution to it. But anyway, it's just a problem
    As you say - it's a time-problem for me currently (have less spare-time at hand due to my
    current work, compared to what I was used to a few years ago)...

    There's a Wikia-Page for the RichClient though - (opened by 'Moyack' apparently - but no
    clue who's behind that Name, maybe he's even a member of this forum - don't know...).
    http://vbrichclientframework.wikia.com
    Feel free to contribute...

    Despite that, I still think that it's concrete Code-Examples which help the most (because
    that's "documentation with a context") - and Forums like that here are a good platform to
    share those examples.

    Olaf

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width