forked from XAYRGA/ibnktool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstrumentBankv1.cs
522 lines (448 loc) · 16.4 KB
/
InstrumentBankv1.cs
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Be.IO;
using Newtonsoft.Json;
namespace ibnktool
{
public class JInstrumentBankv1
{
[JsonIgnore]
public int mBaseAddress = 0;
[JsonIgnore]
public uint mHash = 0;
private const int IBNK = 0x49424e4b;
private const int BANK = 0x42414E4B;
public uint size = 0;
public uint globalID = 0;
public JInstrument[] instruments = new JInstrument[0xF0];
public void loadFromStream(BeBinaryReader reader)
{
int mountpos = (int)reader.BaseStream.Position;
if (reader.ReadUInt32() != IBNK)
throw new InvalidDataException("Data is not IBNK!");
size = reader.ReadUInt32();
globalID = reader.ReadUInt32();
reader.BaseStream.Position = mountpos + 0x20;
if (reader.ReadUInt32() != BANK)
throw new InvalidDataException("Data is not BANK");
var instPtrs = util.readInt32Array(reader, 0xF0);
for (int i = 0; i < 0xF0; i++) {
reader.BaseStream.Position = instPtrs[i] + mountpos;
if (instPtrs[i] != 0)
instruments[i] = JInstrument.CreateFromStream(reader, mountpos);
}
}
public static JInstrumentBankv1 CreateFromStream(BeBinaryReader reader)
{
var b = new JInstrumentBankv1();
b.loadFromStream(reader);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(IBNK);
wr.Write(size);
wr.Write(globalID); wr.Flush();
wr.Write(new byte[0x14]);
wr.Write(BANK);
for (int i = 0; i < instruments.Length; i++)
wr.Write(instruments[i] == null ? 0 : instruments[i].mBaseAddress);
}
}
public class JInstrumentEnvelopev1
{
[JsonIgnore]
public int mBaseAddress = 0;
[JsonIgnore]
public uint mHash = 0;
public JEnvelopeVector[] points;
public class JEnvelopeVector
{
public ushort Mode;
public ushort Delay;
public short Value;
}
private void loadFromStream(BeBinaryReader reader)
{
var origPos = reader.BaseStream.Position;
int count = 0;
while (reader.ReadUInt16() < 0xB) {
reader.ReadUInt32();
count++;
}
count++;
reader.BaseStream.Position = origPos;
points = new JEnvelopeVector[count];
for (int i = 0; i < count; i++)
points[i] = new JEnvelopeVector { Mode = reader.ReadUInt16(), Delay = reader.ReadUInt16(), Value = reader.ReadInt16() };
}
public static JInstrumentEnvelopev1 CreateFromStream(BeBinaryReader reader)
{
var b = new JInstrumentEnvelopev1();
b.loadFromStream(reader);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
var remainingLength = 32;
for (int i=0; i < points.Length;i++)
{
remainingLength -= 6;
wr.Write(points[i].Mode);
wr.Write(points[i].Delay);
wr.Write(points[i].Value);
}
if (remainingLength > 0)
wr.Write(new byte[remainingLength]);
else
util.padTo(wr, 32);
}
}
public class JInstrumentOscillatorv1
{
[JsonIgnore]
public int mBaseAddress = 0;
[JsonIgnore]
public uint mHash = 0;
public byte Target;
public float Rate;
public JInstrumentEnvelopev1 Attack;
public JInstrumentEnvelopev1 Release;
public float Width;
public float Vertex;
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
mBaseAddress = (int)reader.BaseStream.Position;
Target = reader.ReadByte();
reader.ReadBytes(3);
Rate = reader.ReadSingle();
var envA = reader.ReadUInt32();
var envB = reader.ReadUInt32();
Width = reader.ReadSingle();
Vertex = reader.ReadSingle();
reader.BaseStream.Position = envA + seekbase;
if (envA > 0)
Attack = JInstrumentEnvelopev1.CreateFromStream(reader);
reader.BaseStream.Position = envB + seekbase;
if (envB > 0)
Release = JInstrumentEnvelopev1.CreateFromStream(reader);
}
public static JInstrumentOscillatorv1 CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JInstrumentOscillatorv1();
b.loadFromStream(reader,seekbase);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(Target);
wr.Write(new byte[0x3]);
wr.Write(Rate);
wr.Write(Attack.mBaseAddress);
wr.Write(Release.mBaseAddress);
wr.Write(Width);
wr.Write(Vertex);
wr.Write(new byte[8]);
}
}
public class JInstrumentSenseEffectv1
{
[JsonIgnore]
public int mBaseAddress = 0;
public byte Target;
public byte Register;
public byte Key;
public float Floor;
public float Ceiling;
private void loadFromStream(BeBinaryReader reader)
{
Target = reader.ReadByte();
reader.ReadBytes(3);
Register = reader.ReadByte();
Key = reader.ReadByte();
Floor = reader.ReadSingle();
Ceiling = reader.ReadSingle();
}
public static JInstrumentSenseEffectv1 CreateFromStream(BeBinaryReader reader)
{
var b = new JInstrumentSenseEffectv1();
b.loadFromStream(reader);
return b;
}
}
public class JInstrumentRandEffectv1
{
[JsonIgnore]
public int mBaseAddress = 0;
public byte Target;
public float Floor;
public float Ceiling;
private void loadFromStream(BeBinaryReader reader)
{
Target = reader.ReadByte();
reader.ReadBytes(3);
Floor = reader.ReadSingle();
Ceiling = reader.ReadSingle();
}
public static JInstrumentRandEffectv1 CreateFromStream(BeBinaryReader reader)
{
var b = new JInstrumentRandEffectv1();
b.loadFromStream(reader);
return b;
}
}
public class JInstrument
{
[JsonIgnore]
public int mBaseAddress = 0;
internal const int INST = 0x494E5354;
internal const int PER2 = 0x50455232;
public bool Percussion = false;
public static JInstrument CreateFromStream(BeBinaryReader reader, int seekbase)
{
var magic = reader.ReadUInt32();
if (magic == INST)
return JStandardInstrumentv1.CreateFromStream(reader, seekbase);
else if (magic == PER2)
return JPercussion.CreateFromStream(reader, seekbase);
return null;
}
}
public class JStandardInstrumentv1 : JInstrument
{
public float Pitch;
public float Volume;
public JInstrumentOscillatorv1 oscillatorA;
public JInstrumentOscillatorv1 oscillatorB;
public JInstrumentSenseEffectv1 effectA;
public JInstrumentSenseEffectv1 effectB;
public JInstrumentRandEffectv1 randA;
public JInstrumentRandEffectv1 randB;
public JKeyRegionv1[] keys;
public bool Percussion;
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
reader.ReadUInt32(); // Empty
Pitch = reader.ReadSingle();
Volume = reader.ReadSingle();
var oscA = reader.ReadUInt32();
var oscB = reader.ReadUInt32();
var effA = reader.ReadUInt32();
var effB = reader.ReadUInt32();
var ranA = reader.ReadUInt32();
var ranB = reader.ReadUInt32();
var keyRegCount = reader.ReadUInt32();
var keyRegPtrs = util.readInt32Array(reader, (int)keyRegCount);
keys = new JKeyRegionv1[keyRegCount];
reader.BaseStream.Position = oscA + seekbase;
if (oscA > 0)
oscillatorA = JInstrumentOscillatorv1.CreateFromStream(reader, seekbase);
reader.BaseStream.Position = oscB + seekbase;
if (oscB > 0)
oscillatorB = JInstrumentOscillatorv1.CreateFromStream(reader, seekbase);
reader.BaseStream.Position = effA + seekbase;
if (effA > 0)
effectA = JInstrumentSenseEffectv1.CreateFromStream(reader);
reader.BaseStream.Position = effB + seekbase;
if (effB > 0)
effectB = JInstrumentSenseEffectv1.CreateFromStream(reader);
reader.BaseStream.Position = ranA + seekbase;
if (ranA > 0)
randA = JInstrumentRandEffectv1.CreateFromStream(reader);
reader.BaseStream.Position = ranB + seekbase;
if (ranB > 0)
randB = JInstrumentRandEffectv1.CreateFromStream(reader);
for (int i=0; i < keyRegCount; i++)
{
reader.BaseStream.Position = keyRegPtrs[i] + seekbase;
if (keyRegPtrs[i] != 0)
keys[i] = JKeyRegionv1.CreateFromStream(reader, seekbase);
}
}
new public static JStandardInstrumentv1 CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JStandardInstrumentv1();
b.loadFromStream(reader,seekbase);
return b;
}
public void WritetoStream(BeBinaryWriter wr)
{
wr.Write(INST);
wr.Write(0);
wr.Write(Pitch);
wr.Write(Volume);
wr.Write(oscillatorA == null ? 0 : oscillatorA.mBaseAddress);
wr.Write(oscillatorB == null ? 0 : oscillatorB.mBaseAddress);
wr.Write(effectA == null ? 0 : effectA.mBaseAddress);
wr.Write(effectB == null ? 0 : effectB.mBaseAddress);
wr.Write(randA == null ? 0 : randA.mBaseAddress);
wr.Write(randB == null ? 0 : randB.mBaseAddress);
wr.Write(keys.Length);
for (int i = 0; i < keys.Length; i++)
wr.Write(keys[i].mBaseAddress);
}
}
public class JPercussion : JInstrument
{
public JPercussionEntry[] Sounds = new JPercussionEntry[100];
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
Percussion = true;
reader.ReadBytes(0x84); // Padding.
var keyRegPtrs = util.readInt32Array(reader, 100);
var anchor = reader.BaseStream.Position; // Store anchor at end of pointer table at base + 0x218
for (int i=0; i < 100; i++)
if (keyRegPtrs[i]!=0)
{
reader.BaseStream.Position = keyRegPtrs[i] + seekbase;
Sounds[i] = JPercussionEntry.CreateFromStream(reader, seekbase);
}
reader.BaseStream.Position = anchor; // Restore anchor.
reader.ReadBytes(0x70); // Padding
for (int i = 0; i < 100; i++)
{
var b = reader.ReadByte();
if (keyRegPtrs[i] != 0)
Sounds[i].uflag1 = b;
}
reader.ReadBytes(0x1c); // Also padding
for (int i = 0; i < 100; i++)
{
var b = reader.ReadUInt16();
if (keyRegPtrs[i] != 0)
Sounds[i].uflag2 = b;
}
// 0x50 padding.
reader.ReadBytes(0x50);
}
new public static JPercussion CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JPercussion();
b.loadFromStream(reader, seekbase);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(PER2);
wr.Write(new byte[0x84]);
for (int i = 0; i < 100; i++)
wr.Write(Sounds[i] != null ? Sounds[i].mBaseAddress : 0);
wr.Write(new byte[0x70]);
for (int i = 0; i < 100; i++)
wr.Write((byte)(Sounds[i] != null ? Sounds[i].uflag1 : 0));
wr.Write(new byte[0x1C]);
for (int i = 0; i < 100; i++)
wr.Write((short)(Sounds[i] != null ? Sounds[i].uflag2 : 0));
wr.Write(new byte[0x50]);
}
}
public class JPercussionEntry
{
[JsonIgnore]
public int mBaseAddress = 0;
public JVelocityRegionv1[] Velocities;
public float Pitch;
public float Volume;
public byte uflag1;
public ushort uflag2;
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
Pitch = reader.ReadSingle();
Volume = reader.ReadSingle();
reader.ReadBytes(8);
var velregCount = reader.ReadInt32();
Velocities = new JVelocityRegionv1[velregCount];
var velRegPtrs = util.readInt32Array(reader, velregCount);
for (int i = 0; i < velregCount; i++)
{
reader.BaseStream.Position = seekbase + velRegPtrs[i];
Velocities[i] = JVelocityRegionv1.CreateFromStream(reader, seekbase);
}
}
public static JPercussionEntry CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JPercussionEntry();
b.loadFromStream(reader, seekbase);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(Pitch);
wr.Write(Volume);
wr.Write(0L);
wr.Write(Velocities.Length);
for (int i = 0; i < Velocities.Length; i++)
wr.Write(Velocities[i].mBaseAddress);
}
}
public class JKeyRegionv1
{
[JsonIgnore]
public int mBaseAddress = 0;
public byte BaseKey;
public JVelocityRegionv1[] Velocities;
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
BaseKey = reader.ReadByte();
reader.ReadBytes(3);
var velregCount = reader.ReadInt32();
Velocities = new JVelocityRegionv1[velregCount];
var velRegPtrs = util.readInt32Array(reader, velregCount);
for (int i = 0; i < velregCount; i++) {
reader.BaseStream.Position = seekbase + velRegPtrs[i];
Velocities[i] = JVelocityRegionv1.CreateFromStream(reader, seekbase);
}
}
public static JKeyRegionv1 CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JKeyRegionv1();
b.loadFromStream(reader, seekbase);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(BaseKey);
wr.Write(new byte[0x3]);
wr.Write(Velocities.Length);
for (int i=0; i < Velocities.Length; i++)
wr.Write(Velocities[i].mBaseAddress);
util.padTo(wr,16);
}
}
public class JVelocityRegionv1
{
[JsonIgnore]
public int mBaseAddress = 0;
public byte Velocity;
public ushort WSYSID;
public ushort WaveID;
public float Volume;
public float Pitch;
private void loadFromStream(BeBinaryReader reader, int seekbase)
{
Velocity = reader.ReadByte();
reader.ReadBytes(3); // empty
WSYSID = reader.ReadUInt16();
WaveID = reader.ReadUInt16();
Volume = reader.ReadSingle();
Pitch = reader.ReadSingle();
}
public static JVelocityRegionv1 CreateFromStream(BeBinaryReader reader, int seekbase)
{
var b = new JVelocityRegionv1();
b.loadFromStream(reader, seekbase);
return b;
}
public void WriteToStream(BeBinaryWriter wr)
{
wr.Write(Velocity);
wr.Write(new byte[0x3]);
wr.Write(WSYSID);
wr.Write(WaveID);
wr.Write(Volume);
wr.Write(Pitch);
}
}
}