private void ReadHeader() { BinaryReader Reader = new BinaryReader(m_Stream); if (ReadChunk(Reader) != "RIFF") throw new Exception("Invalid file format"); Reader.ReadInt32(); // File length minus first 8 bytes of RIFF description, we don't use it 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) // bad format chunk length throw new Exception("Invalid file format"); 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(); // advance in the stream to skip the wave format block len -= 16; // minimum format size while (len > 0) { Reader.ReadByte(); len--; } // assume the data chunk is aligned 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; }
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, IntPtr.Zero, 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>Write a new header</summary> 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)); //File length minus first 8 bytes of RIFF description writer.Write(System.Text.Encoding.ASCII.GetBytes("WAVEfmt ".ToCharArray())); writer.Write((Int32)16); //length of following chunk: 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; }
/// <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 Start() { Stop(); try { _waveFormat = new WaveFormat(Properties.Settings.Default.SettingSamplesPerSecond, Properties.Settings.Default.SettingBitsPerSample, Properties.Settings.Default.SettingChannels); _recorder = new WaveInRecorder(Properties.Settings.Default.SettingAudioInputDevice, _waveFormat, Properties.Settings.Default.SettingBytesPerFrame * Properties.Settings.Default.SettingChannels, 3, new BufferDoneEventHandler(DataArrived)); if (_isPlayer == true) _player = new WaveOutPlayer(Properties.Settings.Default.SettingAudioOutputDevice, _waveFormat, Properties.Settings.Default.SettingBytesPerFrame * Properties.Settings.Default.SettingChannels, 3, new BufferFillEventHandler(Filler)); textBoxConsole.AppendText(DateTime.Now.ToString() + " : Audio input device polling started\r\n"); textBoxConsole.AppendText(DateTime.Now + " : Device = " + Properties.Settings.Default.SettingAudioInputDevice.ToString() + "\r\n"); textBoxConsole.AppendText(DateTime.Now + " : Channels = " + Properties.Settings.Default.SettingChannels.ToString() + "\r\n"); textBoxConsole.AppendText(DateTime.Now + " : Bits per sample = " + Properties.Settings.Default.SettingBitsPerSample.ToString() + "\r\n"); textBoxConsole.AppendText(DateTime.Now + " : Samples per second = " + Properties.Settings.Default.SettingSamplesPerSecond.ToString() + "\r\n"); textBoxConsole.AppendText(DateTime.Now + " : Frame size = " + Properties.Settings.Default.SettingBytesPerFrame.ToString() + "\r\n"); _audioFrame.sampleMessageTextLabel = sampleMessageTextLabel; _audioFrame.interactLabel = interactLabel; _audioFrame.neuralOutputLabel = neuralOutputlabel; AudioFrame.sampleNameTextBox = sampleNameTextBox; } catch (Exception ex) { textBoxConsole.AppendText(DateTime.Now + " : " + ex.InnerException.ToString() + "\r\n"); } }
public WaveOutPlayer(int device, WaveFormat format, int bufferSize, int bufferCount, BufferFillEventHandler fillProc) { m_zero = format.wBitsPerSample == 8 ? (byte)128 : (byte)0; m_FillProc = fillProc; WaveOutHelper.Try(WaveNative.waveOutOpen(out m_WaveOut, device, format, m_BufferProc, IntPtr.Zero, WaveNative.CALLBACK_FUNCTION)); AllocateBuffers(bufferSize, bufferCount); m_Thread = new Thread(new ThreadStart(ThreadProc)); m_Thread.Start(); }
public static extern int waveOutOpen(out IntPtr hWaveOut, int uDeviceID, WaveFormat lpFormat, WaveDelegate dwCallback, IntPtr dwInstance, int dwFlags);