Bienvenido a Tecnohackers

Tecnohackers » Programacion » Area de Programacion » Programacion a Alto Nivel. Visual Basic, Java, delphi, etc.
 » 

Microphone Recorder 1.0 - VB.Net



Autor Tema: Microphone Recorder 1.0 - VB.Net  (Leído 657 veces)

Desconectado zolo

  • Consigliere
  • Master
  • *****
  • Mensajes: 23165
  • Un Mes, Un Año o Toda Una Vida, Da Igual, Estare
Microphone Recorder 1.0 - VB.Net
« en: Julio 09, 2016, 11:12:07 am »

Código: You are not allowed to view links. Register or Login
########################################################################################
########################################################################################
#+-
#+- Title: Microphone Recorder 1.0
#+- Author: Timsel10
#+- Category: Tool Multimedia
#+- Description:  It use the WinMM.Net dll to get data from the microphone.  It will save the recording in a .mr file I made up. So if you want to listen to the recording again you have to use the right side of the application.
#+- Note: Your virus scanner would probably say the WinMM dll is a virus, because some RATs use it, so don't worry it is a false positive. It not save as an MP3 file.
#+- Date: 2016-02-10
#+- 
########################################################################################
########################################################################################
 

Código: You are not allowed to view links. Register or Login
Imports WinMM
Imports System
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections.Generic

Friend NotInheritable Class MainForm

    Private WithEvents Recorder As WaveIn
    Private WithEvents SoundPlayer As WaveOut
    Private ReadOnly Devices As New List(Of WaveInDeviceCaps)
    Private ReadOnly WaveFormatDictionary As New Dictionary(Of String, WaveFormat)
    Private SaveLocation As String
    Private ReadLocation As String
    Private Paused As Boolean
    Private LastFormat As String

    Friend Sub New()
        WaveFormatDictionary.Add("8Khz 16Bit Mono", WaveFormat.Pcm8Khz16BitMono)
        WaveFormatDictionary.Add("8Khz 16Bit Stereo", WaveFormat.Pcm8Khz16BitStereo)
        WaveFormatDictionary.Add("8Khz 8Bit Mono", WaveFormat.Pcm8Khz8BitMono)
        WaveFormatDictionary.Add("8Khz 8Bit Stereo", WaveFormat.Pcm8Khz8BitStereo)
        WaveFormatDictionary.Add("11Khz 16Bit Mono", WaveFormat.Pcm11Khz16BitMono)
        WaveFormatDictionary.Add("11Khz 16Bit Stereo", WaveFormat.Pcm11Khz16BitStereo)
        WaveFormatDictionary.Add("11Khz 8Bit Mono", WaveFormat.Pcm11Khz8BitMono)
        WaveFormatDictionary.Add("11Khz 8Bit Stereo", WaveFormat.Pcm11Khz8BitStereo)
        WaveFormatDictionary.Add("12Khz 16Bit Mono", WaveFormat.Pcm12Khz16BitMono)
        WaveFormatDictionary.Add("12Khz 16Bit Stereo", WaveFormat.Pcm12Khz16BitStereo)
        WaveFormatDictionary.Add("12Khz 8Bit Mono", WaveFormat.Pcm12Khz8BitMono)
        WaveFormatDictionary.Add("12Khz 8Bit Stereo", WaveFormat.Pcm12Khz8BitStereo)
        WaveFormatDictionary.Add("16Khz 16Bit Mono", WaveFormat.Pcm16Khz16BitMono)
        WaveFormatDictionary.Add("16Khz 16Bit Stereo", WaveFormat.Pcm16Khz16BitStereo)
        WaveFormatDictionary.Add("16Khz 8Bit Mono", WaveFormat.Pcm16Khz8BitMono)
        WaveFormatDictionary.Add("16Khz 8Bit Stereo", WaveFormat.Pcm16Khz8BitStereo)
        WaveFormatDictionary.Add("22Khz 16Bit Mono", WaveFormat.Pcm22Khz16BitMono)
        WaveFormatDictionary.Add("22Khz 16Bit Stereo", WaveFormat.Pcm22Khz16BitStereo)
        WaveFormatDictionary.Add("22Khz 8Bit Mono", WaveFormat.Pcm22Khz8BitMono)
        WaveFormatDictionary.Add("22Khz 8Bit Stereo", WaveFormat.Pcm22Khz8BitStereo)
        WaveFormatDictionary.Add("24Khz 16Bit Mono", WaveFormat.Pcm24Khz16BitMono)
        WaveFormatDictionary.Add("24Khz 16Bit Stereo", WaveFormat.Pcm24Khz16BitStereo)
        WaveFormatDictionary.Add("24Khz 8Bit Mono", WaveFormat.Pcm24Khz8BitMono)
        WaveFormatDictionary.Add("24Khz 8Bit Stereo", WaveFormat.Pcm24Khz8BitStereo)
        WaveFormatDictionary.Add("32Khz 16Bit Mono", WaveFormat.Pcm32Khz16BitMono)
        WaveFormatDictionary.Add("32Khz 16Bit Stereo", WaveFormat.Pcm32Khz16BitStereo)
        WaveFormatDictionary.Add("32Khz 8Bit Mono", WaveFormat.Pcm32Khz8BitMono)
        WaveFormatDictionary.Add("32Khz 8Bit Stereo", WaveFormat.Pcm32Khz8BitStereo)
        WaveFormatDictionary.Add("44Khz 16Bit Mono", WaveFormat.Pcm44Khz16BitMono)
        WaveFormatDictionary.Add("44Khz 16Bit Stereo", WaveFormat.Pcm44Khz16BitStereo)
        WaveFormatDictionary.Add("44Khz 8Bit Mono", WaveFormat.Pcm44Khz8BitMono)
        WaveFormatDictionary.Add("44Khz 8Bit Stereo", WaveFormat.Pcm44Khz8BitStereo)

        InitializeComponent()
    End Sub

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = True

        For Each Mic As WaveInDeviceCaps In WaveIn.Devices
            If Mic IsNot Nothing Then Devices.Add(Mic)
            DevicesComboBox.Items.Add(Mic.Name)
        Next

        DevicesComboBox.SelectedIndex = 0
        BufferSizeComboBox.SelectedIndex = 6
        FormatComboBox.SelectedIndex = 6
    End Sub

    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If Recorder IsNot Nothing Then
            Recorder.Close()
            Recorder.Dispose()
        End If

        If SoundPlayer IsNot Nothing Then
            SoundPlayer.Stop()
            SoundPlayer.Dispose()
        End If

        Recorder = Nothing
        SoundPlayer = Nothing
    End Sub

    Private Sub RecordButton_Click(sender As Object, e As EventArgs) Handles RecordButton.Click
        If String.IsNullOrEmpty(SaveLocationTextBox.Text) Then
            MessageBox.Show("Please select a save location.", "Recorder", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        If File.Exists(SaveLocationTextBox.Text) Then
            MessageBox.Show("Cannot save please select an empty save location.", "Recorder", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If

        Dim Microphone As WaveInDeviceCaps = Device(DevicesComboBox.Text)
        If Microphone Is Nothing Then
            MessageBox.Show("Cannot find microphone.", "Recorder", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim Frequency As String = String.Format("{0}{1}", FormatComboBox.Text.Split(" "c))
        Dim Bit As Byte = If(Bit8RadioButton.Checked, CByte(8), CByte(16))
        Dim Mono As Boolean = Not StereoCheckBox.Checked

        Dim Format As WaveFormat = GetWaveFormat(Frequency, Bit, Mono)
        LastFormat = GetWaveFormatString(Frequency, Bit, Mono)

        If Format Is Nothing Then
            MessageBox.Show("Cannot find the format.", "Recorder", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Recorder = New WaveIn(Microphone.DeviceId)
        Recorder.Open(Format)
        Recorder.BufferSize = CInt(BufferSizeComboBox.Text.Split(" "c)(0)) * 1024
        Recorder.Start()
        RecordButton.Enabled = False
        StopButton.Enabled = True
    End Sub

    Private Sub StopButton_Click(sender As Object, e As EventArgs) Handles StopButton.Click
        StopButton.Enabled = False
        If Recorder IsNot Nothing Then Recorder.Stop()
        Recorder = Nothing

        Using ReadDataStream As New FileStream(String.Format("{0}data", SaveLocation), FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim Data As Byte() = New Byte(CInt(ReadDataStream.Length - 1)) {}
            ReadDataStream.Read(Data, 0, CInt(ReadDataStream.Length))

            Dim Audio As New AudioStructure
            Audio.Data = Data
            Audio.Format = LastFormat

            SaveFile(SaveLocation, Audio)

            ReadDataStream.Close()
        End Using

        File.Delete(String.Format("{0}data", SaveLocation))

        RecordButton.Enabled = True
    End Sub

    Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click
        If String.IsNullOrEmpty(ReadLocationTextBox.Text) Then
            MessageBox.Show("Please select a recorded file.", "Recorder", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        PlayButton.Enabled = False

        If Paused Then
            SoundPlayer.Resume()
            Paused = False
            PauseButton.Enabled = True
            Exit Sub
        End If

        Paused = False
        SoundPlayer = New WaveOut(0)
        Dim Audio As AudioStructure = ReadFile(ReadLocation)
        SoundPlayer.Open(WaveFormatDictionary(Audio.Format))

        SoundPlayer.Write(Audio.Data)

        PauseButton.Enabled = True
        StopButton.Enabled = True
    End Sub

    Private Sub PauseButton_Click(sender As Object, e As EventArgs) Handles PauseButton.Click
        If SoundPlayer IsNot Nothing Then SoundPlayer.Pause()
        Paused = True

        PauseButton.Enabled = False
        PlayButton.Enabled = True
    End Sub

    Private Sub StopAudioButton_Click(sender As Object, e As EventArgs) Handles StopAudioButton.Click
        If SoundPlayer IsNot Nothing Then SoundPlayer.Pause()
        Paused = False

        PauseButton.Enabled = False
        PlayButton.Enabled = True
    End Sub

    Private Sub SaveLocationButton_Click(sender As Object, e As EventArgs) Handles SaveLocationButton.Click
        Using RecorderSaveFileDialog As New SaveFileDialog
            RecorderSaveFileDialog.Title = "Save recording"
            RecorderSaveFileDialog.Filter = "Microphone Recorder Files (*.mr)|*.mr"

            If RecorderSaveFileDialog.ShowDialog = DialogResult.OK Then
                SaveLocation = RecorderSaveFileDialog.FileName
                SaveLocationTextBox.Text = RecorderSaveFileDialog.FileName
            End If
        End Using
    End Sub

    Private Sub ReadLocationButton_Click(sender As Object, e As EventArgs) Handles ReadLocationButton.Click
        Using RecorderOpenFileDialog As New OpenFileDialog
            RecorderOpenFileDialog.Title = "Open recorded file"
            RecorderOpenFileDialog.Filter = "Microphone Recorder Files (*.mr)|*.mr"

            If RecorderOpenFileDialog.ShowDialog = DialogResult.OK Then
                ReadLocation = RecorderOpenFileDialog.FileName
                ReadLocationTextBox.Text = RecorderOpenFileDialog.FileName
            End If
            Dim Audio As AudioStructure = ReadFile(ReadLocation)
            PlayFormatLabel.Text = Audio.Format
        End Using
    End Sub

    Private Sub BufferSizeComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BufferSizeComboBox.SelectedIndexChanged
        If Recorder IsNot Nothing Then Recorder.BufferSize = CInt(BufferSizeComboBox.Text.Split(" "c)(0)) * 1024
    End Sub

    Private Sub DevicesComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DevicesComboBox.SelectedIndexChanged
        If PlayButton.Text = "Stop" Then PlayButton.PerformClick()
        Dim Mic As WaveInDeviceCaps = Device(DevicesComboBox.Text)
        NameLabel.Text = Mic.Name
        ManufacturerLabel.Text = Mic.Manufacturer
        ChannelsLabel.Text = Mic.Channels.ToString
        IDLabel.Text = Mic.ProductId.ToString
        If Mic.Channels = 1 Then
            StereoCheckBox.Enabled = False
            StereoCheckBox.Checked = False
        End If


    End Sub

    Private Sub VolumeTrackBar_Scroll(sender As Object, e As EventArgs) Handles VolumeRTrackBar.Scroll, VolumeLTrackBar.Scroll
        If SoundPlayer IsNot Nothing Then
            Dim Vol As New Volume
            Vol.Right = CSng(VolumeRTrackBar.Value / 100)
            Vol.Left = CSng(VolumeLTrackBar.Value / 100)
            SoundPlayer.Volume = Vol
        End If
    End Sub

    Private Sub Recorder_DataReady(sender As Object, e As DataReadyEventArgs) Handles Recorder.DataReady
        Using DataStream As New FileStream(String.Format("{0}data", SaveLocation), FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
            DataStream.Write(e.Data, 0, e.Data.Length)
            DataStream.Close()
        End Using
    End Sub

    Private Sub SoundPlayer_MessageReceived(sender As Object, e As WaveOutMessageReceivedEventArgs) Handles SoundPlayer.MessageReceived
        Invoke(New MethodInvoker(Sub() 'Without this invoke you will get an error sometimes
                                     StatusLabel.Text = e.Message.ToString

                                     If e.Message = WaveOutMessage.WriteDone Then
                                         PauseButton.Enabled = False
                                         PlayButton.Enabled = True
                                         StopButton.Enabled = False
                                     End If
                                 End Sub))
    End Sub

    Private Function Device(DeviceName As String) As WaveInDeviceCaps
        For Each RecordDevice As WaveInDeviceCaps In Devices
            If RecordDevice.Name = DeviceName Then Return RecordDevice
        Next

        Return Nothing
    End Function

    Private Function GetWaveFormat(Frequency As String, Bit As Byte, Mono As Boolean) As WaveFormat
        Dim Channel As String = If(Mono, "Mono", "Stereo")
        Dim Key As String = String.Format("{0} {1}Bit {2}", Frequency, Bit.ToString, Channel)
        If WaveFormatDictionary.ContainsKey(Key) Then
            Return WaveFormatDictionary(Key)
        Else
            Return Nothing
        End If
    End Function

    Private Function GetWaveFormatString(Frequency As String, Bit As Byte, Mono As Boolean) As String
        Dim Channel As String = If(Mono, "Mono", "Stereo")
        Return String.Format("{0} {1}Bit {2}", Frequency, Bit.ToString, Channel)
    End Function

    Private Sub SaveFile(Location As String, Audio As AudioStructure)
        Using Stream As New MemoryStream()
            Using Writer As New BinaryWriter(DirectCast(Stream, Stream), Encoding.UTF8)
                Writer.Write(Audio.Format)
                Writer.Write(Audio.Data.Length)
                Writer.Write(Audio.Data)

                Writer.Close()
                Dim Data As Byte() = Stream.ToArray

                Using WriteDataStream As New FileStream(Location, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
                    WriteDataStream.Write(Data, 0, Data.Length)
                    WriteDataStream.Close()
                End Using
            End Using
        End Using
    End Sub

    Private Function ReadFile(Location As String) As AudioStructure
        Dim Audio As New AudioStructure
        Using DataStream As New FileStream(Location, FileMode.Open, FileAccess.Read, FileShare.Read)
            Dim Data As Byte() = New Byte(CInt(DataStream.Length - 1)) {}
            DataStream.Read(Data, 0, CInt(DataStream.Length))

            Using Stream As New MemoryStream(Data)
                Using Reader As New BinaryReader(DirectCast(Stream, Stream), Encoding.UTF8)
                    Audio.Format = Reader.ReadString
                    Audio.Data = Reader.ReadBytes(Reader.ReadInt32)

                    Stream.Close()
                    Reader.Close()
                    DataStream.Close()
                    Return Audio
                End Using
            End Using
        End Using
    End Function

    Private Structure AudioStructure
        Friend Format As String
        Friend Data As Byte()
    End Structure

End Class
 



You are not allowed to view links. Register or Login


Fuente: Zephomet / El-hacker
You are not allowed to view links. Register or Login

Tags:
Tags:

 


SMF 2.0.19 | SMF © 2016, Simple Machines
Paginas Afiliadas
Twitter - FaceBook - Daraxblog
Designed by Smf Personal