public void AddWave(string wave_filename) { byte[] wave_data = File.ReadAllBytes(wave_filename); MemoryStream header = new MemoryStream(); header.Write(wave_data, 0, 36); header.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(header); reader.ReadBytes(20); IntPtr wave_ptr = Marshal.AllocHGlobal(wave_data.Length); IntPtr wave_data_ptr = new IntPtr(wave_ptr.ToInt32() + 44); try { Marshal.Copy(wave_data, 0, wave_ptr, wave_data.Length); VfwApi.PCMWAVEFORMAT pwformat = new VfwApi.PCMWAVEFORMAT(); pwformat.wFormatTag = reader.ReadInt16(); pwformat.nChannels = reader.ReadInt16(); pwformat.nSamplesPerSec = reader.ReadInt32(); pwformat.nAvgBytesPerSec = reader.ReadInt32(); pwformat.nBlockAlign = reader.ReadInt16(); pwformat.wBitsPerSample = reader.ReadInt16(); int sample_size = pwformat.nChannels * pwformat.wBitsPerSample / 8; CreateAudioStream(pwformat.nSamplesPerSec, (wave_data.Length - 44) / sample_size, sample_size); int hr = VfwApi.AVIStreamSetFormat(pavi_audio_, 0, ref pwformat, Marshal.SizeOf(pwformat)); if (hr != 0) { throw new VfwException("AVIStreamSetFormat", hr); } hr = VfwApi.AVIStreamWrite(pavi_audio_, 0, wave_data.Length - 44, wave_data_ptr, wave_data.Length - 44, 16, IntPtr.Zero, IntPtr.Zero); if (hr != 0) { throw new VfwException("AVIStreamWrite", hr); } VfwApi.AVIStreamRelease(pavi_audio_); pavi_audio_ = IntPtr.Zero; } finally { Marshal.FreeHGlobal(wave_ptr); } }
public void Close() { if (pavi_audio_ != IntPtr.Zero) { VfwApi.AVIStreamRelease(pavi_audio_); pavi_audio_ = IntPtr.Zero; } if (pavi_ != IntPtr.Zero) { VfwApi.AVIStreamRelease(pavi_); pavi_ = IntPtr.Zero; } if (pfile_ != IntPtr.Zero) { VfwApi.AVIFileRelease(pfile_); pfile_ = IntPtr.Zero; } DeInitialize(); }
public void Close() { if (pgf_ != IntPtr.Zero) { int hr = VfwApi.AVIStreamGetFrameClose(pgf_); if (hr != 0) { throw new VfwException("AVIStreamGetFrameClose", hr); } pgf_ = IntPtr.Zero; } if (pavi_ != IntPtr.Zero) { VfwApi.AVIStreamRelease(pavi_); pavi_ = IntPtr.Zero; } if (pfile_ != IntPtr.Zero) { VfwApi.AVIFileRelease(pfile_); pfile_ = IntPtr.Zero; } DeInitialize(); }