Skip to content

Commit

Permalink
Qt: Add default option for cubeb driver
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed May 5, 2024
1 parent fea00d0 commit d6e80de
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
19 changes: 15 additions & 4 deletions src/duckstation-qt/audiosettingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ AudioBackend AudioSettingsWidget::getEffectiveBackend() const
void AudioSettingsWidget::updateDriverNames()
{
const AudioBackend backend = getEffectiveBackend();
const std::vector<std::string> names = AudioStream::GetDriverNames(backend);
const std::vector<std::pair<std::string, std::string>> names = AudioStream::GetDriverNames(backend);

m_ui.driver->disconnect();
m_ui.driver->clear();
Expand All @@ -193,11 +193,11 @@ void AudioSettingsWidget::updateDriverNames()
else
{
m_ui.driver->setEnabled(true);
for (const std::string& name : names)
m_ui.driver->addItem(QString::fromStdString(name));
for (const auto& [name, display_name] : names)
m_ui.driver->addItem(QString::fromStdString(display_name), QString::fromStdString(name));

SettingWidgetBinder::BindWidgetToStringSetting(m_dialog->getSettingsInterface(), m_ui.driver, "Audio", "Driver",
std::move(names.front()));
std::move(names.front().first));
connect(m_ui.driver, &QComboBox::currentIndexChanged, this, &AudioSettingsWidget::updateDeviceNames);
}

Expand All @@ -223,11 +223,22 @@ void AudioSettingsWidget::updateDeviceNames()
else
{
m_ui.outputDevice->setEnabled(true);

bool is_known_device = false;
for (const AudioStream::DeviceInfo& di : devices)
{
m_ui.outputDevice->addItem(QString::fromStdString(di.display_name), QString::fromStdString(di.name));
if (di.name == current_device)
{
m_output_device_latency = di.minimum_latency_frames;
is_known_device = true;
}
}

if (!is_known_device)
{
m_ui.outputDevice->addItem(tr("Unknown Device \"%1\"").arg(QString::fromStdString(current_device)),
QString::fromStdString(current_device));
}

SettingWidgetBinder::BindWidgetToStringSetting(m_dialog->getSettingsInterface(), m_ui.outputDevice, "Audio",
Expand Down
4 changes: 2 additions & 2 deletions src/duckstation-qt/audiosettingswidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
<item>
<widget class="QToolButton" name="resetVolume">
<property name="toolTip">
<string>Stretch Settings</string>
<string>Reset Volume</string>
</property>
<property name="icon">
<iconset theme="refresh-line"/>
Expand Down Expand Up @@ -298,7 +298,7 @@
<item>
<widget class="QToolButton" name="resetFastForwardVolume">
<property name="toolTip">
<string>Stretch Settings</string>
<string>Reset Fast Forward Volume</string>
</property>
<property name="icon">
<iconset theme="refresh-line"/>
Expand Down
20 changes: 14 additions & 6 deletions src/util/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ std::unique_ptr<AudioStream> AudioStream::CreateNullStream(u32 sample_rate, u32
return stream;
}

#ifndef __ANDROID__

std::vector<std::string> AudioStream::GetDriverNames(AudioBackend backend)
std::vector<std::pair<std::string, std::string>> AudioStream::GetDriverNames(AudioBackend backend)
{
std::vector<std::string> ret;
std::vector<std::pair<std::string, std::string>> ret;
switch (backend)
{
#ifndef __ANDROID__
case AudioBackend::Cubeb:
ret = GetCubebDriverNames();
break;
#endif

default:
break;
Expand All @@ -93,9 +93,11 @@ std::vector<AudioStream::DeviceInfo> AudioStream::GetOutputDevices(AudioBackend
std::vector<AudioStream::DeviceInfo> ret;
switch (backend)
{
#ifndef __ANDROID__
case AudioBackend::Cubeb:
ret = GetCubebOutputDevices(driver);
break;
#endif

default:
break;
Expand All @@ -110,11 +112,19 @@ std::unique_ptr<AudioStream> AudioStream::CreateStream(AudioBackend backend, u32
{
switch (backend)
{
#ifndef __ANDROID__
case AudioBackend::Cubeb:
return CreateCubebAudioStream(sample_rate, parameters, driver_name, device_name, error);

case AudioBackend::SDL:
return CreateSDLAudioStream(sample_rate, parameters, error);
#else
case AudioBackend::AAudio:
return CreateAAudioAudioStream(sample_rate, parameters, error);

case AudioBackend::OpenSLES:
return CreateOpenSLESAudioStream(sample_rate, parameters, error);
#endif

case AudioBackend::Null:
return CreateNullStream(sample_rate, parameters.buffer_ms);
Expand All @@ -125,8 +135,6 @@ std::unique_ptr<AudioStream> AudioStream::CreateStream(AudioBackend backend, u32
}
}

#endif

u32 AudioStream::GetAlignedBufferSize(u32 size)
{
static_assert(Common::IsPow2(CHUNK_SIZE));
Expand Down
9 changes: 7 additions & 2 deletions src/util/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class AudioStream

void SetStretchMode(AudioStretchMode mode);

static std::vector<std::string> GetDriverNames(AudioBackend backend);
static std::vector<std::pair<std::string, std::string>> GetDriverNames(AudioBackend backend);
static std::vector<DeviceInfo> GetOutputDevices(AudioBackend backend, const char* driver);
static std::unique_ptr<AudioStream> CreateStream(AudioBackend backend, u32 sample_rate,
const AudioStreamParameters& parameters, const char* driver_name,
Expand Down Expand Up @@ -241,13 +241,18 @@ class AudioStream
static constexpr u32 TARGET_IPS = 691;

#ifndef __ANDROID__
static std::vector<std::string> GetCubebDriverNames();
static std::vector<std::pair<std::string, std::string>> GetCubebDriverNames();
static std::vector<DeviceInfo> GetCubebOutputDevices(const char* driver);
static std::unique_ptr<AudioStream> CreateCubebAudioStream(u32 sample_rate, const AudioStreamParameters& parameters,
const char* driver_name, const char* device_name,
Error* error);
static std::unique_ptr<AudioStream> CreateSDLAudioStream(u32 sample_rate, const AudioStreamParameters& parameters,
Error* error);
#else
static std::unique_ptr<AudioStream> CreateAAudioAudioStream(u32 sample_rate, const AudioStreamParameters& parameters,
Error* error);
static std::unique_ptr<AudioStream> CreateOpenSLESAudioStream(u32 sample_rate,
const AudioStreamParameters& parameters, Error* error);
#endif

ALWAYS_INLINE bool IsExpansionEnabled() const { return m_parameters.expansion_mode != AudioExpansionMode::Disabled; }
Expand Down
10 changes: 6 additions & 4 deletions src/util/cubeb_audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,21 @@ std::unique_ptr<AudioStream> AudioStream::CreateCubebAudioStream(u32 sample_rate
return stream;
}

std::vector<std::string> AudioStream::GetCubebDriverNames()
std::vector<std::pair<std::string, std::string>> AudioStream::GetCubebDriverNames()
{
std::vector<std::string> names;
std::vector<std::pair<std::string, std::string>> names;
names.emplace_back(std::string(), TRANSLATE_STR("AudioStream", "Default"));

const char** cubeb_names = cubeb_get_backend_names();
for (u32 i = 0; cubeb_names[i] != nullptr; i++)
names.emplace_back(cubeb_names[i]);
names.emplace_back(cubeb_names[i], cubeb_names[i]);
return names;
}

std::vector<AudioStream::DeviceInfo> AudioStream::GetCubebOutputDevices(const char* driver)
{
std::vector<AudioStream::DeviceInfo> ret;
ret.emplace_back(std::string(), TRANSLATE_STR("AudioStream", "Default Output Device"), 0);
ret.emplace_back(std::string(), TRANSLATE_STR("AudioStream", "Default"), 0);

cubeb* context;
int rv = cubeb_init(&context, "DuckStation", (driver && *driver) ? driver : nullptr);
Expand Down

0 comments on commit d6e80de

Please sign in to comment.