1 |
using System; |
2 |
using System.IO; |
3 |
|
4 |
namespace Oni.Sound |
5 |
{ |
6 |
internal class WavExporter : SoundExporter |
7 |
{ |
8 |
#region Private data |
9 |
private const int fcc_RIFF = 0x46464952; |
10 |
private const int fcc_WAVE = 0x45564157; |
11 |
private const int fcc_fmt = 0x20746d66; |
12 |
private const int fcc_data = 0x61746164; |
13 |
|
14 |
private static readonly byte[] formatTemplate = new byte[50] |
15 |
{ |
16 |
0x02, 0, 0, 0, 0x22, 0x56, 0, 0, 0, 0, 0, 0, 0, 0x02, 0x04, 0, |
17 |
0x20, 0, 0xf4, 0x03, 0x07, 0, 0, 0x01, 0, 0, 0, 0x02, 0, 0xff, 0, 0, 0, 0, 0xc0, 0, 0x40, 0, |
18 |
0xf0, 0, 0, 0, 0xcc, 0x01, 0x30, 0xff, 0x88, 0x01, 0x18, 0xff |
19 |
}; |
20 |
|
21 |
#endregion |
22 |
|
23 |
public WavExporter(InstanceFileManager fileManager, string outputDirPath) |
24 |
: base(fileManager, outputDirPath) |
25 |
{ |
26 |
} |
27 |
|
28 |
protected override void ExportInstance(InstanceDescriptor descriptor) |
29 |
{ |
30 |
var sound = SoundData.Read(descriptor); |
31 |
|
32 |
using (var stream = File.Create(Path.Combine(OutputDirPath, descriptor.FullName + ".wav"))) |
33 |
using (var writer = new BinaryWriter(stream)) |
34 |
{ |
35 |
var format = (byte[])formatTemplate.Clone(); |
36 |
|
37 |
Array.Copy(BitConverter.GetBytes(sound.ChannelCount), 0, format, 2, 2); |
38 |
Array.Copy(BitConverter.GetBytes(sound.ChannelCount == 1 ? 11155 : 22311), 0, format, 8, 4); |
39 |
Array.Copy(BitConverter.GetBytes(sound.ChannelCount == 1 ? 512 : 1024), 0, format, 12, 2); |
40 |
|
41 |
writer.Write(fcc_RIFF); |
42 |
writer.Write(8 + format.Length + 8 + sound.Data.Length); |
43 |
writer.Write(fcc_WAVE); |
44 |
|
45 |
// |
46 |
// write format chunk |
47 |
// |
48 |
|
49 |
writer.Write(fcc_fmt); |
50 |
writer.Write(format.Length); |
51 |
writer.Write(format); |
52 |
|
53 |
// |
54 |
// write data chunk |
55 |
// |
56 |
|
57 |
writer.Write(fcc_data); |
58 |
writer.Write(sound.Data.Length); |
59 |
writer.Write(sound.Data); |
60 |
} |
61 |
} |
62 |
} |
63 |
} |