Introduction
This tip describes how to capture audio and then replay it using Microsoft Direct Sound DLL. I have been searching about how to capture audio using direct Sound in VB over the internet for a couple of weeks, but I could not get any meaningful code. Finally, through try and error, I managed to get the code working. I then decided to write and share this article with everyone who is researching about sound capturing using Direct Sound. The code is very easy to understand. I will try to explain the whole thing, but I assume that you have an idea or have researched a bit about sound in waveformat and you have an idea on what audio is and how it is stored. What we are simply doing is capturing audio in wavformat using a capturebuffer
, then write it to a secondary buffer. That audio which is stored in a secondary buffer is then replayed. Note that I used VB Studio 8 with this code.
Let's start. You need the following:
- Three buttons, named start, stop, play
- One Label
- Add reference to Microsoft Direct Sound DLL.
- Change configuration to x86 platform (In your VB Studio, go to Build>Configuration Manager)
- Remove loader lock (In your VB Studio, go to Debug>Exceptions>Managed Debugging Assistants>Uncheck Loader Lock)
Create a new VB application. Add your three buttons and a label which will be showing background activities happening upon clicking of your buttons. Double click on your form and import the following to your form. Paste the following code:
Imports Microsoft.DirectX.DirectSound
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Imports System.Text
Imports System.Drawing
Imports System.Data
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Console
Imports System.Net.SocketsColourised in 11ms
Next. In the class of your form, declare the capture, create a capturebuffer
used to capture sound, set a device to play your sound with, set a secondary buffer to store your sound, set bufferposition
and set buffer size. Paste the following code:
Public Class Form1
Inherits Form
Private _captureDevice As Capture Private _captureBuffer As CaptureBuffer Private _playbackDevice As Device Private _playbackBuffer As SecondaryBuffer store captured sound
Private _bufferSize As Integer = 5000000 Private Const _bufferPositions As Integer = 4Colourised in 14ms
Next, you double click the first button, "button start". You declare a capture, the channels to be used, bits per sample, samples per second and set a wav format. You then create a secondary buffer used to store audio and a capture buffer to capture recorded audio. Overall, you start your capture buffer and recording of audio starts. The label then reflects that capturing has started. Paste the following code:
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonStart.Click
_captureDevice = New Capture()
Dim channels As Short = 1
Dim bitsPerSample As Short = 8
Dim samplesPerSecond As Integer = 22050
Dim waveFormat As New WaveFormat()
waveFormat.Channels = channels
waveFormat.FormatTag = WaveFormatTag.Pcm
waveFormat.SamplesPerSecond = samplesPerSecond
waveFormat.BitsPerSample = bitsPerSample
waveFormat.BlockAlign = CType((channels * (bitsPerSample / 8)), Short)
waveFormat.AverageBytesPerSecond = waveFormat.BlockAlign *
samplesPerSecond
Dim captureBufferDescription As New CaptureBufferDescription()
captureBufferDescription.BufferBytes = _bufferPositions * _bufferSize
captureBufferDescription.Format = waveFormat
_captureBuffer = New CaptureBuffer(captureBufferDescription,
_captureDevice)
_playbackDevice = New Device()
_playbackDevice.SetCooperativeLevel(Me, CooperativeLevel.Normal)
Dim playbackBufferDescription As New BufferDescription()
playbackBufferDescription.BufferBytes = _bufferPositions * _bufferSize
playbackBufferDescription.Format = waveFormat
_playbackBuffer = New SecondaryBuffer(playbackBufferDescription,
_playbackDevice)
_captureBuffer.Start(True) Label1.Text = "Now Capturing audio..."
End SubColourised in 56ms
Now that recording of audio has started, the next step is to add the code to stop audio capturing. Double click the "Stop Button". Under this button, we simply stop capture of audio by stopping the capture buffer and reflect it on the label that the capture buffer has been stopped. Paste the following code:
Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ButtonStop.Click
_captureBuffer .Stop
Label1.Text = "Capturing stopped..."
End SubColourised in 10ms
Next, double click the last button which is the "Replay button". Under this button, you now copy the recorded audio from the capturebuffer
to the secondary buffer for playing. The secondary buffer is played and audio starts to playback. Paste the following code.
Private Sub ButtonPlay_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles ButtonPlay.Click
Dim offset As Integer = 0
Dim buffer As Byte() = CType(_captureBuffer.Read(offset,
GetType(Byte), LockFlag.None, _bufferSize), Byte()) capture buffer
_playbackBuffer.Write(offset, buffer, LockFlag.None) data from capture buffer into secondary buffer
_playbackBuffer.Play(0, BufferPlayFlags.Default) playing back sound
Label1.Text = "Buffer is now Playing..."
End SubColourised in 19ms
Thank you for reading.
Good luck!