/// <summary> /// Read the WaveFormatEx from the input file and find the place to start /// writing data. /// </summary> private void LoadWFE() { MMCKINFO mmckinfoParentIn = new MMCKINFO(); MMCKINFO mmckinfoSubchunkIn = new MMCKINFO(); int mm = MMIO.Seek(m_OutputFile, 0, MMIOSeekFlags.Set); if (mm < 0) { throw new Exception("seek failure"); } // Check if this is a wave file mmckinfoParentIn.fccType = new FourCC("WAVE"); MMIOError rc = MMIO.Descend(m_OutputFile, mmckinfoParentIn, null, RiffChunkFlags.FindRiff); MMIO.ThrowExceptionForError(rc); // Get format info mmckinfoSubchunkIn.ckid = new FourCC("fmt "); rc = MMIO.Descend(m_OutputFile, mmckinfoSubchunkIn, mmckinfoParentIn, RiffChunkFlags.FindChunk); MMIO.ThrowExceptionForError(rc); // Read the data format from the file (WaveFormatEx) IntPtr ip = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(WaveFormatEx))); try { rc = MMIO.Read(m_OutputFile, ip, mmckinfoSubchunkIn.ckSize); if (rc < 0) { throw new Exception("Read failed"); } m_wfe = new WaveFormatEx(); Marshal.PtrToStructure(ip, m_wfe); } finally { Marshal.FreeCoTaskMem(ip); } rc = MMIO.Ascend(m_OutputFile, mmckinfoSubchunkIn, 0); MMIO.ThrowExceptionForError(rc); // Find the data subchunk mmckinfoSubchunkIn.ckid = new FourCC("data"); rc = MMIO.Descend(m_OutputFile, mmckinfoSubchunkIn, mmckinfoParentIn, RiffChunkFlags.FindChunk); MMIO.ThrowExceptionForError(rc); // Here is where data gets written m_DataOffset = MMIO.Seek(m_OutputFile, 0, MMIOSeekFlags.Cur); // Get the length of the audio m_AudioLength = mmckinfoSubchunkIn.ckSize; }
/// <summary> /// Re-write the header, updating sizes /// </summary> private void WriteHeader() { int rc = MMIO.Seek(m_OutputFile, 0, MMIOSeekFlags.Set); if (rc < 0) { throw new Exception("seek failure"); } WriteWaveHeader(); WriteFormatBlock(); WriteDataBlock(); }
/// <summary> /// Start (or resume) recording. Note that the file must be opened first. /// </summary> public void Record() { if (!m_Disposed) { MMIO.Seek(m_OutputFile, 0, MMIOSeekFlags.End); int mmr = waveIn.Start(m_hDevice); waveIn.ThrowExceptionForError(mmr); } else { throw new Exception("Instance is Disposed"); } }
public void DoTests() { MMIOINFO mminfo = new MMIOINFO(); MMIOINFO mminfo2 = new MMIOINFO(); IntPtr ipOutput; ipOutput = MMIO.Open(sFilename, mminfo, MMIOFlags.ReadWrite); int i = MMIO.Seek(ipOutput, 0, MMIOSeekFlags.Set); Debug.Assert(i >= 0); MMIOError mm = MMIO.Flush(ipOutput, MMIOFlushFlags.None); MMIO.ThrowExceptionForError(mm); mm = MMIO.GetInfo(ipOutput, mminfo, Marshal.SizeOf(typeof(MMIOINFO))); MMIO.ThrowExceptionForError(mm); Debug.Assert(mminfo.dwFlags == MMIOFlags.ReadWrite); mm = MMIO.SetInfo(ipOutput, mminfo, Marshal.SizeOf(typeof(MMIOINFO))); MMIO.ThrowExceptionForError(mm); IntPtr ipbuff = Marshal.AllocCoTaskMem(BUFSIZE); mm = MMIO.SetBuffer(ipOutput, ipbuff, BUFSIZE, 0); MMIO.ThrowExceptionForError(mm); mm = MMIO.GetInfo(ipOutput, mminfo2, Marshal.SizeOf(typeof(MMIOINFO))); MMIO.ThrowExceptionForError(mm); mm = MMIO.Advance(ipOutput, mminfo2, RWMode.Read); MMIO.ThrowExceptionForError(mm); Debug.Assert(mminfo2.lDiskOffset == BUFSIZE); mm = MMIO.Close(ipOutput, MMIOCloseFlags.None); MMIO.ThrowExceptionForError(mm); mm = MMIO.Rename(sFilename, sFilename2, null, 0); MMIO.ThrowExceptionForError(mm); mm = MMIO.Rename(sFilename2, sFilename, null, 0); MMIO.ThrowExceptionForError(mm); }