private bool FeedBuffer(AudioRenderClient renderClient, byte[] buffer, int numFramesCount, int frameSize) { //calculate the number of bytes to "feed" int count = numFramesCount * frameSize; count -= (count % _source.WaveFormat.BlockAlign); //if the driver did not request enough data, return true to continue playback if (count <= 0) return true; //get the requested data int read = _source.Read(buffer, 0, count); //if the source did not provide enough data, we abort the playback by returning false if (read <= 0) return false; //calculate the number of FRAMES to request int actualNumFramesCount = read / frameSize; //again there are some special requirements for exclusive mode AND eventsync if (_shareMode == AudioClientShareMode.Exclusive && _eventSync && read < count) { /* The caller can request a packet size that is less than or equal to the amount * of available space in the buffer (except in the case of an exclusive-mode stream * that uses event-driven buffering; for more information, see IAudioClient::Initialize). * see https://msdn.microsoft.com/en-us/library/windows/desktop/dd368243%28v=vs.85%29.aspx - remarks*/ //since we have to provide exactly the requested number of frames, we clear the rest of the array Array.Clear(buffer, read, count - read); //set the number of frames to request memory for, to the number of requested frames actualNumFramesCount = numFramesCount; } IntPtr ptr = renderClient.GetBuffer(actualNumFramesCount); //we may should introduce a try-finally statement here, but the Marshal.Copy method should not //throw any relevant exceptions ... so we should be able to always release the packet Marshal.Copy(buffer, 0, ptr, read); renderClient.ReleaseBuffer(actualNumFramesCount, AudioClientBufferFlags.None); return true; }
private bool FeedBuffer(AudioRenderClient renderClient, byte[] buffer, int numFramesCount, int frameSize) { int count = numFramesCount * frameSize; count -= (count % _source.WaveFormat.BlockAlign); if (count <= 0) return true; int read = _source.Read(buffer, 0, count); var ptr = renderClient.GetBuffer(numFramesCount); Marshal.Copy(buffer, 0, ptr, read); renderClient.ReleaseBuffer((int)(read / frameSize), AudioClientBufferFlags.None); return read > 0; }