summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/audio_core/CMakeLists.txt2
-rw-r--r--src/audio_core/command_generator.cpp357
-rw-r--r--src/audio_core/command_generator.h5
-rw-r--r--src/audio_core/common.h23
-rw-r--r--src/audio_core/delay_line.cpp104
-rw-r--r--src/audio_core/delay_line.h46
-rw-r--r--src/audio_core/effect_context.cpp22
-rw-r--r--src/audio_core/effect_context.h31
8 files changed, 572 insertions, 18 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index d1d177b51..a0ae07752 100644
--- a/src/audio_core/CMakeLists.txt
+++ b/src/audio_core/CMakeLists.txt
@@ -15,6 +15,8 @@ add_library(audio_core STATIC
15 command_generator.cpp 15 command_generator.cpp
16 command_generator.h 16 command_generator.h
17 common.h 17 common.h
18 delay_line.cpp
19 delay_line.h
18 effect_context.cpp 20 effect_context.cpp
19 effect_context.h 21 effect_context.h
20 info_updater.cpp 22 info_updater.cpp
diff --git a/src/audio_core/command_generator.cpp b/src/audio_core/command_generator.cpp
index 5b1065520..437cc5ccd 100644
--- a/src/audio_core/command_generator.cpp
+++ b/src/audio_core/command_generator.cpp
@@ -2,6 +2,8 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <cmath>
6#include <numbers>
5#include "audio_core/algorithm/interpolate.h" 7#include "audio_core/algorithm/interpolate.h"
6#include "audio_core/command_generator.h" 8#include "audio_core/command_generator.h"
7#include "audio_core/effect_context.h" 9#include "audio_core/effect_context.h"
@@ -13,6 +15,20 @@ namespace AudioCore {
13namespace { 15namespace {
14constexpr std::size_t MIX_BUFFER_SIZE = 0x3f00; 16constexpr std::size_t MIX_BUFFER_SIZE = 0x3f00;
15constexpr std::size_t SCALED_MIX_BUFFER_SIZE = MIX_BUFFER_SIZE << 15ULL; 17constexpr std::size_t SCALED_MIX_BUFFER_SIZE = MIX_BUFFER_SIZE << 15ULL;
18using DelayLineTimes = std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT>;
19
20constexpr DelayLineTimes FDN_MIN_DELAY_LINE_TIMES{5.0f, 6.0f, 13.0f, 14.0f};
21constexpr DelayLineTimes FDN_MAX_DELAY_LINE_TIMES{45.704f, 82.782f, 149.94f, 271.58f};
22constexpr DelayLineTimes DECAY0_MAX_DELAY_LINE_TIMES{17.0f, 13.0f, 9.0f, 7.0f};
23constexpr DelayLineTimes DECAY1_MAX_DELAY_LINE_TIMES{19.0f, 11.0f, 10.0f, 6.0f};
24constexpr std::array<f32, AudioCommon::I3DL2REVERB_TAPS> EARLY_TAP_TIMES{
25 0.017136f, 0.059154f, 0.161733f, 0.390186f, 0.425262f, 0.455411f, 0.689737f,
26 0.745910f, 0.833844f, 0.859502f, 0.000000f, 0.075024f, 0.168788f, 0.299901f,
27 0.337443f, 0.371903f, 0.599011f, 0.716741f, 0.817859f, 0.851664f};
28constexpr std::array<f32, AudioCommon::I3DL2REVERB_TAPS> EARLY_GAIN{
29 0.67096f, 0.61027f, 1.0f, 0.35680f, 0.68361f, 0.65978f, 0.51939f,
30 0.24712f, 0.45945f, 0.45021f, 0.64196f, 0.54879f, 0.92925f, 0.38270f,
31 0.72867f, 0.69794f, 0.5464f, 0.24563f, 0.45214f, 0.44042f};
16 32
17template <std::size_t N> 33template <std::size_t N>
18void ApplyMix(s32* output, const s32* input, s32 gain, s32 sample_count) { 34void ApplyMix(s32* output, const s32* input, s32 gain, s32 sample_count) {
@@ -65,6 +81,154 @@ s32 ApplyMixDepop(s32* output, s32 first_sample, s32 delta, s32 sample_count) {
65 } 81 }
66} 82}
67 83
84float Pow10(float x) {
85 if (x >= 0.0f) {
86 return 1.0f;
87 } else if (x <= -5.3f) {
88 return 0.0f;
89 }
90 return std::pow(10.0f, x);
91}
92
93float SinD(float degrees) {
94 return std::sin(degrees * std::numbers::pi_v<float> / 180.0f);
95}
96
97float CosD(float degrees) {
98 return std::cos(degrees * std::numbers::pi_v<float> / 180.0f);
99}
100
101float ToFloat(s32 sample) {
102 return static_cast<float>(sample) / 65536.f;
103}
104
105s32 ToS32(float sample) {
106 constexpr auto min = -8388608.0f;
107 constexpr auto max = 8388607.f;
108 float rescaled_sample = sample * 65536.0f;
109 if (rescaled_sample < min) {
110 rescaled_sample = min;
111 }
112 if (rescaled_sample > max) {
113 rescaled_sample = max;
114 }
115 return static_cast<s32>(rescaled_sample);
116}
117
118constexpr std::array<std::size_t, 20> REVERB_TAP_INDEX_1CH{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
120
121constexpr std::array<std::size_t, 20> REVERB_TAP_INDEX_2CH{0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
122 1, 1, 1, 0, 0, 0, 0, 1, 1, 1};
123
124constexpr std::array<std::size_t, 20> REVERB_TAP_INDEX_4CH{0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
125 1, 1, 1, 0, 0, 0, 0, 3, 3, 3};
126
127constexpr std::array<std::size_t, 20> REVERB_TAP_INDEX_6CH{4, 0, 0, 1, 1, 1, 1, 2, 2, 2,
128 1, 1, 1, 0, 0, 0, 0, 3, 3, 3};
129
130template <std::size_t CHANNEL_COUNT>
131void ApplyReverbGeneric(I3dl2ReverbState& state,
132 const std::array<const s32*, AudioCommon::MAX_CHANNEL_COUNT>& input,
133 const std::array<s32*, AudioCommon::MAX_CHANNEL_COUNT>& output,
134 s32 sample_count) {
135
136 auto GetTapLookup = []() {
137 if constexpr (CHANNEL_COUNT == 1) {
138 return REVERB_TAP_INDEX_1CH;
139 } else if constexpr (CHANNEL_COUNT == 2) {
140 return REVERB_TAP_INDEX_2CH;
141 } else if constexpr (CHANNEL_COUNT == 4) {
142 return REVERB_TAP_INDEX_4CH;
143 } else if constexpr (CHANNEL_COUNT == 6) {
144 return REVERB_TAP_INDEX_6CH;
145 }
146 };
147
148 const auto& tap_index_lut = GetTapLookup();
149 for (s32 sample = 0; sample < sample_count; sample++) {
150 std::array<f32, CHANNEL_COUNT> out_samples{};
151 std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> fsamp{};
152 std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> mixed{};
153 std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> osamp{};
154
155 // Mix everything into a single sample
156 s32 temp_mixed_sample = 0;
157 for (std::size_t i = 0; i < CHANNEL_COUNT; i++) {
158 temp_mixed_sample += input[i][sample];
159 }
160 const auto current_sample = ToFloat(temp_mixed_sample);
161 const auto early_tap = state.early_delay_line.TapOut(state.early_to_late_taps);
162
163 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_TAPS; i++) {
164 const auto tapped_samp =
165 state.early_delay_line.TapOut(state.early_tap_steps[i]) * EARLY_GAIN[i];
166 out_samples[tap_index_lut[i]] += tapped_samp;
167
168 if constexpr (CHANNEL_COUNT == 6) {
169 // handle lfe
170 out_samples[5] += tapped_samp;
171 }
172 }
173
174 state.lowpass_0 = current_sample * state.lowpass_2 + state.lowpass_0 * state.lowpass_1;
175 state.early_delay_line.Tick(state.lowpass_0);
176
177 for (std::size_t i = 0; i < CHANNEL_COUNT; i++) {
178 out_samples[i] *= state.early_gain;
179 }
180
181 // Two channel seems to apply a latet gain, we require to save this
182 f32 filter{};
183 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
184 filter = state.fdn_delay_line[i].GetOutputSample();
185 const auto computed = filter * state.lpf_coefficients[0][i] + state.shelf_filter[i];
186 state.shelf_filter[i] =
187 filter * state.lpf_coefficients[1][i] + computed * state.lpf_coefficients[2][i];
188 fsamp[i] = computed;
189 }
190
191 // Mixing matrix
192 mixed[0] = fsamp[1] + fsamp[2];
193 mixed[1] = -fsamp[0] - fsamp[3];
194 mixed[2] = fsamp[0] - fsamp[3];
195 mixed[3] = fsamp[1] - fsamp[2];
196
197 if constexpr (CHANNEL_COUNT == 2) {
198 for (auto& mix : mixed) {
199 mix *= (filter * state.late_gain);
200 }
201 }
202
203 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
204 const auto late = early_tap * state.late_gain;
205 osamp[i] = state.decay_delay_line0[i].Tick(late + mixed[i]);
206 osamp[i] = state.decay_delay_line1[i].Tick(osamp[i]);
207 state.fdn_delay_line[i].Tick(osamp[i]);
208 }
209
210 if constexpr (CHANNEL_COUNT == 1) {
211 output[0][sample] = ToS32(state.dry_gain * ToFloat(input[0][sample]) +
212 (out_samples[0] + osamp[0] + osamp[1]));
213 } else if constexpr (CHANNEL_COUNT == 2 || CHANNEL_COUNT == 4) {
214 for (std::size_t i = 0; i < CHANNEL_COUNT; i++) {
215 output[i][sample] =
216 ToS32(state.dry_gain * ToFloat(input[i][sample]) + (out_samples[i] + osamp[i]));
217 }
218 } else if constexpr (CHANNEL_COUNT == 6) {
219 const auto temp_center = state.center_delay_line.Tick(0.5f * (osamp[2] - osamp[3]));
220 for (std::size_t i = 0; i < 4; i++) {
221 output[i][sample] =
222 ToS32(state.dry_gain * ToFloat(input[i][sample]) + (out_samples[i] + osamp[i]));
223 }
224 output[4][sample] =
225 ToS32(state.dry_gain * ToFloat(input[4][sample]) + (out_samples[4] + temp_center));
226 output[5][sample] =
227 ToS32(state.dry_gain * ToFloat(input[5][sample]) + (out_samples[5] + osamp[3]));
228 }
229 }
230}
231
68} // namespace 232} // namespace
69 233
70CommandGenerator::CommandGenerator(AudioCommon::AudioRendererParameter& worker_params_, 234CommandGenerator::CommandGenerator(AudioCommon::AudioRendererParameter& worker_params_,
@@ -271,11 +435,10 @@ void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voic
271 } 435 }
272 436
273 // Generate biquad filter 437 // Generate biquad filter
274 // GenerateBiquadFilterCommand(mix_buffer_count, biquad_filter, 438 // GenerateBiquadFilterCommand(mix_buffer_count, biquad_filter,
275 // dsp_state.biquad_filter_state, 439 // dsp_state.biquad_filter_state,
276 // mix_buffer_count + channel, mix_buffer_count + 440 // mix_buffer_count + channel, mix_buffer_count + channel,
277 // channel, worker_params.sample_count, 441 // worker_params.sample_count, voice_info.GetInParams().node_id);
278 // voice_info.GetInParams().node_id);
279 } 442 }
280} 443}
281 444
@@ -376,21 +539,54 @@ void CommandGenerator::GenerateEffectCommand(ServerMixInfo& mix_info) {
376 539
377void CommandGenerator::GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, EffectBase* info, 540void CommandGenerator::GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, EffectBase* info,
378 bool enabled) { 541 bool enabled) {
379 if (!enabled) { 542 auto* reverb = dynamic_cast<EffectI3dl2Reverb*>(info);
543 const auto& params = reverb->GetParams();
544 auto& state = reverb->GetState();
545 const auto channel_count = params.channel_count;
546
547 if (channel_count != 1 && channel_count != 2 && channel_count != 4 && channel_count != 6) {
380 return; 548 return;
381 } 549 }
382 const auto& params = dynamic_cast<EffectI3dl2Reverb*>(info)->GetParams(); 550
383 const auto channel_count = params.channel_count; 551 std::array<const s32*, AudioCommon::MAX_CHANNEL_COUNT> input{};
552 std::array<s32*, AudioCommon::MAX_CHANNEL_COUNT> output{};
553
554 const auto status = params.status;
384 for (s32 i = 0; i < channel_count; i++) { 555 for (s32 i = 0; i < channel_count; i++) {
385 // TODO(ogniK): Actually implement reverb 556 input[i] = GetMixBuffer(mix_buffer_offset + params.input[i]);
386 /* 557 output[i] = GetMixBuffer(mix_buffer_offset + params.output[i]);
387 if (params.input[i] != params.output[i]) { 558 }
388 const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); 559
389 auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); 560 if (enabled) {
390 ApplyMix<1>(output, input, 32768, worker_params.sample_count); 561 if (status == ParameterStatus::Initialized) {
391 }*/ 562 InitializeI3dl2Reverb(reverb->GetParams(), state, info->GetWorkBuffer());
392 auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); 563 } else if (status == ParameterStatus::Updating) {
393 std::memset(output, 0, worker_params.sample_count * sizeof(s32)); 564 UpdateI3dl2Reverb(reverb->GetParams(), state, false);
565 }
566 }
567
568 if (enabled) {
569 switch (channel_count) {
570 case 1:
571 ApplyReverbGeneric<1>(state, input, output, worker_params.sample_count);
572 break;
573 case 2:
574 ApplyReverbGeneric<2>(state, input, output, worker_params.sample_count);
575 break;
576 case 4:
577 ApplyReverbGeneric<4>(state, input, output, worker_params.sample_count);
578 break;
579 case 6:
580 ApplyReverbGeneric<6>(state, input, output, worker_params.sample_count);
581 break;
582 }
583 } else {
584 for (s32 i = 0; i < channel_count; i++) {
585 // Only copy if the buffer input and output do not match!
586 if ((mix_buffer_offset + params.input[i]) != (mix_buffer_offset + params.output[i])) {
587 std::memcpy(output[i], input[i], worker_params.sample_count * sizeof(s32));
588 }
589 }
394 } 590 }
395} 591}
396 592
@@ -528,6 +724,133 @@ s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u3
528 return sample_count; 724 return sample_count;
529} 725}
530 726
727void CommandGenerator::InitializeI3dl2Reverb(I3dl2ReverbParams& info, I3dl2ReverbState& state,
728 std::vector<u8>& work_buffer) {
729 // Reset state
730 state.lowpass_0 = 0.0f;
731 state.lowpass_1 = 0.0f;
732 state.lowpass_2 = 0.0f;
733
734 state.early_delay_line.Reset();
735 state.early_tap_steps.fill(0);
736 state.early_gain = 0.0f;
737 state.late_gain = 0.0f;
738 state.early_to_late_taps = 0;
739 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
740 state.fdn_delay_line[i].Reset();
741 state.decay_delay_line0[i].Reset();
742 state.decay_delay_line1[i].Reset();
743 }
744 state.last_reverb_echo = 0.0f;
745 state.center_delay_line.Reset();
746 for (auto& coef : state.lpf_coefficients) {
747 coef.fill(0.0f);
748 }
749 state.shelf_filter.fill(0.0f);
750 state.dry_gain = 0.0f;
751
752 const auto sample_rate = info.sample_rate / 1000;
753 f32* work_buffer_ptr = reinterpret_cast<f32*>(work_buffer.data());
754
755 s32 delay_samples{};
756 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
757 delay_samples =
758 AudioCommon::CalculateDelaySamples(sample_rate, FDN_MAX_DELAY_LINE_TIMES[i]);
759 state.fdn_delay_line[i].Initialize(delay_samples, work_buffer_ptr);
760 work_buffer_ptr += delay_samples + 1;
761
762 delay_samples =
763 AudioCommon::CalculateDelaySamples(sample_rate, DECAY0_MAX_DELAY_LINE_TIMES[i]);
764 state.decay_delay_line0[i].Initialize(delay_samples, 0.0f, work_buffer_ptr);
765 work_buffer_ptr += delay_samples + 1;
766
767 delay_samples =
768 AudioCommon::CalculateDelaySamples(sample_rate, DECAY1_MAX_DELAY_LINE_TIMES[i]);
769 state.decay_delay_line1[i].Initialize(delay_samples, 0.0f, work_buffer_ptr);
770 work_buffer_ptr += delay_samples + 1;
771 }
772 delay_samples = AudioCommon::CalculateDelaySamples(sample_rate, 5.0f);
773 state.center_delay_line.Initialize(delay_samples, work_buffer_ptr);
774 work_buffer_ptr += delay_samples + 1;
775
776 delay_samples = AudioCommon::CalculateDelaySamples(sample_rate, 400.0f);
777 state.early_delay_line.Initialize(delay_samples, work_buffer_ptr);
778
779 UpdateI3dl2Reverb(info, state, true);
780}
781
782void CommandGenerator::UpdateI3dl2Reverb(I3dl2ReverbParams& info, I3dl2ReverbState& state,
783 bool should_clear) {
784
785 state.dry_gain = info.dry_gain;
786 state.shelf_filter.fill(0.0f);
787 state.lowpass_0 = 0.0f;
788 state.early_gain = Pow10(std::min(info.room + info.reflection, 5000.0f) / 2000.0f);
789 state.late_gain = Pow10(std::min(info.room + info.reverb, 5000.0f) / 2000.0f);
790
791 const auto sample_rate = info.sample_rate / 1000;
792 const f32 hf_gain = Pow10(info.room_hf / 2000.0f);
793 if (hf_gain >= 1.0f) {
794 state.lowpass_2 = 1.0f;
795 state.lowpass_1 = 0.0f;
796 } else {
797 const auto a = 1.0f - hf_gain;
798 const auto b = 2.0f * (1.0f - hf_gain * CosD(256.0f * info.hf_reference /
799 static_cast<f32>(info.sample_rate)));
800 const auto c = std::sqrt(b * b - 4.0f * a * a);
801
802 state.lowpass_1 = (b - c) / (2.0f * a);
803 state.lowpass_2 = 1.0f - state.lowpass_1;
804 }
805 state.early_to_late_taps = AudioCommon::CalculateDelaySamples(
806 sample_rate, 1000.0f * (info.reflection_delay + info.reverb_delay));
807
808 state.last_reverb_echo = 0.6f * info.diffusion * 0.01f;
809 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
810 const auto length =
811 FDN_MIN_DELAY_LINE_TIMES[i] +
812 (info.density / 100.0f) * (FDN_MAX_DELAY_LINE_TIMES[i] - FDN_MIN_DELAY_LINE_TIMES[i]);
813 state.fdn_delay_line[i].SetDelay(AudioCommon::CalculateDelaySamples(sample_rate, length));
814
815 const auto delay_sample_counts = state.fdn_delay_line[i].GetDelay() +
816 state.decay_delay_line0[i].GetDelay() +
817 state.decay_delay_line1[i].GetDelay();
818
819 float a = (-60.0f * static_cast<f32>(delay_sample_counts)) /
820 (info.decay_time * static_cast<f32>(info.sample_rate));
821 float b = a / info.hf_decay_ratio;
822 float c = CosD(128.0f * 0.5f * info.hf_reference / static_cast<f32>(info.sample_rate)) /
823 SinD(128.0f * 0.5f * info.hf_reference / static_cast<f32>(info.sample_rate));
824 float d = Pow10((b - a) / 40.0f);
825 float e = Pow10((b + a) / 40.0f) * 0.7071f;
826
827 state.lpf_coefficients[0][i] = e * ((d * c) + 1.0f) / (c + d);
828 state.lpf_coefficients[1][i] = e * (1.0f - (d * c)) / (c + d);
829 state.lpf_coefficients[2][i] = (c - d) / (c + d);
830
831 state.decay_delay_line0[i].SetCoefficient(state.last_reverb_echo);
832 state.decay_delay_line1[i].SetCoefficient(-0.9f * state.last_reverb_echo);
833 }
834
835 if (should_clear) {
836 for (std::size_t i = 0; i < AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT; i++) {
837 state.fdn_delay_line[i].Clear();
838 state.decay_delay_line0[i].Clear();
839 state.decay_delay_line1[i].Clear();
840 }
841 state.early_delay_line.Clear();
842 state.center_delay_line.Clear();
843 }
844
845 const auto max_early_delay = state.early_delay_line.GetMaxDelay();
846 const auto reflection_time = 1000.0f * (0.0098f * info.reverb_delay + 0.02f);
847 for (std::size_t tap = 0; tap < AudioCommon::I3DL2REVERB_TAPS; tap++) {
848 const auto length = AudioCommon::CalculateDelaySamples(
849 sample_rate, 1000.0f * info.reflection_delay + reflection_time * EARLY_TAP_TIMES[tap]);
850 state.early_tap_steps[tap] = std::min(length, max_early_delay);
851 }
852}
853
531void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float current_volume, 854void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float current_volume,
532 s32 channel, s32 node_id) { 855 s32 channel, s32 node_id) {
533 const auto last = static_cast<s32>(last_volume * 32768.0f); 856 const auto last = static_cast<s32>(last_volume * 32768.0f);
diff --git a/src/audio_core/command_generator.h b/src/audio_core/command_generator.h
index b937350b1..2ebb755b0 100644
--- a/src/audio_core/command_generator.h
+++ b/src/audio_core/command_generator.h
@@ -21,6 +21,8 @@ class ServerMixInfo;
21class EffectContext; 21class EffectContext;
22class EffectBase; 22class EffectBase;
23struct AuxInfoDSP; 23struct AuxInfoDSP;
24struct I3dl2ReverbParams;
25struct I3dl2ReverbState;
24using MixVolumeBuffer = std::array<float, AudioCommon::MAX_MIX_BUFFERS>; 26using MixVolumeBuffer = std::array<float, AudioCommon::MAX_MIX_BUFFERS>;
25 27
26class CommandGenerator { 28class CommandGenerator {
@@ -80,6 +82,9 @@ private:
80 s32 ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, s32* out_data, 82 s32 ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, s32* out_data,
81 u32 sample_count, u32 read_offset, u32 read_count); 83 u32 sample_count, u32 read_offset, u32 read_count);
82 84
85 void InitializeI3dl2Reverb(I3dl2ReverbParams& info, I3dl2ReverbState& state,
86 std::vector<u8>& work_buffer);
87 void UpdateI3dl2Reverb(I3dl2ReverbParams& info, I3dl2ReverbState& state, bool should_clear);
83 // DSP Code 88 // DSP Code
84 s32 DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, 89 s32 DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count,
85 s32 channel, std::size_t mix_offset); 90 s32 channel, std::size_t mix_offset);
diff --git a/src/audio_core/common.h b/src/audio_core/common.h
index ec59a3ba9..fe546c55d 100644
--- a/src/audio_core/common.h
+++ b/src/audio_core/common.h
@@ -33,6 +33,29 @@ constexpr std::size_t TEMP_MIX_BASE_SIZE = 0x3f00; // TODO(ogniK): Work out this
33// and our const ends up being 0x3f04, the 4 bytes are most 33// and our const ends up being 0x3f04, the 4 bytes are most
34// likely the sample history 34// likely the sample history
35constexpr std::size_t TOTAL_TEMP_MIX_SIZE = TEMP_MIX_BASE_SIZE + AudioCommon::MAX_SAMPLE_HISTORY; 35constexpr std::size_t TOTAL_TEMP_MIX_SIZE = TEMP_MIX_BASE_SIZE + AudioCommon::MAX_SAMPLE_HISTORY;
36constexpr f32 I3DL2REVERB_MAX_LEVEL = 5000.0f;
37constexpr f32 I3DL2REVERB_MIN_REFLECTION_DURATION = 0.02f;
38constexpr std::size_t I3DL2REVERB_TAPS = 20;
39constexpr std::size_t I3DL2REVERB_DELAY_LINE_COUNT = 4;
40using Fractional = s32;
41
42template <typename T>
43constexpr Fractional ToFractional(T x) {
44 return static_cast<Fractional>(x * static_cast<T>(0x4000));
45}
46
47constexpr Fractional MultiplyFractional(Fractional lhs, Fractional rhs) {
48 return static_cast<Fractional>(static_cast<s64>(lhs) * rhs >> 14);
49}
50
51constexpr s32 FractionalToFixed(Fractional x) {
52 const auto s = x & (1 << 13);
53 return static_cast<s32>(x >> 14) + s;
54}
55
56constexpr s32 CalculateDelaySamples(s32 sample_rate_khz, float time) {
57 return FractionalToFixed(MultiplyFractional(ToFractional(sample_rate_khz), ToFractional(time)));
58}
36 59
37static constexpr u32 VersionFromRevision(u32_le rev) { 60static constexpr u32 VersionFromRevision(u32_le rev) {
38 // "REV7" -> 7 61 // "REV7" -> 7
diff --git a/src/audio_core/delay_line.cpp b/src/audio_core/delay_line.cpp
new file mode 100644
index 000000000..f4e4dd8d2
--- /dev/null
+++ b/src/audio_core/delay_line.cpp
@@ -0,0 +1,104 @@
1#include <cstring>
2#include "audio_core/delay_line.h"
3
4namespace AudioCore {
5DelayLineBase::DelayLineBase() = default;
6DelayLineBase::~DelayLineBase() = default;
7
8void DelayLineBase::Initialize(s32 max_delay_, float* src_buffer) {
9 buffer = src_buffer;
10 buffer_end = buffer + max_delay_;
11 max_delay = max_delay_;
12 output = buffer;
13 SetDelay(max_delay_);
14 Clear();
15}
16
17void DelayLineBase::SetDelay(s32 new_delay) {
18 if (max_delay < new_delay) {
19 return;
20 }
21 delay = new_delay;
22 input = (buffer + ((output - buffer) + new_delay) % (max_delay + 1));
23}
24
25s32 DelayLineBase::GetDelay() const {
26 return delay;
27}
28
29s32 DelayLineBase::GetMaxDelay() const {
30 return max_delay;
31}
32
33f32 DelayLineBase::TapOut(s32 last_sample) {
34 const float* ptr = input - (last_sample + 1);
35 if (ptr < buffer) {
36 ptr += (max_delay + 1);
37 }
38
39 return *ptr;
40}
41
42f32 DelayLineBase::Tick(f32 sample) {
43 *(input++) = sample;
44 const auto out_sample = *(output++);
45
46 if (buffer_end < input) {
47 input = buffer;
48 }
49
50 if (buffer_end < output) {
51 output = buffer;
52 }
53
54 return out_sample;
55}
56
57float* DelayLineBase::GetInput() {
58 return input;
59}
60
61const float* DelayLineBase::GetInput() const {
62 return input;
63}
64
65f32 DelayLineBase::GetOutputSample() const {
66 return *output;
67}
68
69void DelayLineBase::Clear() {
70 std::memset(buffer, 0, sizeof(float) * max_delay);
71}
72
73void DelayLineBase::Reset() {
74 buffer = nullptr;
75 buffer_end = nullptr;
76 max_delay = 0;
77 input = nullptr;
78 output = nullptr;
79 delay = 0;
80}
81
82DelayLineAllPass::DelayLineAllPass() = default;
83DelayLineAllPass::~DelayLineAllPass() = default;
84
85void DelayLineAllPass::Initialize(u32 delay_, float coeffcient_, f32* src_buffer) {
86 DelayLineBase::Initialize(delay_, src_buffer);
87 SetCoefficient(coeffcient_);
88}
89
90void DelayLineAllPass::SetCoefficient(float coeffcient_) {
91 coefficient = coeffcient_;
92}
93
94f32 DelayLineAllPass::Tick(f32 sample) {
95 const auto temp = sample - coefficient * *output;
96 return coefficient * temp + DelayLineBase::Tick(temp);
97}
98
99void DelayLineAllPass::Reset() {
100 coefficient = 0.0f;
101 DelayLineBase::Reset();
102}
103
104} // namespace AudioCore
diff --git a/src/audio_core/delay_line.h b/src/audio_core/delay_line.h
new file mode 100644
index 000000000..cafddd432
--- /dev/null
+++ b/src/audio_core/delay_line.h
@@ -0,0 +1,46 @@
1#pragma once
2
3#include "common/common_types.h"
4
5namespace AudioCore {
6
7class DelayLineBase {
8public:
9 DelayLineBase();
10 ~DelayLineBase();
11
12 void Initialize(s32 max_delay_, float* src_buffer);
13 void SetDelay(s32 new_delay);
14 s32 GetDelay() const;
15 s32 GetMaxDelay() const;
16 f32 TapOut(s32 last_sample);
17 f32 Tick(f32 sample);
18 float* GetInput();
19 const float* GetInput() const;
20 f32 GetOutputSample() const;
21 void Clear();
22 void Reset();
23
24protected:
25 float* buffer{nullptr};
26 float* buffer_end{nullptr};
27 s32 max_delay{};
28 float* input{nullptr};
29 float* output{nullptr};
30 s32 delay{};
31};
32
33class DelayLineAllPass final : public DelayLineBase {
34public:
35 DelayLineAllPass();
36 ~DelayLineAllPass();
37
38 void Initialize(u32 delay, float coeffcient_, f32* src_buffer);
39 void SetCoefficient(float coeffcient_);
40 f32 Tick(f32 sample);
41 void Reset();
42
43private:
44 float coefficient{};
45};
46} // namespace AudioCore
diff --git a/src/audio_core/effect_context.cpp b/src/audio_core/effect_context.cpp
index f770b9608..89e4573c7 100644
--- a/src/audio_core/effect_context.cpp
+++ b/src/audio_core/effect_context.cpp
@@ -90,6 +90,14 @@ s32 EffectBase::GetProcessingOrder() const {
90 return processing_order; 90 return processing_order;
91} 91}
92 92
93std::vector<u8>& EffectBase::GetWorkBuffer() {
94 return work_buffer;
95}
96
97const std::vector<u8>& EffectBase::GetWorkBuffer() const {
98 return work_buffer;
99}
100
93EffectI3dl2Reverb::EffectI3dl2Reverb() : EffectGeneric(EffectType::I3dl2Reverb) {} 101EffectI3dl2Reverb::EffectI3dl2Reverb() : EffectGeneric(EffectType::I3dl2Reverb) {}
94EffectI3dl2Reverb::~EffectI3dl2Reverb() = default; 102EffectI3dl2Reverb::~EffectI3dl2Reverb() = default;
95 103
@@ -117,6 +125,12 @@ void EffectI3dl2Reverb::Update(EffectInfo::InParams& in_params) {
117 usage = UsageState::Initialized; 125 usage = UsageState::Initialized;
118 params.status = ParameterStatus::Initialized; 126 params.status = ParameterStatus::Initialized;
119 skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; 127 skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0;
128 if (!skipped) {
129 auto& cur_work_buffer = GetWorkBuffer();
130 // Has two buffers internally
131 cur_work_buffer.resize(in_params.buffer_size * 2);
132 std::fill(cur_work_buffer.begin(), cur_work_buffer.end(), 0);
133 }
120 } 134 }
121} 135}
122 136
@@ -129,6 +143,14 @@ void EffectI3dl2Reverb::UpdateForCommandGeneration() {
129 GetParams().status = ParameterStatus::Updated; 143 GetParams().status = ParameterStatus::Updated;
130} 144}
131 145
146I3dl2ReverbState& EffectI3dl2Reverb::GetState() {
147 return state;
148}
149
150const I3dl2ReverbState& EffectI3dl2Reverb::GetState() const {
151 return state;
152}
153
132EffectBiquadFilter::EffectBiquadFilter() : EffectGeneric(EffectType::BiquadFilter) {} 154EffectBiquadFilter::EffectBiquadFilter() : EffectGeneric(EffectType::BiquadFilter) {}
133EffectBiquadFilter::~EffectBiquadFilter() = default; 155EffectBiquadFilter::~EffectBiquadFilter() = default;
134 156
diff --git a/src/audio_core/effect_context.h b/src/audio_core/effect_context.h
index c5e0b398c..5e0655dd7 100644
--- a/src/audio_core/effect_context.h
+++ b/src/audio_core/effect_context.h
@@ -8,6 +8,7 @@
8#include <memory> 8#include <memory>
9#include <vector> 9#include <vector>
10#include "audio_core/common.h" 10#include "audio_core/common.h"
11#include "audio_core/delay_line.h"
11#include "common/common_funcs.h" 12#include "common/common_funcs.h"
12#include "common/common_types.h" 13#include "common/common_types.h"
13#include "common/swap.h" 14#include "common/swap.h"
@@ -194,6 +195,8 @@ public:
194 [[nodiscard]] bool IsEnabled() const; 195 [[nodiscard]] bool IsEnabled() const;
195 [[nodiscard]] s32 GetMixID() const; 196 [[nodiscard]] s32 GetMixID() const;
196 [[nodiscard]] s32 GetProcessingOrder() const; 197 [[nodiscard]] s32 GetProcessingOrder() const;
198 [[nodiscard]] std::vector<u8>& GetWorkBuffer();
199 [[nodiscard]] const std::vector<u8>& GetWorkBuffer() const;
197 200
198protected: 201protected:
199 UsageState usage{UsageState::Invalid}; 202 UsageState usage{UsageState::Invalid};
@@ -201,6 +204,7 @@ protected:
201 s32 mix_id{}; 204 s32 mix_id{};
202 s32 processing_order{}; 205 s32 processing_order{};
203 bool enabled = false; 206 bool enabled = false;
207 std::vector<u8> work_buffer{};
204}; 208};
205 209
206template <typename T> 210template <typename T>
@@ -212,7 +216,7 @@ public:
212 return internal_params; 216 return internal_params;
213 } 217 }
214 218
215 const I3dl2ReverbParams& GetParams() const { 219 const T& GetParams() const {
216 return internal_params; 220 return internal_params;
217 } 221 }
218 222
@@ -229,6 +233,27 @@ public:
229 void UpdateForCommandGeneration() override; 233 void UpdateForCommandGeneration() override;
230}; 234};
231 235
236struct I3dl2ReverbState {
237 f32 lowpass_0{};
238 f32 lowpass_1{};
239 f32 lowpass_2{};
240
241 DelayLineBase early_delay_line{};
242 std::array<u32, AudioCommon::I3DL2REVERB_TAPS> early_tap_steps{};
243 f32 early_gain{};
244 f32 late_gain{};
245
246 u32 early_to_late_taps{};
247 std::array<DelayLineBase, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> fdn_delay_line{};
248 std::array<DelayLineAllPass, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> decay_delay_line0{};
249 std::array<DelayLineAllPass, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> decay_delay_line1{};
250 f32 last_reverb_echo{};
251 DelayLineBase center_delay_line{};
252 std::array<std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT>, 3> lpf_coefficients{};
253 std::array<f32, AudioCommon::I3DL2REVERB_DELAY_LINE_COUNT> shelf_filter{};
254 f32 dry_gain{};
255};
256
232class EffectI3dl2Reverb : public EffectGeneric<I3dl2ReverbParams> { 257class EffectI3dl2Reverb : public EffectGeneric<I3dl2ReverbParams> {
233public: 258public:
234 explicit EffectI3dl2Reverb(); 259 explicit EffectI3dl2Reverb();
@@ -237,8 +262,12 @@ public:
237 void Update(EffectInfo::InParams& in_params) override; 262 void Update(EffectInfo::InParams& in_params) override;
238 void UpdateForCommandGeneration() override; 263 void UpdateForCommandGeneration() override;
239 264
265 I3dl2ReverbState& GetState();
266 const I3dl2ReverbState& GetState() const;
267
240private: 268private:
241 bool skipped = false; 269 bool skipped = false;
270 I3dl2ReverbState state{};
242}; 271};
243 272
244class EffectBiquadFilter : public EffectGeneric<BiquadFilterParams> { 273class EffectBiquadFilter : public EffectGeneric<BiquadFilterParams> {