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