forked from ACEmulator/ACE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWave.cs
74 lines (58 loc) · 2.7 KB
/
Wave.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ACE.DatLoader.FileTypes
{
/// <summary>
/// These are client_portal.dat files starting with 0x0A. All are stored in .WAV data format, though the header slightly different than a .WAV file header.
/// I'm not sure of an instance where the server would ever need this data, but it's fun nonetheless and included for completion sake.
/// </summary>
[DatFileType(DatFileType.Wave)]
public class Wave : FileType
{
public byte[] Header { get; private set; }
public byte[] Data { get; private set; }
public override void Unpack(BinaryReader reader)
{
int objectId = reader.ReadInt32();
int headerSize = reader.ReadInt32();
int dataSize = reader.ReadInt32();
Header = reader.ReadBytes(headerSize);
Data = reader.ReadBytes(dataSize);
}
/// <summary>
/// Exports Wave to a playable .wav file
/// </summary>
public void ExportWave(string directory)
{
string ext = ".wav";
if (Header[0] == 0x55) ext = ".mp3";
string filename = Path.Combine(directory, Id.ToString("X8") + ext);
// Good summary of the header for a WAV file and what all this means
// http://www.topherlee.com/software/pcm-tut-wavformat.html
FileStream f = new FileStream(filename, FileMode.Create);
ReadData(f);
f.Close();
}
public void ReadData(Stream stream)
{
var binaryWriter = new BinaryWriter(stream);
binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));
uint filesize = (uint)(Data.Length + 36); // 36 is added for all the extra we're adding for the WAV header format
binaryWriter.Write(filesize);
binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));
binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("fmt"));
binaryWriter.Write((byte)0x20); // Null ending to the fmt
binaryWriter.Write((int)0x10); // 16 ... length of all the above
// AC audio headers start at Format Type,
// and are usually 18 bytes, with some exceptions
// notably objectID A000393 which is 30 bytes
// WAV headers are always 16 bytes from Format Type to end of header,
// so this extra data is truncated here.
binaryWriter.Write(Header.Take(16).ToArray());
binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("data"));
binaryWriter.Write((uint)Data.Length);
binaryWriter.Write(Data);
}
}
}