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(); } } }
internal InfoChunk(RiffChunk chunk) { bool ifilPresent = false; bool inamPresent = false; if (chunk.ReadChunkID() != "INFO") { throw new InvalidDataException("Not an INFO chunk"); } //this.chunk = chunk; RiffChunk c; while ((c = chunk.GetNextSubChunk()) != null) { switch (c.ChunkID) { case "ifil": ifilPresent = true; SoundFontVersion = c.GetDataAsStructure(new SFVersionBuilder()); break; case "isng": WaveTableSoundEngine = c.GetDataAsString(); break; case "INAM": inamPresent = true; BankName = c.GetDataAsString(); break; case "irom": DataROM = c.GetDataAsString(); break; case "iver": ROMVersion = c.GetDataAsStructure(new SFVersionBuilder()); break; case "ICRD": CreationDate = c.GetDataAsString(); break; case "IENG": Author = c.GetDataAsString(); break; case "IPRD": TargetProduct = c.GetDataAsString(); break; case "ICOP": Copyright = c.GetDataAsString(); break; case "ICMT": Comments = c.GetDataAsString(); break; case "ISFT": Tools = c.GetDataAsString(); break; default: throw new InvalidDataException($"Unknown chunk type {c.ChunkID}"); } } if (!ifilPresent) { throw new InvalidDataException("Missing SoundFont version information"); } // n.b. issue #150 - it is valid for isng not to be present if (!inamPresent) { throw new InvalidDataException("Missing SoundFont name information"); } }
internal PresetsChunk(RiffChunk chunk) { string header = chunk.ReadChunkID(); if (header != "pdta") { throw new InvalidDataException(String.Format("Not a presets data chunk ({0})", header)); } RiffChunk c; while ((c = chunk.GetNextSubChunk()) != null) { switch (c.ChunkID) { case "PHDR": case "phdr": c.GetDataAsStructureArray(presetHeaders); break; case "PBAG": case "pbag": c.GetDataAsStructureArray(presetZones); break; case "PMOD": case "pmod": c.GetDataAsStructureArray(presetZoneModulators); break; case "PGEN": case "pgen": c.GetDataAsStructureArray(presetZoneGenerators); break; case "INST": case "inst": c.GetDataAsStructureArray(instruments); break; case "IBAG": case "ibag": c.GetDataAsStructureArray(instrumentZones); break; case "IMOD": case "imod": c.GetDataAsStructureArray(instrumentZoneModulators); break; case "IGEN": case "igen": c.GetDataAsStructureArray(instrumentZoneGenerators); break; case "SHDR": case "shdr": c.GetDataAsStructureArray(sampleHeaders); break; default: throw new InvalidDataException(String.Format("Unknown chunk type {0}", c.ChunkID)); } } // now link things up instrumentZoneGenerators.Load(sampleHeaders.SampleHeaders); instrumentZones.Load(instrumentZoneModulators.Modulators, instrumentZoneGenerators.Generators); instruments.LoadZones(instrumentZones.Zones); presetZoneGenerators.Load(instruments.Instruments); presetZones.Load(presetZoneModulators.Modulators, presetZoneGenerators.Generators); presetHeaders.LoadZones(presetZones.Zones); sampleHeaders.RemoveEOS(); }