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

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

$
0
0
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
Attached Files

Viewing all articles
Browse latest Browse all 1530

Trending Articles



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