public static Stream CreateStream(Stream waveData, WaveFormat format) { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); writer.Write(System.Text.Encoding.ASCII.GetBytes("RIFF".ToCharArray())); writer.Write((Int32)(waveData.Length + 36)); writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVEfmt ".ToCharArray())); writer.Write((Int32)16); writer.Write((Int16)format.wFormatTag); writer.Write((Int16)format.nChannels); writer.Write((Int32)format.nSamplesPerSec); writer.Write((Int32)format.nAvgBytesPerSec); writer.Write((Int16)format.nBlockAlign); writer.Write((Int16)format.wBitsPerSample); writer.Write(System.Text.Encoding.ASCII.GetBytes("data".ToCharArray())); writer.Write((Int32)waveData.Length); waveData.Seek(0, SeekOrigin.Begin); byte[] b = new byte[waveData.Length]; waveData.Read(b, 0, (int)waveData.Length); writer.Write(b); writer.Seek(0, SeekOrigin.Begin); return stream; }
public WaveInRecorder(int device, WaveFormat format, int bufferSize, int bufferCount, BufferDoneEventHandler doneProc) { m_DoneProc = doneProc; WaveInHelper.Try(WaveNative.waveInOpen(out m_WaveIn, device, format, m_BufferProc, 0, WaveNative.CALLBACK_FUNCTION)); AllocateBuffers(bufferSize, bufferCount); for (int i = 0; i < bufferCount; i++) { SelectNextBuffer(); m_CurrentBuffer.Record(); } WaveInHelper.Try(WaveNative.waveInStart(m_WaveIn)); m_Thread = new Thread(new ThreadStart(ThreadProc)); m_Thread.Start(); }
/// <summary>ReadHeader() - Changed to CopyHeader(destination)</summary> private void CopyHeader(Stream destinationStream) { BinaryReader reader = new BinaryReader(m_Stream); BinaryWriter writer = new BinaryWriter(destinationStream); if (CopyChunk(reader, writer) != "RIFF") { throw new Exception("Invalid file format"); } writer.Write(reader.ReadInt32()); // File length minus first 8 bytes of RIFF description if (CopyChunk(reader, writer) != "WAVE") { throw new Exception("Invalid file format"); } if (CopyChunk(reader, writer) != "fmt ") { throw new Exception("Invalid file format"); } int len = reader.ReadInt32(); if (len < 16) { // bad format chunk length throw new Exception("Invalid file format"); } else { writer.Write(len); } m_Format = new WaveFormat(22050, 16, 2); // initialize to any format m_Format.wFormatTag = reader.ReadInt16(); m_Format.nChannels = reader.ReadInt16(); m_Format.nSamplesPerSec = reader.ReadInt32(); m_Format.nAvgBytesPerSec = reader.ReadInt32(); m_Format.nBlockAlign = reader.ReadInt16(); m_Format.wBitsPerSample = reader.ReadInt16(); //copy format information writer.Write(m_Format.wFormatTag); writer.Write(m_Format.nChannels); writer.Write(m_Format.nSamplesPerSec); writer.Write(m_Format.nAvgBytesPerSec); writer.Write(m_Format.nBlockAlign); writer.Write(m_Format.wBitsPerSample); // advance in the stream to skip the wave format block len -= 16; // minimum format size writer.Write(reader.ReadBytes(len)); len = 0; /*while (len > 0) * { * reader.ReadByte(); * len--; * }*/ // assume the data chunk is aligned while (m_Stream.Position < m_Stream.Length && CopyChunk(reader, writer) != "data") { ; } if (m_Stream.Position >= m_Stream.Length) { throw new Exception("Invalid file format"); } m_Length = reader.ReadInt32(); writer.Write(m_Length); m_DataPos = m_Stream.Position; Position = 0; }
private void CopyHeader(Stream destinationStream) { BinaryReader reader = new BinaryReader(m_Stream); BinaryWriter writer = new BinaryWriter(destinationStream); if (CopyChunk(reader, writer) != "RIFF") { throw new Exception("Invalid file format"); } writer.Write(reader.ReadInt32()); if (CopyChunk(reader, writer) != "WAVE") { throw new Exception("Invalid file format"); } if (CopyChunk(reader, writer) != "fmt ") { throw new Exception("Invalid file format"); } int len = reader.ReadInt32(); if (len < 16) { throw new Exception("Invalid file format"); } else { writer.Write(len); } m_Format = new WaveFormat(22050, 16, 2); m_Format.wFormatTag = reader.ReadInt16(); m_Format.nChannels = reader.ReadInt16(); m_Format.nSamplesPerSec = reader.ReadInt32(); m_Format.nAvgBytesPerSec = reader.ReadInt32(); m_Format.nBlockAlign = reader.ReadInt16(); m_Format.wBitsPerSample = reader.ReadInt16(); //copy format information writer.Write(m_Format.wFormatTag); writer.Write(m_Format.nChannels); writer.Write(m_Format.nSamplesPerSec); writer.Write(m_Format.nAvgBytesPerSec); writer.Write(m_Format.nBlockAlign); writer.Write(m_Format.wBitsPerSample); len -= 16; writer.Write(reader.ReadBytes(len)); len = 0; while (m_Stream.Position < m_Stream.Length && CopyChunk(reader, writer) != "data") { ; } if (m_Stream.Position >= m_Stream.Length) { throw new Exception("Invalid file format"); } m_Length = reader.ReadInt32(); writer.Write(m_Length); m_DataPos = m_Stream.Position; Position = 0; }
private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); if (ReadChunk(Reader) != "RIFF") throw new Exception("Invalid file format"); Reader.ReadInt32(); if (ReadChunk(Reader) != "WAVE") throw new Exception("Invalid file format"); if (ReadChunk(Reader) != "fmt ") throw new Exception("Invalid file format"); int len = Reader.ReadInt32(); if (len < 16) throw new Exception("Invalid file format"); m_Format = new WaveFormat(22050, 16, 2); m_Format.wFormatTag = Reader.ReadInt16(); m_Format.nChannels = Reader.ReadInt16(); m_Format.nSamplesPerSec = Reader.ReadInt32(); m_Format.nAvgBytesPerSec = Reader.ReadInt32(); m_Format.nBlockAlign = Reader.ReadInt16(); m_Format.wBitsPerSample = Reader.ReadInt16(); len -= 16; while (len > 0) { Reader.ReadByte(); len--; } while(m_Stream.Position < m_Stream.Length && ReadChunk(Reader) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Invalid file format"); m_Length = Reader.ReadInt32(); m_DataPos = m_Stream.Position; Position = 0; }
private void CopyHeader(Stream destinationStream) { BinaryReader reader = new BinaryReader(m_Stream); BinaryWriter writer = new BinaryWriter(destinationStream); if (CopyChunk(reader, writer) != "RIFF") throw new Exception("Invalid file format"); writer.Write( reader.ReadInt32() ); if (CopyChunk(reader, writer) != "WAVE") throw new Exception("Invalid file format"); if (CopyChunk(reader, writer) != "fmt ") throw new Exception("Invalid file format"); int len = reader.ReadInt32(); if (len < 16){ throw new Exception("Invalid file format"); }else{ writer.Write(len); } m_Format = new WaveFormat(22050, 16, 2); m_Format.wFormatTag = reader.ReadInt16(); m_Format.nChannels = reader.ReadInt16(); m_Format.nSamplesPerSec = reader.ReadInt32(); m_Format.nAvgBytesPerSec = reader.ReadInt32(); m_Format.nBlockAlign = reader.ReadInt16(); m_Format.wBitsPerSample = reader.ReadInt16(); //copy format information writer.Write( m_Format.wFormatTag ); writer.Write( m_Format.nChannels ); writer.Write( m_Format.nSamplesPerSec ); writer.Write( m_Format.nAvgBytesPerSec ); writer.Write( m_Format.nBlockAlign ); writer.Write( m_Format.wBitsPerSample ); len -= 16; writer.Write( reader.ReadBytes(len) ); len = 0; while(m_Stream.Position < m_Stream.Length && CopyChunk(reader, writer) != "data") ; if (m_Stream.Position >= m_Stream.Length) throw new Exception("Invalid file format"); m_Length = reader.ReadInt32(); writer.Write( m_Length ); m_DataPos = m_Stream.Position; Position = 0; }
public static extern int waveInOpen(out IntPtr phwi, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, int dwInstance, int dwFlags);