From fdade344a335617c4afc8cb329d8836cd58ca9be Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 2 Feb 2015 20:20:57 -0800 Subject: [PATCH] Move Audio to new settings --- interface/src/Audio.cpp | 55 ++++++++++++----------------------------- interface/src/Audio.h | 25 ++++++++++--------- 2 files changed, 29 insertions(+), 51 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index 2bc5aa5023..126a0533fe 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include @@ -47,17 +47,6 @@ static const int RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES = 100; -namespace SettingHandles { - const SettingHandle audioOutputStarveDetectionEnabled("audioOutputStarveDetectionEnabled", - DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED); - const SettingHandle audioOutputStarveDetectionThreshold("audioOutputStarveDetectionThreshold", - DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_THRESHOLD); - const SettingHandle audioOutputStarveDetectionPeriod("audioOutputStarveDetectionPeriod", - DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_PERIOD); - const SettingHandle audioOutputBufferSize("audioOutputBufferSize", - DEFAULT_MAX_FRAMES_OVER_DESIRED); -} - Audio::Audio() : AbstractAudioInterface(), _audioInput(NULL), @@ -74,12 +63,16 @@ Audio::Audio() : _inputRingBuffer(0), _receivedAudioStream(0, RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES, InboundAudioStream::Settings()), _isStereoInput(false), - _outputBufferSizeFrames(DEFAULT_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES), - _outputStarveDetectionEnabled(true), _outputStarveDetectionStartTimeMsec(0), _outputStarveDetectionCount(0), - _outputStarveDetectionPeriodMsec(DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_PERIOD), - _outputStarveDetectionThreshold(DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_THRESHOLD), + _outputBufferSizeFrames("audioOutputBufferSize", + DEFAULT_MAX_FRAMES_OVER_DESIRED), + _outputStarveDetectionEnabled("audioOutputStarveDetectionEnabled", + DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED), + _outputStarveDetectionPeriodMsec("audioOutputStarveDetectionPeriod", + DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_PERIOD), + _outputStarveDetectionThreshold("audioOutputStarveDetectionThreshold", + DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_THRESHOLD), _averagedLatency(0.0f), _lastInputLoudness(0.0f), _timeSinceLastClip(-1.0f), @@ -968,16 +961,16 @@ bool Audio::switchInputToAudioDevice(const QAudioDeviceInfo& inputDeviceInfo) { void Audio::outputNotify() { int recentUnfulfilled = _audioOutputIODevice.getRecentUnfulfilledReads(); if (recentUnfulfilled > 0) { - if (_outputStarveDetectionEnabled) { + if (_outputStarveDetectionEnabled.get()) { quint64 now = usecTimestampNow() / 1000; quint64 dt = now - _outputStarveDetectionStartTimeMsec; - if (dt > _outputStarveDetectionPeriodMsec) { + if (dt > _outputStarveDetectionPeriodMsec.get()) { _outputStarveDetectionStartTimeMsec = now; _outputStarveDetectionCount = 0; } else { _outputStarveDetectionCount += recentUnfulfilled; - if (_outputStarveDetectionCount > _outputStarveDetectionThreshold) { - int newOutputBufferSizeFrames = _outputBufferSizeFrames + 1; + if (_outputStarveDetectionCount > _outputStarveDetectionThreshold.get()) { + int newOutputBufferSizeFrames = _outputBufferSizeFrames.get() + 1; qDebug() << "Starve detection threshold met, increasing buffer size to " << newOutputBufferSizeFrames; setOutputBufferSize(newOutputBufferSizeFrames); @@ -1015,7 +1008,7 @@ bool Audio::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDeviceInfo) // setup our general output device for audio-mixer audio _audioOutput = new QAudioOutput(outputDeviceInfo, _outputFormat, this); - _audioOutput->setBufferSize(_outputBufferSizeFrames * _outputFrameSize * sizeof(int16_t)); + _audioOutput->setBufferSize(_outputBufferSizeFrames.get() * _outputFrameSize * sizeof(int16_t)); connect(_audioOutput, &QAudioOutput::notify, this, &Audio::outputNotify); @@ -1038,9 +1031,9 @@ bool Audio::switchOutputToAudioDevice(const QAudioDeviceInfo& outputDeviceInfo) void Audio::setOutputBufferSize(int numFrames) { numFrames = std::min(std::max(numFrames, MIN_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES), MAX_AUDIO_OUTPUT_BUFFER_SIZE_FRAMES); - if (numFrames != _outputBufferSizeFrames) { + if (numFrames != _outputBufferSizeFrames.get()) { qDebug() << "Audio output buffer size (frames): " << numFrames; - _outputBufferSizeFrames = numFrames; + _outputBufferSizeFrames.set(numFrames); if (_audioOutput) { // The buffer size can't be adjusted after QAudioOutput::start() has been called, so @@ -1140,25 +1133,9 @@ void Audio::checkDevices() { void Audio::loadSettings() { _receivedAudioStream.loadSettings(); - - setOutputStarveDetectionEnabled(SettingHandles::audioOutputStarveDetectionEnabled.get()); - setOutputStarveDetectionThreshold(SettingHandles::audioOutputStarveDetectionThreshold.get()); - setOutputStarveDetectionPeriod(SettingHandles::audioOutputStarveDetectionPeriod.get()); - - if (QThread::currentThread() != thread()) { - QMetaObject::invokeMethod(this, "setOutputBufferSize", - Q_ARG(int, SettingHandles::audioOutputBufferSize.get())); - } else { - setOutputBufferSize(SettingHandles::audioOutputBufferSize.get()); - } } void Audio::saveSettings() { _receivedAudioStream.saveSettings(); - - SettingHandles::audioOutputStarveDetectionEnabled.set(getOutputStarveDetectionEnabled()); - SettingHandles::audioOutputStarveDetectionThreshold.set(getOutputStarveDetectionThreshold()); - SettingHandles::audioOutputStarveDetectionPeriod.set(getOutputStarveDetectionPeriod()); - SettingHandles::audioOutputBufferSize.set(getOutputBufferSize()); } diff --git a/interface/src/Audio.h b/interface/src/Audio.h index d815b3035c..9e98cbed63 100644 --- a/interface/src/Audio.h +++ b/interface/src/Audio.h @@ -115,16 +115,16 @@ public: void setRecorder(RecorderPointer recorder) { _recorder = recorder; } - int getOutputBufferSize() { return _outputBufferSizeFrames; } + int getOutputBufferSize() { return _outputBufferSizeFrames.get(); } - bool getOutputStarveDetectionEnabled() { return _outputStarveDetectionEnabled; } - void setOutputStarveDetectionEnabled(bool enabled) { _outputStarveDetectionEnabled = enabled; } + bool getOutputStarveDetectionEnabled() { return _outputStarveDetectionEnabled.get(); } + void setOutputStarveDetectionEnabled(bool enabled) { _outputStarveDetectionEnabled.set(enabled); } - int getOutputStarveDetectionPeriod() { return _outputStarveDetectionPeriodMsec; } - void setOutputStarveDetectionPeriod(int msecs) { _outputStarveDetectionPeriodMsec = msecs; } + int getOutputStarveDetectionPeriod() { return _outputStarveDetectionPeriodMsec.get(); } + void setOutputStarveDetectionPeriod(int msecs) { _outputStarveDetectionPeriodMsec.set(msecs); } - int getOutputStarveDetectionThreshold() { return _outputStarveDetectionThreshold; } - void setOutputStarveDetectionThreshold(int threshold) { _outputStarveDetectionThreshold = threshold; } + int getOutputStarveDetectionThreshold() { return _outputStarveDetectionThreshold.get(); } + void setOutputStarveDetectionThreshold(int threshold) { _outputStarveDetectionThreshold.set(threshold); } static const float CALLBACK_ACCELERATOR_RATIO; @@ -208,13 +208,14 @@ private: QString _inputAudioDeviceName; QString _outputAudioDeviceName; - int _outputBufferSizeFrames; - bool _outputStarveDetectionEnabled; quint64 _outputStarveDetectionStartTimeMsec; int _outputStarveDetectionCount; - int _outputStarveDetectionPeriodMsec; - int _outputStarveDetectionThreshold; // Maximum number of starves per _outputStarveDetectionPeriod before increasing buffer size - + + Setting::Handle _outputBufferSizeFrames; + Setting::Handle _outputStarveDetectionEnabled; + Setting::Handle _outputStarveDetectionPeriodMsec; + // Maximum number of starves per _outputStarveDetectionPeriod before increasing buffer size + Setting::Handle _outputStarveDetectionThreshold; StDev _stdev; QElapsedTimer _timeSinceLastReceived;