private void WriteFormatBlock() { MMCKINFO mmckinfoParentIn = new MMCKINFO(); // Get format info mmckinfoParentIn.ckid = new FourCC("fmt "); mmckinfoParentIn.ckSize = FORMATBLOCKSIZE; MMIOError rc = MMIO.CreateChunk(m_OutputFile, mmckinfoParentIn, RiffChunkFlags.None); MMIO.ThrowExceptionForError(rc); IntPtr ip = Marshal.AllocCoTaskMem(FORMATBLOCKSIZE); try { Marshal.StructureToPtr(m_wfe, ip, false); int iBytes = MMIO.Write(m_OutputFile, ip, FORMATBLOCKSIZE); if (iBytes < 0) { throw new Exception("mmioWrite failed"); } } finally { Marshal.FreeCoTaskMem(ip); } rc = MMIO.Ascend(m_OutputFile, mmckinfoParentIn, 0); MMIO.ThrowExceptionForError(rc); }
/// <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; }
private void WriteDataBlock() { MMCKINFO mmckinfoSubchunkIn = new MMCKINFO(); mmckinfoSubchunkIn.ckid = new FourCC("data"); mmckinfoSubchunkIn.ckSize = m_AudioLength; MMIOError rc = MMIO.CreateChunk(m_OutputFile, mmckinfoSubchunkIn, 0); MMIO.ThrowExceptionForError(rc); }
private void WriteWaveHeader() { MMCKINFO mmckinfoParentIn = new MMCKINFO(); mmckinfoParentIn.fccType = new FourCC("WAVE"); mmckinfoParentIn.ckSize = m_AudioLength + m_DataOffset - 8; MMIOError rc = MMIO.CreateChunk(m_OutputFile, mmckinfoParentIn, RiffChunkFlags.CreateRiff); MMIO.ThrowExceptionForError(rc); }
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); }
public static void ThrowExceptionForError(MMIOError i) { if (i != MMIOError.NoError) { throw new Exception(Errorstring(i)); } }
public static string Errorstring(MMIOError i) { string sRet; switch (i) { case MMIOError.NoError: sRet = "No error"; break; case MMIOError.FileNotFound: sRet = "File not found"; break; case MMIOError.OutOfMemory: sRet = "Out of memory"; break; case MMIOError.CannotOpen: sRet = "Cannot open"; break; case MMIOError.CannotClose: sRet = "Cannot close"; break; case MMIOError.CannotRead: sRet = "Cannot read"; break; case MMIOError.CannotWrite: sRet = "Cannot write"; break; case MMIOError.CannotSeek: sRet = "Cannot seek"; break; case MMIOError.CannotExpand: sRet = "Cannot expand file"; break; case MMIOError.ChunkNotFound: sRet = "Chunk not found"; break; case MMIOError.Unbuffered: sRet = "Unbuffered"; break; case MMIOError.PathNotFound: sRet = "Path incorrect"; break; case MMIOError.AccessDenied: sRet = "File was protected (Access denied)"; break; case MMIOError.SharingViolation: sRet = "file in use (Sharing violation)"; break; case MMIOError.NetworkError: sRet = "Network not responding"; break; case MMIOError.TooManyOpenFiles: sRet = "No more file handles"; break; case MMIOError.InvalidFile: sRet = "Invalid File"; break; default: sRet = "Unknown error number: " + i.ToString(); break; } return sRet; }