protected virtual bool ReadAndQueueBuffer() { // Initially accessed from Main thread, then from Playing thread //Debug.WriteLine("Get samples from provider"); // Read samples from Provider _watch.Restart(); bool filled = _sampleProvider.Fill(_buffer); _watch.Stop(); if (!filled) { return(false); // provider failed or wants to stop the engine } //Debug.WriteLine("Sample buffer filled in {0} ms. Audio buffer: {1}", _watch.ElapsedMilliseconds, _currentAudioBuffer); if (_watch.ElapsedMilliseconds >= _bufferLengthMs * 9 / 10) { Debug.WriteLine("Warning! Sample buffer filled in {0} ms", _watch.ElapsedMilliseconds); } // Get free audio buffer and copy data from sample buffer SA.AudioBuffer audioBuffer = _audioBuffers[_currentAudioBuffer]; audioBuffer.BufferData(_buffer, _audioFormat); // Queue audioBuffer to engine source //Debug.WriteLine("Queue audio buffer"); _source.QueueBuffer(audioBuffer); // Switch to next audio buffer _currentAudioBuffer += 1; _currentAudioBuffer %= _audioBufferCount; return(true); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target">The audiosource where we want to queue the data to</param> /// <param name="buffer">The data to buffer</param> /// <param name="format">The format of the data</param> public void QueueData <T>(AudioSource target, T[] buffer, AudioFormat format) where T : unmanaged { var buf = BufferData(buffer, format); target.QueueBuffer(buf); }