Move Audio to new settings

This commit is contained in:
Atlante45 2015-02-02 20:20:57 -08:00
parent cc58cb0f03
commit fdade344a3
2 changed files with 29 additions and 51 deletions

View file

@ -38,7 +38,7 @@
#include <NodeList.h>
#include <PacketHeaders.h>
#include <PositionalAudioStream.h>
#include <Settings.h>
#include <SettingHandle.h>
#include <SharedUtil.h>
#include <UUID.h>
@ -47,17 +47,6 @@
static const int RECEIVED_AUDIO_STREAM_CAPACITY_FRAMES = 100;
namespace SettingHandles {
const SettingHandle<bool> audioOutputStarveDetectionEnabled("audioOutputStarveDetectionEnabled",
DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_ENABLED);
const SettingHandle<int> audioOutputStarveDetectionThreshold("audioOutputStarveDetectionThreshold",
DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_THRESHOLD);
const SettingHandle<int> audioOutputStarveDetectionPeriod("audioOutputStarveDetectionPeriod",
DEFAULT_AUDIO_OUTPUT_STARVE_DETECTION_PERIOD);
const SettingHandle<int> 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());
}

View file

@ -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<int> _outputBufferSizeFrames;
Setting::Handle<bool> _outputStarveDetectionEnabled;
Setting::Handle<int> _outputStarveDetectionPeriodMsec;
// Maximum number of starves per _outputStarveDetectionPeriod before increasing buffer size
Setting::Handle<int> _outputStarveDetectionThreshold;
StDev _stdev;
QElapsedTimer _timeSinceLastReceived;