public static short[] ConvertToWav(Data.Buffer data, int level = 0, bool invert = false) { List <short> wavData = new List <short>(); while (data.Readable()) { int value = data.Pop <byte>(); value = value + level; if (invert) { value = 0xFF - value; } value *= 0xFF; wavData.Add((short)value); } return(wavData.ToArray()); }
public XMI(Data.Buffer data) { // Note: Chunk length and so on are encoded as big endian. // But as we don't use them we use little endian because // the XMI data is encoded in little endian. data.SetEndianess(Endian.Endianess.Little); // Form chunk if (data.ToString(4) != "FORM") { return; } data.Pop(4); // FORM data.Pop <uint>(); // FORM chunk length // format XDIR if (data.ToString(4) != "XDIR") { return; } data.Pop(4); // XDIR if (data.ToString(4) != "INFO") { return; } data.Pop(4); // INFO data.Pop <uint>(); // INFO chunk length int numTracks = data.Pop <ushort>(); if (numTracks != 1) { return; // we only support one track per file } if (data.ToString(4) != "CAT ") { return; } data.Pop(4); // CAT_ data.Pop <uint>(); // CAT chunk length // format XMID if (data.ToString(4) != "XMID") { return; } data.Pop(4); // XMID // load the one track // Form chunk if (data.ToString(4) != "FORM") { return; } data.Pop(4); // FORM data.Pop <uint>(); // FORM chunk length // format XMID if (data.ToString(4) != "XMID") { return; } data.Pop(4); // XMID // TIMB chunk if (data.ToString(4) != "TIMB") { return; } data.Pop(4); // TIMB data.Pop <uint>(); // TIMB chunk length int count = data.Pop <ushort>(); for (int j = 0; j < count; ++j) { // we don't need the TIMB information, just skip it data.Pop(2); } // EVNT chunk if (data.ToString(4) != "EVNT") { return; } data.Pop(4); // EVNT data.Pop <uint>(); // EVNT chunk length // read xmi/midi events while (data.Readable()) { ParseEvent(data); } var eventIndices = new Dictionary <Event, int>(); for (int i = 0; i < events.Count; ++i) { eventIndices.Add(events[i], i); } events.Sort((a, b) => { int result = a.StartTime.CompareTo(b.StartTime); if (result == 0) { return(eventIndices[a].CompareTo(eventIndices[b])); } return(result); }); }