From ef655f5e787983c159cbea21858685be7dd44ec6 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 16 Oct 2013 11:50:19 -0700 Subject: [PATCH] clear agent audio injector after send, don't send if empty --- assignment-client/src/Agent.cpp | 5 ++++- libraries/audio/src/AudioInjector.cpp | 8 +++++++- libraries/audio/src/AudioInjector.h | 4 ++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index d9cf479b17..8e8835de45 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -161,13 +161,16 @@ void Agent::run() { emit willSendAudioDataCallback(); - if (audioMixer) { + if (audioMixer && scriptedAudioInjector.hasSamplesToInject()) { int usecToSleep = usecTimestamp(&startTime) + (thisFrame++ * INJECT_INTERVAL_USECS) - usecTimestampNow(); if (usecToSleep > 0) { usleep(usecToSleep); } scriptedAudioInjector.injectAudio(NodeList::getInstance()->getNodeSocket(), audioMixer->getPublicSocket()); + + // clear out the audio injector so that it doesn't re-send what we just sent + scriptedAudioInjector.clear(); } // allow the scripter's call back to setup visual data diff --git a/libraries/audio/src/AudioInjector.cpp b/libraries/audio/src/AudioInjector.cpp index acdee03e8f..e3b137fe4d 100644 --- a/libraries/audio/src/AudioInjector.cpp +++ b/libraries/audio/src/AudioInjector.cpp @@ -64,7 +64,7 @@ AudioInjector::~AudioInjector() { } void AudioInjector::injectAudio(UDPSocket* injectorSocket, sockaddr* destinationSocket) { - if (_audioSampleArray) { + if (_audioSampleArray && _indexOfNextSlot > 0) { _isInjectingAudio = true; timeval startTime; @@ -139,6 +139,11 @@ void AudioInjector::addSamples(int16_t* sampleBuffer, int numSamples) { } } +void AudioInjector::clear() { + _indexOfNextSlot = 0; + memset(_audioSampleArray, 0, _numTotalSamples * sizeof(int16_t)); +} + int16_t& AudioInjector::sampleAt(const int index) { assert(index >= 0 && index < _numTotalSamples); @@ -149,4 +154,5 @@ void AudioInjector::insertSample(const int index, int sample) { assert (index >= 0 && index < _numTotalSamples); _audioSampleArray[index] = (int16_t) sample; + _indexOfNextSlot = index + 1; } diff --git a/libraries/audio/src/AudioInjector.h b/libraries/audio/src/AudioInjector.h index 626cdc3149..8847ce14b9 100644 --- a/libraries/audio/src/AudioInjector.h +++ b/libraries/audio/src/AudioInjector.h @@ -51,8 +51,12 @@ public: float getRadius() const { return _radius; } void setRadius(float radius) { _radius = radius; } + bool hasSamplesToInject() const { return _indexOfNextSlot > 0; } + void addSample(const int16_t sample); void addSamples(int16_t* sampleBuffer, int numSamples); + + void clear(); public slots: int16_t& sampleAt(const int index); void insertSample(const int index, int sample);