BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
BrawlLib.Wii.Audio.AudioConverter Class Reference

Static Public Member Functions

static void CalcCoefs (short *source, int samples, short *dest, IProgressTracker progress)
 
static void EncodeBlock (short *source, int samples, byte *dest, short *coefs)
 

Member Function Documentation

◆ CalcCoefs()

static void BrawlLib.Wii.Audio.AudioConverter.CalcCoefs ( short *  source,
int  samples,
short *  dest,
IProgressTracker  progress 
)
inlinestatic
10 {
11 //short[,] coefs = new short[8,2];
12
13 //double d10 = 10.0;
14 //int channels = 2;
15 //int num2 = 2;
16 //int chunkSamples = 14;
17 //int var30 = 0;
18 short* sPtr = source;
19 int numBlocks = (samples + 13) / 14;
20
21 int nBits = 3;
22 //while ((1 << ++nBits) < 8) ;
23
24 double** bufferArray = stackalloc double*[8];
25 for (int z = 0; z < 8; z++)
26 {
27 bufferArray[z] = (double*) Marshal.AllocHGlobal(3 * 8);
28 }
29
30 double* buffer2 = stackalloc double[3];
31 short* sampleBuffer = (short*) Marshal.AllocHGlobal(0x7000);
32 short* chunkBuffer = stackalloc short[28];
33 //double** chunkBuffer = (double**)chunkBufferData;
34 for (int z = 0; z < 28; z++)
35 {
36 chunkBuffer[z] = 0;
37 }
38
39 double* sChunkBuffer = stackalloc double[3];
40 double* omgBuffer = stackalloc double[3];
41
42 double** pChannels = stackalloc double*[3];
43 for (int z = 0; z <= 2; z++)
44 {
45 pChannels[z] = (double*) Marshal.AllocHGlobal(3 * 8);
46 }
47
48 int* anotherBuffer = stackalloc int[3];
49
50 double** multiBuffer = (double**) Marshal.AllocHGlobal(numBlocks * 4 * 2);
51 int unused = 0;
52 int multiIndex = 0;
53
54 int blockSamples;
55 int temp;
56
57 float initValue = 0;
58 int lastUpdate = 0;
59 if (progress != null)
60 {
61 initValue = progress.CurrentValue;
62 }
63
64 for (int x = samples; x > 0;)
65 {
66 if (x > 0x3800)
67 {
68 blockSamples = 0x3800;
69 x -= 0x3800;
70 }
71 else
72 {
73 blockSamples = x;
74 for (int z = 0; z < 14 && z + blockSamples < 0x3800; z++)
75 {
76 sampleBuffer[blockSamples + z] = 0;
77 }
78
79 x = 0;
80 }
81
82 short* tPtr = sampleBuffer;
83 for (int z = 0; z < blockSamples; z++)
84 {
85 *tPtr++ = *sPtr++;
86 }
87
88 for (int i = 0; i < blockSamples;)
89 {
90 for (int z = 0; z < 14; z++)
91 {
92 chunkBuffer[z] = chunkBuffer[z + 14];
93 }
94
95 for (int z = 0; z < 14; z++)
96 {
97 chunkBuffer[z + 14] = sampleBuffer[i++];
98 }
99
100 Something1(&chunkBuffer[14], sChunkBuffer);
101 if (Math.Abs(sChunkBuffer[0]) > 10.0)
102 {
103 Something2(&chunkBuffer[14], pChannels);
104 if (!Something3(pChannels, anotherBuffer, &temp))
105 {
106 Something4(pChannels, anotherBuffer, sChunkBuffer);
107 sChunkBuffer[0] = 1.0;
108 if (Something5(sChunkBuffer, omgBuffer) == 0)
109 {
110 multiBuffer[multiIndex] = (double*) Marshal.AllocHGlobal(3 * 8);
111 multiBuffer[multiIndex][0] = 1.0;
112 for (int z = 1; z <= 2; z++)
113 {
114 if (omgBuffer[z] >= 1.0)
115 {
116 omgBuffer[z] = 0.9999999999;
117 }
118
119 if (omgBuffer[z] <= -1.0)
120 {
121 omgBuffer[z] = -0.9999999999;
122 }
123 }
124
125 Something6(omgBuffer, multiBuffer[multiIndex]);
126 multiIndex++;
127 }
128 }
129 }
130
131 unused++;
132 }
133
134 if (progress != null)
135 {
136 lastUpdate += blockSamples;
137 if (lastUpdate % 0x3800 == 0)
138 {
139 progress.Update(progress.CurrentValue + 0x3800);
140 }
141 }
142 }
143
144 progress?.Update(initValue + samples);
145
146 sChunkBuffer[0] = 1.0;
147 for (int y = 1; y <= 2; y++)
148 {
149 sChunkBuffer[y] = 0.0;
150 }
151
152 for (int z = 0; z < multiIndex; z++)
153 {
154 Something7(multiBuffer[z], bufferArray[0]);
155 for (int y = 1; y <= 2; y++)
156 {
157 sChunkBuffer[y] = sChunkBuffer[y] + bufferArray[0][y];
158 }
159 }
160
161 for (int y = 1; y <= 2; y++)
162 {
163 sChunkBuffer[y] /= multiIndex;
164 }
165
166 double tmp;
167 Something8(sChunkBuffer, omgBuffer, bufferArray[0], &tmp);
168 for (int y = 1; y <= 2; y++)
169 {
170 if (omgBuffer[y] >= 1.0)
171 {
172 omgBuffer[y] = 0.9999999999;
173 }
174
175 if (omgBuffer[y] <= -1.0)
176 {
177 omgBuffer[y] = -0.9999999999;
178 }
179 }
180
181 Something6(omgBuffer, bufferArray[0]);
182
183 for (int w = 0; w < nBits;)
184 {
185 //int mask = 1 << w;
186 for (int z = 0; z <= 2; z++)
187 {
188 buffer2[z] = 0.0;
189 }
190
191 buffer2[1] = -1.0;
192 Something9(bufferArray, buffer2, 1 << w++, 0.01);
193 //w++;
194 Something10(bufferArray, 1 << w, multiBuffer, multiIndex, 0.0);
195 }
196
197 //Write output
198 for (int z = 0; z < 8; z++)
199 {
200 for (int y = 0; y < 2; y++)
201 {
202 double d = -bufferArray[z][y + 1] * 2048.0;
203 if (d > 0.0)
204 {
205 dest[(z << 1) + y] = d > 32767.0 ? (short) 32767 : (short) (d + 0.5);
206 }
207 else
208 {
209 dest[(z << 1) + y] = d < -32768.0 ? (short) -32768 : (short) (d - 0.5);
210 }
211 }
212 }
213
214 //Free memory
215 for (int i = 0; i < multiIndex; i++)
216 {
217 Marshal.FreeHGlobal((IntPtr) multiBuffer[i]);
218 }
219
220 Marshal.FreeHGlobal((IntPtr) multiBuffer);
221
222 for (int i = 0; i <= 2; i++)
223 {
224 Marshal.FreeHGlobal((IntPtr) pChannels[i]);
225 }
226
227 for (int i = 0; i < 8; i++)
228 {
229 Marshal.FreeHGlobal((IntPtr) bufferArray[i]);
230 }
231
232 Marshal.FreeHGlobal((IntPtr) sampleBuffer);
233
234 //return coefs;
235 }
float CurrentValue
Definition: IProgressTracker.cs:12

◆ EncodeBlock()

static void BrawlLib.Wii.Audio.AudioConverter.EncodeBlock ( short *  source,
int  samples,
byte *  dest,
short *  coefs 
)
inlinestatic
238 {
239 for (int i = 0; i < samples; i += 14, source += 14, dest += 8)
240 {
241 EncodeChunk(source, Math.Min(samples - i, 14), dest, coefs);
242 }
243 }

The documentation for this class was generated from the following file: