mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-21 21:05:50 +12:00
Add volume slider & mute audio settings
This commit is contained in:
parent
b251f84ab1
commit
3b6190b69a
7 changed files with 70 additions and 12 deletions
|
@ -1,10 +1,14 @@
|
|||
#include "audio/miniaudio_device.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
#include "helpers.hpp"
|
||||
|
||||
MiniAudioDevice::MiniAudioDevice() : initialized(false), running(false), samples(nullptr) {}
|
||||
MiniAudioDevice::MiniAudioDevice(AudioDeviceConfig& audioSettings)
|
||||
: initialized(false), running(false), samples(nullptr), audioSettings(audioSettings) {}
|
||||
|
||||
void MiniAudioDevice::init(Samples& samples, bool safe) {
|
||||
this->samples = &samples;
|
||||
|
@ -106,6 +110,40 @@ void MiniAudioDevice::init(Samples& samples, bool safe) {
|
|||
std::memcpy(&self->lastStereoSample[0], &output[(samplesWritten - 1) * 2], sizeof(lastStereoSample));
|
||||
}
|
||||
|
||||
// Adjust the volume of our samples based on the emulator's volume slider
|
||||
float audioVolume = self->audioSettings.getVolume();
|
||||
// If volume is 1.0 we don't need to do anything
|
||||
if (audioVolume != 1.0f) {
|
||||
s16* sample = output;
|
||||
|
||||
// If our volume is > 1.0 then we boost samples using a logarithmic scale,
|
||||
// In this case we also have to clamp samples to make sure they don't wrap around
|
||||
if (audioVolume > 1.0f) {
|
||||
audioVolume = 0.6 + 20 * std::log10(audioVolume);
|
||||
|
||||
constexpr s32 min = s32(std::numeric_limits<s16>::min());
|
||||
constexpr s32 max = s32(std::numeric_limits<s16>::max());
|
||||
|
||||
for (usize i = 0; i < samplesWritten; i += 2) {
|
||||
s16 l = s16(std::clamp<s32>(s32(float(sample[0]) * audioVolume), min, max));
|
||||
s16 r = s16(std::clamp<s32>(s32(float(sample[1]) * audioVolume), min, max));
|
||||
|
||||
*sample++ = l;
|
||||
*sample++ = r;
|
||||
}
|
||||
} 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
|
||||
for (usize i = 0; i < samplesWritten; i += 2) {
|
||||
s16 l = s16(float(sample[0]) * audioVolume);
|
||||
s16 r = s16(float(sample[1]) * audioVolume);
|
||||
|
||||
*sample++ = l;
|
||||
*sample++ = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If underruning, copy the last output sample
|
||||
{
|
||||
s16* pointer = &output[samplesWritten * 2];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue