// readonly Int32 _bufferSize; // allocated bytes - may not be the same as bytes read // /// <summary> // /// The buffer-size in bytes. // /// </summary> // public int BufferSize // { // get { return _bufferSize; } // } #endregion properties #region cTor /// <summary> /// Creates a new WaveOutBuffer. /// </summary> /// <param name="hWaveOut">WaveOut device to write to</param> /// <param name="bufferSize">buffer size in bytes</param> /// <param name="bufferFillStream">stream to provide more data</param> /// <param name="waveOutLock">lock to protect WaveOut API's from being called on 2+ threads</param> internal WaveOutBuffer(IntPtr hWaveOut, Int32 bufferSize, WaveFileReader bufferFillStream, object waveOutLock) { // _bufferSize = bufferSize; _buffer = new byte[bufferSize]; _hBuffer = GCHandle.Alloc(_buffer, GCHandleType.Pinned); _hWaveOut = hWaveOut; _waveProvider = bufferFillStream; _waveOutLock = waveOutLock; _header = new WaveHeader(); _hHeader = GCHandle.Alloc(_header, GCHandleType.Pinned); _header.dataBuffer = _hBuffer.AddrOfPinnedObject(); _header.bufferLength = bufferSize; _header.loops = 1; _hThis = GCHandle.Alloc(this); _header.userData = (IntPtr)_hThis; lock (_waveOutLock) { MultimediaException.Try(WaveInterop.waveOutPrepareHeader(_hWaveOut, _header, Marshal.SizeOf(_header)), "waveOutPrepareHeader"); } }
public WaveOutEvent(WaveFileReader waveProvider) { //lipsync_editor.logfile.Log("WaveOutEvent()"); _waveProvider = waveProvider; //lipsync_editor.logfile.Log(". format= " + _waveProvider.WaveFormat); _syncContext = SynchronizationContext.Current; // TODO: what is that - looks like if !null another thread starts if (_syncContext != null) { string label = _syncContext.GetType().Name; if (label == "LegacyAspNetSynchronizationContext" || label == "AspNetSynchronizationContext") { //lipsync_editor.logfile.Log(". syncContext= " + label); //lipsync_editor.logfile.Log(". set syncContext NULL"); _syncContext = null; } } _callbackEvent = new AutoResetEvent(false); _waveOutLock = new object(); MultimediaResult result; lock (_waveOutLock) { result = WaveInterop.waveOutOpenWindow(out _hWaveOut, DEVICEID, _waveProvider.WaveFormat, _callbackEvent.SafeWaitHandle.DangerousGetHandle(), IntPtr.Zero, WaveInterop.WaveInOutOpenFlags.CallbackEvent); } MultimediaException.Try(result, "waveOutOpen"); PlaybackState = PlaybackState.Stopped; _buffers = new WaveOutBuffer[BUFFERS]; int bufferSize = calcBytesPerLatency((LATENCY * 2 + BUFFERS - 1) / BUFFERS); // round up. //lipsync_editor.logfile.Log(". bufferSize= " + bufferSize); for (var i = 0; i != BUFFERS; ++i) { _buffers[i] = new WaveOutBuffer(_hWaveOut, bufferSize, _waveProvider, _waveOutLock); } }
/// <summary> /// Gets the position of the waveout stream from the WindowsAPI. /// </summary> /// <returns></returns> internal uint GetPosition() { lock (_waveOutLock) { var multimediaTime = new MultimediaTime(); multimediaTime.wType = MultimediaTime.TIME_BYTES; MultimediaException.Try(WaveInterop.waveOutGetPosition(_hWaveOut, ref multimediaTime, Marshal.SizeOf(multimediaTime)), "waveOutGetPosition"); // if (multimediaTime.wType != MultimediaTime.TIME_BYTES) // throw new Exception(string.Format("waveOutGetPosition: wType -> Expected {0}, Received {1}", // MultimediaTime.TIME_BYTES, multimediaTime.wType)); return(multimediaTime.cb); } }