From cdc6f33128dc3825ba79e7ba525243892040beaa Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 16 Dec 2013 12:16:08 -0800 Subject: [PATCH] fix scope for input --- interface/src/Audio.cpp | 20 ++++--------------- interface/src/Oscilloscope.cpp | 35 +++++++++++++++++++--------------- interface/src/Oscilloscope.h | 2 +- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 9c6b66edc5..066dd9c40b 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -309,8 +309,10 @@ void Audio::handleAudioInput() { _inputFormat, _desiredInputFormat); // add input data just written to the scope - // QMetaObject::invokeMethod(_scope, "addStereoSamples", Qt::QueuedConnection, - // Q_ARG(QByteArray, inputByteArray), Q_ARG(bool, true)); + QMetaObject::invokeMethod(_scope, "addSamples", Qt::QueuedConnection, + Q_ARG(QByteArray, QByteArray((char*) monoAudioSamples, + NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL)), + Q_ARG(bool, false), Q_ARG(bool, true)); } // if (Menu::getInstance()->isOptionChecked(MenuOption::EchoLocalAudio)) { @@ -420,20 +422,6 @@ void Audio::addReceivedAudioToBuffer(const QByteArray& audioByteArray) { int16_t outputBuffer[numRequiredOutputSamples]; - // linearResampling((int16_t*) inputByteArray.data(), - // monoAudioSamples, - // inputByteArray.size() / sizeof(int16_t), - // numResampledNetworkInputSamples, - // _inputFormat, _desiredInputFormat); - - // copy the packet from the RB to the output - // linearResampling(monoAudioSamples, - // (int16_t*) _outputBuffer.data(), - // numResampledNetworkInputSamples, - // numResampledOutputBytes / sizeof(int16_t), - // _desiredInputFormat, _outputFormat); - - // if there is anything in the ring buffer, decide what to do if (_ringBuffer.samplesAvailable() > 0) { if (!_ringBuffer.isNotStarvedOrHasMinimumSamples(NETWORK_BUFFER_LENGTH_SAMPLES_STEREO diff --git a/interface/src/Oscilloscope.cpp b/interface/src/Oscilloscope.cpp index 2c65f2a07e..ff8da61130 100644 --- a/interface/src/Oscilloscope.cpp +++ b/interface/src/Oscilloscope.cpp @@ -68,24 +68,18 @@ Oscilloscope::~Oscilloscope() { delete[] _samples; } -void Oscilloscope::addStereoSamples(const QByteArray& audioByteArray, bool isInput) { +void Oscilloscope::addSamples(const QByteArray& audioByteArray, bool isStereo, bool isInput) { if (! enabled || inputPaused) { return; } - unsigned int numSamplesPerChannel = audioByteArray.size() / (sizeof(int16_t) * 2); - int16_t samples[numSamplesPerChannel]; - const int16_t* stereoSamples = (int16_t*) audioByteArray.constData(); + int numSamplesPerChannel = audioByteArray.size() / (sizeof(int16_t) * (isStereo ? 2 : 1)); + int16_t* samples = (int16_t*) audioByteArray.data(); - for (int channel = 0; channel < (isInput ? 1 : 2); channel++) { + for (int channel = 0; channel < (isStereo ? 1 : 2); channel++) { // add samples for each of the channels - - // enumerate the interleaved stereoSamples array and pull out the samples for this channel - for (int i = 0; i < audioByteArray.size() / sizeof(int16_t); i += 2) { - samples[i / 2] = stereoSamples[i + channel]; - } - + // determine start/end offset of this channel's region unsigned baseOffs = MAX_SAMPLES_PER_CHANNEL * (channel + !isInput); unsigned endOffs = baseOffs + MAX_SAMPLES_PER_CHANNEL; @@ -103,10 +97,21 @@ void Oscilloscope::addStereoSamples(const QByteArray& audioByteArray, bool isInp numSamplesPerChannel -= n2; } - // copy data - memcpy(_samples + writePos, samples, numSamplesPerChannel * sizeof(int16_t)); - if (n2 > 0) { - memcpy(_samples + baseOffs, samples + numSamplesPerChannel, n2 * sizeof(int16_t)); + if (!isStereo) { + // copy data + memcpy(_samples + writePos, samples, numSamplesPerChannel * sizeof(int16_t)); + if (n2 > 0) { + memcpy(_samples + baseOffs, samples + numSamplesPerChannel, n2 * sizeof(int16_t)); + } + } else { + // we have interleaved samples we need to separate into two channels + for (int i = 0; i < numSamplesPerChannel + n2; i++) { + if (i < numSamplesPerChannel - n2) { + _samples[writePos] = samples[(i * 2) + channel]; + } else { + _samples[baseOffs] = samples[(i * 2) + channel]; + } + } } // set new write position for this channel diff --git a/interface/src/Oscilloscope.h b/interface/src/Oscilloscope.h index f17976d4e4..a245f79f05 100644 --- a/interface/src/Oscilloscope.h +++ b/interface/src/Oscilloscope.h @@ -59,7 +59,7 @@ public: // just uses every nTh sample. void setDownsampleRatio(unsigned n) { assert(n > 0); _downsampleRatio = n; } public slots: - void addStereoSamples(const QByteArray& audioByteArray, bool isInput); + void addSamples(const QByteArray& audioByteArray, bool isStereo, bool isInput); private: // don't copy/assign Oscilloscope(Oscilloscope const&); // = delete;