protected override void WndProc(ref Message m) { WaveInterop.WaveMessage msg = (WaveInterop.WaveMessage)m.Msg; switch (msg) { case WaveInterop.WaveMessage.WaveOutOpen: case WaveInterop.WaveMessage.WaveOutClose: case WaveInterop.WaveMessage.WaveInOpen: case WaveInterop.WaveMessage.WaveInClose: this.waveCallback(m.WParam, msg, IntPtr.Zero, null, IntPtr.Zero); return; case WaveInterop.WaveMessage.WaveOutDone: case WaveInterop.WaveMessage.WaveInData: { IntPtr wParam = m.WParam; WaveHeader waveHeader = new WaveHeader(); Marshal.PtrToStructure(m.LParam, waveHeader); this.waveCallback(wParam, msg, IntPtr.Zero, waveHeader, IntPtr.Zero); return; } default: base.WndProc(ref m); return; } }
public WaveInBuffer(IntPtr waveInHandle, int bufferSize) { this.bufferSize = bufferSize; this.buffer = new byte[bufferSize]; this.hBuffer = GCHandle.Alloc(this.buffer, GCHandleType.Pinned); this.waveInHandle = waveInHandle; this.header = new WaveHeader(); this.hHeader = GCHandle.Alloc(this.header, GCHandleType.Pinned); this.header.dataBuffer = this.hBuffer.AddrOfPinnedObject(); this.header.bufferLength = bufferSize; this.header.loops = 1; this.hThis = GCHandle.Alloc(this); this.header.userData = (IntPtr)this.hThis; MmException.Try(WaveInterop.waveInPrepareHeader(waveInHandle, this.header, Marshal.SizeOf(this.header)), "waveInPrepareHeader"); }
public WaveOutBuffer(IntPtr hWaveOut, int bufferSize, IWaveProvider bufferFillStream, object waveOutLock) { this.bufferSize = bufferSize; this.buffer = new byte[bufferSize]; this.hBuffer = GCHandle.Alloc(this.buffer, GCHandleType.Pinned); this.hWaveOut = hWaveOut; this.waveStream = bufferFillStream; this.waveOutLock = waveOutLock; this.header = new WaveHeader(); this.hHeader = GCHandle.Alloc(this.header, GCHandleType.Pinned); this.header.dataBuffer = this.hBuffer.AddrOfPinnedObject(); this.header.bufferLength = bufferSize; this.header.loops = 1; this.hThis = GCHandle.Alloc(this); this.header.userData = (IntPtr)this.hThis; lock (waveOutLock) { MmException.Try(WaveInterop.waveOutPrepareHeader(hWaveOut, this.header, Marshal.SizeOf(this.header)), "waveOutPrepareHeader"); } }
// made non-static so that playing can be stopped here private void Callback(IntPtr hWaveOut, WaveInterop.WaveMessage uMsg, IntPtr dwInstance, WaveHeader wavhdr, IntPtr dwReserved) { if (uMsg == WaveInterop.WaveMessage.WaveOutDone) { GCHandle hBuffer = (GCHandle)wavhdr.userData; WaveOutBuffer buffer = (WaveOutBuffer)hBuffer.Target; Interlocked.Decrement(ref queuedBuffers); Exception exception = null; // check that we're not here through pressing stop if (PlaybackState == PlaybackState.Playing) { // to avoid deadlocks in Function callback mode, // we lock round this whole thing, which will include the // reading from the stream. // this protects us from calling waveOutReset on another // thread while a WaveOutWrite is in progress lock (waveOutLock) { try { if (buffer.OnDone()) { Interlocked.Increment(ref queuedBuffers); } } catch (Exception e) { // one likely cause is soundcard being unplugged exception = e; } } } if (queuedBuffers == 0) { if (callbackInfo.Strategy == WaveCallbackStrategy.FunctionCallback && playbackState == Wave.PlaybackState.Stopped) { // the user has pressed stop // DO NOT raise the playback stopped event from here // since on the main thread we are still in the waveOutReset function // Playback stopped will be raised elsewhere } else { playbackState = PlaybackState.Stopped; // set explicitly for when we reach the end of the audio RaisePlaybackStoppedEvent(exception); } } } }
private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved) { if (message == WaveInterop.WaveMessage.WaveInData && this.recording) { WaveInBuffer waveInBuffer = (WaveInBuffer)((GCHandle)waveHeader.userData).Target; if (waveInBuffer == null) { return; } this.lastReturnedBufferIndex = Array.IndexOf <WaveInBuffer>(this.buffers, waveInBuffer); this.RaiseDataAvailable(waveInBuffer); try { waveInBuffer.Reuse(); } catch (Exception e) { this.recording = false; this.RaiseRecordingStopped(e); } } }
public static extern MmResult waveOutWrite(IntPtr hWaveOut, WaveHeader lpWaveOutHdr, int uSize);
public static extern MmResult waveOutUnprepareHeader(IntPtr hWaveOut, WaveHeader lpWaveOutHdr, int uSize);
public static extern MmResult waveInPrepareHeader(IntPtr hWaveIn, WaveHeader lpWaveInHdr, int uSize);
public static extern MmResult waveInAddBuffer(IntPtr hWaveIn, WaveHeader pwh, int cbwh);