Skip to content

Commit

Permalink
Add volume curve setting (#658)
Browse files Browse the repository at this point in the history
* Add volume curve setting

* Qt: Add missing translations
  • Loading branch information
wheremyfoodat authored Dec 3, 2024
1 parent 545ac81 commit 5f822b3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
14 changes: 13 additions & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#pragma once
#include <filesystem>
#include <string>

#include "audio/dsp_core.hpp"
#include "renderer.hpp"
#include "frontend_settings.hpp"
#include "renderer.hpp"

struct AudioDeviceConfig {
// Audio curve to use for volumes between 0-100
enum class VolumeCurve : int {
Cubic = 0, // Samples are scaled by volume ^ 3
Linear = 1, // Samples are scaled by volume
};

float volumeRaw = 1.0f;
VolumeCurve volumeCurve = VolumeCurve::Cubic;

bool muteAudio = false;

float getVolume() const {
Expand All @@ -16,6 +25,9 @@ struct AudioDeviceConfig {

return volumeRaw;
}

static VolumeCurve volumeCurveFromString(std::string inString);
static const char* volumeCurveToString(VolumeCurve curve);
};

// Remember to initialize every field here to its default value otherwise bad things will happen
Expand Down
27 changes: 27 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "config.hpp"

#include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <map>
Expand Down Expand Up @@ -105,6 +107,7 @@ void EmulatorConfig::load() {
// Our volume ranges from 0.0 (muted) to 2.0 (boosted, using a logarithmic scale). 1.0 is the "default" volume, ie we don't adjust the PCM
// samples at all.
audioDeviceConfig.volumeRaw = float(std::clamp(toml::find_or<toml::floating>(audio, "AudioVolume", 1.0), 0.0, 2.0));
audioDeviceConfig.volumeCurve = AudioDeviceConfig::volumeCurveFromString(toml::find_or<std::string>(audio, "VolumeCurve", "cubic"));
}
}

Expand Down Expand Up @@ -188,6 +191,7 @@ void EmulatorConfig::save() {
data["Audio"]["EnableAACAudio"] = aacEnabled;
data["Audio"]["MuteAudio"] = audioDeviceConfig.muteAudio;
data["Audio"]["AudioVolume"] = double(audioDeviceConfig.volumeRaw);
data["Audio"]["VolumeCurve"] = std::string(AudioDeviceConfig::volumeCurveToString(audioDeviceConfig.volumeCurve));
data["Audio"]["PrintDSPFirmware"] = printDSPFirmware;

data["Battery"]["ChargerPlugged"] = chargerPlugged;
Expand All @@ -203,3 +207,26 @@ void EmulatorConfig::save() {
file << data;
file.close();
}

AudioDeviceConfig::VolumeCurve AudioDeviceConfig::volumeCurveFromString(std::string inString) {
// Transform to lower-case to make the setting case-insensitive
std::transform(inString.begin(), inString.end(), inString.begin(), [](unsigned char c) { return std::tolower(c); });

if (inString == "cubic") {
return VolumeCurve::Cubic;
} else if (inString == "linear") {
return VolumeCurve::Linear;
}

// Default to cubic curve
return VolumeCurve::Cubic;
}

const char* AudioDeviceConfig::volumeCurveToString(AudioDeviceConfig::VolumeCurve curve) {
switch (curve) {
case VolumeCurve::Linear: return "linear";

case VolumeCurve::Cubic:
default: return "cubic";
}
}
6 changes: 6 additions & 0 deletions src/core/audio/miniaudio_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ void MiniAudioDevice::init(Samples& samples, bool safe) {
} else {
// If our volume is in [0.0, 1.0) then just multiply by the volume. No need to clamp, since there is no danger of our samples wrapping
// around due to overflow

// If we're applying cubic volume curve, raise volume to the power of 3
if (self->audioSettings.volumeCurve == AudioDeviceConfig::VolumeCurve::Cubic) {
audioVolume = audioVolume * audioVolume * audioVolume;
}

for (usize i = 0; i < samplesWritten; i += 2) {
s16 l = s16(float(sample[0]) * audioVolume);
s16 r = s16(float(sample[1]) * audioVolume);
Expand Down
4 changes: 2 additions & 2 deletions src/panda_qt/cheats_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void CheatEntryWidget::editClicked() {
}

CheatEditDialog::CheatEditDialog(Emulator* emu, CheatEntryWidget& cheatEntry) : QDialog(), emu(emu), cheatEntry(cheatEntry) {
setWindowTitle("Edit Cheat");
setWindowTitle(tr("Edit Cheat"));

setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
Expand Down Expand Up @@ -161,7 +161,7 @@ void CheatEditDialog::rejected() {

CheatsWindow::CheatsWindow(Emulator* emu, const std::filesystem::path& cheatPath, QWidget* parent)
: QWidget(parent, Qt::Window), emu(emu), cheatPath(cheatPath) {
setWindowTitle("Cheats");
setWindowTitle(tr("Cheats"));
mainWindow = static_cast<MainWindow*>(parent);

QVBoxLayout* layout = new QVBoxLayout;
Expand Down
10 changes: 10 additions & 0 deletions src/panda_qt/config_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ ConfigWindow::ConfigWindow(ConfigCallback configCallback, MainWindowCallback win
connectCheckbox(muteAudio, config.audioDeviceConfig.muteAudio);
audioLayout->addRow(muteAudio);

QComboBox* volumeCurveType = new QComboBox;
volumeCurveType->addItem(tr("Cubic"));
volumeCurveType->addItem(tr("Linear"));
volumeCurveType->setCurrentIndex(static_cast<int>(config.audioDeviceConfig.volumeCurve));
connect(volumeCurveType, &QComboBox::currentIndexChanged, this, [&](int index) {
config.audioDeviceConfig.volumeCurve = static_cast<AudioDeviceConfig::VolumeCurve>(index);
updateConfig();
});
audioLayout->addRow(tr("Volume curve"), volumeCurveType);

QSpinBox* volumeRaw = new QSpinBox();
volumeRaw->setRange(0, 200);
volumeRaw->setValue(config.audioDeviceConfig.volumeRaw * 100);
Expand Down
2 changes: 1 addition & 1 deletion src/panda_qt/patch_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "io_file.hpp"

PatchWindow::PatchWindow(QWidget* parent) : QWidget(parent, Qt::Window) {
setWindowTitle("ROM patcher");
setWindowTitle(tr("ROM patcher"));

QVBoxLayout* layout = new QVBoxLayout;
layout->setContentsMargins(6, 6, 6, 6);
Expand Down
2 changes: 1 addition & 1 deletion src/panda_qt/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace Zep;

TextEditorWindow::TextEditorWindow(QWidget* parent, const std::string& filename, const std::string& initialText)
: QDialog(parent), zepWidget(this, qApp->applicationDirPath().toStdString(), fontSize) {
setWindowTitle("Lua Editor");
setWindowTitle(tr("Lua Editor"));
resize(600, 600);

// Register our extensions
Expand Down

0 comments on commit 5f822b3

Please sign in to comment.