none
Sound activated program in vb RRS feed

  • Question

  • Hi,
    I'm working on a sound activation project. The idea is simpel! I clap and a program pops up.
    Now i found a program with source code that does what i want but it's in C#. I cant work with C#.
    That's why i have tried to translate it to vb but i didn't really work.... this is my code so far. can any one translate the code further for me it would be a great help!
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Documents
    Imports System.Windows.Input
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Navigation
    Imports System.Windows.Shapes
    Imports NAudio
    Imports NAudio.Wave
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
        End Sub
    
    
        Private bigValue As Single
        Private waveIn As WaveIn
        Private MaxValue As Double
    
    
        Public Class Form1
            Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
                If Convert.ToInt16(TextBox1.Text) > 100 Then
                    MessageBox.Show("Invalid Value")
                    Return
                Else
                    MaxValue = Convert.ToDouble(Textbox1.Text) / 100
                End If
                bigValue = 0
                waveIn = New WaveIn()
                Dim waveInDevices As Integer = waveIn.DeviceCount
                For waveInDevice As Integer = 0 To waveInDevices - 1
                    'MessageBox.Show("Device " + waveInDevice + ": " + deviceInfo.ProductName + "," + deviceInfo.Channels + "channels");
                    Dim deviceInfo As WaveInCapabilities = waveIn.GetCapabilities(waveInDevice)
                Next
    
    
                waveIn.DeviceNumber = 0
                waveIn.DataAvailable += New EventHandler(Of WaveInEventArgs)(waveIn_DataAvailable)
                Dim sampleRate As Integer = 8000
                ' 8 kHz
                Dim channels As Integer = 1
                ' mono
                waveIn.WaveFormat = New WaveFormat(sampleRate, channels)
                waveIn.StartRecording()
            End Sub
    
            Private Sub waveIn_DataAvailable(sender As Object, e As WaveInEventArgs)
                For index As Integer = 0 To e.BytesRecorded - 1 Step 2
                    Dim sample As Short = CShort((e.Buffer(index + 1) << 8) Or e.Buffer(index + 0))
                    Dim sample32 As Single = sample / 32768.0F
                    Label1.Content = sample32.ToString()
                    If bigValue < sample32 Then
                        bigValue = sample32
                        Label2.Content = bigValue.ToString()
                        If bigValue > MaxValue Then
                            waveIn.StopRecording()
    
    
                            System.Diagnostics.Process.Start("C:\Users\Chris\Music\Afspeellijsten\Family Guy ses 3.wpl")
                        End If
                    End If
                Next
    
            End Sub
    
            Private Sub textBox1_TextChanged(sender As Object, e As TextChangedEventArgs)
    
            End Sub
    
            Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    
            End Sub
    
    
    
    
        End Class
    
        End Sub
    End Class
    
    
    Friday, February 3, 2012 12:09 AM

All replies

  • Hi,
    I'm working on a sound activation project. The idea is simpel! I clap and a program pops up.
    Now i found a program with source code that does what i want but it's in C#. I cant work with C#.
    That's why i have tried to translate it to vb but i didn't really work.... this is my code so far. can any one translate the code further for me it would be a great help!
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Windows
    Imports System.Windows.Controls
    Imports System.Windows.Data
    Imports System.Windows.Documents
    Imports System.Windows.Input
    Imports System.Windows.Media
    Imports System.Windows.Media.Imaging
    Imports System.Windows.Navigation
    Imports System.Windows.Shapes
    Imports NAudio
    Imports NAudio.Wave
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
        End Sub
    
    
        Private bigValue As Single
        Private waveIn As WaveIn
        Private MaxValue As Double
    
    
        Public Class Form1
            Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
                If Convert.ToInt16(TextBox1.Text) > 100 Then
                    MessageBox.Show("Invalid Value")
                    Return
                Else
                    MaxValue = Convert.ToDouble(Textbox1.Text) / 100
                End If
                bigValue = 0
                waveIn = New WaveIn()
                Dim waveInDevices As Integer = waveIn.DeviceCount
                For waveInDevice As Integer = 0 To waveInDevices - 1
                    'MessageBox.Show("Device " + waveInDevice + ": " + deviceInfo.ProductName + "," + deviceInfo.Channels + "channels");
                    Dim deviceInfo As WaveInCapabilities = waveIn.GetCapabilities(waveInDevice)
                Next
    
    
                waveIn.DeviceNumber = 0
                waveIn.DataAvailable += New EventHandler(Of WaveInEventArgs)(waveIn_DataAvailable)
                Dim sampleRate As Integer = 8000
                ' 8 kHz
                Dim channels As Integer = 1
                ' mono
                waveIn.WaveFormat = New WaveFormat(sampleRate, channels)
                waveIn.StartRecording()
            End Sub
    
            Private Sub waveIn_DataAvailable(sender As Object, e As WaveInEventArgs)
                For index As Integer = 0 To e.BytesRecorded - 1 Step 2
                    Dim sample As Short = CShort((e.Buffer(index + 1) << 8) Or e.Buffer(index + 0))
                    Dim sample32 As Single = sample / 32768.0F
                    Label1.Content = sample32.ToString()
                    If bigValue < sample32 Then
                        bigValue = sample32
                        Label2.Content = bigValue.ToString()
                        If bigValue > MaxValue Then
                            waveIn.StopRecording()
    
    
                            System.Diagnostics.Process.Start("C:\Users\Chris\Music\Playlist\Family Guy ses 3.wpl")
                        End If
                    End If
                Next
    
            End Sub
    
            Private Sub textBox1_TextChanged(sender As Object, e As TextChangedEventArgs)
    
            End Sub
    
            Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    
            End Sub
    
    
    
    
        End Class
    
        End Sub
    End Class
    
    

     

    • Edited by ChrisDV Thursday, February 2, 2012 11:38 PM
    • Merged by lucy-liu Monday, February 6, 2012 5:57 AM duplicate thread
    Thursday, February 2, 2012 11:37 PM
  • This is not the proper forum, this has nothing to do with Visual Studio extensibility.

    Ryan

    Thursday, February 2, 2012 11:56 PM
  • ow oke yeah i see now! thank you anyway for replying

     


    • Edited by ChrisDV Friday, February 3, 2012 12:10 AM
    Friday, February 3, 2012 12:08 AM