mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 12:18:36 +02:00
Add proper loop handling in injector
This commit is contained in:
parent
b56668892e
commit
a3b2dd2295
2 changed files with 32 additions and 29 deletions
|
@ -238,16 +238,19 @@ int64_t AudioInjector::injectNextFrame() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int bytesToCopy = std::min((_options.stereo ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL,
|
int totalBytesLeftToCopy = (_options.stereo ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL;
|
||||||
_audioData.size() - _currentSendOffset);
|
if (!_options.loop) {
|
||||||
|
// If we aren't looping, let's make sure we don't read past the end
|
||||||
|
totalBytesLeftToCopy = std::min(totalBytesLeftToCopy, _audioData.size() - _currentSendOffset);
|
||||||
|
}
|
||||||
|
|
||||||
// Measure the loudness of this frame
|
// Measure the loudness of this frame
|
||||||
_loudness = 0.0f;
|
_loudness = 0.0f;
|
||||||
for (int i = 0; i < bytesToCopy; i += sizeof(int16_t)) {
|
for (int i = 0; i < totalBytesLeftToCopy; i += sizeof(int16_t)) {
|
||||||
_loudness += abs(*reinterpret_cast<int16_t*>(_audioData.data() + _currentSendOffset + i)) /
|
_loudness += abs(*reinterpret_cast<int16_t*>(_audioData.data() + ((_currentSendOffset + i) % _audioData.size()))) /
|
||||||
(AudioConstants::MAX_SAMPLE_VALUE / 2.0f);
|
(AudioConstants::MAX_SAMPLE_VALUE / 2.0f);
|
||||||
}
|
}
|
||||||
_loudness /= (float)(bytesToCopy / sizeof(int16_t));
|
_loudness /= (float)(totalBytesLeftToCopy/ sizeof(int16_t));
|
||||||
|
|
||||||
_currentPacket->seek(0);
|
_currentPacket->seek(0);
|
||||||
|
|
||||||
|
@ -264,8 +267,16 @@ int64_t AudioInjector::injectNextFrame() {
|
||||||
|
|
||||||
_currentPacket->seek(audioDataOffset);
|
_currentPacket->seek(audioDataOffset);
|
||||||
|
|
||||||
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
while (totalBytesLeftToCopy > 0) {
|
||||||
_currentPacket->write(_audioData.data() + _currentSendOffset, bytesToCopy);
|
int bytesToCopy = std::min(totalBytesLeftToCopy, _audioData.size() - _currentSendOffset);
|
||||||
|
|
||||||
|
_currentPacket->write(_audioData.data() + _currentSendOffset, bytesToCopy);
|
||||||
|
_currentSendOffset += bytesToCopy;
|
||||||
|
totalBytesLeftToCopy -= bytesToCopy;
|
||||||
|
if (_currentSendOffset >= _audioData.size()) {
|
||||||
|
_currentSendOffset = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set the correct size used for this packet
|
// set the correct size used for this packet
|
||||||
_currentPacket->setPayloadSize(_currentPacket->pos());
|
_currentPacket->setPayloadSize(_currentPacket->pos());
|
||||||
|
@ -280,21 +291,13 @@ int64_t AudioInjector::injectNextFrame() {
|
||||||
_outgoingSequenceNumber++;
|
_outgoingSequenceNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
_currentSendOffset += bytesToCopy;
|
if (_currentSendOffset >= _audioData.size() && !_options.loop) {
|
||||||
|
finish();
|
||||||
if (_currentSendOffset >= _audioData.size()) {
|
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
||||||
// we're at the end of the audio data to send
|
|
||||||
if (_options.loop) {
|
|
||||||
// we were asked to loop, set our send offset to 0
|
|
||||||
_currentSendOffset = 0;
|
|
||||||
} else {
|
|
||||||
// we weren't to loop, say that we're done now
|
|
||||||
finish();
|
|
||||||
return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_currentSendOffset == bytesToCopy) {
|
if (!_hasSentFirstFrame) {
|
||||||
|
_hasSentFirstFrame = true;
|
||||||
// ask AudioInjectorManager to call us right away again to
|
// ask AudioInjectorManager to call us right away again to
|
||||||
// immediately send the first two frames so the mixer can start using the audio right away
|
// immediately send the first two frames so the mixer can start using the audio right away
|
||||||
return NEXT_FRAME_DELTA_IMMEDIATELY;
|
return NEXT_FRAME_DELTA_IMMEDIATELY;
|
||||||
|
@ -303,15 +306,14 @@ int64_t AudioInjector::injectNextFrame() {
|
||||||
const int MAX_ALLOWED_FRAMES_TO_FALL_BEHIND = 7;
|
const int MAX_ALLOWED_FRAMES_TO_FALL_BEHIND = 7;
|
||||||
int64_t currentTime = _frameTimer->nsecsElapsed() / 1000;
|
int64_t currentTime = _frameTimer->nsecsElapsed() / 1000;
|
||||||
auto currentFrameBasedOnElapsedTime = currentTime / AudioConstants::NETWORK_FRAME_USECS;
|
auto currentFrameBasedOnElapsedTime = currentTime / AudioConstants::NETWORK_FRAME_USECS;
|
||||||
|
|
||||||
if (currentFrameBasedOnElapsedTime - _nextFrame > MAX_ALLOWED_FRAMES_TO_FALL_BEHIND) {
|
if (currentFrameBasedOnElapsedTime - _nextFrame > MAX_ALLOWED_FRAMES_TO_FALL_BEHIND) {
|
||||||
// If we are falling behind by more frames than our threshold, let's skip the frames ahead
|
// If we are falling behind by more frames than our threshold, let's skip the frames ahead
|
||||||
_nextFrame = currentFrameBasedOnElapsedTime - MAX_ALLOWED_FRAMES_TO_FALL_BEHIND;
|
qDebug() << "AudioInjector::injectNextFrame() skipping ahead, fell behind by " << (currentFrameBasedOnElapsedTime - _nextFrame) << " frames";
|
||||||
} else {
|
_nextFrame = currentFrameBasedOnElapsedTime;
|
||||||
++_nextFrame;
|
_currentSendOffset = _nextFrame * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL * (_options.stereo ? 2 : 1) % _audioData.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t playNextFrameAt = _nextFrame * AudioConstants::NETWORK_FRAME_USECS;
|
int64_t playNextFrameAt = ++_nextFrame * AudioConstants::NETWORK_FRAME_USECS;
|
||||||
return std::max(INT64_C(0), playNextFrameAt - currentTime);
|
return std::max(INT64_C(0), playNextFrameAt - currentTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,10 +90,11 @@ private:
|
||||||
QByteArray _audioData;
|
QByteArray _audioData;
|
||||||
AudioInjectorOptions _options;
|
AudioInjectorOptions _options;
|
||||||
State _state { State::NotFinished };
|
State _state { State::NotFinished };
|
||||||
bool _hasSetup = false;
|
bool _hasSentFirstFrame { false };
|
||||||
bool _shouldStop = false;
|
bool _hasSetup { false };
|
||||||
float _loudness = 0.0f;
|
bool _shouldStop { false };
|
||||||
int _currentSendOffset = 0;
|
float _loudness { 0.0f };
|
||||||
|
int _currentSendOffset { 0 };
|
||||||
std::unique_ptr<NLPacket> _currentPacket { nullptr };
|
std::unique_ptr<NLPacket> _currentPacket { nullptr };
|
||||||
AbstractAudioInterface* _localAudioInterface { nullptr };
|
AbstractAudioInterface* _localAudioInterface { nullptr };
|
||||||
AudioInjectorLocalBuffer* _localBuffer { nullptr };
|
AudioInjectorLocalBuffer* _localBuffer { nullptr };
|
||||||
|
|
Loading…
Reference in a new issue