Skip to content

Commit

Permalink
Provide a setting for system default input
Browse files Browse the repository at this point in the history
Provide the setting "[System Default]" which instructs the SDL driver to
use the default device of the system as the input device. In the
configuration file this option is represented as an empty string. This
should play well with the current existing configuration of the users.
  • Loading branch information
michaelgregorius committed Jun 1, 2024
1 parent 33139b9 commit 29c43c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/AudioSdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class AudioSdl : public AudioDevice
private:
QLineEdit * m_device;
QComboBox* m_inputDeviceComboBox = nullptr;

static QString s_defaultInputDevice;
} ;


Expand Down
27 changes: 22 additions & 5 deletions src/core/audio/AudioSdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ AudioSdl::AudioSdl( bool & _success_ful, AudioEngine* _audioEngine ) :
m_inputAudioHandle.callback = sdlInputAudioCallback;

const QString inputDevice = ConfigManager::inst()->value("audiosdl", "inputdevice");
const bool isDefaultInput = inputDevice.isEmpty();

// Try with the configured device
const auto inputDeviceCStr = inputDevice.toLocal8Bit().data();
const auto inputDeviceCStr = isDefaultInput ? nullptr : inputDevice.toLocal8Bit().data();
m_inputDevice = SDL_OpenAudioDevice (inputDeviceCStr, 1, &m_inputAudioHandle, &actual, 0);

// If we did not get a device ID try again with the default device
if (m_inputDevice == 0)
// If we did not get a device ID try again with the default device if we did not try that before
if (m_inputDevice == 0 && !isDefaultInput)
{
m_inputDevice = SDL_OpenAudioDevice(nullptr, 1, &m_inputAudioHandle, &actual, 0);
}
Expand Down Expand Up @@ -290,6 +291,8 @@ void AudioSdl::sdlInputAudioCallback(Uint8 *_buf, int _len) {

#endif

QString AudioSdl::setupWidget::s_defaultInputDevice = QObject::tr("[System Default]");

AudioSdl::setupWidget::setupWidget( QWidget * _parent ) :
AudioDeviceSetupWidget( AudioSdl::name(), _parent )
{
Expand All @@ -303,6 +306,8 @@ AudioSdl::setupWidget::setupWidget( QWidget * _parent ) :
m_inputDeviceComboBox = new QComboBox(this);

#ifdef LMMS_HAVE_SDL2
m_inputDeviceComboBox->addItem(s_defaultInputDevice);

const int numberOfInputDevices = SDL_GetNumAudioDevices(1);
for (int i = 0; i < numberOfInputDevices; ++i)
{
Expand All @@ -312,7 +317,14 @@ AudioSdl::setupWidget::setupWidget( QWidget * _parent ) :

// Set the current device to the one in the configuration
const auto inputDevice = ConfigManager::inst()->value("audiosdl", "inputdevice");
m_inputDeviceComboBox->setCurrentText(inputDevice);
if (inputDevice.isEmpty())
{
m_inputDeviceComboBox->setCurrentText(s_defaultInputDevice);
}
else
{
m_inputDeviceComboBox->setCurrentText(inputDevice);
}
#endif

form->addRow(tr("Input device"), m_inputDeviceComboBox);
Expand All @@ -327,7 +339,12 @@ void AudioSdl::setupWidget::saveSettings()
m_device->text() );

const auto currentInputDevice = m_inputDeviceComboBox->currentText();
if (!currentInputDevice.isEmpty())
if (currentInputDevice == s_defaultInputDevice)
{
// Represent the default input device with an empty string
ConfigManager::inst()->setValue("audiosdl", "inputdevice", "");
}
else if (!currentInputDevice.isEmpty())
{
ConfigManager::inst()->setValue("audiosdl", "inputdevice", currentInputDevice);
}
Expand Down

0 comments on commit 29c43c2

Please sign in to comment.