cutoi
asked on
500 POINTS, Please help me as soon as possible.
I asked this question, but I must say that I'm not satisfied with the answer.
How to record from Microphone ~~>Save in to file and process.
Buy component, look Example CaptureSound, and another solution that too old, required directx7 or too difficult Solution,
I"m not an experts at all.
The best answer is that: if you can modify this code? It seems work, but it automatically save this file in "C:\test.wav"
Can't here anything. Please help me. If this code is good, I can easily use this by call. "MyVar.StartRecord()" for example.
Please checked it carefully for me?
Thanks a lot.
Here is the code:
Imports Microsoft.VisualBasic
Imports System
Imports Microsoft.DirectX.DirectSo und
Imports System.Windows.Forms
Imports Buffer = Microsoft.DirectX.DirectSo und.Buffer
Imports System.IO
Imports System.Collections
Namespace DirectSoundRecording
''' <summary>
''' Class used to handle the sound capture qualities of the program
''' </summary>
Public Class SoundRec
Private devices As CaptureDevicesCollection = New CaptureDevicesCollection
Private MyCapture As Capture = New Capture(devices(0).DriverG uid) ' Am I right to choose this device?
Private MySndBuf As CaptureBuffer
''' <summary>
''' Sets a default format capture buffer with no effects; 22KHz 8bit/sample, mono
''' </summary>
Public Sub New()
Dim MySBufDesc As CaptureBufferDescription = New CaptureBufferDescription()
'setting the default wave capture format for use
'by the buffer descriptor
Dim DefaultFormat As WaveFormat = New WaveFormat()
DefaultFormat.SamplesPerSe cond = 22000 'default freq 22khz
DefaultFormat.Channels = 1
DefaultFormat.BitsPerSampl e = 8
DefaultFormat.AverageBytes PerSecond = 22000
DefaultFormat.BlockAlign = 1
'setting the buffer descriptor to tell the capture buffer object how the
'buffer should perform
MySBufDesc.Format = DefaultFormat
MySBufDesc.BufferBytes = 2000000
MySBufDesc.ControlEffects = False
MySBufDesc.WaveMapped = True
MySndBuf = New CaptureBuffer(MySBufDesc, MyCapture)
End Sub
''' <summary>
''' Constructor that sets format and buffersize, as well as enables
''' echo cancellation and noise suppression effects
''' </summary>
''' <param name="MyFormat"></param>
''' <param name="bufsize"></param>
Public Sub New(ByVal MyFormat As WaveFormat, ByVal bufsize As Integer)
Dim MySBufDesc As CaptureBufferDescription = New CaptureBufferDescription()
'Format has been defined in MainForm
MySBufDesc.Format = MyFormat
MySBufDesc.BufferBytes = bufsize
MySBufDesc.ControlEffects = True
MySBufDesc.WaveMapped = True
' CaptureAcousticEchoCancell ationEffec t AECEffect;
'
' MySBufDesc.CaptureEffectDe scription = new CaptureEffectDescription[1 ];
' MySBufDesc.CaptureEffectDe scription[ 0].LocateI nSoftware = true;
' MySBufDesc.CaptureEffectDe scription[ 0].GuidEff ectsClass = DSoundHelper.CaptureEffect sMsAcousti cEchoCance llation;
' MySBufDesc.CaptureEffectDe scription[ 0].GuidEff ectsInstan ce = DSoundHelper.InterfaceCapt ureEffects AcousticEc hoCancella tion;
' MySBufDesc.CaptureEffectDe scription[ 1].LocateI nSoftware = true;
' MySBufDesc.CaptureEffectDe scription[ 1].GuidEff ectsClass = DSoundHelper.CaptureEffect sMsNoiseSu ppression;
Try
'Create the CaptureBuffer
MySndBuf = New CaptureBuffer(MySBufDesc, MyCapture)
Catch se As SoundException
MessageBox.Show("There is a " & se.ErrorString & " sound exception", "DirectSound Error")
End Try
End Sub
''' <summary>
''' Starts the capture from the capture device
''' </summary>
Public Sub StartRecord()
MySndBuf.Start(True)
End Sub
''' <summary>
''' Stops the recording of sound.
''' </summary>
Public Sub StopRecord()
MySndBuf.Stop()
End Sub
''' <summary>
''' Saves the data in the capture buffer into a wave file
''' </summary>
Public Sub ReadData()
Dim readposition, writeposition As Integer
Dim byteArrayList As ArrayList = New ArrayList()
Dim ascii As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding( )
'Create a new wav file to store the capture buffer data.
'if already exists will overwrite filename is test.wav
Dim path As String = "c:\test.wav"
Dim MemStream As Stream = New MemoryStream()
MySndBuf.GetCurrentPositio n(writepos ition, readposition)
MySndBuf.Read(0, MemStream, writeposition, LockFlag.None)
Dim MyStream As Stream = New FileStream(path, FileMode.Create)
'begin to write the wave file header. for more details
'Search google.com for "wave formats"
'RIFF header
Try
byteArrayList.AddRange(asc ii.GetByte s("RIFF"))
byteArrayList.AddRange(ToB ytes(36 + CInt(Fix(MemStream.Length) ), 4))
byteArrayList.AddRange(asc ii.GetByte s("WAVE"))
'fmt chunk
byteArrayList.AddRange(asc ii.GetByte s("fmt "))
'length of fmt chunk (never changes)
byteArrayList.AddRange(ToB ytes(16, 4))
'"1" for pcm encoding
byteArrayList.AddRange(ToB ytes(1, 2))
byteArrayList.AddRange(ToB ytes(MySnd Buf.Format .Channels, 2))
byteArrayList.AddRange(ToB ytes(MySnd Buf.Format .SamplesPe rSecond, 4))
byteArrayList.AddRange(ToB ytes(MySnd Buf.Format .AverageBy tesPerSeco nd, 4))
byteArrayList.AddRange(ToB ytes(MySnd Buf.Format .BlockAlig n, 2))
byteArrayList.AddRange(ToB ytes(MySnd Buf.Format .BitsPerSa mple, 2))
'the data chunk
byteArrayList.AddRange(asc ii.GetByte s("data"))
byteArrayList.AddRange(ToB ytes(CInt( Fix(MemStr eam.Length )), 4))
Dim buffer As Byte() = New Byte(MemStream.Length - 1) {}
MemStream.Read(buffer, 0, CInt(Fix(MemStream.Length) ))
byteArrayList.AddRange(buf fer)
buffer = New Byte(byteArrayList.Count - 1) {}
byteArrayList.CopyTo(buffe r)
MyStream.Write(buffer, 0, buffer.Length)
Catch ae As ArgumentException
MessageBox.Show("Argument Exception with Message:" & Constants.vbLf + Constants.vbTab + ae.Message)
Finally
MemStream.Close()
MyStream.Close()
End Try
End Sub
''' <summary>
''' returns capture status (boolean)
''' </summary>
''' <returns></returns>
Public Function Capturing() As Boolean
Return MySndBuf.Capturing
End Function
''' <summary>
''' Recursive method that returns a target number in the form
''' of an ArrayList of bytes with designated number of bytes. If not enough
''' bytes to hold the targetnumber, will throw argumentexception.
''' Should be used in a try-catch clause
''' </summary>
''' <param name="targetnumber">the value intended to convert</param>
''' <param name="numofbytes">number of bytes needed</param>
''' <returns></returns>
Private Function ToBytes(ByVal targetnumber As Integer, ByVal numofbytes As Short) As ArrayList
Dim remainder, result As Integer
Dim returningarray As ArrayList
Dim wrongnumofbytes As ArgumentException = New ArgumentException("Not enough bytes to represent number", "numofbytes")
result = Math.DivRem(targetnumber, 256, remainder)
'if not enough bytes specified to represent target number
If targetnumber >= Math.Pow(256, CDbl(numofbytes)) Then
Throw wrongnumofbytes
End If
'if there are higher significant hexadecima, run a recursion
If result >= 1 Then
returningarray = ToBytes(result, CShort(Fix(numofbytes - 1)))
returningarray.Insert(0, Convert.ToByte(remainder))
Return returningarray
Else 'if (result < 1) recursion terminating condition
returningarray = New ArrayList(numofbytes)
returningarray.Add(Convert .ToByte(ta rgetnumber ))
Dim i As Integer = 0
Do While i < numofbytes - 1
returningarray.Add(Convert .ToByte(0) ) 'fill up most significant hexadecima with 0's
i += 1
Loop
Return returningarray
End If
End Function
End Class 'end class
End Namespace
How to record from Microphone ~~>Save in to file and process.
Buy component, look Example CaptureSound, and another solution that too old, required directx7 or too difficult Solution,
I"m not an experts at all.
The best answer is that: if you can modify this code? It seems work, but it automatically save this file in "C:\test.wav"
Can't here anything. Please help me. If this code is good, I can easily use this by call. "MyVar.StartRecord()" for example.
Please checked it carefully for me?
Thanks a lot.
Here is the code:
Imports Microsoft.VisualBasic
Imports System
Imports Microsoft.DirectX.DirectSo
Imports System.Windows.Forms
Imports Buffer = Microsoft.DirectX.DirectSo
Imports System.IO
Imports System.Collections
Namespace DirectSoundRecording
''' <summary>
''' Class used to handle the sound capture qualities of the program
''' </summary>
Public Class SoundRec
Private devices As CaptureDevicesCollection = New CaptureDevicesCollection
Private MyCapture As Capture = New Capture(devices(0).DriverG
Private MySndBuf As CaptureBuffer
''' <summary>
''' Sets a default format capture buffer with no effects; 22KHz 8bit/sample, mono
''' </summary>
Public Sub New()
Dim MySBufDesc As CaptureBufferDescription = New CaptureBufferDescription()
'setting the default wave capture format for use
'by the buffer descriptor
Dim DefaultFormat As WaveFormat = New WaveFormat()
DefaultFormat.SamplesPerSe
DefaultFormat.Channels = 1
DefaultFormat.BitsPerSampl
DefaultFormat.AverageBytes
DefaultFormat.BlockAlign = 1
'setting the buffer descriptor to tell the capture buffer object how the
'buffer should perform
MySBufDesc.Format = DefaultFormat
MySBufDesc.BufferBytes = 2000000
MySBufDesc.ControlEffects = False
MySBufDesc.WaveMapped = True
MySndBuf = New CaptureBuffer(MySBufDesc, MyCapture)
End Sub
''' <summary>
''' Constructor that sets format and buffersize, as well as enables
''' echo cancellation and noise suppression effects
''' </summary>
''' <param name="MyFormat"></param>
''' <param name="bufsize"></param>
Public Sub New(ByVal MyFormat As WaveFormat, ByVal bufsize As Integer)
Dim MySBufDesc As CaptureBufferDescription = New CaptureBufferDescription()
'Format has been defined in MainForm
MySBufDesc.Format = MyFormat
MySBufDesc.BufferBytes = bufsize
MySBufDesc.ControlEffects = True
MySBufDesc.WaveMapped = True
' CaptureAcousticEchoCancell
'
' MySBufDesc.CaptureEffectDe
' MySBufDesc.CaptureEffectDe
' MySBufDesc.CaptureEffectDe
' MySBufDesc.CaptureEffectDe
' MySBufDesc.CaptureEffectDe
' MySBufDesc.CaptureEffectDe
Try
'Create the CaptureBuffer
MySndBuf = New CaptureBuffer(MySBufDesc, MyCapture)
Catch se As SoundException
MessageBox.Show("There is a " & se.ErrorString & " sound exception", "DirectSound Error")
End Try
End Sub
''' <summary>
''' Starts the capture from the capture device
''' </summary>
Public Sub StartRecord()
MySndBuf.Start(True)
End Sub
''' <summary>
''' Stops the recording of sound.
''' </summary>
Public Sub StopRecord()
MySndBuf.Stop()
End Sub
''' <summary>
''' Saves the data in the capture buffer into a wave file
''' </summary>
Public Sub ReadData()
Dim readposition, writeposition As Integer
Dim byteArrayList As ArrayList = New ArrayList()
Dim ascii As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding(
'Create a new wav file to store the capture buffer data.
'if already exists will overwrite filename is test.wav
Dim path As String = "c:\test.wav"
Dim MemStream As Stream = New MemoryStream()
MySndBuf.GetCurrentPositio
MySndBuf.Read(0, MemStream, writeposition, LockFlag.None)
Dim MyStream As Stream = New FileStream(path, FileMode.Create)
'begin to write the wave file header. for more details
'Search google.com for "wave formats"
'RIFF header
Try
byteArrayList.AddRange(asc
byteArrayList.AddRange(ToB
byteArrayList.AddRange(asc
'fmt chunk
byteArrayList.AddRange(asc
'length of fmt chunk (never changes)
byteArrayList.AddRange(ToB
'"1" for pcm encoding
byteArrayList.AddRange(ToB
byteArrayList.AddRange(ToB
byteArrayList.AddRange(ToB
byteArrayList.AddRange(ToB
byteArrayList.AddRange(ToB
byteArrayList.AddRange(ToB
'the data chunk
byteArrayList.AddRange(asc
byteArrayList.AddRange(ToB
Dim buffer As Byte() = New Byte(MemStream.Length - 1) {}
MemStream.Read(buffer, 0, CInt(Fix(MemStream.Length)
byteArrayList.AddRange(buf
buffer = New Byte(byteArrayList.Count - 1) {}
byteArrayList.CopyTo(buffe
MyStream.Write(buffer, 0, buffer.Length)
Catch ae As ArgumentException
MessageBox.Show("Argument Exception with Message:" & Constants.vbLf + Constants.vbTab + ae.Message)
Finally
MemStream.Close()
MyStream.Close()
End Try
End Sub
''' <summary>
''' returns capture status (boolean)
''' </summary>
''' <returns></returns>
Public Function Capturing() As Boolean
Return MySndBuf.Capturing
End Function
''' <summary>
''' Recursive method that returns a target number in the form
''' of an ArrayList of bytes with designated number of bytes. If not enough
''' bytes to hold the targetnumber, will throw argumentexception.
''' Should be used in a try-catch clause
''' </summary>
''' <param name="targetnumber">the value intended to convert</param>
''' <param name="numofbytes">number of bytes needed</param>
''' <returns></returns>
Private Function ToBytes(ByVal targetnumber As Integer, ByVal numofbytes As Short) As ArrayList
Dim remainder, result As Integer
Dim returningarray As ArrayList
Dim wrongnumofbytes As ArgumentException = New ArgumentException("Not enough bytes to represent number", "numofbytes")
result = Math.DivRem(targetnumber, 256, remainder)
'if not enough bytes specified to represent target number
If targetnumber >= Math.Pow(256, CDbl(numofbytes)) Then
Throw wrongnumofbytes
End If
'if there are higher significant hexadecima, run a recursion
If result >= 1 Then
returningarray = ToBytes(result, CShort(Fix(numofbytes - 1)))
returningarray.Insert(0, Convert.ToByte(remainder))
Return returningarray
Else 'if (result < 1) recursion terminating condition
returningarray = New ArrayList(numofbytes)
returningarray.Add(Convert
Dim i As Integer = 0
Do While i < numofbytes - 1
returningarray.Add(Convert
i += 1
Loop
Return returningarray
End If
End Function
End Class 'end class
End Namespace
ASKER
OK, My purpose is very simple.
Instead of chosing a lot of choice to record.
I just want to record:
- from Microphone
-44100,16bit,stereo
-saved to "c:\test.wav"
please help me.
Instead of chosing a lot of choice to record.
I just want to record:
- from Microphone
-44100,16bit,stereo
-saved to "c:\test.wav"
please help me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh, I'm doing with it, But I don't want all the Form, What I need is not a form, It has 2 button:
StartRecording and StopRecording(and save file in C:\test.wav) - 44100,16bit,stereo.
To read all code and translate is too difficult for me. I've just learn VB and need it to do my work. That's all.
Please help me.
StartRecording and StopRecording(and save file in C:\test.wav) - 44100,16bit,stereo.
To read all code and translate is too difficult for me. I've just learn VB and need it to do my work. That's all.
Please help me.
ASKER