Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat(audio): add setting to switch between new and old audio backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sudden6 committed Aug 5, 2017
1 parent 809c5e6 commit 9d0498e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 42 deletions.
18 changes: 16 additions & 2 deletions src/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "audio.h"
#include "src/audio/backend/openal.h"
#include "src/audio/backend/openal2.h"
#include "src/persistence/settings.h"

#include <QDebug>

Expand Down Expand Up @@ -169,6 +170,19 @@
*/
Audio& Audio::getInstance()
{
static OpenAL2 instance;
return instance;
static bool initialized = false;
static bool Backend2 = false;

if(!initialized) {
Backend2 = Settings::getInstance().getEnableBackend2();
initialized = true;
}

if(Backend2) {
static OpenAL2 instance;
return instance;
} else {
static OpenAL instance;
return instance;
}
}
22 changes: 13 additions & 9 deletions src/audio/backend/openal2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,29 +288,36 @@ bool OpenAL2::loadOpenALExtensions(ALCdevice* dev)
// load OpenAL extension functions
alcLoopbackOpenDeviceSOFT = reinterpret_cast<LPALCLOOPBACKOPENDEVICESOFT>
(alcGetProcAddress(dev, "alcLoopbackOpenDeviceSOFT"));

checkAlcError(dev);
if(!alcLoopbackOpenDeviceSOFT) {
qDebug() << "Failed to load alcLoopbackOpenDeviceSOFT function!";
return false;
}

alcIsRenderFormatSupportedSOFT = reinterpret_cast<LPALCISRENDERFORMATSUPPORTEDSOFT>
(alcGetProcAddress(dev, "alcIsRenderFormatSupportedSOFT"));

checkAlcError(dev);
if(!alcIsRenderFormatSupportedSOFT) {
qDebug() << "Failed to load alcIsRenderFormatSupportedSOFT function!";
return false;
}

alGetSourcedvSOFT = reinterpret_cast<LPALGETSOURCEDVSOFT>
(alcGetProcAddress(dev, "alGetSourcedvSOFT"));

checkAlcError(dev);
if(!alGetSourcedvSOFT) {
qDebug() << "Failed to load alGetSourcedvSOFT function!";
return false;
}

alcRenderSamplesSOFT = reinterpret_cast<LPALCRENDERSAMPLESSOFT>
(alcGetProcAddress(alOutDev, "alcRenderSamplesSOFT"));
checkAlcError(dev);
if(!alcRenderSamplesSOFT) {
qDebug() << "Failed to load alcRenderSamplesSOFT function!";
return false;
}

return true;
}

Expand Down Expand Up @@ -631,17 +638,14 @@ void OpenAL2::doOutput()
}

ALdouble latency[2] = {0};
if(alGetSourcedvSOFT) {
alGetSourcedvSOFT(alProxySource, AL_SEC_OFFSET_LATENCY_SOFT, latency);
checkAlError();
}
alGetSourcedvSOFT(alProxySource, AL_SEC_OFFSET_LATENCY_SOFT, latency);
checkAlError();
//qDebug() << "Playback latency: " << latency[1] << "offset: " << latency[0];

ALshort outBuf[AUDIO_FRAME_SAMPLE_COUNT] = {0};
alcMakeContextCurrent(alProxyContext);
LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT =
reinterpret_cast<LPALCRENDERSAMPLESSOFT> (alcGetProcAddress(alOutDev, "alcRenderSamplesSOFT"));
alcRenderSamplesSOFT(alProxyDev, outBuf, AUDIO_FRAME_SAMPLE_COUNT);
checkAlcError(alProxyDev);

alcMakeContextCurrent(alOutContext);
alBufferData(bufids[0], AL_FORMAT_MONO16, outBuf,
Expand Down
1 change: 1 addition & 0 deletions src/audio/backend/openal2.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class OpenAL2 : public Audio
LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT = nullptr;
LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT = nullptr;
LPALGETSOURCEDVSOFT alGetSourcedvSOFT = nullptr;
LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT = nullptr;
};

#endif // OPENAL2_H
21 changes: 21 additions & 0 deletions src/persistence/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ void Settings::loadGlobal()
audioOutDevEnabled = s.value("audioOutDevEnabled", true).toBool();
audioInGainDecibel = s.value("inGain", 0).toReal();
outVolume = s.value("outVolume", 100).toInt();
enableBackend2 = false;
#ifdef USE_FILTERAUDIO
enableBackend2 = s.value("enableBackend2", false).toBool();
#endif
}
s.endGroup();

Expand Down Expand Up @@ -561,6 +565,7 @@ void Settings::saveGlobal()
s.setValue("audioOutDevEnabled", audioOutDevEnabled);
s.setValue("inGain", audioInGainDecibel);
s.setValue("outVolume", outVolume);
s.setValue("enableBackend2", enableBackend2);
}
s.endGroup();

Expand Down Expand Up @@ -1840,6 +1845,22 @@ void Settings::setOutVolume(int volume)
}
}

bool Settings::getEnableBackend2() const
{
QMutexLocker locker{&bigLock};
return enableBackend2;
}

void Settings::setEnableBackend2(bool enabled)
{
QMutexLocker locker{&bigLock};

if (enabled != enableBackend2) {
enableBackend2 = enabled;
emit enableBackend2Changed(enabled);
}
}

QRect Settings::getScreenRegion() const
{
QMutexLocker locker(&bigLock);
Expand Down
6 changes: 6 additions & 0 deletions src/persistence/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class Settings : public QObject
Q_PROPERTY(bool audioOutDevEnabled READ getAudioOutDevEnabled WRITE setAudioOutDevEnabled NOTIFY
audioOutDevEnabledChanged FINAL)
Q_PROPERTY(int outVolume READ getOutVolume WRITE setOutVolume NOTIFY outVolumeChanged FINAL)
Q_PROPERTY(bool enableBackend2 READ getEnableBackend2 WRITE setEnableBackend2 NOTIFY enableBackend2Changed FINAL)

// Video
Q_PROPERTY(QString videoDev READ getVideoDev WRITE setVideoDev NOTIFY videoDevChanged FINAL)
Expand Down Expand Up @@ -234,6 +235,7 @@ public slots:
void audioOutDevEnabledChanged(bool enabled);
void outVolumeChanged(int volume);
void enableTestSoundChanged(bool enabled);
void enableBackend2Changed(bool enabled);

// Video
void videoDevChanged(const QString& name);
Expand Down Expand Up @@ -363,6 +365,9 @@ public slots:
bool getEnableTestSound() const;
void setEnableTestSound(bool newValue);

bool getEnableBackend2() const;
void setEnableBackend2(bool enabled);

QString getVideoDev() const;
void setVideoDev(const QString& deviceSpecifier);

Expand Down Expand Up @@ -617,6 +622,7 @@ public slots:
bool audioOutDevEnabled;
int outVolume;
bool enableTestSound;
bool enableBackend2;

// Video
QString videoDev;
Expand Down
9 changes: 9 additions & 0 deletions src/widget/form/settings/avform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ AVForm::AVForm()

cbEnableTestSound->setToolTip(tr("Play a test sound while changing the output volume."));

cbEnableBackend2->setChecked(s.getEnableBackend2());

connect(rescanButton, &QPushButton::clicked, this, &AVForm::rescanDevices);

playbackSlider->setTracking(false);
Expand Down Expand Up @@ -141,6 +143,11 @@ void AVForm::rescanDevices()
getVideoDevices();
}

void AVForm::on_cbEnableBackend2_stateChanged()
{
Settings::getInstance().setEnableBackend2(cbEnableBackend2->isChecked());
}

void AVForm::on_videoModescomboBox_currentIndexChanged(int index)
{
assert(0 <= index && index < videoModes.size());
Expand Down Expand Up @@ -552,3 +559,5 @@ void AVForm::retranslateUi()
{
Ui::AVForm::retranslateUi(this);
}


2 changes: 2 additions & 0 deletions src/widget/form/settings/avform.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ private slots:

void rescanDevices();

void on_cbEnableBackend2_stateChanged();

protected:
void updateVideoModes(int curIndex);

Expand Down
75 changes: 44 additions & 31 deletions src/widget/form/settings/avform.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>824</width>
<height>489</height>
<width>830</width>
<height>495</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
Expand All @@ -44,30 +44,20 @@
<property name="bottomMargin">
<number>6</number>
</property>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="outDevCombobox"/>
</item>
<item row="3" column="1" colspan="2">
<widget class="QSlider" name="microphoneSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="microphoneLabel">
<item row="2" column="0">
<widget class="QLabel" name="inDevLabel">
<property name="text">
<string>Gain</string>
<string>Capture device</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QComboBox" name="inDevCombobox"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="outDevLabel">
<item row="1" column="2">
<widget class="QCheckBox" name="cbEnableTestSound">
<property name="text">
<string>Playback device</string>
<string>Test Sound</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
Expand All @@ -84,12 +74,11 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="inDevLabel">
<property name="text">
<string>Capture device</string>
</property>
</widget>
<item row="2" column="1" colspan="2">
<widget class="QComboBox" name="inDevCombobox"/>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="outDevCombobox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="playbackLabel">
Expand All @@ -98,14 +87,38 @@
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="cbEnableTestSound">
<item row="3" column="0">
<widget class="QLabel" name="microphoneLabel">
<property name="text">
<string>Test Sound</string>
<string>Gain</string>
</property>
<property name="checked">
</widget>
</item>
<item row="3" column="1" colspan="2">
<widget class="QSlider" name="microphoneSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="outDevLabel">
<property name="text">
<string>Playback device</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="cbEnableBackend2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="toolTip">
<string>Enables the experimental audio backend with echo cancelling support, needs qTox restart to take effect.</string>
</property>
<property name="text">
<string>Enable experimental audio backend</string>
</property>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 9d0498e

Please sign in to comment.