This project has moved. For the latest updates, please go here.

Detect Silence

Jul 9, 2010 at 10:30 PM

Maybe someone could help with a conversion to How do I get the value from or call it. I am new to this and working on a simple project (I think) that will detect audio from my sound card.



Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports NAudio.Wave
Imports System.Diagnostics

Public Class Form1
    Private AudioThresh As Double = 0.09
    Private waveInStream As WaveInStream
    Private WaveIn As WaveIn
    'When button Test is clicked, start listening for sound

    Private Sub btnTest_Click(ByVal sender As Object, ByVal e As EventArgs)
        'waveInStream = WaveIn(44100, 1)
        AddHandler waveInStream.DataAvailable, AddressOf waveInStream_DataAvailable
    End Sub

    'Handler for the sound listener
    Private Sub waveInStream_DataAvailable(ByVal sender As Object, ByVal e As WaveInEventArgs)
        Dim result As Boolean = ProcessData(e)
        If result Then
            MessageBox.Show("peak in sound?")
            'no peak in sound
        End If
    End Sub

    'calculate the sound level based on the AudioThresh
    Private Function ProcessData(ByVal e As WaveInEventArgs) As Boolean
        Dim result As Boolean = False
        Dim Tr As Boolean = False
        Dim Sum2 As Double = 0
        Dim Count As Integer = e.BytesRecorded / 2
        For index As Integer = 0 To e.BytesRecorded - 1 Step 2
            Dim Tmp As Double = CShort((e.Buffer(index + 1) << 8) Or e.Buffer(index + 0))
            Tmp /= 32768.0
            Sum2 += Tmp * Tmp
            If Tmp > AudioThresh Then
                Tr = True
            End If
        Sum2 /= Count
        ' If the Mean-Square is greater than a threshold, set a flag to indicate that noise has happened
        If Tr OrElse Sum2 > AudioThresh Then
            result = True

            result = False

        End If
        Return result
    End Function

End Class