//Add. private void addToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog f = new OpenFileDialog(); f.Filter = "Swav File|*.swav|Wave|*.wav"; f.Title = "Import the file"; f.ShowDialog(); if (f.FileName != "") { if (f.FilterIndex == 1) { List <byte[]> files = file.data[tree.SelectedNode.Index].files.ToList(); files.Add(File.ReadAllBytes(f.FileName)); file.data[tree.SelectedNode.Index].files = files.ToArray(); file.fixOffsets(); updateNodes(); } else { //Soundout. RIFF r = new RIFF(); r.load(File.ReadAllBytes(f.FileName)); List <byte[]> files = file.data[tree.SelectedNode.Index].files.ToList(); files.Add(r.toSwav().toBytes()); file.data[tree.SelectedNode.Index].files = files.ToArray(); file.fixOffsets(); updateNodes(); } } }
private void Import_Click(object sender, EventArgs e) { OpenFileDialog f = new OpenFileDialog(); f.Filter = "Supported Files|*.swav;*.wav|Swav File|*.swav|Wave|*.wav"; f.Title = "Import the file"; f.ShowDialog(); if (f.FileName != "") { if (f.FileName.EndsWith(".swav")) { f.FilterIndex = 1; } if (f.FileName.EndsWith(".wav")) { f.FilterIndex = 2; } if (f.FilterIndex == 1) { file.data[0].files[tree.SelectedNode.Index] = File.ReadAllBytes(f.FileName); } else { //Make new swav. RIFF r = new RIFF(); r.load(File.ReadAllBytes(f.FileName)); file.data[0].files[tree.SelectedNode.Index] = r.toSwav().toBytes(); } } }
//Sound menu. #region soundMenu //Add above. private void addAbove_Click(object sender, EventArgs e) { OpenFileDialog f = new OpenFileDialog(); f.Filter = "Swav File|*.swav|Wave|*.wav"; f.Title = "Import the file"; f.ShowDialog(); if (f.FileName != "") { if (f.FilterIndex == 1) { List <byte[]> files = file.data[0].files.ToList(); files.Insert(tree.SelectedNode.Index, File.ReadAllBytes(f.FileName)); file.data[0].files = files.ToArray(); file.fixOffsets(); updateNodes(); } else { //Make new swav. RIFF r = new RIFF(); r.load(File.ReadAllBytes(f.FileName)); List <byte[]> files = file.data[0].files.ToList(); files.Insert(tree.SelectedNode.Index, r.toSwav().toBytes()); file.data[0].files = files.ToArray(); file.fixOffsets(); updateNodes(); } } }
public RIFFsub(BinaryReader br, FileStream sss, RIFF rfx) { long pos = sss.Position; header = br.ReadChars(4); Length = br.ReadInt32(); Tag = br.ReadChars(4); rfx.rss.Add(pos, this); }
private void button1_Click(object sender, EventArgs e) { Track selTrack = tracks[listBox1.SelectedIndex]; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "WAV|*.wav"; sfd.FileName = selTrack.Type != 0 ? $"{listBox1.SelectedIndex}" : $"{listBox1.SelectedIndex} - {selTrack.Name}"; if (sfd.ShowDialog() == DialogResult.OK) { BinaryWriter writer = new BinaryWriter(new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write)); BinaryReader mb = new BinaryReader(new FileStream(mb_name, FileMode.Open)); mb.BaseStream.Position = curTrack.Offset + (curTrack.Type == 0 ? 0x30 : 0); byte[] rawData = mb.ReadBytes(curTrack.Type == 0 ? curTrack.Size - 0x30 : curTrack.Size); mb.Close(); writer.Write(curTrack.Type == 1 ? RIFF.SaveRiff(ADPCM.ToPCMStereo(rawData, rawData.Length, interleave), 2, curTrack.SampleRate) : RIFF.SaveRiff(ADPCM.ToPCMMono(rawData, rawData.Length), 1, curTrack.SampleRate)); writer.Close(); } }
private void button3_Click(object sender, EventArgs e) { if (tracks[listBox1.SelectedIndex].Type == 2) { return; } player.Stop(); player.Stream = null; button3.Enabled = trackBar1.Enabled = false; curTrack = tracks[listBox1.SelectedIndex]; BinaryReader mb = new BinaryReader(new FileStream(mb_name, FileMode.Open)); mb.BaseStream.Position = curTrack.Offset + (curTrack.Type == 0 ? 0x30 : 0); byte[] rawData = mb.ReadBytes(curTrack.Type == 0 ? curTrack.Size - 0x30 : curTrack.Size); mb.Close(); sndData = curTrack.Type == 1 ? RIFF.SaveRiff(ADPCM.ToPCMStereo(rawData, rawData.Length, interleave), 2, curTrack.SampleRate) : RIFF.SaveRiff(ADPCM.ToPCMMono(rawData, rawData.Length), 1, curTrack.SampleRate); player.Stream = new MemoryStream(sndData); player.Play(); seekWatch = Stopwatch.StartNew(); curName = (string)listBox1.SelectedItem; button3.Enabled = button4.Enabled = trackBar1.Enabled = seekTimer.Enabled = true; }
// Token: 0x060002F8 RID: 760 RVA: 0x0000B3B4 File Offset: 0x000095B4 public static Wave FromStream(Stream stream) { Wave result; using (RIFF riff = new RIFF(stream)) { Wave wave = new Wave(); if (riff.Identification != "WAVE") { throw new Exception("Given Stream does not contain a WAVE file."); } riff.GotoChunk(riff.Items.Chunks.Single((RIFFChunk chunk) => chunk.Identification == "fmt ")); wave.Format = (WaveFormat)riff.BinaryReader.Read16(); wave.ChannelCount = riff.BinaryReader.Read16(); wave.SampleRate = riff.BinaryReader.Read32(); riff.BinaryReader.Step(8L); wave.BitsPerSample = riff.BinaryReader.Read16(); RIFFChunk riffchunk = riff.Items.Chunks.Single((RIFFChunk chunk) => chunk.Identification == "data"); riff.GotoChunk(riffchunk); wave.Data = riff.BinaryReader.Read8s((int)riffchunk.Length); result = wave; } return(result); }
/// <summary> /// Read all but sample data from sf2 into memory. /// </summary> /// <param name="fname">File name.</param> public void init(string fname) { outp = string.Empty; nn = MidiHelper.OctaveMacro(); fstream = File.Open(fname, FileMode.Open, FileAccess.Read); bread = new BinaryReader(fstream,System.Text.Encoding.ASCII); riff = new RIFF(fstream,bread); int len; string format = string.Format( "RIFF: '{0}', " + "Length: {1:###,###,###,##0}, " + "Tag: '{2}'\n" + "----------------------\n", IOHelper.GetString(riff.header), riff.Length, IOHelper.GetString(riff.Tag) ); System.Diagnostics.Debug.Print(format); outp += format; foreach (KeyValuePair<long,SoundFont2.RIFFsub> rsx in riff.rss) { outp += ("" + rsx.Key.ToString("##,###,###,##0")).PadLeft(12,' ') + " | " + IOHelper.GetString(rsx.Value.header)+(" <"+rsx.Value.Length.ToString() +">").PadLeft(16)+" \'" + IOHelper.GetString(rsx.Value.Tag) + "\'\r\n"; string foo = (IOHelper.GetString(rsx.Value.Tag)); switch (foo) { case "INFO": len = rsx.Value.Length; nfo = new INFO(len,bread,fstream); foreach (KeyValuePair<long, INFOsub> mox in nfo.nfosub) { string vers = ""; long po; ZSTR nx; switch (mox.Value.Type) { case "ifil": po = fstream.Position; fstream.Seek(mox.Key+8, SeekOrigin.Begin); nfo.ifil.Major = bread.ReadInt16(); nfo.ifil.Minor = bread.ReadInt16(); fstream.Seek(po, SeekOrigin.Begin); vers = nfo.ifil.Major.ToString() + "." + nfo.ifil.Minor.ToString(); break; case "isng": po = fstream.Position; fstream.Seek(mox.Key+8, SeekOrigin.Begin); nfo.isng.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nfo.isng.StrValue).ToString() + ""; break; case "INAM": po = fstream.Position; fstream.Seek(mox.Key+8, SeekOrigin.Begin); nfo.inam.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nfo.inam.StrValue).ToString() + ""; break; default: po = fstream.Position; nx = new ZSTR(); fstream.Seek(mox.Key+8, SeekOrigin.Begin); nx.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nx.StrValue).ToString().Trim((char)0).Trim((char)0x22) + ""; break; } outp += "" + mox.Key.ToString("##,###,###,##0").PadLeft(12) + ": " + (mox.Value.Type) + ": " +vers.TrimEnd((char)0)+ "\r\n"; } break; case "pdta": len = rsx.Value.Length; hyde = new listHYDRA(fstream,bread,rsx.Key); outp += "PHDR"+hyde.phdr.Count.ToString()+"\r\n"; foreach (PHDR ph in hyde.phdr) { outp += ph.preset.ToString().PadLeft(3,'0') + ":" + ph.bank.ToString().PadLeft(3,'0') + " " + IOHelper.GetZerodStr(ph.presetName) + " (lib=" + ph.genera.ToString() + " genera=" + ph.genera.ToString() + " morphology=" + ph.morphology.ToString() + " pbagndx=" + ph.presetBagIndex.ToString() + ")\r\n"; } outp += "PBAG"+hyde.pbag.Count.ToString()+"\r\n"; foreach (PBAG pb in hyde.pbag) outp += "\t" + pb.gen.ToString() + ":" + pb.mod.ToString() + "\r\n"; outp += "PGEN"+hyde.pgen.Count.ToString()+"\r\n"; foreach (PGEN pm in hyde.pgen) { outp += "\tsfGen="+pm.SFGen.ToString() + " hi=" + pm.TypeHi.ToString() + " lo=" + pm.TypeLo.ToString() + "\r\n"; } outp += "PMOD"+hyde.pmod.Count.ToString()+"\r\n"; foreach (PMOD pm in hyde.pmod) { outp += "\tsrc="+pm.src.ToString() + " dst=" + pm.dst.ToString() + " amt=" + pm.amt.ToString() + " amtsrc=" + pm.amtsrc.ToString() + " trans=" + pm.trans.ToString()+ "\r\n"; } outp += "INST"+hyde.inst.Count.ToString()+"\r\n"; foreach (INST pms in hyde.inst) { outp += "name="+IOHelper.GetZerodStr(pms.iName) + " iBag=" + pms.bagIndex.ToString() + "\r\n"; } outp += "IBAG: "+hyde.ibag.Count.ToString()+"\r\n"; foreach (IBAG pms in hyde.ibag) { outp += "\tBagIndex=" + pms.gen.ToString() + " dst=" + pms.mod.ToString() + "\r\n"; } outp += "IMOD: "+hyde.imod.Count.ToString()+"\r\n"; foreach (IMOD pm in hyde.imod) { outp += string.Format( "\t amt: {0}, src: {1}, dst: {2}, amtsrc: {3}, trans: {4}\n", pm.amt,pm.src,pm.dst,pm.amtsrc,pm.trans ); } outp += "SHDR: "+hyde.shdr.Count.ToString()+"\r\n"; foreach (SHDR pm in hyde.shdr) { outp += IOHelper.GetZerodStr(pm.iName).ToString() + " src=" + pm.LenA.ToString() + " trans=" + pm.LenB.ToString() + "\r\n"; } break; } } fstream.Close(); bread.Close(); // } // ofd.Dispose(); }
/// <summary> /// Recursively merge all files in a directory with a WAV header /// </summary> /// <param name="dir">Merging directory</param> /// <returns></returns> private IEnumerable <FileInfo> CollapseWaves(DirectoryInfo dir) { const short CHANNELS = 1; const int SAMPLE_RATE = 16000; const short BITS_PER_SAMPLE = 16; const int HEADER_SIZE = 44; //size of a wav header //these are all case-sensitive / length-sensitive const string RIFF = "RIFF"; const string WAVEfmt = "WAVEfmt "; const string DATA = "data"; var files = dir.GetFiles().Where(f => f.Extension.ToLower() == ".wav" || f.Extension.ToLower() == ".efd"); if (files.Count() <= 1) { return(files); } else { //get first file in the directory var first = new FileStream(files.ElementAt(0).FullName, FileMode.Open, FileAccess.Read); //get second file var second = new FileStream(files.ElementAt(1).FullName, FileMode.Open, FileAccess.Read); //each file has a header, so add the total bytes and then subtract the header size for each int length = (int)(first.Length + second.Length - (2 * HEADER_SIZE)); var merged = new FileStream(Path.Combine(dir.FullName, Path.GetRandomFileName()) + ".wav", FileMode.Create, FileAccess.Write); var bw = new BinaryWriter(merged); bw.Write(RIFF.ToCharArray()); bw.Write(length + HEADER_SIZE); bw.Write(WAVEfmt.ToCharArray()); //chunk information and audio format info bw.Write((int)16); bw.Write((short)1); bw.Write(CHANNELS); bw.Write(SAMPLE_RATE); bw.Write((int)(SAMPLE_RATE * ((BITS_PER_SAMPLE * CHANNELS) / 8))); bw.Write((short)((BITS_PER_SAMPLE * CHANNELS) / 8)); bw.Write(BITS_PER_SAMPLE); bw.Write(DATA.ToCharArray()); bw.Write(length); //read the files with an offset of the headersize byte[] arrFirst = new byte[first.Length]; first.Read(arrFirst, HEADER_SIZE, arrFirst.Length - HEADER_SIZE); byte[] arrSecond = new byte[second.Length]; second.Read(arrSecond, HEADER_SIZE, arrSecond.Length - HEADER_SIZE); first.Close(); second.Close(); bw.Write(arrFirst); bw.Write(arrSecond); bw.Close(); merged.Close(); //delete merged files File.Delete(files.ElementAt(0).FullName); File.Delete(files.ElementAt(1).FullName); return(CollapseWaves(dir)); } }
public RIFFsub(BinaryReader br, FileStream sss,RIFF rfx) { long pos = sss.Position; header = br.ReadChars(4); Length = br.ReadInt32(); Tag = br.ReadChars(4); rfx.rss.Add(pos,this); }
public void LoadSoundData() { RawData = new byte[Data.SoundSize]; Array.Copy(Data.Parent.ExtraData, Data.SoundOffset, RawData, 0, Data.SoundSize); SoundData = RIFF.SaveRiff(ADPCM.ToPCMMono(RawData, (int)Data.SoundSize), 1, Data.Freq); }
/// <summary> /// Read all but sample data from sf2 into memory. /// </summary> /// <param name="fname">File name.</param> public void init(string fname) { outp = string.Empty; nn = MidiHelper.OctaveMacro(); fstream = File.Open(fname, FileMode.Open, FileAccess.Read); bread = new BinaryReader(fstream, System.Text.Encoding.ASCII); riff = new RIFF(fstream, bread); int len; string format = string.Format( "RIFF: '{0}', " + "Length: {1:###,###,###,##0}, " + "Tag: '{2}'\n" + "----------------------\n", IOHelper.GetString(riff.header), riff.Length, IOHelper.GetString(riff.Tag) ); System.Diagnostics.Debug.Print(format); outp += format; foreach (KeyValuePair <long, SoundFont2.RIFFsub> rsx in riff.rss) { outp += ("" + rsx.Key.ToString("##,###,###,##0")).PadLeft(12, ' ') + " | " + IOHelper.GetString(rsx.Value.header) + (" <" + rsx.Value.Length.ToString() + ">").PadLeft(16) + " \'" + IOHelper.GetString(rsx.Value.Tag) + "\'\r\n"; string foo = (IOHelper.GetString(rsx.Value.Tag)); switch (foo) { case "INFO": len = rsx.Value.Length; nfo = new INFO(len, bread, fstream); foreach (KeyValuePair <long, INFOsub> mox in nfo.nfosub) { string vers = ""; long po; ZSTR nx; switch (mox.Value.Type) { case "ifil": po = fstream.Position; fstream.Seek(mox.Key + 8, SeekOrigin.Begin); nfo.ifil.Major = bread.ReadInt16(); nfo.ifil.Minor = bread.ReadInt16(); fstream.Seek(po, SeekOrigin.Begin); vers = nfo.ifil.Major.ToString() + "." + nfo.ifil.Minor.ToString(); break; case "isng": po = fstream.Position; fstream.Seek(mox.Key + 8, SeekOrigin.Begin); nfo.isng.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nfo.isng.StrValue).ToString() + ""; break; case "INAM": po = fstream.Position; fstream.Seek(mox.Key + 8, SeekOrigin.Begin); nfo.inam.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nfo.inam.StrValue).ToString() + ""; break; default: po = fstream.Position; nx = new ZSTR(); fstream.Seek(mox.Key + 8, SeekOrigin.Begin); nx.StrValue = bread.ReadChars(mox.Value.Length); fstream.Seek(po, SeekOrigin.Begin); vers = IOHelper.GetString(nx.StrValue).ToString().Trim((char)0).Trim((char)0x22) + ""; break; } outp += "" + mox.Key.ToString("##,###,###,##0").PadLeft(12) + ": " + (mox.Value.Type) + ": " + vers.TrimEnd((char)0) + "\r\n"; } break; case "pdta": len = rsx.Value.Length; hyde = new listHYDRA(fstream, bread, rsx.Key); outp += "PHDR" + hyde.phdr.Count.ToString() + "\r\n"; foreach (PHDR ph in hyde.phdr) { outp += ph.preset.ToString().PadLeft(3, '0') + ":" + ph.bank.ToString().PadLeft(3, '0') + " " + IOHelper.GetZerodStr(ph.presetName) + " (lib=" + ph.genera.ToString() + " genera=" + ph.genera.ToString() + " morphology=" + ph.morphology.ToString() + " pbagndx=" + ph.presetBagIndex.ToString() + ")\r\n"; } outp += "PBAG" + hyde.pbag.Count.ToString() + "\r\n"; foreach (PBAG pb in hyde.pbag) { outp += "\t" + pb.gen.ToString() + ":" + pb.mod.ToString() + "\r\n"; } outp += "PGEN" + hyde.pgen.Count.ToString() + "\r\n"; foreach (PGEN pm in hyde.pgen) { outp += "\tsfGen=" + pm.SFGen.ToString() + " hi=" + pm.TypeHi.ToString() + " lo=" + pm.TypeLo.ToString() + "\r\n"; } outp += "PMOD" + hyde.pmod.Count.ToString() + "\r\n"; foreach (PMOD pm in hyde.pmod) { outp += "\tsrc=" + pm.src.ToString() + " dst=" + pm.dst.ToString() + " amt=" + pm.amt.ToString() + " amtsrc=" + pm.amtsrc.ToString() + " trans=" + pm.trans.ToString() + "\r\n"; } outp += "INST" + hyde.inst.Count.ToString() + "\r\n"; foreach (INST pms in hyde.inst) { outp += "name=" + IOHelper.GetZerodStr(pms.iName) + " iBag=" + pms.bagIndex.ToString() + "\r\n"; } outp += "IBAG: " + hyde.ibag.Count.ToString() + "\r\n"; foreach (IBAG pms in hyde.ibag) { outp += "\tBagIndex=" + pms.gen.ToString() + " dst=" + pms.mod.ToString() + "\r\n"; } outp += "IMOD: " + hyde.imod.Count.ToString() + "\r\n"; foreach (IMOD pm in hyde.imod) { outp += string.Format( "\t amt: {0}, src: {1}, dst: {2}, amtsrc: {3}, trans: {4}\n", pm.amt, pm.src, pm.dst, pm.amtsrc, pm.trans ); } outp += "SHDR: " + hyde.shdr.Count.ToString() + "\r\n"; foreach (SHDR pm in hyde.shdr) { outp += IOHelper.GetZerodStr(pm.iName).ToString() + " src=" + pm.LenA.ToString() + " trans=" + pm.LenB.ToString() + "\r\n"; } break; } } fstream.Close(); bread.Close(); // } // ofd.Dispose(); }