Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a program (in Visual Basic) that can record sound. I want to record this in real time and play this louder (or transfer it to another "client computer", and play there). How can I retrieve audio data in real time?

Thanks for the answer!

Bob

What I have tried:

My first attempt was taking the audio data every second. This is incorrect, due to it causing stutters.

Dim path As String, number As Integer = 1, timer As Boolean = False, i As Integer = 0
Private Declare Function record Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub RecordGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RecordGo.Click
    record("open new Type waveaudio Alias recsound", "", 0, 0)
    record("record recsound", "", 0, 0)
    Timer1.Enabled = True
    timer = False
End Sub
Private Sub Stop_and_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Stop_and_Save.Click
    record("save recsound " & path, "", 0, 0)
    record("close recsound", "", 0, 0)
    Timer1.Enabled = False
End Sub
Private Sub Play_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Play.Click
    i = 1
    timer = True
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If timer = False Then
        record("save recsound " & path & number & ".wav", "", 0, 0)
        record("close recsound", "", 0, 0)
        record("open new Type waveaudio Alias recsound", "", 0, 0)
        record("record recsound", "", 0, 0)
        number = number + 1
    Else
        My.Computer.Audio.Play(path & i & ".wav", AudioPlayMode.Background)
        i = i + 1
        If i > number - 1 Then
            Timer1.Enabled = False
        End If
    End If
End Sub
Posted
Updated 25-Nov-20 8:21am
v3

1 solution

You can probably do this with the Naudio library, assuming you are using VB.NET
GitHub - naudio/NAudio: Audio and MIDI library for .NET[^]

See example here: naudio - store and play microphone data in real time vb.net[^]
 
Share this answer
 
v2
Comments
Bob of the E five 24-Nov-20 8:24am    
Thanks! I'm indeed using VB.NET. But there is a problem. When I want to open the NAudio file in Visual Studio (Project / Add reference), I can't find the *.dll or *.exe file that Visual Studio can scan.

Bob
RickZeeland 24-Nov-20 8:29am    
Are you using NuGet? then you first have to build before the dll's are pulled.
RickZeeland 24-Nov-20 8:34am    
The information on GitHub is not correct: "Alternatively, you can download the latest release in binary format from here."
This is only source code that you will have to compile first.
Or download NAudio-Release.zip from v1.8.4.
Bob of the E five 25-Nov-20 14:22pm    
Thanks your answer! It is a correct solution!

Bob
RickZeeland 25-Nov-20 14:50pm    
Glad it works, you can mark the solution as solved if you think it is correct.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900