1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(audio): cleanup initialization of audio device lists

API:
* open default in/out device, when device name is empty
* move privatized code
* fix unsigned/signed conversion warnings

UI (settings):
* refactor: select audio devices by combobox index
This commit is contained in:
Nils Fenner 2016-06-04 17:02:41 +02:00
parent bec635ad11
commit 81df534c9a
No known key found for this signature in database
GPG Key ID: 9591A163FF9BE04C
4 changed files with 43 additions and 51 deletions

View File

@ -318,13 +318,12 @@ bool Audio::autoInitOutput()
return alOutDev ? true : initOutput(Settings::getInstance().getOutDev());
}
bool Audio::initInput(QString inDevDescr)
bool Audio::initInput(const QString& deviceName)
{
qDebug() << "Opening audio input" << inDevDescr;
if (inDevDescr == "none")
return true;
if (deviceName.toLower() == QStringLiteral("none"))
return false;
qDebug() << "Opening audio input" << deviceName;
assert(!alInDev);
/// TODO: Try to actually detect if our audio source is stereo
@ -333,27 +332,22 @@ bool Audio::initInput(QString inDevDescr)
const uint16_t frameDuration = AUDIO_FRAME_DURATION;
const uint32_t chnls = AUDIO_CHANNELS;
const ALCsizei bufSize = (frameDuration * sampleRate * 4) / 1000 * chnls;
if (inDevDescr.isEmpty())
{
const ALchar *pDeviceList = Private::inDeviceNames();
if (pDeviceList)
inDevDescr = QString::fromUtf8(pDeviceList, strlen(pDeviceList));
}
if (!inDevDescr.isEmpty())
alInDev = alcCaptureOpenDevice(inDevDescr.toUtf8().constData(),
sampleRate, stereoFlag, bufSize);
const ALchar* tmpDevName = deviceName.isEmpty()
? nullptr
: deviceName.toUtf8().constData();
alInDev = alcCaptureOpenDevice(tmpDevName, sampleRate, stereoFlag, bufSize);
// Restart the capture if necessary
if (!alInDev)
{
qWarning() << "Failed to initialize audio input device:" << inDevDescr;
qWarning() << "Failed to initialize audio input device:" << deviceName;
return false;
}
d->setInputGain(Settings::getInstance().getAudioInGain());
qDebug() << "Opened audio input" << inDevDescr;
qDebug() << "Opened audio input" << deviceName;
alcCaptureStart(alInDev);
return true;
@ -364,35 +358,29 @@ bool Audio::initInput(QString inDevDescr)
Open an audio output device
*/
bool Audio::initOutput(QString outDevDescr)
bool Audio::initOutput(const QString& deviceName)
{
qDebug() << "Opening audio output" << outDevDescr;
outSources.clear();
outputInitialized = false;
if (outDevDescr == "none")
if (deviceName.toLower() == QStringLiteral("none"))
return false;
qDebug() << "Opening audio output" << deviceName;
assert(!alOutDev);
if (outDevDescr.isEmpty())
{
// default to the first available audio device.
const ALchar *pDeviceList = Private::outDeviceNames();
if (pDeviceList)
outDevDescr = QString::fromUtf8(pDeviceList, strlen(pDeviceList));
}
if (!outDevDescr.isEmpty())
alOutDev = alcOpenDevice(outDevDescr.toUtf8().constData());
const ALchar* tmpDevName = deviceName.isEmpty()
? nullptr
: deviceName.toUtf8().constData();
alOutDev = alcOpenDevice(tmpDevName);
if (!alOutDev)
{
qWarning() << "Cannot open output audio device" << outDevDescr;
qWarning() << "Cannot open output audio device" << deviceName;
return false;
}
qDebug() << "Opened audio output" << outDevDescr;
qDebug() << "Opened audio output" << deviceName;
alOutContext = alcCreateContext(alOutDev, nullptr);
checkAlcError(alOutDev);

View File

@ -104,8 +104,8 @@ private:
bool autoInitInput();
bool autoInitOutput();
bool initInput(QString inDevDescr);
bool initOutput(QString outDevDescr);
bool initInput(const QString& deviceName);
bool initOutput(const QString& outDevDescr);
void cleanupInput();
void cleanupOutput();
/// Called after a mono16 sound stopped playing

View File

@ -50,10 +50,10 @@ AVForm::AVForm() :
bodyUI->btnPlayTestSound->setToolTip(
tr("Play a test sound while changing the output volume."));
auto qcbxIndexChangedStr = (void(QComboBox::*)(const QString&)) &QComboBox::currentIndexChanged;
auto qcbxIndexChangedInt = (void(QComboBox::*)(int)) &QComboBox::currentIndexChanged;
connect(bodyUI->inDevCombobox, qcbxIndexChangedStr, this, &AVForm::onInDevChanged);
connect(bodyUI->outDevCombobox, qcbxIndexChangedStr, this, &AVForm::onOutDevChanged);
auto qcbxIndexChangedInt = static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged);
connect(bodyUI->inDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioInDevChanged);
connect(bodyUI->outDevCombobox, qcbxIndexChangedInt, this, &AVForm::onAudioOutDevChanged);
connect(bodyUI->videoDevCombobox, qcbxIndexChangedInt, this, &AVForm::onVideoDevChanged);
connect(bodyUI->videoModescomboBox, qcbxIndexChangedInt, this, &AVForm::onVideoModesIndexChanged);
connect(bodyUI->rescanButton, &QPushButton::clicked, this, [=]()
@ -370,27 +370,31 @@ void AVForm::getAudioOutDevices()
bodyUI->outDevCombobox->setCurrentIndex(idx > 0 ? idx : 0);
}
void AVForm::onInDevChanged(QString deviceDescriptor)
void AVForm::onAudioInDevChanged(int deviceIndex)
{
if (!bodyUI->inDevCombobox->currentIndex())
deviceDescriptor = "none";
QString deviceName = deviceIndex > 0
? bodyUI->inDevCombobox->itemText(deviceIndex)
: QStringLiteral("none");
Settings::getInstance().setInDev(deviceName);
Settings::getInstance().setInDev(deviceDescriptor);
Audio& audio = Audio::getInstance();
audio.reinitInput(deviceDescriptor);
bodyUI->microphoneSlider->setEnabled(bodyUI->inDevCombobox->currentIndex() != 0);
audio.reinitInput(deviceName);
bodyUI->microphoneSlider->setEnabled(deviceIndex > 0);
bodyUI->microphoneSlider->setSliderPosition(qRound(audio.inputGain() * 10.0));
}
void AVForm::onOutDevChanged(QString deviceDescriptor)
void AVForm::onAudioOutDevChanged(int deviceIndex)
{
if (!bodyUI->outDevCombobox->currentIndex())
deviceDescriptor = "none";
QString deviceName = deviceIndex > 0
? bodyUI->outDevCombobox->itemText(deviceIndex)
: QStringLiteral("none");
Settings::getInstance().setOutDev(deviceName);
Settings::getInstance().setOutDev(deviceDescriptor);
Audio& audio = Audio::getInstance();
audio.reinitOutput(deviceDescriptor);
bodyUI->playbackSlider->setEnabled(audio.isOutputReady());
audio.reinitOutput(deviceName);
bodyUI->playbackSlider->setEnabled(deviceIndex > 0);
bodyUI->playbackSlider->setSliderPosition(qRound(audio.outputVolume() * 100.0));
}

View File

@ -54,8 +54,8 @@ private:
private slots:
// audio
void onInDevChanged(QString deviceDescriptor);
void onOutDevChanged(QString deviceDescriptor);
void onAudioInDevChanged(int deviceIndex);
void onAudioOutDevChanged(int deviceIndex);
void onPlaybackValueChanged(int value);
void onMicrophoneValueChanged(int value);