mirror of
https://git.eden-emu.dev/eden-emu/eden
synced 2026-10-20 18:59:26 +02:00
[audio] Fix BOTW by increasing ring-size (#2822)
Signed-off-by: lizzie <lizzie@eden-emu.dev> Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/2822 Reviewed-by: MaranBr <maranbr@eden-emu.dev> Reviewed-by: Maufeat <sahyno1996@gmail.com> Co-authored-by: lizzie <lizzie@eden-emu.dev> Co-committed-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
parent
b5d54b8df7
commit
4834fec159
2 changed files with 60 additions and 115 deletions
|
|
@ -22,110 +22,61 @@
|
||||||
namespace AudioCore::Sink {
|
namespace AudioCore::Sink {
|
||||||
|
|
||||||
void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span<s16> samples) {
|
void SinkStream::AppendBuffer(SinkBuffer& buffer, std::span<s16> samples) {
|
||||||
SCOPE_EXIT {
|
if (type == StreamType::In)
|
||||||
queue.EmplaceWait(buffer);
|
|
||||||
++queued_buffers;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (type == StreamType::In) {
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
constexpr s32 min{(std::numeric_limits<s16>::min)()};
|
constexpr s32 min = (std::numeric_limits<s16>::min)();
|
||||||
constexpr s32 max{(std::numeric_limits<s16>::max)()};
|
constexpr s32 max = (std::numeric_limits<s16>::max)();
|
||||||
|
auto yuzu_volume = Settings::Volume();
|
||||||
auto yuzu_volume{Settings::Volume()};
|
if (yuzu_volume > 1.0f)
|
||||||
if (yuzu_volume > 1.0f) {
|
yuzu_volume = 0.6f + 20.0f * std::log10(yuzu_volume);
|
||||||
yuzu_volume = 0.6f + 20 * std::log10(yuzu_volume);
|
auto const volume = system_volume * device_volume * yuzu_volume;
|
||||||
}
|
if (system_channels > device_channels) {
|
||||||
auto volume{system_volume * device_volume * yuzu_volume};
|
// "Topological" coefficients, basically makes back sounds be less noisy :)
|
||||||
|
// Front = 1.0; Center = 0.596; LFE = 0.354; Back = 0.707
|
||||||
if (system_channels == 6 && device_channels == 2) {
|
static constexpr std::array<f32, 4> tcoeff{1.0f, 0.596f, 0.354f, 0.707f};
|
||||||
// We're given 6 channels, but our device only outputs 2, so downmix.
|
// We're given 6 channels, but our device only outputs 2, so downmix.
|
||||||
// Front = 1.0
|
for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); r_offs += system_channels, w_offs += device_channels) {
|
||||||
// Center = 0.596
|
std::array<f32, 6> ccoeff{0.f};
|
||||||
// LFE = 0.354
|
for (u32 i = 0; i < system_channels; ++i)
|
||||||
// Back = 0.707
|
ccoeff[i] = f32(samples[r_offs + i]);
|
||||||
static constexpr std::array<f32, 4> down_mix_coeff{1.0f, 0.596f, 0.354f, 0.707f};
|
std::array<f32, 6> rcoeff{
|
||||||
|
ccoeff[u32(Channels::FrontLeft)],
|
||||||
for (u32 read_index = 0, write_index = 0; read_index < samples.size();
|
ccoeff[u32(Channels::BackLeft)],
|
||||||
read_index += system_channels, write_index += device_channels) {
|
ccoeff[u32(Channels::Center)],
|
||||||
const auto fl =
|
ccoeff[u32(Channels::LFE)],
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontLeft)]);
|
ccoeff[u32(Channels::BackRight)],
|
||||||
const auto fr =
|
ccoeff[u32(Channels::FrontRight)],
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontRight)]);
|
};
|
||||||
const auto c =
|
std::array<f32, 6> scoeff{
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::Center)]);
|
rcoeff[0] * tcoeff[0] + rcoeff[2] * tcoeff[1] + rcoeff[3] * tcoeff[2] + rcoeff[1] * tcoeff[3],
|
||||||
const auto lfe =
|
rcoeff[5] * tcoeff[0] + rcoeff[2] * tcoeff[1] + rcoeff[3] * tcoeff[2] + rcoeff[4] * tcoeff[3],
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::LFE)]);
|
rcoeff[4] * tcoeff[0] + rcoeff[3] * tcoeff[1] + rcoeff[2] * tcoeff[2] + rcoeff[2] * tcoeff[3],
|
||||||
const auto bl =
|
rcoeff[3] * tcoeff[0] + rcoeff[3] * tcoeff[1] + rcoeff[2] * tcoeff[2] + rcoeff[3] * tcoeff[3],
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::BackLeft)]);
|
rcoeff[2] * tcoeff[0] + rcoeff[4] * tcoeff[1] + rcoeff[1] * tcoeff[2] + rcoeff[0] * tcoeff[3],
|
||||||
const auto br =
|
rcoeff[1] * tcoeff[0] + rcoeff[4] * tcoeff[1] + rcoeff[1] * tcoeff[2] + rcoeff[5] * tcoeff[3]
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::BackRight)]);
|
};
|
||||||
|
for (u32 i = 0; i < system_channels; ++i)
|
||||||
const auto left_sample{
|
samples[w_offs + i] = s16(std::clamp(s32(scoeff[i] * volume), min, max));
|
||||||
static_cast<s32>((fl * down_mix_coeff[0] + c * down_mix_coeff[1] +
|
|
||||||
lfe * down_mix_coeff[2] + bl * down_mix_coeff[3]) *
|
|
||||||
volume)};
|
|
||||||
|
|
||||||
const auto right_sample{
|
|
||||||
static_cast<s32>((fr * down_mix_coeff[0] + c * down_mix_coeff[1] +
|
|
||||||
lfe * down_mix_coeff[2] + br * down_mix_coeff[3]) *
|
|
||||||
volume)};
|
|
||||||
|
|
||||||
samples[write_index + static_cast<u32>(Channels::FrontLeft)] =
|
|
||||||
static_cast<s16>(std::clamp(left_sample, min, max));
|
|
||||||
samples[write_index + static_cast<u32>(Channels::FrontRight)] =
|
|
||||||
static_cast<s16>(std::clamp(right_sample, min, max));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
samples_buffer.Push(samples.subspan(0, samples.size() / system_channels * device_channels));
|
samples_buffer.Push(samples.subspan(0, samples.size() / system_channels * device_channels));
|
||||||
return;
|
} else if (system_channels < device_channels) {
|
||||||
}
|
|
||||||
|
|
||||||
if (system_channels == 2 && device_channels == 6) {
|
|
||||||
// We need moar samples! Not all games will provide 6 channel audio.
|
// We need moar samples! Not all games will provide 6 channel audio.
|
||||||
// TODO: Implement some upmixing here. Currently just passthrough, with other
|
|
||||||
// channels left as silence.
|
|
||||||
std::vector<s16> new_samples(samples.size() / system_channels * device_channels);
|
std::vector<s16> new_samples(samples.size() / system_channels * device_channels);
|
||||||
|
for (u32 r_offs = 0, w_offs = 0; r_offs < samples.size(); r_offs += system_channels, w_offs += device_channels)
|
||||||
for (u32 read_index = 0, write_index = 0; read_index < samples.size();
|
for (u32 channel = 0; channel < system_channels; ++channel)
|
||||||
read_index += system_channels, write_index += device_channels) {
|
new_samples[w_offs + channel] = s16(std::clamp(s32(f32(samples[r_offs + channel]) * volume), min, max));
|
||||||
const auto left_sample{static_cast<s16>(std::clamp(
|
|
||||||
static_cast<s32>(
|
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontLeft)]) *
|
|
||||||
volume),
|
|
||||||
min, max))};
|
|
||||||
|
|
||||||
new_samples[write_index + static_cast<u32>(Channels::FrontLeft)] = left_sample;
|
|
||||||
|
|
||||||
const auto right_sample{static_cast<s16>(std::clamp(
|
|
||||||
static_cast<s32>(
|
|
||||||
static_cast<f32>(samples[read_index + static_cast<u32>(Channels::FrontRight)]) *
|
|
||||||
volume),
|
|
||||||
min, max))};
|
|
||||||
|
|
||||||
new_samples[write_index + static_cast<u32>(Channels::FrontRight)] = right_sample;
|
|
||||||
}
|
|
||||||
|
|
||||||
samples_buffer.Push(new_samples);
|
samples_buffer.Push(new_samples);
|
||||||
return;
|
} else {
|
||||||
|
for (u32 i = 0; i < samples.size() && volume != 1.0f; ++i)
|
||||||
|
samples[i] = s16(std::clamp(s32(f32(samples[i]) * volume), min, max));
|
||||||
|
samples_buffer.Push(samples);
|
||||||
}
|
}
|
||||||
|
queue.EmplaceWait(buffer);
|
||||||
if (volume != 1.0f) {
|
++queued_buffers;
|
||||||
for (u32 i = 0; i < samples.size(); ++i) {
|
|
||||||
samples[i] = static_cast<s16>(
|
|
||||||
std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
samples_buffer.Push(samples);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
|
std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
|
||||||
constexpr s32 min = (std::numeric_limits<s16>::min)();
|
|
||||||
constexpr s32 max = (std::numeric_limits<s16>::max)();
|
|
||||||
|
|
||||||
auto samples{samples_buffer.Pop(num_samples)};
|
auto samples{samples_buffer.Pop(num_samples)};
|
||||||
|
|
||||||
// TODO: Up-mix to 6 channels if the game expects it.
|
// TODO: Up-mix to 6 channels if the game expects it.
|
||||||
|
|
@ -133,23 +84,22 @@ std::vector<s16> SinkStream::ReleaseBuffer(u64 num_samples) {
|
||||||
|
|
||||||
// Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
|
// Incoming mic volume seems to always be very quiet, so multiply by an additional 8 here.
|
||||||
// TODO: Play with this and find something that works better.
|
// TODO: Play with this and find something that works better.
|
||||||
|
constexpr s32 min = (std::numeric_limits<s16>::min)();
|
||||||
|
constexpr s32 max = (std::numeric_limits<s16>::max)();
|
||||||
auto volume{system_volume * device_volume * 8};
|
auto volume{system_volume * device_volume * 8};
|
||||||
for (u32 i = 0; i < samples.size(); i++) {
|
for (u32 i = 0; i < samples.size(); i++)
|
||||||
samples[i] = static_cast<s16>(
|
samples[i] = s16(std::clamp(s32(f32(samples[i]) * volume), min, max));
|
||||||
std::clamp(static_cast<s32>(static_cast<f32>(samples[i]) * volume), min, max));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (samples.size() < num_samples) {
|
if (samples.size() < num_samples)
|
||||||
samples.resize(num_samples, 0);
|
samples.resize(num_samples, 0);
|
||||||
}
|
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SinkStream::ClearQueue() {
|
void SinkStream::ClearQueue() {
|
||||||
samples_buffer.Pop();
|
samples_buffer.Pop();
|
||||||
SinkBuffer tmp;
|
SinkBuffer tmp;
|
||||||
while (queue.TryPop(tmp)) {
|
while (queue.TryPop(tmp))
|
||||||
}
|
;
|
||||||
queued_buffers = 0;
|
queued_buffers = 0;
|
||||||
playing_buffer = {};
|
playing_buffer = {};
|
||||||
playing_buffer.consumed = true;
|
playing_buffer.consumed = true;
|
||||||
|
|
@ -163,9 +113,8 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
|
||||||
|
|
||||||
// If we're paused or going to shut down, we don't want to consume buffers as coretiming is
|
// If we're paused or going to shut down, we don't want to consume buffers as coretiming is
|
||||||
// paused and we'll desync, so just return.
|
// paused and we'll desync, so just return.
|
||||||
if (system.IsPaused() || system.IsShuttingDown()) {
|
if (system.IsPaused() || system.IsShuttingDown())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
while (frames_written < num_frames) {
|
while (frames_written < num_frames) {
|
||||||
// If the playing buffer has been consumed or has no frames, we need a new one
|
// If the playing buffer has been consumed or has no frames, we need a new one
|
||||||
|
|
@ -195,9 +144,8 @@ void SinkStream::ProcessAudioIn(std::span<const s16> input_buffer, std::size_t n
|
||||||
|
|
||||||
// If that's all the frames in the current buffer, add its samples and mark it as
|
// If that's all the frames in the current buffer, add its samples and mark it as
|
||||||
// consumed
|
// consumed
|
||||||
if (playing_buffer.frames_played >= playing_buffer.frames) {
|
if (playing_buffer.frames_played >= playing_buffer.frames)
|
||||||
playing_buffer.consumed = true;
|
playing_buffer.consumed = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes);
|
std::memcpy(&last_frame[0], &input_buffer[(frames_written - 1) * frame_size], frame_size_bytes);
|
||||||
|
|
@ -222,9 +170,8 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr std::array<s16, 6> silence{};
|
static constexpr std::array<s16, 6> silence{};
|
||||||
for (size_t i = frames_written; i < num_frames; i++) {
|
for (size_t i = frames_written; i < num_frames; i++)
|
||||||
std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
|
std::memcpy(&output_buffer[i * frame_size], &silence[0], frame_size_bytes);
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -234,17 +181,16 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
|
||||||
if (!queue.TryPop(playing_buffer)) {
|
if (!queue.TryPop(playing_buffer)) {
|
||||||
// If no buffer was available we've underrun, fill the remaining buffer with
|
// If no buffer was available we've underrun, fill the remaining buffer with
|
||||||
// the last written frame and continue.
|
// the last written frame and continue.
|
||||||
for (size_t i = frames_written; i < num_frames; i++) {
|
for (size_t i = frames_written; i < num_frames; i++)
|
||||||
std::memcpy(&output_buffer[i * frame_size], &last_frame[0], frame_size_bytes);
|
std::memcpy(&output_buffer[i * frame_size], &last_frame[0], frame_size_bytes);
|
||||||
}
|
|
||||||
frames_written = num_frames;
|
frames_written = num_frames;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Successfully dequeued a new buffer.
|
// Successfully dequeued a new buffer.
|
||||||
queued_buffers--;
|
{
|
||||||
|
std::unique_lock lk{release_mutex};\
|
||||||
{ std::unique_lock lk{release_mutex}; }
|
queued_buffers--;
|
||||||
|
}
|
||||||
release_cv.notify_one();
|
release_cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -291,8 +237,7 @@ u64 SinkStream::GetExpectedPlayedSampleCount() {
|
||||||
|
|
||||||
void SinkStream::WaitFreeSpace(std::stop_token stop_token) {
|
void SinkStream::WaitFreeSpace(std::stop_token stop_token) {
|
||||||
std::unique_lock lk{release_mutex};
|
std::unique_lock lk{release_mutex};
|
||||||
release_cv.wait_for(lk, std::chrono::milliseconds(5),
|
release_cv.wait_for(lk, std::chrono::milliseconds(5), [this]() { return paused || queued_buffers < max_queue_size; });
|
||||||
[this]() { return paused || queued_buffers < max_queue_size; });
|
|
||||||
if (queued_buffers > max_queue_size + 3) {
|
if (queued_buffers > max_queue_size + 3) {
|
||||||
release_cv.wait(lk, stop_token, [this] { return paused || queued_buffers < max_queue_size; });
|
release_cv.wait(lk, stop_token, [this] { return paused || queued_buffers < max_queue_size; });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -240,7 +240,7 @@ private:
|
||||||
/// Ring buffer of the samples waiting to be played or consumed
|
/// Ring buffer of the samples waiting to be played or consumed
|
||||||
Common::RingBuffer<s16, 0x10000> samples_buffer;
|
Common::RingBuffer<s16, 0x10000> samples_buffer;
|
||||||
/// Audio buffers queued and waiting to play
|
/// Audio buffers queued and waiting to play
|
||||||
Common::SPSCQueue<SinkBuffer, 0x10000> queue;
|
Common::SPSCQueue<SinkBuffer, 0x40000> queue;
|
||||||
/// The currently-playing audio buffer
|
/// The currently-playing audio buffer
|
||||||
SinkBuffer playing_buffer{};
|
SinkBuffer playing_buffer{};
|
||||||
/// The last played (or received) frame of audio, used when the callback underruns
|
/// The last played (or received) frame of audio, used when the callback underruns
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue