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"); } } }
public SampleDataChunk(RiffChunk chunk) { string header = chunk.ReadChunkID(); if (header != "sdta") { throw new InvalidDataException(String.Format("Not a sample data chunk ({0})", header)); } var smplChunk = chunk.GetNextSubChunk(); sampleData = smplChunk.GetData(); var sm24Chunk = chunk.GetNextSubChunk(); if (sm24Chunk?.ChunkID == "sm24" && sm24Chunk.ChunkSize == smplChunk.ChunkSize / 2) { sampleData24 = sm24Chunk.GetData(); } else { //ignore if next subchunk is not a sm24 chunk or the size is not exactly half of smpl data } }
internal InfoChunk(RiffChunk chunk) { bool ifilPresent = false; bool isngPresent = 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; verSoundFont = (SFVersion) c.GetDataAsStructure(new SFVersionBuilder()); break; case "isng": isngPresent = true; waveTableSoundEngine = c.GetDataAsString(); break; case "INAM": INAMPresent = true; bankName = c.GetDataAsString(); break; case "irom": dataROM = c.GetDataAsString(); break; case "iver": verROM = (SFVersion) 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(String.Format("Unknown chunk type {0}",c.ChunkID)); } } if(!ifilPresent) { throw new InvalidDataException("Missing SoundFont version information"); } if(!isngPresent) { throw new InvalidDataException("Missing wavetable sound engine information"); } if(!INAMPresent) { throw new InvalidDataException("Missing SoundFont name information"); } }
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 InfoChunk(RiffChunk chunk) { bool flag = false; bool flag2 = false; bool flag3 = false; if (chunk.ReadChunkID() != "INFO") { throw new InvalidDataException("Not an INFO chunk"); } RiffChunk nextSubChunk; while ((nextSubChunk = chunk.GetNextSubChunk()) != null) { string chunkID; switch (chunkID = nextSubChunk.ChunkID) { case "ifil": flag = true; this.verSoundFont = nextSubChunk.GetDataAsStructure <SFVersion>(new SFVersionBuilder()); continue; case "isng": flag2 = true; this.waveTableSoundEngine = nextSubChunk.GetDataAsString(); continue; case "INAM": flag3 = true; this.bankName = nextSubChunk.GetDataAsString(); continue; case "irom": this.dataROM = nextSubChunk.GetDataAsString(); continue; case "iver": this.verROM = nextSubChunk.GetDataAsStructure <SFVersion>(new SFVersionBuilder()); continue; case "ICRD": this.creationDate = nextSubChunk.GetDataAsString(); continue; case "IENG": this.author = nextSubChunk.GetDataAsString(); continue; case "IPRD": this.targetProduct = nextSubChunk.GetDataAsString(); continue; case "ICOP": this.copyright = nextSubChunk.GetDataAsString(); continue; case "ICMT": this.comments = nextSubChunk.GetDataAsString(); continue; case "ISFT": this.tools = nextSubChunk.GetDataAsString(); continue; } throw new InvalidDataException(string.Format("Unknown chunk type {0}", nextSubChunk.ChunkID)); } if (!flag) { throw new InvalidDataException("Missing SoundFont version information"); } if (!flag2) { throw new InvalidDataException("Missing wavetable sound engine information"); } if (!flag3) { throw new InvalidDataException("Missing SoundFont name information"); } }
internal InfoChunk(RiffChunk chunk) { bool ifilPresent = false; bool isngPresent = false; bool INAMPresent = false; if(chunk.ReadChunkID() != "INFO") { throw new ApplicationException("Not an INFO chunk"); } //this.chunk = chunk; RiffChunk c; while((c = chunk.GetNextSubChunk()) != null) { switch(c.ChunkID) { case "ifil": ifilPresent = true; verSoundFont = (SFVersion) c.GetDataAsStructure(new SFVersionBuilder()); break; case "isng": isngPresent = true; waveTableSoundEngine = c.GetDataAsString(); break; case "INAM": INAMPresent = true; bankName = c.GetDataAsString(); break; case "irom": dataROM = c.GetDataAsString(); break; case "iver": verROM = (SFVersion) 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 ApplicationException(String.Format("Unknown chunk type {0}",c.ChunkID)); } } if(!ifilPresent) { throw new ApplicationException("Missing SoundFont version information"); } if(!isngPresent) { throw new ApplicationException("Missing wavetable sound engine information"); } if(!INAMPresent) { throw new ApplicationException("Missing SoundFont name information"); } }
// Token: 0x06000438 RID: 1080 RVA: 0x0000E058 File Offset: 0x0000C258 internal PresetsChunk(RiffChunk chunk) { string text = chunk.ReadChunkID(); if (text != "pdta") { throw new InvalidDataException(string.Format("Not a presets data chunk ({0})", text)); } RiffChunk nextSubChunk; while ((nextSubChunk = chunk.GetNextSubChunk()) != null) { string chunkID; switch (chunkID = nextSubChunk.ChunkID) { case "PHDR": case "phdr": nextSubChunk.GetDataAsStructureArray <Preset>(this.presetHeaders); continue; case "PBAG": case "pbag": nextSubChunk.GetDataAsStructureArray <Zone>(this.presetZones); continue; case "PMOD": case "pmod": nextSubChunk.GetDataAsStructureArray <Modulator>(this.presetZoneModulators); continue; case "PGEN": case "pgen": nextSubChunk.GetDataAsStructureArray <Generator>(this.presetZoneGenerators); continue; case "INST": case "inst": nextSubChunk.GetDataAsStructureArray <Instrument>(this.instruments); continue; case "IBAG": case "ibag": nextSubChunk.GetDataAsStructureArray <Zone>(this.instrumentZones); continue; case "IMOD": case "imod": nextSubChunk.GetDataAsStructureArray <Modulator>(this.instrumentZoneModulators); continue; case "IGEN": case "igen": nextSubChunk.GetDataAsStructureArray <Generator>(this.instrumentZoneGenerators); continue; case "SHDR": case "shdr": nextSubChunk.GetDataAsStructureArray <SampleHeader>(this.sampleHeaders); continue; } throw new InvalidDataException(string.Format("Unknown chunk type {0}", nextSubChunk.ChunkID)); } this.instrumentZoneGenerators.Load(this.sampleHeaders.SampleHeaders); this.instrumentZones.Load(this.instrumentZoneModulators.Modulators, this.instrumentZoneGenerators.Generators); this.instruments.LoadZones(this.instrumentZones.Zones); this.presetZoneGenerators.Load(this.instruments.Instruments); this.presetZones.Load(this.presetZoneModulators.Modulators, this.presetZoneGenerators.Generators); this.presetHeaders.LoadZones(this.presetZones.Zones); this.sampleHeaders.RemoveEOS(); }
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(); }
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(); }