NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
snd_Util.h
1#ifndef NW4R_SND_UTIL_H
2#define NW4R_SND_UTIL_H
3
4/*******************************************************************************
5 * headers
6 */
7
8#include <types.h>
9
10#include "nw4r/NW4RAssert.hpp"
11
12/*******************************************************************************
13 * classes and functions
14 */
15
16namespace nw4r { namespace snd { namespace detail
17{
18 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2a4bc2
19 class Util
20 {
21 public:
22 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2a4932
23 enum RefType
24 {
25 REFTYPE_ADDRESS,
26 REFTYPE_OFFSET,
27 };
28
29 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2a497b
30 enum PanCurve
31 {
32 PAN_CURVE_SQRT,
33 PAN_CURVE_SINCOS,
34 PAN_CURVE_LINEAR,
35 };
36
37 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x241fd, 0x242b9...
38 template <typename, typename = void, typename = void, typename = void>
39 struct DataRef
40 {
41 u8 refType; // size 0x01, offset 0x00
42 u8 dataType; // size 0x01, offset 0x01
43 u16 reserved;
44 ulong value; // size 0x04, offset 0x04
45 }; // size 0x08
46
47 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x247c7, 0x249c5...
48 template <typename T>
49 struct Table
50 {
51 ulong count; // size 0x04, offset 0x00
52 T item[1]; // size T, offset 0x04
53 }; // size 0x04 + T
54
55 // [R89JEL]:/bin/RVL/Debug/mainD.elf:.debug::0x2a49db
56 struct PanInfo
57 {
58 PanCurve curve; // size 0x04, offset 0x00
59 bool centerZeroFlag; // size 0x01, offset 0x04
60 bool zeroClampFlag; // size 0x01, offset 0x05
61 /* 2 bytes padding */
62 }; // size 0x08
63
64 static f32 CalcPitchRatio(int pitch);
65 static f32 CalcVolumeRatio(f32 dB);
66 static f32 CalcPanRatio(f32 pan, PanInfo const &info);
67 static f32 CalcSurroundPanRatio(f32 pan, PanInfo const &info);
68
69 static u16 CalcLpfFreq(f32 scale);
70
71 static void GetRemoteFilterCoefs(int filter, u16 *b0, u16 *b1, u16 *b2,
72 u16 *a1, u16 *a2);
73
74 static u16 CalcRandom();
75
76 static void const *GetDataRefAddressImpl(RefType refType, ulong value,
77 void const *baseAddress);
78
79// The only way these asserts happen one line after the other is with macros
80#define DEF_GET_DATA_REF_ADDRESS_(line_, index_) \
81 template <typename T0, typename T1, typename T2, typename T3> \
82 static inline T ## index_ const *GetDataRefAddress ## index_( \
83 Util::DataRef<T0, T1, T2, T3> const &ref, void const *baseAddress) \
84 { \
85 /* specifically not the header variant */ \
86 NW4RAssert_Line(line_, ref.dataType == index_); \
87 \
88 return static_cast<T ## index_ const *>(GetDataRefAddressImpl( \
89 static_cast<RefType>(ref.refType), ref.value, baseAddress)); \
90 } \
91 STATIC_ASSERT(true) // swallow semicolon
92
93 DEF_GET_DATA_REF_ADDRESS_(141, 0);
94 DEF_GET_DATA_REF_ADDRESS_(142, 1);
95 DEF_GET_DATA_REF_ADDRESS_(143, 2);
96 DEF_GET_DATA_REF_ADDRESS_(144, 3);
97
98#undef DEF_GET_DATA_REF_ADDRESS_
99
100 static inline u16 ReadBigEndian(u16 x)
101 {
102 return x;
103 }
104
105 static inline ulong ReadBigEndian(ulong x)
106 {
107 return x;
108 }
109
110 static f32 const CALC_LPF_FREQ_INTERCEPT;
111 static int const CALC_LPF_FREQ_TABLE_SIZE = 3 * 8;
112 static int const BIQUAD_COEF_COUNT = 5;
113 static int const IIR_COEF_COUNT;
114 static int const COEF_TABLE_SIZE = 128;
115 static int const COEF_TABLE_MAX = 127;
116 static int const COEF_TABLE_MIN = 0;
117 static int const PAN_TABLE_SIZE = 256 + 1;
118 static int const PAN_TABLE_CENTER = 128;
119 static int const PAN_TABLE_MIN = 0;
120 static int const PAN_TABLE_MAX = 256;
121 static int const DECIBEL_SQUARE_TABLE_SIZE;
122 static int const DECIBEL_TABLE_SIZE = 964 + 1;
123 static int const VOLUME_TABLE_SIZE;
124 static int const OCTAVE_DIVISION = 12; // ? makes sense to me
125 static int const PAN_CURVE_NUM = 3;
126 static int const CALC_DECIBEL_SCALE_MAX;
127 static int const PITCH_DIVISION_RANGE = 256; // ? maybe makes sense?
128 static int const PITCH_DIVISION_BIT;
129 static int const PAN_CENTER;
130 static int const PAN_MAX;
131 static int const PAN_MIN;
132 static int const VOLUME_DB_MAX = 60;
133 static int const VOLUME_DB_MIN = -904;
134 static u16 const VOLUME_MAX;
135 static u16 const VOLUME_MIN;
136
137 static u16 const CalcLpfFreqTable[CALC_LPF_FREQ_TABLE_SIZE];
138 static u16 const RemoteFilterCoefTable[COEF_TABLE_SIZE][BIQUAD_COEF_COUNT];
139 static f32 const Pan2RatioTableLinear[PAN_TABLE_SIZE];
140 static f32 const Pan2RatioTableSinCos[PAN_TABLE_SIZE];
141 static f32 const Pan2RatioTableSqrt[PAN_TABLE_SIZE];
142 static f32 const *PanTableTable[PAN_CURVE_NUM];
143 static f32 const Decibel2RatioTable[DECIBEL_TABLE_SIZE];
144 static f32 const PitchTable[PITCH_DIVISION_RANGE];
145 static f32 const NoteTable[OCTAVE_DIVISION];
146 }; // "namespace" Util
147}}} // namespace nw4r::snd::detail
148
149#endif // NW4R_SND_UTIL_H