mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-06-05 03:20:45 +02:00
tweaks to audioring buffer
This commit is contained in:
parent
4c0f83913b
commit
b56e0ff3d4
1 changed files with 15 additions and 2 deletions
|
@ -24,6 +24,7 @@ AudioRingBuffer::AudioRingBuffer(int numFrameSamples) :
|
||||||
{
|
{
|
||||||
if (numFrameSamples) {
|
if (numFrameSamples) {
|
||||||
_buffer = new int16_t[_sampleCapacity];
|
_buffer = new int16_t[_sampleCapacity];
|
||||||
|
memset(_buffer, 0, _sampleCapacity * sizeof(int16_t));
|
||||||
_nextOutput = _buffer;
|
_nextOutput = _buffer;
|
||||||
_endOfLastWrite = _buffer;
|
_endOfLastWrite = _buffer;
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,6 +48,7 @@ void AudioRingBuffer::resizeForFrameSize(qint64 numFrameSamples) {
|
||||||
delete[] _buffer;
|
delete[] _buffer;
|
||||||
_sampleCapacity = numFrameSamples * RING_BUFFER_LENGTH_FRAMES;
|
_sampleCapacity = numFrameSamples * RING_BUFFER_LENGTH_FRAMES;
|
||||||
_buffer = new int16_t[_sampleCapacity];
|
_buffer = new int16_t[_sampleCapacity];
|
||||||
|
memset(_buffer, 0, _sampleCapacity * sizeof(int16_t));
|
||||||
_nextOutput = _buffer;
|
_nextOutput = _buffer;
|
||||||
_endOfLastWrite = _buffer;
|
_endOfLastWrite = _buffer;
|
||||||
}
|
}
|
||||||
|
@ -63,7 +65,8 @@ qint64 AudioRingBuffer::readSamples(int16_t* destination, qint64 maxSamples) {
|
||||||
qint64 AudioRingBuffer::readData(char *data, qint64 maxSize) {
|
qint64 AudioRingBuffer::readData(char *data, qint64 maxSize) {
|
||||||
|
|
||||||
// only copy up to the number of samples we have available
|
// only copy up to the number of samples we have available
|
||||||
int numReadSamples = std::min((unsigned) (maxSize / sizeof(int16_t)), samplesAvailable());
|
//int numReadSamples = std::min((unsigned) (maxSize / sizeof(int16_t)), samplesAvailable());
|
||||||
|
int numReadSamples = _endOfLastWrite ? (maxSize / sizeof(int16_t)) : samplesAvailable();
|
||||||
|
|
||||||
if (_nextOutput + numReadSamples > _buffer + _sampleCapacity) {
|
if (_nextOutput + numReadSamples > _buffer + _sampleCapacity) {
|
||||||
// we're going to need to do two reads to get this data, it wraps around the edge
|
// we're going to need to do two reads to get this data, it wraps around the edge
|
||||||
|
@ -71,12 +74,15 @@ qint64 AudioRingBuffer::readData(char *data, qint64 maxSize) {
|
||||||
// read to the end of the buffer
|
// read to the end of the buffer
|
||||||
int numSamplesToEnd = (_buffer + _sampleCapacity) - _nextOutput;
|
int numSamplesToEnd = (_buffer + _sampleCapacity) - _nextOutput;
|
||||||
memcpy(data, _nextOutput, numSamplesToEnd * sizeof(int16_t));
|
memcpy(data, _nextOutput, numSamplesToEnd * sizeof(int16_t));
|
||||||
|
memset(_nextOutput, 0, numSamplesToEnd * sizeof(int16_t)); // clear it
|
||||||
|
|
||||||
// read the rest from the beginning of the buffer
|
// read the rest from the beginning of the buffer
|
||||||
memcpy(data + (numSamplesToEnd * sizeof(int16_t)), _buffer, (numReadSamples - numSamplesToEnd) * sizeof(int16_t));
|
memcpy(data + (numSamplesToEnd * sizeof(int16_t)), _buffer, (numReadSamples - numSamplesToEnd) * sizeof(int16_t));
|
||||||
|
memset(_buffer, 0, (numReadSamples - numSamplesToEnd) * sizeof(int16_t)); // clear it
|
||||||
} else {
|
} else {
|
||||||
// read the data
|
// read the data
|
||||||
memcpy(data, _nextOutput, numReadSamples * sizeof(int16_t));
|
memcpy(data, _nextOutput, numReadSamples * sizeof(int16_t));
|
||||||
|
memset(_nextOutput, 0, numReadSamples * sizeof(int16_t)); // clear it
|
||||||
}
|
}
|
||||||
|
|
||||||
// push the position of _nextOutput by the number of samples read
|
// push the position of _nextOutput by the number of samples read
|
||||||
|
@ -102,7 +108,7 @@ qint64 AudioRingBuffer::writeData(const char* data, qint64 maxSize) {
|
||||||
&& (less(_endOfLastWrite, _nextOutput)
|
&& (less(_endOfLastWrite, _nextOutput)
|
||||||
&& lessEqual(_nextOutput, shiftedPositionAccomodatingWrap(_endOfLastWrite, samplesToCopy)))) {
|
&& lessEqual(_nextOutput, shiftedPositionAccomodatingWrap(_endOfLastWrite, samplesToCopy)))) {
|
||||||
// this read will cross the next output, so call us starved and reset the buffer
|
// this read will cross the next output, so call us starved and reset the buffer
|
||||||
qDebug() << "Filled the ring buffer. Resetting.";
|
qDebug() << "Filled the ring buffer. Resetting. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
|
||||||
_endOfLastWrite = _buffer;
|
_endOfLastWrite = _buffer;
|
||||||
_nextOutput = _buffer;
|
_nextOutput = _buffer;
|
||||||
_isStarved = true;
|
_isStarved = true;
|
||||||
|
@ -143,6 +149,13 @@ unsigned int AudioRingBuffer::samplesAvailable() const {
|
||||||
sampleDifference += _sampleCapacity;
|
sampleDifference += _sampleCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sampleDifference == 0) {
|
||||||
|
qDebug() << "ran dry!!! _endOfLastWrite=" << _endOfLastWrite
|
||||||
|
<< "_nextOutput=" << _nextOutput
|
||||||
|
<< "_buffer + _sampleCapacity=" << (_buffer + _sampleCapacity)
|
||||||
|
<< " samplesAvailable() == 0!!!!!!!!!!!!!!!!!!!!";
|
||||||
|
}
|
||||||
|
|
||||||
return sampleDifference;
|
return sampleDifference;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue