private void Menu_ExportVAG() { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "VAG|*.vag"; var id_str = Data.ID.ToString(); sfd.FileName = id_str; if (sfd.ShowDialog() == DialogResult.OK) { FileStream file = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write); BinaryWriter writer = new BinaryWriter(file); writer.Write("VAGp".ToCharArray()); writer.Write(20); writer.Write(0); writer.Write(RawData.Length); writer.Write(BitConv.FlipBytes(Data.Freq)); writer.Write(0); writer.Write(0); writer.Write(0); char[] name = new char[16] { (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0, (char)0 }; Array.Copy(id_str.ToCharArray(), name, Math.Min(id_str.Length, 16)); writer.Write(name); //writer.Write(0); writer.Write(0); writer.Write(0); writer.Write(0); writer.Write(RawData); writer.Close(); } }
private void PopulateList() { listBox1.Items.Clear(); tracks.Clear(); BinaryReader mh = new BinaryReader(new FileStream(fileName, FileMode.Open)); int count = mh.ReadInt32(); interleave = mh.ReadInt32(); while (count-- > 0) { tracks.Add(new Track() { Type = mh.ReadInt32(), Size = mh.ReadInt32(), Offset = mh.ReadUInt32(), SampleRate = mh.ReadInt32(), Unknown = mh.ReadInt32() }); } mh.Close(); mb_name = fileName.Substring(0, fileName.LastIndexOf('.') + 1) + "MB"; BinaryReader mb = new BinaryReader(new FileStream(mb_name, FileMode.Open)); for (int i = 0; i < tracks.Count; ++i) { mb.BaseStream.Position = tracks[i].Offset; if (tracks[i].Type == 0) { byte[] header = mb.ReadBytes(0x30); if (BitConverter.ToUInt32(header, 0) != msvp_key || BitConverter.ToUInt32(header, 4) != 0x20000000 || BitConverter.ToUInt32(header, 8) != 0 || BitConv.FlipBytes(BitConverter.ToUInt32(header, 12)) != tracks[i].Size - 0x30) { throw new Exception("Type 0 audio stream is in invalid format."); } else if (BitConv.FlipBytes(BitConverter.ToInt32(header, 16)) != tracks[i].SampleRate) { tracks[i].SampleRate = BitConv.FlipBytes(BitConverter.ToInt32(header, 16)); } char[] name = new char[0x10]; Array.Copy(header, 0x20, name, 0, 0x10); tracks[i].Name = new string(name); if (tracks[i].Name.IndexOf('\0') > 0) { tracks[i].Name = tracks[i].Name.Substring(0, tracks[i].Name.IndexOf('\0')); listBox1.Items.Add($"{tracks[i].Name} (Track {i})"); } else { tracks[i].Name = string.Empty; listBox1.Items.Add($"Track {i}"); } } else if (tracks[i].Type == 2) { listBox1.Items.Add($"Track {i} (null track)"); } else { listBox1.Items.Add($"Track {i}"); } } mb.Close(); label1.Text = $"Track count: {tracks.Count}"; label10.Text = $"Interleave: {interleave} bytes"; }
private void addTrackToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "WAV files|*.wav"; if (ofd.ShowDialog() == DialogResult.OK) { BinaryReader reader = new BinaryReader(new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)); if (new string(reader.ReadChars(4)) != "RIFF" || reader.ReadInt32() != reader.BaseStream.Length - 8 || new string(reader.ReadChars(4)) != "WAVE" || new string(reader.ReadChars(4)) != "fmt " || reader.ReadInt32() != 16 || reader.ReadInt16() != 1) { return; } ushort channels = reader.ReadUInt16(); int samplerate = reader.ReadInt32(); if (channels > 2 || reader.ReadInt32() != samplerate * channels * 2 || reader.ReadInt16() != channels * 2 || reader.ReadInt16() != 16 || new string(reader.ReadChars(4)) != "data") { return; } BinaryWriter mb = new BinaryWriter(new FileStream(mb_name, FileMode.Append, FileAccess.Write)); long mb_start_seek = mb.BaseStream.Position; if (mb_start_seek > uint.MaxValue) { return; } int readsize = reader.ReadInt32(); byte[] vag_data; if (channels == 1) { vag_data = ADPCM.FromPCMMono(reader.ReadBytes(readsize)); mb.Write("MSVp".ToCharArray()); mb.Write(0x20000000); mb.Write(0); mb.Write(BitConv.FlipBytes(vag_data.Length)); mb.Write(BitConv.FlipBytes(samplerate)); mb.Write(0); mb.Write(0); mb.Write(0); string fname_no_ext = ofd.SafeFileName.Substring(0, ofd.SafeFileName.LastIndexOf('.')); for (int i = 0; i < 16; ++i) { if (i < fname_no_ext.Length) { mb.Write(fname_no_ext[i]); } else { mb.Write(0); } } mb.Write(vag_data); } else if (channels == 2) { vag_data = ADPCM.FromPCMStereo(reader.ReadBytes(readsize), interleave); mb.Write(vag_data); } } }