1 | using System; |
---|
2 | using System.IO; |
---|
3 | |
---|
4 | namespace Oni.Sound |
---|
5 | { |
---|
6 | internal class AifExporter : SoundExporter |
---|
7 | { |
---|
8 | #region Private data |
---|
9 | private bool do_pc_demo_test; |
---|
10 | |
---|
11 | private const int fcc_FORM = 0x464f524d; |
---|
12 | private const int fcc_AIFC = 0x41494643; |
---|
13 | private const int fcc_COMM = 0x434f4d4d; |
---|
14 | private const int fcc_ima4 = 0x696d6134; |
---|
15 | private const int fcc_SSND = 0x53534e44; |
---|
16 | |
---|
17 | private static readonly byte[] sampleRate = new byte[10] |
---|
18 | { |
---|
19 | 0x40, 0x0d, 0xac, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
---|
20 | }; |
---|
21 | |
---|
22 | #endregion |
---|
23 | |
---|
24 | public AifExporter(InstanceFileManager fileManager, string outputDirPath, bool noDemo = false) |
---|
25 | : base(fileManager, outputDirPath) |
---|
26 | { |
---|
27 | do_pc_demo_test = !noDemo; |
---|
28 | } |
---|
29 | |
---|
30 | protected override void ExportInstance(InstanceDescriptor descriptor) |
---|
31 | { |
---|
32 | var sound = SoundData.Read(descriptor, do_pc_demo_test); |
---|
33 | if (!(sound.IsIMA4)) |
---|
34 | throw new NotSupportedException("Transcoding from MS ADPCM (PC) to IMA4 ADPCM (Mac) not supported!"); |
---|
35 | |
---|
36 | using (var stream = File.Create(Path.Combine(OutputDirPath, descriptor.FullName + ".aif"))) |
---|
37 | using (var writer = new BinaryWriter(stream)) |
---|
38 | { |
---|
39 | writer.Write(Utils.ByteSwap(fcc_FORM)); |
---|
40 | writer.Write(Utils.ByteSwap(50 + sound.Data.Length)); |
---|
41 | writer.Write(Utils.ByteSwap(fcc_AIFC)); |
---|
42 | |
---|
43 | // |
---|
44 | // write COMM chunk |
---|
45 | // |
---|
46 | |
---|
47 | writer.Write(Utils.ByteSwap(fcc_COMM)); |
---|
48 | writer.Write(Utils.ByteSwap(22)); // chunk size |
---|
49 | writer.Write(Utils.ByteSwap((short)sound.ChannelCount)); |
---|
50 | writer.Write(Utils.ByteSwap(sound.Data.Length / (sound.ChannelCount * 34))); // numSampleFrames |
---|
51 | writer.Write(Utils.ByteSwap((short)16)); // sampleSize |
---|
52 | writer.Write(sampleRate); // sampleRate |
---|
53 | writer.Write(Utils.ByteSwap(fcc_ima4)); |
---|
54 | |
---|
55 | // |
---|
56 | // write SSND chunk |
---|
57 | // |
---|
58 | |
---|
59 | writer.Write(Utils.ByteSwap(fcc_SSND)); |
---|
60 | writer.Write(Utils.ByteSwap(8 + sound.Data.Length)); // chunk size |
---|
61 | writer.Write(0); |
---|
62 | writer.Write(0); |
---|
63 | writer.Write(sound.Data); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | } |
---|
68 | } |
---|