Audio Input mic/line in Visual Basic

Duarte Vinagre 60 Reputation points
2023-06-21T17:52:50.27+00:00

Hello all...so..i do not code since 2004 but im trying to make a wav sound recorder..i have this code...but..i do not know how to select the soundcard and input line..(mic/line in)..any help?..i tried google but did not had much of luck..Thanks!

Public Class SSVoiceRecordCtrl

    '-----------------------------------------------------
    '   Name    :   SoundRecording.cls
    '   Author  :   Suresh Suthar
    '
    '   Notes   :   A record class, which when turned
    '           :   into an object lets you record sound
    '           :   through mic, cd-line, etc... into a file
    '-----------------------------------------------------

    ' -=-=-=- PROPERTIES -=-=-=-
    ' FileName      Determines the name of the file used (wav only)
    ' State        The current status of the object (Read Only)
    'SoundFormat    The Formate for Recording File

    ' -=-=-=- METHODS -=-=-=-=-
    ' PauseRecord     Toggle pause the record (if you are already recording)
    ' StartRecord     Start Recording (you must have been set a good filename)
    ' StopRecord      Stop recording (and will save the sound into <filename>)

    '-------------------------------------------------------------
    ' NOTES
    ' -----
    '
    ' Before you can Start or Stop recording you must set a good
    ' filename which the class will use to save your sound into.
    '--------------------------------------------------------------

    ''>>How To Use (Sample Coding)
    ''First Add This Control To Your Project And then Named it rec
    'Try
    'If rec.State = SSVoiceRecordCtrl.SSVoiceRecordCtrl.MyState.Recording Or rec.State = SSVoiceRecordCtrl.SSVoiceRecordCtrl.MyState.Paused Then
    '     rec.CloseRecord()
    'End If
    'If rec.State = SSVoiceRecordCtrl.SSVoiceRecordCtrl.MyState.Idle Then
    '              rec.SoundFormat = SSVoiceRecordCtrl.SSVoiceRecordCtrl.SoundFormats.Mono_6kbps_8_Bit
    '              rec.FileName = "C:\Test.wav" 'Wav File Only (Extension Should Be There)
    '              rec.StartRecord()
    'End If
    'Catch ex As Exception
    '      MsgBox("Sound Recording Error : " & ex.Message & vbNewLine & "Please Contact IT Department", MsgBoxStyle.Information)
    'End Try
    ''Stop Recording And Save the File
    'rec.StopRecord
    ''>>>

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer



    Dim lSamples, lRet, lBits, lChannels As Integer
    Dim iBlockAlign As Short
    Dim lBytesPerSec As Integer



    Public Enum SoundFormats
        Mono_6kbps_8_Bit
        Mono_8kbps_8_Bit
        Mono_11kbps_8_Bit
        Mono_16kbps_8_Bit
        Mono_22kbps_8_Bit
        Mono_32kbps_8_Bit
        Mono_44kbps_8_Bit
        Mono_48kbps_8_Bit
        Stereo_24kbps_16_Bit
        Stereo_32kbps_16_Bit
        Stereo_44kbps_16_Bit
        Stereo_64kbps_16_Bit
        Stereo_88kbps_16_Bit
        Stereo_128kbps_16_Bit
        Stereo_176kbps_16_Bit
        Stereo_192kbps_16_Bit
        Stereo_192kbps_24_Bit
    End Enum

    Public Enum MyState
        Idle
        Recording
        Paused
    End Enum

    Private xState As MyState
    Public ReadOnly Property State() As MyState
        Get
            State = xState
        End Get
    End Property

    Private _SoundFormat As SoundFormats
    Public Property SoundFormat() As SoundFormats
        Get
            SoundFormat = _SoundFormat
        End Get
        Set(ByVal Value As SoundFormats)
            _SoundFormat = Value
        End Set
    End Property

    Private Sub GetSoundFormat()
        If _SoundFormat = SoundFormats.Mono_6kbps_8_Bit Then
            lSamples = 6000 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_8kbps_8_Bit Then
            lSamples = 8000 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_11kbps_8_Bit Then
            lSamples = 11025 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_16kbps_8_Bit Then
            lSamples = 16000 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_22kbps_8_Bit Then
            lSamples = 22050 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_32kbps_8_Bit Then
            lSamples = 32000 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_44kbps_8_Bit Then
            lSamples = 44100 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Mono_48kbps_8_Bit Then
            lSamples = 48000 : lBits = 8 : lChannels = 1
        ElseIf _SoundFormat = SoundFormats.Stereo_24kbps_16_Bit Then
            lSamples = 6000 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_32kbps_16_Bit Then
            lSamples = 8000 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_44kbps_16_Bit Then
            lSamples = 11025 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_64kbps_16_Bit Then
            lSamples = 16000 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_88kbps_16_Bit Then
            lSamples = 22050 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_128kbps_16_Bit Then
            lSamples = 32000 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_176kbps_16_Bit Then
            lSamples = 44100 : lBits = 16 : lChannels = 2
        ElseIf _SoundFormat = SoundFormats.Stereo_192kbps_16_Bit Then
            lSamples = 48000 : lBits = 16 : lChannels = 2
        End If

        iBlockAlign = lChannels * lBits / 8
        lBytesPerSec = lSamples * iBlockAlign
    End Sub
    Private FName As String
    Public Property FileName() As String
        Get
            FileName = FName
        End Get
        Set(ByVal Value As String)
            FName = Value
        End Set
    End Property


    Public Function StartRecord() As Boolean
        Call GetSoundFormat()
        On Error GoTo ER
        If FName = "" Then GoTo ER
        Dim i As Integer
        i = mciSendString("open new type waveaudio alias capture", vbNullString, 0, 0)
        I = mciSendString("set capture samplespersec " & lSamples & " channels " & lChannels & " bitspersample " & lBits & " alignment " & iBlockAlign & " bytespersec " & lBytesPerSec, vbNullString, 0, 0)
        I = mciSendString("record capture", vbNullString, 0, 0)
        xState = MyState.Recording
        StartRecord = True
        Exit Function
ER:
        StartRecord = False
        Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
        Throw SSVoiceRecordControlExec
    End Function

    Public Function StopRecord() As Boolean
        On Error GoTo ER
        If FName = "" Then GoTo ER
        Dim i As Integer
        I = mciSendString("save capture " & FName, vbNullString, 0, 0)
        I = mciSendString("close capture", vbNullString, 0, 0)
        xState = MyState.Idle
        StopRecord = True
        Exit Function
ER:
        i = mciSendString("close capture", vbNullString, 0, 0)
        StopRecord = False
        Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
        Throw SSVoiceRecordControlExec
    End Function

    ''Closing Recording But Not Saved
    Public Sub CloseRecord()
        Dim i As Integer
        i = mciSendString("close capture", vbNullString, 0, 0)
        xState = MyState.Idle
    End Sub

    Private Sub Class_Initialize_Renamed()
        xState = MyState.Idle
    End Sub
    Private Sub Class_Terminate_Renamed()
        StopRecord()
    End Sub
    Protected Overrides Sub Finalize()
        Class_Terminate_Renamed()
        MyBase.Finalize()
    End Sub

    Public Function PauseRecord() As Boolean
        On Error GoTo ER
        If FName = "" Then GoTo ER
        Dim RS As String
        Dim cb, I As Integer
        RS = Space(128)
        If xState = MyState.Paused Then
            I = mciSendString("record capture", vbNullString, 0, 0)
            xState = MyState.Recording
        ElseIf xState = MyState.Recording Then
            I = mciSendString("pause capture", vbNullString, 0, 0)
            xState = MyState.Paused
        End If
        PauseRecord = True
        Exit Function
ER:
        PauseRecord = False
        Dim SSVoiceRecordControlExec As New ArgumentException("File Name Not Specified.")
        Throw SSVoiceRecordControlExec
    End Function

End Class

Developer technologies VB
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2023-06-22T06:47:40.2966667+00:00

    It is done now with WASAPI : https://docs.microsoft.com/en-us/windows/desktop/coreaudio/capturing-a-stream

    I had adapted the MS sample, to be improved (you can use pMMDeviceEnumerator.EnumAudioEndpoints instead of pMMDeviceEnumerator.GetDefaultAudioEndpoint to enumerate endpoints) :

    User's image

    Impossible to paste code, so a link : https://pastebin.fr/129040


  2. Duarte Vinagre 60 Reputation points
    2023-06-22T09:25:03.8633333+00:00

    There is a lot of code in the link i probably do not need and also do not understand..could you simplify?..all i wanna do is record mic and line in

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.