public static void WaveOutCallback(IntPtr hdrvr, int uMsg, int dwUser, ref WaveHeader wavhdr, int dwParam2) { if (uMsg == (int)WaveFormOutputMessage.Done) { GCHandle handle = (GCHandle)wavhdr.UserData; WaveOutBuffer buffer = (WaveOutBuffer)handle.Target; if (buffer._Player.IsPlaying) { buffer.NextBuffer.Play(); #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("FillBuffer start at {0} ticks.", DateTime.Now.Ticks)); #endif buffer._Player.FillBuffer(buffer); #if DEBUG System.Diagnostics.Debug.WriteLine(string.Format("FillBuffer end at {0} ticks.", DateTime.Now.Ticks)); #endif buffer._IsEmpty = false; buffer._HasData.Set(); } else { buffer._Player.SignalDone(); } } }
// // private void FillBufferLoop() // { // WaveOutBuffer current = _RootBuffer; // // while( IsPlaying ) // { // // Wait until buffer has been read // current.WaitUntilEmpty(); // // // Fill the buffer // FillBuffer( current ); // // // Move to next buffer // current = current.NextBuffer; // } // } private void PlayLoop() { int index = 0; WaveOutBuffer buffer = _Buffers[index]; WaveOutBuffer playing = null; FillBuffer(buffer); while (IsPlaying) { buffer.Play(); playing = buffer; index = ++index % BufferCount; buffer = _Buffers[index]; FillBuffer(buffer); playing.WaitUntilCompleted(); // // WaveOutBuffer buffer = _Buffers[index]; // FillBuffer( buffer ); // // // buffer.Play(); // buffer.WaitUntilCompleted(); // // current = current.NextBuffer; } this._FinishEvent.Set(); }
private void AllocateBuffers() { this._RootBuffer = new WaveOutBuffer(this, this._DeviceHandle, this.BufferSize); var prev = this._RootBuffer; for (int i = 1; i < this.BufferCount; i++) { prev.NextBuffer = new WaveOutBuffer(this, this._DeviceHandle, this.BufferSize); prev = prev.NextBuffer; } prev.NextBuffer = _RootBuffer; }
internal void FillBuffer(WaveOutBuffer buffer) { #if DEBUG this._FillCount++; #endif WaveBufferEmptyEventArgs args = new WaveBufferEmptyEventArgs(buffer.Data, buffer.Length); _FillerDelegate(buffer, args); // Stop playing if we no longer have data. if (args.BytesWritten == 0) { Stop(); } }
/// <summary> /// Callback that receives signal when completed /// </summary> /// <param name="audioDevice">Device that signaled the event</param> /// <param name="message">Message passed back to us.</param> /// <param name="instance">Not used</param> /// <param name="header">Header that is being signalled</param> /// <param name="reserved">Not used</param> internal static void WaveOutProcCallback(IntPtr audioDevice, int message, int instance, ref WaveHeader header, int reserved) { // Make sure that we're signalled done if (message == (int)WaveFormOutputMessage.Done) { // Get IntPtr to WaveOutBuffer GCHandle handle = (GCHandle)header.UserData; // Get buffer WaveOutBuffer buffer = (WaveOutBuffer)handle.Target; // Signal completed to buffer buffer.OnCompleted(EventArgs.Empty); } }
private void FillBuffer(WaveOutBuffer buffer) { WaveBufferEmptyEventArgs args = new WaveBufferEmptyEventArgs(buffer.Data, buffer.Size); // Fill the buffer this._BufferEmptyHandler(this, args); // TODO: Examine args.BytesWritten to determine when end of data reached. if (args.BytesWritten <= 0) { // TODO: Take action to signal playing should stop. this._IsPlaying = false; } // // // Signal the buffer that it has data. // buffer.BufferFilled(); }
public void Play() { #if DEBUG this._FillCount = 0; #endif if (_DataStream != null) { _DataStream.Position = _StartPosition; } WaveFormatEx format = Format.GetFormat(); int result = WaveFormNative.waveOutOpen(out _DeviceHandle, Device, ref format, _CallBack, 0, (int)DeviceOpenFlags.CallbackFunction); if (result != WaveError.MMSYSERR_NOERROR) { throw new SoundCoreException(WaveError.GetMessage(result), result); } _IsPlaying = true; AllocateBuffers(); // Perform Initial fill WaveOutBuffer current = _RootBuffer; do { FillBuffer(current); current.BufferFilled(); current = current.NextBuffer; } while (current != _RootBuffer); _RootBuffer.Play(); // // _FillThread = new Thread( new ThreadStart( FillProc ) ); //// _FillThread.Priority = ThreadPriority.Highest; // _FillThread.Start(); // // _PlayThread = new Thread( new ThreadStart( PlayProc ) ); // _PlayThread.Start(); }
private void AllocateBuffers() { // _RootBuffer = new WaveOutBuffer( this._Device, this.BufferDataSize ); //// _RootBuffer.Completed += new EventHandler(buffer_Completed); // // WaveOutBuffer prev = _RootBuffer; _Buffers = new WaveOutBuffer[BufferCount]; _Buffers[0] = new WaveOutBuffer(this._Device, this.BufferDataSize, new WaveBufferEmptyHandler(FillBuffer)); _Buffers[1] = new WaveOutBuffer(this._Device, this.BufferDataSize, new WaveBufferEmptyHandler(FillBuffer)); _Buffers[0].NextBuffer = _Buffers[1]; _Buffers[1].NextBuffer = _Buffers[0]; // for ( int i = 0; i < this.BufferCount; i++ ) // { // _Buffers[i] = new WaveOutBuffer( this._Device, this.BufferDataSize ); // prev.NextBuffer = new WaveOutBuffer( this._Device, this.BufferDataSize ); // prev = prev.NextBuffer; // prev.Completed += new EventHandler( buffer_Completed ); // } // // prev.NextBuffer = _RootBuffer; }