public static OJM.OMC Decode(string filename) { if (!Check(filename)) { throw new NotSupportedException("Invalid OMC OJM"); } var omc = new OJM.OMC(); using (var stream = new BufferStream(filename)) { stream.Seek(4, SeekOrigin.Begin); short waveCount = stream.ReadInt16(); short oggCount = stream.ReadInt16(); int waveOffset = stream.ReadInt32(); int oggOffset = stream.ReadInt32(); int fileSize = stream.ReadInt32(); var waveSamples = new Dictionary <int, OJM.OMC.WaveSample>(); stream.Seek(waveOffset, SeekOrigin.Begin); for (short i = 0; i < waveCount; i++) { var sample = new OJM.OMC.WaveSample() { Id = i, Name = stream.ReadString(32), AudioFormat = stream.ReadInt16(), ChannelCount = stream.ReadInt16(), SampleRate = stream.ReadInt32(), Bitrate = stream.ReadInt32(), BlockAlign = stream.ReadInt16(), BitsPerSample = stream.ReadInt16(), UnkData = stream.ReadInt32() }; int size = stream.ReadInt32(); if (size > 0) { using (var waveStream = new BufferStream()) { waveStream.Write("RIFF"); waveStream.Write((int)(size + 36)); waveStream.Write("WAVE"); waveStream.Write("fmt "); waveStream.Write((int)16); // PCM OJM Format waveStream.Write((short)sample.AudioFormat); waveStream.Write((short)sample.ChannelCount); waveStream.Write((int)sample.SampleRate); waveStream.Write((int)sample.Bitrate); waveStream.Write((short)sample.BlockAlign); waveStream.Write((short)sample.BitsPerSample); waveStream.Write("data"); waveStream.Write((int)size); waveStream.Write(DecodeWave(stream.ReadBytes(size))); sample.Payload = waveStream.ToArray(); } waveSamples.Add(sample.Id, sample); } } var oggSamples = new Dictionary <int, OJM.OMC.OggSample>(); stream.Seek(oggOffset, SeekOrigin.Begin); for (short i = 1000; i < oggCount + 1000; i++) { var sample = new OJM.OMC.OggSample() { Id = i, Name = stream.ReadString(32) }; int size = stream.ReadInt32(); if (size > 0) { sample.Payload = stream.ReadBytes(size); oggSamples.Add(sample.Id, sample); } } omc.WaveSamples = waveSamples; omc.OggSamples = oggSamples; return(omc); } }
public static byte[] Encode(OJM.OMC omc) { using (var stream = new BufferStream()) { stream.Write("OMC\0"); stream.Write(omc.WaveCount); stream.Write(omc.OggCount); stream.Write(omc.WaveOffset); stream.Write(omc.OggOffset); stream.Write(omc.FileSize); stream.Seek(omc.WaveOffset, SeekOrigin.Begin); for (short i = 0; i < omc.WaveSamples.Max((sample) => sample.Value.Id); i++) { OJM.OMC.WaveSample sample = null; string name = string.Empty.PadNull(32); short audioFormat = 0; short channelCount = 0; int sampleRate = 0; int bitrate = 0; short blockAlign = 0; short bitsPerSample = 0; int unkData = 0; int size = 0; if (omc.WaveSamples.ContainsKey(i)) { sample = omc.WaveSamples[i]; name = sample.Name.PadNull(32); audioFormat = sample.AudioFormat; channelCount = sample.ChannelCount; sampleRate = sample.SampleRate; bitrate = sample.Bitrate; blockAlign = sample.BlockAlign; bitsPerSample = sample.BitsPerSample; unkData = sample.UnkData; size = sample.Size; } stream.Write(name); stream.Write(audioFormat); stream.Write(channelCount); stream.Write(sampleRate); stream.Write(bitrate); stream.Write(blockAlign); stream.Write(bitsPerSample); stream.Write(unkData); stream.Write(size); if (sample != null && size > 0) { byte[] payload = sample.Payload; var info = EncodeTo <WavEncoder>(payload); sample.Payload = payload; using (var waveStream = new BufferStream(payload)) { waveStream.Seek(44, SeekOrigin.Begin); stream.Write(EncodeWave(waveStream.ReadRemaining())); } } } stream.Seek(omc.OggOffset, SeekOrigin.Begin); for (short i = 1000; i < omc.OggSamples.Max((sample) => sample.Value.Id); i++) { var sample = omc.OggSamples.ContainsKey(i) ? omc.OggSamples[i] : null; if (sample == null) { continue; } stream.Write(sample.Name.PadNull(32)); if (sample.Size > 0) { byte[] payload = sample.Payload; EncodeTo <OggEncoder>(payload); sample.Payload = payload; stream.Write(sample.Size); stream.Write(payload); } } return(stream.ToArray()); } }