Introduction
volume meter in vb.net tested working gets the audio level from usb microphone (in 2 channels)
at realtime
utilizing directx
tested on vb 2008 express edition win xp pro sp3 .net 3.5
Background
the credit goes to 2 very good programers :
1 nigel ealand, who evolved the code to work in vb 2008
2 jacob klint the original poster of the code in codeguru at the link:
http://www.codeproject.com/KB/direct...87#xx3514687xx
Using the code
form controls (designer) :
1 ComboBox name : ComboBox1
2 button name : FindButton
3 button name : StartButton
4 progressbar name : ProgressBar1 maximum : 32770
5 progressbar name : ProgressBar2 maximum : 32770
download directx 9 sdk from the link :
http://www.microsoft.com/downloads/d...displaylang=en[^]
install, restart comuter, connect usb microphone with drivers installed from its cd (auto plug n play install might not suffice)
paste source code (in the end of this text) or
sln file : http://www.esac.org.uk/VUTest.zip[^] if your lazy
project, add reference, .net, microsoft.directx.sound
disabling loader lock error (debug, exeptions, managed debuging assistants,
uncheck loader lock (thrown))
if not unchecked press debug again after exeption will have been thrown
run : press button 1 , press button 2
Points of Interest
samples delay variables (in source code) :
Private m_sampleDelay As Integer = 15 ' miliseconds
Private m_frameDelay As Integer = 15
look up in youtube: vb.net volume meter
'end of tutorial
you can delete :
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
microphones cd drivers need to be installed.
take your time, this king of thing requires lazyness.
volume meter in vb.net tested working gets the audio level from usb microphone (in 2 channels)
at realtime
utilizing directx
tested on vb 2008 express edition win xp pro sp3 .net 3.5
Background
the credit goes to 2 very good programers :
1 nigel ealand, who evolved the code to work in vb 2008
2 jacob klint the original poster of the code in codeguru at the link:
http://www.codeproject.com/KB/direct...87#xx3514687xx
Using the code
form controls (designer) :
1 ComboBox name : ComboBox1
2 button name : FindButton
3 button name : StartButton
4 progressbar name : ProgressBar1 maximum : 32770
5 progressbar name : ProgressBar2 maximum : 32770
download directx 9 sdk from the link :
http://www.microsoft.com/downloads/d...displaylang=en[^]
install, restart comuter, connect usb microphone with drivers installed from its cd (auto plug n play install might not suffice)
paste source code (in the end of this text) or
sln file : http://www.esac.org.uk/VUTest.zip[^] if your lazy
project, add reference, .net, microsoft.directx.sound
disabling loader lock error (debug, exeptions, managed debuging assistants,
uncheck loader lock (thrown))
if not unchecked press debug again after exeption will have been thrown
Code:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX.DirectSound
Imports System.Threading
Imports System.Collections.Specialized
Public Class Sound_Card_Form
Private Sub StartButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindButton.Click
'Dim MyVU As New VolumeMeter
'MyVU.Start()
Start()
End Sub
Private Sub FindButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FindButton.Click
'Dim MyVU As New VolumeMeter
'MyVU.FindDevices()
FindDevices()
End Sub
' Public Class VolumeMeter
'Inherits System.Windows.Forms.UserControl
'Public Delegate Sub VolumeChangedEventHandler(ByVal vcea As VolumeChangedEventArgs)
'Public Event VolumeChanged As VolumeChangedEventHandler
Private Const SAMPLES As Integer = 8
Private Shared SAMPLE_FORMAT_ARRAY As Integer() = {SAMPLES, 2, 1}
Public Shared audioDevices As CaptureDevicesCollection
Private Shared m_deviceNames As StringCollection
Private deviceName As String = "(none)"
Private deviceIndex As Integer = -1
Private buffer As Microsoft.DirectX.DirectSound.CaptureBuffer
Private liveVolumeThread As System.Threading.Thread
Private m_sampleDelay As Integer = 100
Private m_frameDelay As Integer = 10
Private m_autoStart As Boolean = True
'Private components As System.ComponentModel.Container = Nothing
Public Sub FindDevices()
Dim audioDevices As New CaptureDevicesCollection
Dim x As Integer = 0
While x < audioDevices.Count
ComboBox1.Items.Add(audioDevices.Item(x).Description)
x = x + 1
End While
ComboBox1.SelectedIndex = 0
End Sub
Public Sub Start()
[Stop]()
Dim audioDevices As New CaptureDevicesCollection
deviceIndex = ComboBox1.SelectedIndex
If deviceIndex <> -1 Then
' initialize the capture buffer and start the animation thread
Dim cap As New Capture(audioDevices(deviceIndex).DriverGuid)
Dim desc As New CaptureBufferDescription()
Dim wf As New WaveFormat()
wf.BitsPerSample = 16
wf.SamplesPerSecond = 44100
wf.Channels = 2
wf.BlockAlign = CShort(wf.Channels * wf.BitsPerSample / 8)
wf.AverageBytesPerSecond = wf.BlockAlign * wf.SamplesPerSecond
wf.FormatTag = WaveFormatTag.Pcm
desc.Format = wf
desc.BufferBytes = SAMPLES * wf.BlockAlign
buffer = New Microsoft.DirectX.DirectSound.CaptureBuffer(desc, cap)
buffer.Start(True)
' Start a seperate thread to read the buffer and update the progress bars
liveVolumeThread = New Thread(AddressOf updateProgress) 'Thread starts at updateProgress
Control.CheckForIllegalCrossThreadCalls = False ' This is needed otherwise the form will not update
liveVolumeThread.Priority = ThreadPriority.Lowest ' Thread works in the background
liveVolumeThread.Start()
End If
End Sub
Public Sub [Stop]()
If liveVolumeThread IsNot Nothing Then
liveVolumeThread.Abort()
liveVolumeThread.Join()
liveVolumeThread = Nothing
End If
If buffer IsNot Nothing Then
If buffer.Capturing Then
buffer.[Stop]()
End If
buffer.Dispose()
buffer = Nothing
End If
End Sub
Public Sub updateProgress()
While True
Dim tempFrameDelay As Integer = m_frameDelay
Dim tempSampleDelay As Integer = m_sampleDelay
Dim samples__1 As Array = buffer.Read(0, GetType(Int16), LockFlag.FromWriteCursor, SAMPLE_FORMAT_ARRAY)
' for each channel, determine the step size necessary for each iteration
Dim leftGoal As Integer = 0
Dim rightGoal As Integer = 0
' Sum the 8 samples
For i As Integer = 0 To SAMPLES - 1
leftGoal += CType(samples__1.GetValue(i, 0, 0), Int16)
rightGoal += CType(samples__1.GetValue(i, 1, 0), Int16)
Next
' Calculate the average of the 8 samples
leftGoal = CInt(Math.Abs(leftGoal \ SAMPLES))
rightGoal = CInt(Math.Abs(rightGoal \ SAMPLES))
Dim range1 As Double = leftGoal - ProgressBar1.Value ' calculates the difference between new and the current progress bar value
Dim range2 As Double = rightGoal - ProgressBar2.Value
' Assign the exact current value to the progress bar
Dim exactValue1 As Double = ProgressBar1.Value
Dim exactValue2 As Double = ProgressBar2.Value
Dim stepSize1 As Double = range1 / tempSampleDelay * tempFrameDelay
' Limit the value range to positive values
If Math.Abs(stepSize1) < 0.01 Then
stepSize1 = Math.Sign(range1) * 0.01
End If
Dim absStepSize1 As Double = Math.Abs(stepSize1)
Dim stepSize2 As Double = range2 / tempSampleDelay * tempFrameDelay
If Math.Abs(stepSize2) < 0.01 Then
stepSize2 = Math.Sign(range2) * 0.01
End If
Dim absStepSize2 As Double = Math.Abs(stepSize2)
' increment/decrement the bars' values until both equal their desired goals,
' sleeping between iterations
If (ProgressBar1.Value = leftGoal) AndAlso (ProgressBar2.Value = rightGoal) Then
Thread.Sleep(tempSampleDelay)
Else
Do
If ProgressBar1.Value <> leftGoal Then
If absStepSize1 < Math.Abs(leftGoal - ProgressBar1.Value) Then
exactValue1 += stepSize1
ProgressBar1.Value = CInt(Math.Truncate(Math.Round(exactValue1)))
'This is the real value
'decibels = 20 * Log10(ProgressBar1.Value/ 32768.0)
Else
ProgressBar1.Value = leftGoal
End If
End If
If ProgressBar2.Value <> rightGoal Then
If absStepSize2 < Math.Abs(rightGoal - ProgressBar2.Value) Then
exactValue2 += stepSize2
ProgressBar2.Value = CInt(Math.Truncate(Math.Round(exactValue2)))
Else
ProgressBar2.Value = rightGoal
End If
End If
Thread.Sleep(tempFrameDelay)
Loop While (ProgressBar1.Value <> leftGoal) OrElse (ProgressBar2.Value <> rightGoal)
End If
End While
End Sub
End Class
run : press button 1 , press button 2
Points of Interest
samples delay variables (in source code) :
Private m_sampleDelay As Integer = 15 ' miliseconds
Private m_frameDelay As Integer = 15
look up in youtube: vb.net volume meter
'end of tutorial
you can delete :
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
microphones cd drivers need to be installed.
take your time, this king of thing requires lazyness.