source: OniSplit/Sound/AifImporter.cs

Last change on this file 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: 1.6 KB
Line 
1using System;
2using System.IO;
3
4namespace Oni.Sound
5{
6 internal class AifImporter : Importer
7 {
8 private const int fcc_ima4 = 0x696d6134;
9 private static readonly byte[] sampleRate = new byte[10] { 0x40, 0x0d, 0xac, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
10
11 public AifImporter()
12 : base(InstanceFileHeader.OniMacTemplateChecksum)
13 {
14 }
15
16 public override void Import(string filePath, string outputDirPath)
17 {
18 var aif = AifFile.FromFile(filePath);
19
20 if (aif.Format != fcc_ima4)
21 {
22 Console.Error.WriteLine("Unsupported AIF compression (0x{0:X})", aif.Format);
23 return;
24 }
25
26 if (!Utils.ArrayEquals(aif.SampleRate, sampleRate))
27 {
28 Console.Error.WriteLine("Unsupported sample rate");
29 return;
30 }
31
32 if (aif.ChannelCount != 1 && aif.ChannelCount != 2)
33 {
34 Console.Error.WriteLine("Unsupported number of channels ({0})", aif.ChannelCount);
35 return;
36 }
37
38 BeginImport();
39 WriteSNDD(Path.GetFileNameWithoutExtension(filePath), aif);
40 Write(outputDirPath);
41 }
42
43 private void WriteSNDD(string name, AifFile aif)
44 {
45 int duration = (int)(aif.SampleFrames * 64.0f / 22050.0f * 60.0f);
46
47 var sndd = CreateInstance(TemplateTag.SNDD, name);
48
49 using (var writer = sndd.OpenWrite())
50 {
51 writer.Write((aif.ChannelCount == 1) ? 1 : 3);
52 writer.Write(duration);
53 writer.Write(aif.SoundData.Length);
54 writer.Write(WriteRawPart(aif.SoundData));
55 writer.Skip(8);
56 }
57 }
58 }
59}
Note: See TracBrowser for help on using the repository browser.