static void Main(string[] args) { //StreamWriter sw = new StreamWriter("output.dat", false); //bw = new BinaryWriter(sw.BaseStream); int ret; IntPtr waveIn; WaveHdr waveHeader1 = new WaveHdr(); WaveHdr waveHeader2 = new WaveHdr(); GCHandle wh1 = GCHandle.Alloc(waveHeader1, GCHandleType.Pinned); GCHandle wh2 = GCHandle.Alloc(waveHeader2, GCHandleType.Pinned); WaveFormat wf = new WaveFormat(); wf.wFormatTag = 1; // 1 is PCM wf.nChannels = 1; wf.nSamplesPerSec = sampleRate; wf.wBitsPerSample = 16; wf.nBlockAlign = 2; wf.cbSize = 0; wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.wBitsPerSample / 8; d = new WaveDelegate(waveInCallback); int devID = -1; int callbackInstance = 0; ret = waveInOpen(out waveIn, devID, ref wf, d, ref callbackInstance, CALLBACK_FUNCTION); if (ret != 0) return; waveHeader1.dwBufferLength = bufferSize; waveHeader2.dwBufferLength = bufferSize; waveHeader1.lpData = Marshal.AllocHGlobal(bufferSize); waveHeader2.lpData = Marshal.AllocHGlobal(bufferSize); ret = waveInPrepareHeader(waveIn, ref waveHeader1, Marshal.SizeOf(waveHeader1)); ret = waveInAddBuffer(waveIn, ref waveHeader1, Marshal.SizeOf(waveHeader1)); ret = waveInPrepareHeader(waveIn, ref waveHeader2, Marshal.SizeOf(waveHeader2)); ret = waveInAddBuffer(waveIn, ref waveHeader2, Marshal.SizeOf(waveHeader2)); ret = waveInStart(waveIn); Keyboard keyboard = new Keyboard(); while (true) { Thread.Sleep(100); if (keyboard.AltKeyDown == true) break; } ret = waveInStop(waveIn); ret = waveInClose(waveIn); //bw.Close(); }
static void Main(string[] args) { //StreamWriter sw = new StreamWriter("output.dat", false); //bw = new BinaryWriter(sw.BaseStream); int ret; IntPtr waveIn; WaveHdr waveHeader1 = new WaveHdr(); WaveHdr waveHeader2 = new WaveHdr(); GCHandle wh1 = GCHandle.Alloc(waveHeader1, GCHandleType.Pinned); GCHandle wh2 = GCHandle.Alloc(waveHeader2, GCHandleType.Pinned); WaveFormat wf = new WaveFormat(); wf.wFormatTag = 1; // 1 is PCM wf.nChannels = 1; wf.nSamplesPerSec = sampleRate; wf.wBitsPerSample = 16; wf.nBlockAlign = 2; wf.cbSize = 0; wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.wBitsPerSample / 8; d = new WaveDelegate(waveInCallback); int devID = -1; int callbackInstance = 0; ret = waveInOpen(out waveIn, devID, ref wf, d, ref callbackInstance, CALLBACK_FUNCTION); if (ret != 0) { return; } waveHeader1.dwBufferLength = bufferSize; waveHeader2.dwBufferLength = bufferSize; waveHeader1.lpData = Marshal.AllocHGlobal(bufferSize); waveHeader2.lpData = Marshal.AllocHGlobal(bufferSize); ret = waveInPrepareHeader(waveIn, ref waveHeader1, Marshal.SizeOf(waveHeader1)); ret = waveInAddBuffer(waveIn, ref waveHeader1, Marshal.SizeOf(waveHeader1)); ret = waveInPrepareHeader(waveIn, ref waveHeader2, Marshal.SizeOf(waveHeader2)); ret = waveInAddBuffer(waveIn, ref waveHeader2, Marshal.SizeOf(waveHeader2)); ret = waveInStart(waveIn); Keyboard keyboard = new Keyboard(); while (true) { Thread.Sleep(100); if (keyboard.AltKeyDown == true) { break; } } ret = waveInStop(waveIn); ret = waveInClose(waveIn); //bw.Close(); }
public static extern int waveInOpen(out IntPtr phwi, int uDeviceID, ref WaveFormat lpFormat, WaveDelegate dwCallback, ref int dwInstance, int dwFlags);