//------------------------------------------------------------------------------
//	WaveformAudio クラスで Windows API を使うための関数，構造体の宣言と定数の定義
//		作成者：三上直樹，2013/01/27 作成，©三上直樹 2013
//------------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;	// DllImport, StructLayoutAttribute 用

namespace Mikami.WaveformAudio
{
	public partial class WaveformAudio
	{
		//------------------------------------------------------
		//				関数
		//------------------------------------------------------
		// mmioAscend()
		[DllImport("winmm.dll")]
		protected extern static uint mmioAscend(IntPtr hmmio,
											    ref MMCKINFO lpck,
											    uint   wFlags);
		// mmioClose()
		[DllImport("winmm.dll")]
		protected extern static uint mmioClose(IntPtr hmmio, uint wFlags);

		// mmioCloseChunk()
		[DllImport("winmm.dll")]
		protected extern static uint mmioCreateChunk(IntPtr hmmio,
													 ref MMCKINFO lpcki,
													 uint	wFlags);
		// mmioDescend()，その１
		[DllImport("winmm.dll")]
		protected extern static uint mmioDescend(IntPtr hmmio,
												 ref MMCKINFO lpck,
												 ref MMCKINFO lpckParent,
												 uint	wFlags);
		// mmioDescend()，その２
		[DllImport("winmm.dll")]
		protected extern static uint mmioDescend(IntPtr hmmio,
												 ref MMCKINFO lpck,
												 uint	lpcParant,
												 uint	wFlags);
		// mmioFlush()
		[DllImport("winmm.dll")]
		protected extern static uint mmioFlush(IntPtr hmmio, uint fuFlush);

		// mmioOpen()
		[DllImport("winmm.dll")]
		protected extern static IntPtr mmioOpen(string szFileName,
												IntPtr lpmmioinfo,
												uint   dwOpenFlags);
		// mmioRead()，その１
		[DllImport("winmm.dll")]
		protected extern static int mmioRead(IntPtr hmmio,
											 IntPtr pch,
											 int	cch);
		// mmioRead()，その２
		[DllImport("winmm.dll")]
		protected extern static int mmioRead(IntPtr hmmio,
											 ref WAVEFORMATEX pch,
											 int cch);
		// mmioSeek()
		[DllImport("winmm.dll")]
		protected extern static int mmioSeek(IntPtr hmmio,
											 uint	lOffset,
											 int	iOrigin);
		// mmioStringToFOURCC()
		[DllImport("winmm.dll")]
		protected extern static uint mmioStringToFOURCC(string sz, uint wFlags);

		// mmioWrite()，その１
		[DllImport("winmm.dll")]
		protected extern static int mmioWrite(IntPtr hmmio,
											  IntPtr pch,
											  int	 cch);
		// mmioWrite()，その２
		[DllImport("winmm.dll")]
		protected extern static int mmioWrite(IntPtr hmmio,
											  ref WAVEFORMATEX pch,
											  int cch);
		// waveOutClose()
		[DllImport("winmm.dll")]
		protected extern static uint waveOutClose(IntPtr hwo);

		// waveOutOpen()
		[DllImport("winmm.dll")]
		protected extern static uint waveOutOpen(ref IntPtr phwo,
												 uint uDeviceID,
												 ref WAVEFORMATEX pwfx,
												 uint dwCallback,
												 uint dwInstance,
												 uint dwOpen);
		// waveOutPrepareHeader()
		[DllImport("winmm.dll")]
		protected extern static uint waveOutPrepareHeader(IntPtr hwo,
														  ref WAVEHDR pwh,
														  uint cbwh);
		// waveOutReset()
		[DllImport("winmm.dll")]
		protected static extern uint waveOutReset(IntPtr hwo);

		// waveOutUnprepareHeader()
		[DllImport("winmm.dll")]
		protected extern static uint waveOutUnprepareHeader(IntPtr hwo,
														    ref WAVEHDR pwh,
														    uint cbwh);
		// waveOutWrite()
		[DllImport("winmm.dll")]
		protected extern static uint waveOutWrite(IntPtr hwo,
												  ref WAVEHDR pwh,
												  uint cbwh);

		//------------------------------------------------------
		//				構造体
		//------------------------------------------------------
		[StructLayout(LayoutKind.Sequential)]
		protected struct MMCKINFO
		{
			public uint ckid;
			public uint ckSize;
			public uint fccType;
			public uint dwDataOffset;
			public uint dwFlags;
		}

		[StructLayout(LayoutKind.Sequential)]
		protected struct WAVEHDR
		{
			public IntPtr	lpData;
			public uint		dwBufferLength;
			public uint		dwBytesRecorded;
			public IntPtr	dwUser;
			public uint		dwFlags;
			public uint		dwLoops;
			public IntPtr	lpNext;
			public uint		reserved;
		}

		[StructLayout(LayoutKind.Sequential)]
		protected struct WAVEFORMATEX
		{
			public ushort	wFormatTag;
			public ushort	nChannels;
			public uint		nSamplesPerSec;
			public uint		nAvgBytesPerSec;
			public ushort	nBlockAlign;
			public ushort	wBitsPerSample;
			public ushort	cbSize;
		}

		//------------------------------------------------------
		//				定数
		//------------------------------------------------------
		protected const uint CALLBACK_NULL	  = 0;
		protected const int  SEEK_SET		  = 0;
		protected const uint MMIO_ALLOCBUF	  = 0x10000;	// mmioOpen()：入出力バッファ使用
		protected const uint MMIO_CREATE	  = 0x1000;
		protected const uint MMIO_CREATERIFF  = 0x0020;
		protected const uint MMIO_FINDCHUNK	  = 0x10;		// 指定のチャンクを探す
		protected const uint MMIO_FINDRIFF	  = 0x20;		// RIFF チャンクを探す
		protected const uint MMIO_READ		  = 0x0;		// mmioOpen()：読み取り専用
		protected const uint MMIO_WRITE		  = 0x01;
		protected const uint MMSYSERR_NOERROR = 0;			// エラーなし
		protected const uint WAVE_MAPPER	  = unchecked((uint)-1);
		protected const uint WHDR_DONE		  = 0x01;
	}
}
