source: OniSplit/Sound/WavImporter.cs@ 1115

Last change on this file since 1115 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.3 KB
Line 
1using System;
2using System.IO;
3
4namespace Oni.Sound
5{
6 internal class WavImporter : Importer
7 {
8 public override void Import(string filePath, string outputDirPath)
9 {
10 var wav = WavFile.FromFile(filePath);
11
12 if (wav.Format != WavFormat.Pcm && wav.Format != WavFormat.Adpcm)
13 {
14 Console.Error.WriteLine("Unsupported WAV format (0x{0:X})", wav.Format);
15 return;
16 }
17
18 if (wav.ChannelCount != 1 && wav.ChannelCount != 2)
19 {
20 Console.Error.WriteLine("Unsupported number of channels ({0})", wav.ChannelCount);
21 return;
22 }
23
24 if (wav.SampleRate != 22050 && wav.SampleRate != 44100)
25 {
26 Console.Error.WriteLine("Unsupported sample rate ({0} Hz)", wav.SampleRate);
27 return;
28 }
29
30 if (wav.ExtraData.Length > 32)
31 throw new NotSupportedException(string.Format("Unsupported wave format extra data size ({0})", wav.ExtraData.Length));
32
33 BeginImport();
34 WriteSNDD(Path.GetFileNameWithoutExtension(filePath), wav);
35 Write(outputDirPath);
36 }
37
38 private void WriteSNDD(string name, WavFile wav)
39 {
40 float duration = wav.SoundData.Length * 8.0f / wav.BitsPerSample;
41 duration /= wav.SampleRate;
42 duration /= wav.ChannelCount;
43 duration *= 60.0f;
44
45 var sndd = CreateInstance(TemplateTag.SNDD, name);
46
47 using (var writer = sndd.OpenWrite(8))
48 {
49 writer.Write((short)wav.Format);
50 writer.WriteInt16(wav.ChannelCount);
51 writer.Write(wav.SampleRate);
52 writer.Write(wav.AverageBytesPerSecond);
53 writer.WriteInt16(wav.BlockAlign);
54 writer.WriteInt16(wav.BitsPerSample);
55 writer.WriteInt16(wav.ExtraData.Length);
56 writer.Write(wav.ExtraData);
57 writer.Skip(32 - wav.ExtraData.Length);
58 writer.Write((short)duration);
59 writer.Write(wav.SoundData.Length);
60 writer.Write(WriteRawPart(wav.SoundData));
61 }
62 }
63 }
64}
Note: See TracBrowser for help on using the repository browser.