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

View File

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

View File

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

View File

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