public SoundFont(Stream sfFile) { try { RiffChunk topLevelChunk = RiffChunk.GetTopLevelChunk(new BinaryReader(sfFile)); if (!(topLevelChunk.ChunkID == "RIFF")) { throw new InvalidDataException("Not a RIFF file"); } string text = topLevelChunk.ReadChunkID(); if (text != "sfbk") { throw new InvalidDataException(string.Format("Not a SoundFont ({0})", text)); } RiffChunk nextSubChunk = topLevelChunk.GetNextSubChunk(); if (!(nextSubChunk.ChunkID == "LIST")) { throw new InvalidDataException(string.Format("Not info list found ({0})", nextSubChunk.ChunkID)); } this.info = new InfoChunk(nextSubChunk); RiffChunk nextSubChunk2 = topLevelChunk.GetNextSubChunk(); this.sampleData = new SampleDataChunk(nextSubChunk2); nextSubChunk2 = topLevelChunk.GetNextSubChunk(); this.presetsChunk = new PresetsChunk(nextSubChunk2); } finally { if (sfFile != null) { ((IDisposable)sfFile).Dispose(); } } }
/// <summary> /// Loads a SoundFont from a stream /// </summary> /// <param name="sfFile">stream</param> public SoundFont(Stream sfFile) { using (sfFile) // a bit ugly, done to get Win store to compile { RiffChunk riff = RiffChunk.GetTopLevelChunk(new BinaryReader(sfFile)); if (riff.ChunkID == "RIFF") { string formHeader = riff.ReadChunkID(); if (formHeader != "sfbk") { throw new InvalidDataException(String.Format("Not a SoundFont ({0})", formHeader)); } RiffChunk list = riff.GetNextSubChunk(); if (list.ChunkID == "LIST") { //RiffChunk r = list.GetNextSubChunk(); info = new InfoChunk(list); RiffChunk r = riff.GetNextSubChunk(); sampleData = new SampleDataChunk(r); r = riff.GetNextSubChunk(); presetsChunk = new PresetsChunk(r); } else { throw new InvalidDataException(String.Format("Not info list found ({0})", list.ChunkID)); } } else { throw new InvalidDataException("Not a RIFF file"); } } }
/// <summary> /// Loads a SoundFont from a file /// </summary> /// <param name="fileName">Filename of the SoundFont</param> public SoundFont(string fileName) { using (FileStream sfFile = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { RiffChunk riff = RiffChunk.GetTopLevelChunk(new BinaryReader(sfFile)); if (riff.ChunkID == "RIFF") { string formHeader = riff.ReadChunkID(); if (formHeader != "sfbk") { throw new ApplicationException(String.Format("Not a SoundFont ({0})", formHeader)); } RiffChunk list = riff.GetNextSubChunk(); if (list.ChunkID == "LIST") { //RiffChunk r = list.GetNextSubChunk(); info = new InfoChunk(list); RiffChunk r = riff.GetNextSubChunk(); sampleData = new SampleDataChunk(r); r = riff.GetNextSubChunk(); presetsChunk = new PresetsChunk(r); } else { throw new ApplicationException(String.Format("Not info list found ({0})", list.ChunkID)); } } else { throw new ApplicationException("Not a RIFF file"); } } }