mirror of
https://github.com/overte-org/overte.git
synced 2025-08-06 09:29:53 +02:00
Added possibility to change AudioInjector position
This commit is contained in:
parent
36a3372f58
commit
1d014358ee
2 changed files with 16 additions and 10 deletions
|
@ -25,7 +25,8 @@ AudioInjector::AudioInjector(QObject* parent) :
|
||||||
QObject(parent),
|
QObject(parent),
|
||||||
_sound(NULL),
|
_sound(NULL),
|
||||||
_options(),
|
_options(),
|
||||||
_shouldStop(false)
|
_shouldStop(false),
|
||||||
|
_currentSendPosition(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,17 +96,17 @@ void AudioInjector::injectAudio() {
|
||||||
timer.start();
|
timer.start();
|
||||||
int nextFrame = 0;
|
int nextFrame = 0;
|
||||||
|
|
||||||
int currentSendPosition = 0;
|
_currentSendPosition = 0;
|
||||||
|
|
||||||
int numPreAudioDataBytes = injectAudioPacket.size();
|
int numPreAudioDataBytes = injectAudioPacket.size();
|
||||||
bool shouldLoop = _options.getLoop();
|
bool shouldLoop = _options.getLoop();
|
||||||
|
|
||||||
// loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks
|
// loop to send off our audio in NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL byte chunks
|
||||||
quint16 outgoingInjectedAudioSequenceNumber = 0;
|
quint16 outgoingInjectedAudioSequenceNumber = 0;
|
||||||
while (currentSendPosition < soundByteArray.size() && !_shouldStop) {
|
while (_currentSendPosition < soundByteArray.size() && !_shouldStop) {
|
||||||
|
|
||||||
int bytesToCopy = std::min(NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL,
|
int bytesToCopy = std::min(NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL,
|
||||||
soundByteArray.size() - currentSendPosition);
|
soundByteArray.size() - _currentSendPosition);
|
||||||
memcpy(injectAudioPacket.data() + positionOptionOffset,
|
memcpy(injectAudioPacket.data() + positionOptionOffset,
|
||||||
&_options.getPosition(),
|
&_options.getPosition(),
|
||||||
sizeof(_options.getPosition()));
|
sizeof(_options.getPosition()));
|
||||||
|
@ -120,7 +121,7 @@ void AudioInjector::injectAudio() {
|
||||||
memcpy(injectAudioPacket.data() + numPreSequenceNumberBytes, &outgoingInjectedAudioSequenceNumber, sizeof(quint16));
|
memcpy(injectAudioPacket.data() + numPreSequenceNumberBytes, &outgoingInjectedAudioSequenceNumber, sizeof(quint16));
|
||||||
|
|
||||||
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
// copy the next NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL bytes to the packet
|
||||||
memcpy(injectAudioPacket.data() + numPreAudioDataBytes, soundByteArray.data() + currentSendPosition, bytesToCopy);
|
memcpy(injectAudioPacket.data() + numPreAudioDataBytes, soundByteArray.data() + _currentSendPosition, bytesToCopy);
|
||||||
|
|
||||||
// grab our audio mixer from the NodeList, if it exists
|
// grab our audio mixer from the NodeList, if it exists
|
||||||
NodeList* nodeList = NodeList::getInstance();
|
NodeList* nodeList = NodeList::getInstance();
|
||||||
|
@ -130,22 +131,22 @@ void AudioInjector::injectAudio() {
|
||||||
nodeList->writeDatagram(injectAudioPacket, audioMixer);
|
nodeList->writeDatagram(injectAudioPacket, audioMixer);
|
||||||
outgoingInjectedAudioSequenceNumber++;
|
outgoingInjectedAudioSequenceNumber++;
|
||||||
|
|
||||||
currentSendPosition += bytesToCopy;
|
_currentSendPosition += bytesToCopy;
|
||||||
|
|
||||||
// send two packets before the first sleep so the mixer can start playback right away
|
// send two packets before the first sleep so the mixer can start playback right away
|
||||||
|
|
||||||
if (currentSendPosition != bytesToCopy && currentSendPosition < soundByteArray.size()) {
|
if (_currentSendPosition != bytesToCopy && _currentSendPosition < soundByteArray.size()) {
|
||||||
// not the first packet and not done
|
// not the first packet and not done
|
||||||
// sleep for the appropriate time
|
// sleep for the appropriate time
|
||||||
int usecToSleep = (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - timer.nsecsElapsed() / 1000;
|
int usecToSleep = (++nextFrame * BUFFER_SEND_INTERVAL_USECS) - timer.nsecsElapsed() / 1000;
|
||||||
|
|
||||||
if (usecToSleep > 0) {
|
if (usecToSleep > 0) {
|
||||||
usleep(usecToSleep);
|
usleep(usecToSleep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldLoop && currentSendPosition == soundByteArray.size()) {
|
if (shouldLoop && _currentSendPosition >= soundByteArray.size()) {
|
||||||
currentSendPosition = 0;
|
_currentSendPosition = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,16 +26,21 @@ class AudioInjector : public QObject {
|
||||||
public:
|
public:
|
||||||
AudioInjector(QObject* parent);
|
AudioInjector(QObject* parent);
|
||||||
AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions);
|
AudioInjector(Sound* sound, const AudioInjectorOptions& injectorOptions);
|
||||||
|
|
||||||
|
int getCurrentSendPosition() const { return _currentSendPosition; }
|
||||||
public slots:
|
public slots:
|
||||||
void injectAudio();
|
void injectAudio();
|
||||||
void stop() { _shouldStop = true; }
|
void stop() { _shouldStop = true; }
|
||||||
void setOptions(AudioInjectorOptions& options);
|
void setOptions(AudioInjectorOptions& options);
|
||||||
|
void setCurrentSendPosition(int currentSendPosition) { _currentSendPosition = currentSendPosition; }
|
||||||
signals:
|
signals:
|
||||||
void finished();
|
void finished();
|
||||||
private:
|
private:
|
||||||
Sound* _sound;
|
Sound* _sound;
|
||||||
AudioInjectorOptions _options;
|
AudioInjectorOptions _options;
|
||||||
bool _shouldStop;
|
bool _shouldStop;
|
||||||
|
int _currentSendPosition;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(AudioInjector*)
|
Q_DECLARE_METATYPE(AudioInjector*)
|
||||||
|
|
Loading…
Reference in a new issue