private void CreateBuffers() { // Default to three buffers of 100ms each int bufferSize = BufferMilliseconds * WaveFormat.AverageBytesPerSecond / 1000; if (bufferSize % WaveFormat.BlockAlign != 0) { bufferSize -= bufferSize % WaveFormat.BlockAlign; } buffers = new WaveInBuffer[NumberOfBuffers]; for (int n = 0; n < buffers.Length; n++) { buffers[n] = new WaveInBuffer(waveInHandle, bufferSize); } }
/// <summary> /// Called when we get a new buffer of recorded data /// </summary> private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, int userData, WaveHeader waveHeader, int reserved) { if (message == WaveInterop.WaveMessage.WaveInData) { GCHandle hBuffer = (GCHandle)waveHeader.userData; WaveInBuffer buffer = (WaveInBuffer)hBuffer.Target; length += buffer.BytesRecorded; if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded)); } if (recording) { buffer.Reuse(); } else { if (RecordingStopped != null) { RecordingStopped(this, EventArgs.Empty); } } } }
/// <summary> /// Creates a new Wave input stream /// </summary> /// <param name="deviceNumber">The device to open - 0 is default</param> /// <param name="desiredFormat">The PCM format to record in</param> /// <param name="callbackWindow">If this parameter is non-null, the Wave In Messages /// will be sent to the message loop of the supplied control. This is considered a /// safer way to use the waveIn functionality</param> public WaveInStream(int deviceNumber, WaveFormat desiredFormat, System.Windows.Forms.Control callbackWindow) { this.waveFormat = desiredFormat; callback = new WaveInterop.WaveCallback(Callback); if (callbackWindow == null) { MmException.Try(WaveInterop.waveInOpen(out waveInHandle, deviceNumber, desiredFormat, callback, 0, WaveInterop.CallbackFunction), "waveInOpen"); } else { waveInWindow = new WaveWindowNative(callback); MmException.Try(WaveInterop.waveInOpenWindow(out waveInHandle, deviceNumber, desiredFormat, callbackWindow.Handle, 0, WaveInterop.CallbackWindow), "waveInOpen"); waveInWindow.AssignHandle(callbackWindow.Handle); } // Default to three buffers of 100ms each int bufferSize = desiredFormat.AverageBytesPerSecond / 10; numBuffers = 3; buffers = new WaveInBuffer[numBuffers]; for (int n = 0; n < numBuffers; n++) { buffers[n] = new WaveInBuffer(waveInHandle, bufferSize); } }
/// <summary> /// Called when we get a new buffer of recorded data /// </summary> private void Callback(IntPtr waveInHandle, WaveInterop.WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved) { if (message == WaveInterop.WaveMessage.WaveInData) { GCHandle hBuffer = (GCHandle)waveHeader.userData; WaveInBuffer buffer = (WaveInBuffer)hBuffer.Target; Exception exception = null; if (DataAvailable != null) { DataAvailable(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded)); } if (recording) { try { buffer.Reuse(); } catch (Exception e) { recording = false; exception = e; } } if (!recording) { RaiseRecordingStopped(exception); } } }
private void RaiseDataAvailable(WaveInBuffer buffer) { var handler = DataAvailable; if (handler != null) { handler(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded)); } }
private void RaiseDataAvailable(WaveInBuffer buffer) { EventHandler <WaveInEventArgs> dataAvailable = this.DataAvailable; if (dataAvailable != null) { dataAvailable(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded)); } }
private void CreateBuffers() { // Default to three buffers of 100ms each int bufferSize = BufferMillisconds * WaveFormat.AverageBytesPerSecond / 1000; buffers = new WaveInBuffer[NumberOfBuffers]; for (int n = 0; n < buffers.Length; n++) { buffers[n] = new WaveInBuffer(waveInHandle, bufferSize); } }
public void StopRecording() { if (this.recording) { this.recording = false; MmException.Try(WaveInterop.waveInStop(this.waveInHandle), "waveInStop"); for (int i = 0; i < this.buffers.Length; i++) { int num = (i + this.lastReturnedBufferIndex + 1) % this.buffers.Length; WaveInBuffer waveInBuffer = this.buffers[num]; if (waveInBuffer.Done) { this.RaiseDataAvailable(waveInBuffer); } } this.RaiseRecordingStopped(null); } }
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); } } }
private void RaiseDataAvailable(WaveInBuffer buffer) { DataAvailable?.Invoke(this, new WaveInEventArgs(buffer.Data, buffer.BytesRecorded)); }