/// <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"); } } }
public SampleDataChunk(RiffChunk chunk) { string header = chunk.ReadChunkID(); if(header != "sdta") { throw new InvalidDataException(String.Format("Not a sample data chunk ({0})",header)); } sampleData = chunk.GetData(); }
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 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"); } }