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); }
/// <summary>Get the named position in a RIFF</summary> /// <param name="file">file name</param> /// <param name="name">named position</param> /// <returns>CHUNKINFO</returns> private CHUNKINFO GetChunkPos(string file, string name) { int ret; IntPtr hMmioIn; CHUNKINFO ck = new CHUNKINFO(); MMCKINFO mmckinfoParentIn = new MMCKINFO(); MMCKINFO mmckinfoSubchunkIn = new MMCKINFO(); // open for read hMmioIn = mmioOpen(file, IntPtr.Zero, MMIO_READ); if (hMmioIn == IntPtr.Zero) { return(ck); } // convert string to code mmckinfoParentIn.fccType = mmioStringToFOURCC("WAVE", 0); ret = mmioDescendParent(hMmioIn, &mmckinfoParentIn, IntPtr.Zero, MMIO_FINDRIFF); if (ret != 0) { mmioClose(hMmioIn, 0); return(ck); } // get named location mmckinfoSubchunkIn.ckid = mmioStringToFOURCC(name, 0); ret = mmioDescend(hMmioIn, &mmckinfoSubchunkIn, &mmckinfoParentIn, MMIO_FINDCHUNK); if (ret != 0) { mmioClose(hMmioIn, 0); return(ck); } // return position and size ck.Start = (uint)mmioSeek(hMmioIn, 0, SEEK_CUR); ck.Length = mmckinfoSubchunkIn.ckSize; mmioClose(hMmioIn, 0); return(ck); }
internal static extern int mmioDescend(IntPtr hMIO, [MarshalAs(UnmanagedType.LPStruct)] MMCKINFO lpck, [MarshalAs(UnmanagedType.LPStruct)] MMCKINFO lcpkParent, int flags);
internal static extern int mmioAscend(IntPtr hMIO, MMCKINFO lpck, int flags);