-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBfstmFile.cs
More file actions
255 lines (217 loc) · 9.26 KB
/
Copy pathBfstmFile.cs
File metadata and controls
255 lines (217 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System.Text;
using VGAudio.Containers;
using VGAudio.Containers.NintendoWare;
using VGAudio.Containers.Wave;
namespace BarsTool;
public record BfstmInfo(
int SampleRate,
int SampleCount,
int ChannelCount,
bool IsLooped,
int LoopStart,
byte Codec,
int SampleBlockCount,
int SampleBlockSize,
int SampleBlockSampleCount,
int LastBlockSize,
int LastBlockSampleCount,
int LastBlockPadSize,
int SeekSize,
int SeekIntervalSampleCount
);
public static class BfstmFile
{
private const int FILE_HEADER_SIZE = 0x40;
private const int PDAT_HEADER_SIZE = 0x40;
private const int PREFETCH_BLOCKS_PER_CHANNEL = 5;
public static BfstmInfo ReadInfo(byte[] data)
{
string magic = Encoding.ASCII.GetString(data, 0, 4);
if (magic != "FSTM" && magic != "FSTP")
throw new InvalidDataException($"Not a BFSTM/BFSTP file (got '{magic}').");
int numBlocks = BitConverter.ToUInt16(data, 16);
int infoOff = -1;
int pos = 20;
for (int i = 0; i < numBlocks; i++)
{
ushort blkType = BitConverter.ToUInt16(data, pos);
int blkOff = BitConverter.ToInt32(data, pos + 4);
if (blkType == 0x4000) infoOff = blkOff;
pos += 12;
}
if (infoOff < 0) throw new InvalidDataException("INFO block not found.");
int stmInfoRefOff = BitConverter.ToInt32(data, infoOff + 8 + 4);
int si = infoOff + 8 + stmInfoRefOff;
return new BfstmInfo(
SampleRate: BitConverter.ToInt32(data, si + 4),
SampleCount: BitConverter.ToInt32(data, si + 12),
ChannelCount: data[si + 2],
IsLooped: data[si + 1] != 0,
LoopStart: BitConverter.ToInt32(data, si + 8),
Codec: data[si],
SampleBlockCount: BitConverter.ToInt32(data, si + 16),
SampleBlockSize: BitConverter.ToInt32(data, si + 20),
SampleBlockSampleCount: BitConverter.ToInt32(data, si + 24),
LastBlockSize: BitConverter.ToInt32(data, si + 28),
LastBlockSampleCount: BitConverter.ToInt32(data, si + 32),
LastBlockPadSize: BitConverter.ToInt32(data, si + 36),
SeekSize: BitConverter.ToInt32(data, si + 40),
SeekIntervalSampleCount: BitConverter.ToInt32(data, si + 44)
);
}
public static byte[] ConvertToWav(byte[] bfstmData)
{
var reader = new BCFstmReader();
using var ms = new MemoryStream(bfstmData);
var audioData = reader.Read(ms);
var wavWriter = new WaveWriter();
using var outStream = new MemoryStream();
wavWriter.WriteToStream(audioData, outStream);
return outStream.ToArray();
}
public static byte[] ConvertFromWav(byte[] wavData, bool loop = false, int loopStart = 0, int loopEnd = 0)
{
var wavReader = new WaveReader();
using var ms = new MemoryStream(wavData);
var audioData = wavReader.Read(ms);
var writer = new BCFstmWriter(NwTarget.Cafe);
writer.Configuration.Endianness = VGAudio.Utilities.Endianness.LittleEndian;
writer.Configuration.Version = new NwVersion(5, 0, 0, 0);
writer.Configuration.SamplesPerSeekTableEntry = 0x3800;
writer.Configuration.SamplesPerInterleave = 0x3800;
using var outStream = new MemoryStream();
if (loop && loopEnd > 0)
{
var format = audioData.GetAllFormats().First();
writer.WriteToStream(format.WithLoop(true, loopStart, loopEnd), outStream);
}
else
{
writer.WriteToStream(audioData, outStream);
}
return outStream.ToArray();
}
public static byte[] RoundtripBfstm(byte[] bfstmData)
{
var reader = new BCFstmReader();
using var readMs = new MemoryStream(bfstmData);
AudioWithConfig awc = reader.ReadWithConfig(readMs);
var writer = new BCFstmWriter(NwTarget.Cafe);
writer.Configuration.Endianness = VGAudio.Utilities.Endianness.LittleEndian;
writer.Configuration.Version = new NwVersion(5, 0, 0, 0);
writer.Configuration.SamplesPerSeekTableEntry = 0x3800;
writer.Configuration.SamplesPerInterleave = 0x3800;
writer.Configuration.RecalculateSeekTable = false;
using var outMs = new MemoryStream();
writer.WriteToStream(awc.AudioFormat, outMs, awc.Configuration);
return outMs.ToArray();
}
public static byte[] GenerateBfstp(byte[] bfstmData)
{
string magic = Encoding.ASCII.GetString(bfstmData, 0, 4);
if (magic != "FSTM")
throw new InvalidDataException("Input must be a BFSTM file.");
int numBlocks = BitConverter.ToUInt16(bfstmData, 16);
int infoOff = -1, infoSize = -1;
int dataOff = -1;
int pos = 20;
for (int i = 0; i < numBlocks; i++)
{
ushort blkType = BitConverter.ToUInt16(bfstmData, pos);
int blkOff = BitConverter.ToInt32(bfstmData, pos + 4);
int blkSize = BitConverter.ToInt32(bfstmData, pos + 8);
if (blkType == 0x4000) { infoOff = blkOff; infoSize = blkSize; }
if (blkType == 0x4002) { dataOff = blkOff; }
pos += 12;
}
if (infoOff < 0 || dataOff < 0)
throw new InvalidDataException("BFSTM missing INFO or DATA block.");
byte[] infoBlock = new byte[infoSize];
Array.Copy(bfstmData, infoOff, infoBlock, 0, infoSize);
int stmInfoRefOff = BitConverter.ToInt32(infoBlock, 8 + 4);
int stmInfoPos = 8 + stmInfoRefOff;
int channels = infoBlock[stmInfoPos + 2];
int sampleBlkSize = BitConverter.ToInt32(infoBlock, stmInfoPos + 20);
int dataAudioStart = dataOff + 0x20;
int prefetchDataSize = PREFETCH_BLOCKS_PER_CHANNEL * channels * sampleBlkSize;
int pdatBlockSize = PDAT_HEADER_SIZE + prefetchDataSize;
int pdatOff = FILE_HEADER_SIZE + infoSize;
int fileSize = pdatOff + pdatBlockSize;
int sampleDataRefFieldOff = stmInfoPos + 48 + 4;
byte[] modifiedInfo = (byte[])infoBlock.Clone();
int newSdRefValue = pdatOff + PDAT_HEADER_SIZE;
BitConverter.TryWriteBytes(modifiedInfo.AsSpan(sampleDataRefFieldOff), newSdRefValue);
using var ms = new MemoryStream(fileSize);
using var w = new BinaryWriter(ms, Encoding.UTF8);
w.Write(Encoding.ASCII.GetBytes("FSTP"));
w.Write((ushort)0xFEFF); // BOM LE (bytes: FF FE)
w.Write((ushort)FILE_HEADER_SIZE);
w.Write(0x00040000u); // version
w.Write(fileSize);
w.Write((ushort)2); // 2 blocks
w.Write((ushort)0);
w.Write((ushort)0x4000); w.Write((ushort)0);
w.Write(FILE_HEADER_SIZE); w.Write(infoSize);
w.Write((ushort)0x4004); w.Write((ushort)0);
w.Write(pdatOff); w.Write(pdatBlockSize);
while (ms.Position < FILE_HEADER_SIZE) w.Write((byte)0);
w.Write(modifiedInfo);
// PDAT block
w.Write(Encoding.ASCII.GetBytes("PDAT"));
w.Write(pdatBlockSize);
w.Write(1); // flag: 1 = Switch LE
w.Write(0);
w.Write(prefetchDataSize);
w.Write(0);
w.Write(0);
w.Write(0x34); // data start offset
while (ms.Position < pdatOff + PDAT_HEADER_SIZE) w.Write((byte)0);
w.Write(bfstmData, dataAudioStart, prefetchDataSize);
return ms.ToArray();
}
public static (float Loudness, float Peak) ComputeAudioMetrics(byte[] bfstmData)
{
byte[] wavData = ConvertToWav(bfstmData);
using var ms = new MemoryStream(wavData);
using var reader = new BinaryReader(ms, Encoding.UTF8);
reader.ReadBytes(4); // RIFF
reader.ReadInt32();
reader.ReadBytes(4); // WAVE
int channels = 0, bitsPerSample = 0;
while (ms.Position < ms.Length)
{
string chunkId = Encoding.ASCII.GetString(reader.ReadBytes(4));
int chunkSize = reader.ReadInt32();
if (chunkId == "fmt ")
{
reader.ReadInt16();
channels = reader.ReadInt16();
reader.ReadInt32(); reader.ReadInt32(); reader.ReadInt16();
bitsPerSample = reader.ReadInt16();
if (chunkSize > 16) reader.ReadBytes(chunkSize - 16);
}
else if (chunkId == "data")
{
if (bitsPerSample != 16 || chunkSize == 0) return (0f, 0f);
int totalSamples = chunkSize / 2;
double sumSquares = 0;
int maxAbs = 0;
for (int i = 0; i < totalSamples; i++)
{
short sample = reader.ReadInt16();
int abs = Math.Abs((int)sample);
if (abs > maxAbs) maxAbs = abs;
sumSquares += sample / 32768.0 * (sample / 32768.0);
}
float peak = maxAbs / 32768f;
double rms = Math.Sqrt(sumSquares / totalSamples);
float loudness = rms > 0 ? (float)(20 * Math.Log10(rms)) : -100f;
return (loudness, peak);
}
else reader.ReadBytes(chunkSize);
}
return (0f, 0f);
}
private static int AlignUp(int value, int alignment) =>
(value + alignment - 1) / alignment * alignment;
}