libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
SoundMatrixDecoder.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <cmath>
5#include <array>
6#include <vector>
7#include <tuple>
8
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
13namespace Ship {
14
23 public:
28 SoundMatrixDecoder(int32_t sampleRate);
30
35 void ResetState();
36
43 std::tuple<const uint8_t*, int> Process(const uint8_t* buf, size_t len);
44
45 private:
52 struct BiquadCascade {
53 double X[4] = {}; // Input history
54 double Y[4] = {}; // Output history
55 };
56
63 struct FilterCoefficients {
64 double A[5]; // Feedforward (numerator)
65 double B[4]; // Feedback (denominator, excluding b0=1)
66 };
67
75 struct AllPassChain {
76 double Freq = 0;
77 double FreqMin = 0;
78 double FreqMax = 0;
79 double SweepRate = 0;
80 double XHist[4] = {};
81 double YHist[4] = {};
82 bool Ready = false;
83 };
84
88 static constexpr int gMaxDelay = 1024;
89 struct CircularDelay {
90 std::array<float, gMaxDelay> Data = {};
91 int Head = 0;
92 int Length = 0;
93 };
94
101 FilterCoefficients DesignLowPass(double frequency, int32_t sampleRate);
102
109 FilterCoefficients DesignHighPass(double frequency, int32_t sampleRate);
110
118 float ProcessFilter(float sample, BiquadCascade& state, const FilterCoefficients& coef);
119
125 void PrepareAllPass(AllPassChain& chain, int32_t sampleRate);
126
134 float ProcessAllPass(float sample, AllPassChain& chain, bool negate);
135
142 float ProcessDelay(float sample, CircularDelay& buffer);
143
149 static int16_t Saturate(float value);
150
151 int32_t mDelayLength = 0;
152 double mAllPassBaseRate = 1.0; // Precomputed for ProcessAllPass
153
154 // Filter coefficients (computed once per sample rate)
155 FilterCoefficients mCoefCenterHP;
156 FilterCoefficients mCoefCenterLP;
157 FilterCoefficients mCoefSurroundHP;
158 FilterCoefficients mCoefSubLP;
159
160 // Per-channel filter states
161 BiquadCascade mCenterHighPass;
162 BiquadCascade mCenterLowPass;
163 BiquadCascade mSurrLeftMainHP;
164 BiquadCascade mSurrLeftCrossHP;
165 BiquadCascade mSurrRightMainHP;
166 BiquadCascade mSurrRightCrossHP;
167 BiquadCascade mSubLowPass;
168
169 // Phase processing
170 AllPassChain mPhaseLeftMain;
171 AllPassChain mPhaseLeftCross;
172 AllPassChain mPhaseRightMain;
173 AllPassChain mPhaseRightCross;
174
175 // Timing
176 CircularDelay mDelaySurrLeft;
177 CircularDelay mDelaySurrRight;
178
179 // Output buffer
180 std::vector<int16_t> mSurroundBuffer;
181};
182
183} // namespace Ship
Definition SoundMatrixDecoder.h:22
std::tuple< const uint8_t *, int > Process(const uint8_t *buf, size_t len)
SoundMatrixDecoder(int32_t sampleRate)
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14
@ X
Definition SDLMapping.h:8
@ Y
Definition SDLMapping.h:8