source: OniSplit/Sound/AifExporter.cs@ 1157

Last change on this file since 1157 was 1156, checked in by geyser, 4 years ago

Fixed importing of "fact" chunk, as well as an unhandled fatal when attempting to transcode IMA4 to MS ADPCM. Also added missing padding to SNDD template and fixed the duration calculation for WAV-to-SNDD.

File size: 2.5 KB
Line 
1using System;
2using System.IO;
3
4namespace 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}
Note: See TracBrowser for help on using the repository browser.