mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-07-13 13:56:49 -04:00
Merge pull request #215 from TwilitRealm/reverb-wip
Audio FX reverb proof of concept
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
#include <SDL3/SDL_init.h>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <span>
|
||||
|
||||
#include "JSystem/JAudio2/JASAiCtrl.h"
|
||||
@@ -17,8 +15,6 @@
|
||||
#include "JSystem/JAudio2/JASAudioThread.h"
|
||||
#include "JSystem/JAudio2/JASDriverIF.h"
|
||||
|
||||
// #define DUSK_DUMP_AUDIO
|
||||
|
||||
using namespace dusk::audio;
|
||||
|
||||
static OutputSubframe OutBuffer;
|
||||
@@ -91,10 +87,6 @@ void SDLCALL GetNewAudio(
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(DUSK_DUMP_AUDIO)
|
||||
static std::ofstream outRaw("guh.raw", std::ios_base::out | std::ios_base::binary);
|
||||
#endif
|
||||
|
||||
int RenderNewAudioFrame() {
|
||||
JASCriticalSection section;
|
||||
const u32 countSubframes = JASDriver::getSubFrames();
|
||||
@@ -107,10 +99,6 @@ int RenderNewAudioFrame() {
|
||||
JASAudioThread::snIntCount -= 1;
|
||||
}
|
||||
|
||||
#if defined(DUSK_DUMP_AUDIO)
|
||||
outRaw.flush();
|
||||
#endif
|
||||
|
||||
return static_cast<u16>(countSubframes) * DSP_SUBFRAME_SIZE;
|
||||
}
|
||||
|
||||
@@ -147,10 +135,6 @@ void RenderAudioSubframe() {
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(DUSK_DUMP_AUDIO)
|
||||
outRaw.write((const char*)OutInterleaveBuffer.data(), sizeof(OutInterleaveBuffer));
|
||||
#endif
|
||||
|
||||
SDL_PutAudioStreamData(PlaybackStream, &OutInterleaveBuffer, sizeof(OutInterleaveBuffer));
|
||||
}
|
||||
|
||||
|
||||
+90
-14
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <span>
|
||||
|
||||
#include "Adpcm.hpp"
|
||||
@@ -17,8 +18,30 @@ using namespace dusk::audio;
|
||||
|
||||
ChannelAuxData dusk::audio::ChannelAux[DSP_CHANNELS] = {};
|
||||
|
||||
static bool sDumpWasActive = false;
|
||||
static FILE* sChannelDumpFiles[DSP_CHANNELS] = {};
|
||||
|
||||
static void OpenChannelDumpFiles() {
|
||||
char name[32];
|
||||
for (int i = 0; i < DSP_CHANNELS; i++) {
|
||||
snprintf(name, sizeof(name), "channel_%02d.raw", i);
|
||||
sChannelDumpFiles[i] = fopen(name, "wb");
|
||||
}
|
||||
}
|
||||
|
||||
static void CloseChannelDumpFiles() {
|
||||
for (int i = 0; i < DSP_CHANNELS; i++) {
|
||||
if (sChannelDumpFiles[i]) {
|
||||
fclose(sChannelDumpFiles[i]);
|
||||
sChannelDumpFiles[i] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f32 dusk::audio::MasterVolume = 1.0f;
|
||||
f32 dusk::audio::PrevMasterVolume = 1.0f;
|
||||
bool dusk::audio::EnableReverb = true;
|
||||
bool dusk::audio::DumpAudio = false;
|
||||
|
||||
/**
|
||||
* Validate that a DSP channel's format is actually something we know how to play.
|
||||
@@ -106,38 +129,81 @@ static void MixSubframe(DspSubframe& dst, const DspSubframe& src) {
|
||||
}
|
||||
|
||||
void dusk::audio::DspRender(OutputSubframe& subframe) {
|
||||
if (DumpAudio != sDumpWasActive) {
|
||||
sDumpWasActive = DumpAudio;
|
||||
if (DumpAudio) {
|
||||
OpenChannelDumpFiles();
|
||||
} else {
|
||||
CloseChannelDumpFiles();
|
||||
}
|
||||
}
|
||||
|
||||
std::span channels(JASDsp::CH_BUF, DSP_CHANNELS);
|
||||
|
||||
for (int i = 0; i < channels.size(); i++) {
|
||||
auto& channel = channels[i];
|
||||
auto& channelAux = ChannelAux[i];
|
||||
|
||||
if (!channel.mIsActive) {
|
||||
continue;
|
||||
}
|
||||
bool skipRender = false;
|
||||
|
||||
if (channel.mPauseFlag) {
|
||||
if (!channel.mIsActive) {
|
||||
skipRender = true;
|
||||
}
|
||||
else if (channel.mPauseFlag) {
|
||||
// Not really sure what the practical difference between pause and
|
||||
// deactivation is. Either avoids clearing state or allows the DSP to avoid popping?
|
||||
continue;
|
||||
skipRender = true;
|
||||
}
|
||||
|
||||
if (channel.mForcedStop) {
|
||||
else if (channel.mForcedStop) {
|
||||
channel.mIsFinished = true;
|
||||
continue;
|
||||
skipRender = true;
|
||||
}
|
||||
|
||||
if (channel.mWaveAramAddress == 0) {
|
||||
else if (channel.mWaveAramAddress == 0) {
|
||||
// I think these are oscillator channels? Not backed by audio.
|
||||
// No idea how to implement these yet, so skip them.
|
||||
channel.mIsFinished = true;
|
||||
continue;
|
||||
skipRender = true;
|
||||
}
|
||||
|
||||
ValidateChannel(channel);
|
||||
|
||||
OutputSubframe channelSubframe = {};
|
||||
RenderChannel(channel, channelAux, channelSubframe);
|
||||
|
||||
if (!skipRender) {
|
||||
ValidateChannel(channel);
|
||||
RenderChannel(channel, channelAux, channelSubframe);
|
||||
}
|
||||
|
||||
if (EnableReverb) {
|
||||
// scale the input to the reverb rather than using wet/dry on the output.
|
||||
// this way the reverb's internal buffers accumulate energy proportional to mAutoMixerFxMix,
|
||||
// so any tail always decays at the correct level regardless of mAutoMixerFxMix changes
|
||||
// prevents transients when the next sound starts playing with a different reverb level
|
||||
// 700.0f was pulled out of my ass and just sounds good enough for console
|
||||
f32 inputGain = (!skipRender) ? (channel.mAutoMixerFxMix >> 8) / 700.0f : 0.0f;
|
||||
|
||||
OutputSubframe reverbSubframe = {};
|
||||
for (int j = 0; j < DSP_SUBFRAME_SIZE; j++) {
|
||||
reverbSubframe.channels[0][j] = channelSubframe.channels[0][j] * inputGain;
|
||||
reverbSubframe.channels[1][j] = channelSubframe.channels[1][j] * inputGain;
|
||||
}
|
||||
|
||||
channelAux.reverb.processreplace(
|
||||
reverbSubframe.channels[0].data(), reverbSubframe.channels[1].data(),
|
||||
reverbSubframe.channels[0].data(), reverbSubframe.channels[1].data(),
|
||||
DSP_SUBFRAME_SIZE, 1
|
||||
);
|
||||
|
||||
for (int j = 0; j < DSP_SUBFRAME_SIZE; j++) {
|
||||
channelSubframe.channels[0][j] += reverbSubframe.channels[0][j];
|
||||
channelSubframe.channels[1][j] += reverbSubframe.channels[1][j];
|
||||
}
|
||||
}
|
||||
|
||||
if (DumpAudio && sChannelDumpFiles[i]) {
|
||||
for (int j = 0; j < DSP_SUBFRAME_SIZE; j++) {
|
||||
fwrite(&channelSubframe.channels[0][j], sizeof(f32), 1, sChannelDumpFiles[i]);
|
||||
fwrite(&channelSubframe.channels[1][j], sizeof(f32), 1, sChannelDumpFiles[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int o = 0; o < subframe.channels.size(); o++) {
|
||||
MixSubframe(subframe.channels[o], channelSubframe.channels[o]);
|
||||
@@ -463,6 +529,16 @@ static void RenderChannel(
|
||||
}
|
||||
|
||||
void dusk::audio::DspInit() {
|
||||
for (int i = 0; i < DSP_CHANNELS; i++) {
|
||||
auto& channelAux = ChannelAux[i];
|
||||
channelAux.reverb.setwet(1.0f);
|
||||
channelAux.reverb.setdry(0.0f);
|
||||
channelAux.reverb.setroomsize(0.4f);
|
||||
channelAux.reverb.setdamp(0.7f);
|
||||
channelAux.reverb.setwidth(1.0f);
|
||||
channelAux.reverb.setmode(0.0f);
|
||||
channelAux.reverb.mute();
|
||||
}
|
||||
}
|
||||
|
||||
void dusk::audio::ApplyVolume(
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include "SDL3/SDL_audio.h"
|
||||
#include <span>
|
||||
|
||||
#include "revmodel.hpp"
|
||||
|
||||
// ReSharper disable once CppUnusedIncludeDirective
|
||||
#include "global.h"
|
||||
|
||||
@@ -29,6 +31,7 @@ namespace dusk::audio {
|
||||
|
||||
// Used for debugging tools.
|
||||
u32 resetCount;
|
||||
revmodel reverb;
|
||||
|
||||
/**
|
||||
* Previous volume values, per output channel.
|
||||
@@ -51,7 +54,7 @@ namespace dusk::audio {
|
||||
// basically stores our position between resamplePrev and decodeBuf[0] so we don't lose that fractional resampler position next subframe
|
||||
f32 resamplePos;
|
||||
// last consumed sample from decodeBuf
|
||||
s16 resamplePrev;
|
||||
s16 resamplePrev;
|
||||
};
|
||||
|
||||
extern ChannelAuxData ChannelAux[DSP_CHANNELS];
|
||||
@@ -118,4 +121,6 @@ namespace dusk::audio {
|
||||
|
||||
extern f32 MasterVolume;
|
||||
extern f32 PrevMasterVolume;
|
||||
extern bool EnableReverb;
|
||||
extern bool DumpAudio;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "JSystem/JAudio2/JASDSPInterface.h"
|
||||
#include "JSystem/JAudio2/JASTrack.h"
|
||||
#include "dusk/audio/DuskAudioSystem.h"
|
||||
#include "dusk/audio/DuskDsp.hpp"
|
||||
|
||||
static std::array<u8, DSP_CHANNELS> channelSortIndices = {};
|
||||
static std::array<u32, DSP_CHANNELS> lastResetCounts = {};
|
||||
@@ -102,6 +103,7 @@ static void ShowAllDspChannels() {
|
||||
}
|
||||
|
||||
ImGui::Text("Active channels: %d", activeChannels);
|
||||
ImGui::Checkbox("Dump channels to disk", &dusk::audio::DumpAudio);
|
||||
ImGui::Checkbox("Sort by update count", &sortUpdateCount);
|
||||
|
||||
if (sortUpdateCount) {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <imgui_internal.h>
|
||||
|
||||
#include "JSystem/JUtility/JUTGamePad.h"
|
||||
#include "dusk/audio/DuskDsp.hpp"
|
||||
#include "dusk/audio/DuskAudioSystem.h"
|
||||
#include "dusk/hotkeys.h"
|
||||
#include "dusk/settings.h"
|
||||
@@ -34,7 +35,7 @@ namespace dusk {
|
||||
if (ImGui::BeginMenu("Audio")) {
|
||||
ImGui::Text("Master Volume");
|
||||
ImGui::SliderFloat("##masterVolume", &getSettings().audio.masterVolume, 0.0f, 1.0f, "");
|
||||
|
||||
ImGui::Checkbox("Enable Reverb", &getSettings().audio.enableReverb);
|
||||
/*
|
||||
// TODO: implement additional settings
|
||||
ImGui::Text("Main Music Volume");
|
||||
@@ -55,6 +56,7 @@ namespace dusk {
|
||||
*/
|
||||
|
||||
audio::SetMasterVolume(getSettings().audio.masterVolume);
|
||||
audio::EnableReverb = getSettings().audio.enableReverb;
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
@@ -12,11 +12,12 @@ UserSettings g_userSettings = {
|
||||
|
||||
// Audio
|
||||
.audio = {
|
||||
.masterVolume = 1.0f,
|
||||
.masterVolume = 0.8f,
|
||||
.mainMusicVolume = 1.0f,
|
||||
.subMusicVolume = 1.0f,
|
||||
.soundEffectsVolume = 1.0f,
|
||||
.fanfareVolume = 1.0f,
|
||||
.enableReverb = true
|
||||
},
|
||||
|
||||
// Game settings
|
||||
|
||||
Reference in New Issue
Block a user