public PresetHeaderChunk(string id, int size, IReadable input) : base(id, size) { if (size % 38 != 0) { throw new Exception("Invalid SoundFont. The preset chunk was invalid."); } _rawPresets = new RawPreset[((int)(size / 38.0))]; RawPreset lastPreset = null; for (int x = 0; x < _rawPresets.Length; x++) { var p = new RawPreset(); p.Name = input.Read8BitStringLength(20); p.PatchNumber = input.ReadUInt16LE(); p.BankNumber = input.ReadUInt16LE(); p.StartPresetZoneIndex = input.ReadUInt16LE(); p.Library = input.ReadInt32LE(); p.Genre = input.ReadInt32LE(); p.Morphology = input.ReadInt32LE(); if (lastPreset != null) { lastPreset.EndPresetZoneIndex = Platform.Platform.ToUInt16((p.StartPresetZoneIndex - 1)); } _rawPresets[x] = p; lastPreset = p; } }
public SoundFontSampleData(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") throw new Exception("Invalid soundfont. Could not find sdta LIST chunk."); var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "sdta") throw new Exception("Invalid soundfont. The LIST chunk is not of type sdta."); BitsPerSample = 0; byte[] rawSampleData = null; while (input.Position < readTo) { var subID = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (subID.ToLower()) { case "smpl": BitsPerSample = 16; rawSampleData = input.ReadByteArray(size); break; case "sm24": if (rawSampleData == null || size != Math.Ceiling(SampleData.Length / 2.0)) {//ignore this chunk if wrong size or if it comes first input.Skip(size); } else { BitsPerSample = 24; for (var x = 0; x < SampleData.Length; x++) { var b = new byte[3]; b[0] = (byte)input.ReadByte(); b[1] = rawSampleData[2 * x]; b[2] = rawSampleData[2 * x + 1]; } } if (size % 2 == 1) { if (input.ReadByte() != 0) { input.Position--; } } break; default: throw new Exception("Invalid soundfont. Unknown chunk id: " + subID + "."); } } if (BitsPerSample == 16) { SampleData = rawSampleData; } else if (BitsPerSample != 24) throw new Exception("Only 16 and 24 bit samples are supported."); }
public PresetHeaderChunk(string id, int size, IReadable input) : base(id, size) { if (size % 38 != 0) throw new Exception("Invalid SoundFont. The preset chunk was invalid."); _rawPresets = new RawPreset[((int)(size / 38.0))]; RawPreset lastPreset = null; for (int x = 0; x < _rawPresets.Length; x++) { var p = new RawPreset(); p.Name = input.Read8BitStringLength(20); p.PatchNumber = input.ReadUInt16LE(); p.BankNumber = input.ReadUInt16LE(); p.StartPresetZoneIndex = input.ReadUInt16LE(); p.Library = input.ReadInt32LE(); p.Genre = input.ReadInt32LE(); p.Morphology = input.ReadInt32LE(); if (lastPreset != null) { lastPreset.EndPresetZoneIndex = TypeUtils.ToUInt16((p.StartPresetZoneIndex - 1)); } _rawPresets[x] = p; lastPreset = p; } }
public SampleHeader(IReadable input) { Name = input.Read8BitStringLength(20); Start = input.ReadInt32LE(); End = input.ReadInt32LE(); StartLoop = input.ReadInt32LE(); EndLoop = input.ReadInt32LE(); SampleRate = input.ReadInt32LE(); RootKey = (byte) input.ReadByte(); Tune = TypeUtils.ToInt16(input.ReadByte()); SampleLink = input.ReadUInt16LE(); SoundFontSampleLink = (SFSampleLink) input.ReadUInt16LE(); }
public SampleHeader(IReadable input) { Name = input.Read8BitStringLength(20); Start = input.ReadInt32LE(); End = input.ReadInt32LE(); StartLoop = input.ReadInt32LE(); EndLoop = input.ReadInt32LE(); SampleRate = input.ReadInt32LE(); RootKey = (byte)input.ReadByte(); Tune = Platform.Platform.ToInt16(input.ReadByte()); SampleLink = input.ReadUInt16LE(); SoundFontSampleLink = (SFSampleLink)input.ReadUInt16LE(); }
/// <summary> /// Reads an integer as size, skips a byte and reads the string itself /// </summary> /// <returns></returns> public static string GpReadStringIntByte(this IReadable data) { var length = data.ReadInt32LE() - 1; data.ReadByte(); return(data.GpReadString(length)); }
public void Load(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "riff") throw new Exception("Invalid soundfont. Could not find RIFF header."); id = input.Read8BitChars(4); if (id.ToLower() != "sfbk") throw new Exception("Invalid soundfont. Riff type is invalid."); Logger.Debug("Reading info chunk"); Info = new SoundFontInfo(input); Logger.Debug("Reading sampledata chunk"); SampleData = new SoundFontSampleData(input); Logger.Debug("Reading preset chunk"); Presets = new SoundFontPresets(input); }
public void Load(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "riff") { throw new Exception("Invalid soundfont. Could not find RIFF header."); } id = input.Read8BitChars(4); if (id.ToLower() != "sfbk") { throw new Exception("Invalid soundfont. Riff type is invalid."); } Logger.Debug("SF2", "Reading info chunk"); Info = new SoundFontInfo(input); Logger.Debug("SF2", "Reading sampledata chunk"); SampleData = new SoundFontSampleData(input); Logger.Debug("SF2", "Reading preset chunk"); Presets = new SoundFontPresets(input); }
public SoundFontSampleData(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") { throw new Exception("Invalid soundfont. Could not find sdta LIST chunk."); } var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "sdta") { throw new Exception("Invalid soundfont. The LIST chunk is not of type sdta."); } BitsPerSample = 0; byte[] rawSampleData = null; while (input.Position < readTo) { var subID = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (subID.ToLower()) { case "smpl": BitsPerSample = 16; rawSampleData = input.ReadByteArray(size); break; case "sm24": if (rawSampleData == null || size != Math.Ceiling(SampleData.Length / 2.0)) { //ignore this chunk if wrong size or if it comes first input.Skip(size); } else { BitsPerSample = 24; for (var x = 0; x < SampleData.Length; x++) { var b = new byte[3]; b[0] = (byte)input.ReadByte(); b[1] = rawSampleData[2 * x]; b[2] = rawSampleData[2 * x + 1]; } } if (size % 2 == 1) { if (input.ReadByte() != 0) { input.Position--; } } break; default: throw new Exception("Invalid soundfont. Unknown chunk id: " + subID + "."); } } if (BitsPerSample == 16) { SampleData = rawSampleData; } else if (BitsPerSample != 24) { throw new Exception("Only 16 and 24 bit samples are supported."); } }
public SoundFontPresets(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") { throw new Exception("Invalid soundfont. Could not find pdta LIST chunk."); } var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "pdta") { throw new Exception("Invalid soundfont. The LIST chunk is not of type pdta."); } Modulator[] presetModulators = null; Generator[] presetGenerators = null; Modulator[] instrumentModulators = null; Generator[] instrumentGenerators = null; ZoneChunk pbag = null; ZoneChunk ibag = null; PresetHeaderChunk phdr = null; InstrumentChunk inst = null; while (input.Position < readTo) { id = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (id.ToLower()) { case "phdr": phdr = new PresetHeaderChunk(id, size, input); break; case "pbag": pbag = new ZoneChunk(id, size, input); break; case "pmod": presetModulators = new ModulatorChunk(id, size, input).Modulators; break; case "pgen": presetGenerators = new GeneratorChunk(id, size, input).Generators; break; case "inst": inst = new InstrumentChunk(id, size, input); break; case "ibag": ibag = new ZoneChunk(id, size, input); break; case "imod": instrumentModulators = new ModulatorChunk(id, size, input).Modulators; break; case "igen": instrumentGenerators = new GeneratorChunk(id, size, input).Generators; break; case "shdr": SampleHeaders = new SampleHeaderChunk(id, size, input).SampleHeaders; break; default: throw new Exception("Invalid soundfont. Unrecognized sub chunk: " + id); } } var pZones = pbag.ToZones(presetModulators, presetGenerators); PresetHeaders = phdr.ToPresets(pZones); var iZones = ibag.ToZones(instrumentModulators, instrumentGenerators); Instruments = inst.ToInstruments(iZones); }
public SoundFontInfo(IReadable input) { Tools = ""; Comments = ""; Copyright = ""; TargetProduct = ""; Author = ""; DataRom = ""; CreationDate = ""; BankName = ""; SoundEngine = ""; var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") { throw new Exception("Invalid soundfont. Could not find INFO LIST chunk."); } var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "info") { throw new Exception("Invalid soundfont. The LIST chunk is not of type INFO."); } while (input.Position < readTo) { id = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (id.ToLower()) { case "ifil": SfVersionMajor = input.ReadInt16LE(); SfVersionMinor = input.ReadInt16LE(); break; case "isng": SoundEngine = input.Read8BitStringLength(size); break; case "inam": BankName = input.Read8BitStringLength(size); break; case "irom": DataRom = input.Read8BitStringLength(size); break; case "iver": RomVersionMajor = input.ReadInt16LE(); RomVersionMinor = input.ReadInt16LE(); break; case "icrd": CreationDate = input.Read8BitStringLength(size); break; case "ieng": Author = input.Read8BitStringLength(size); break; case "iprd": TargetProduct = input.Read8BitStringLength(size); break; case "icop": Copyright = input.Read8BitStringLength(size); break; case "icmt": Comments = input.Read8BitStringLength(size); break; case "isft": Tools = input.Read8BitStringLength(size); break; default: throw new Exception("Invalid soundfont. The Chunk: " + id + " was not expected."); } } }
public SoundFontPresets(IReadable input) { var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") throw new Exception("Invalid soundfont. Could not find pdta LIST chunk."); var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "pdta") throw new Exception("Invalid soundfont. The LIST chunk is not of type pdta."); Modulator[] presetModulators = null; Generator[] presetGenerators = null; Modulator[] instrumentModulators = null; Generator[] instrumentGenerators = null; ZoneChunk pbag = null; ZoneChunk ibag = null; PresetHeaderChunk phdr = null; InstrumentChunk inst = null; while (input.Position < readTo) { id = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (id.ToLower()) { case "phdr": phdr = new PresetHeaderChunk(id, size, input); break; case "pbag": pbag = new ZoneChunk(id, size, input); break; case "pmod": presetModulators = new ModulatorChunk(id, size, input).Modulators; break; case "pgen": presetGenerators = new GeneratorChunk(id, size, input).Generators; break; case "inst": inst = new InstrumentChunk(id, size, input); break; case "ibag": ibag = new ZoneChunk(id, size, input); break; case "imod": instrumentModulators = new ModulatorChunk(id, size, input).Modulators; break; case "igen": instrumentGenerators = new GeneratorChunk(id, size, input).Generators; break; case "shdr": SampleHeaders = new SampleHeaderChunk(id, size, input).SampleHeaders; break; default: throw new Exception("Invalid soundfont. Unrecognized sub chunk: " + id); } } var pZones = pbag.ToZones(presetModulators, presetGenerators); PresetHeaders = phdr.ToPresets(pZones); var iZones = ibag.ToZones(instrumentModulators, instrumentGenerators); Instruments = inst.ToInstruments(iZones); }
public SoundFontInfo(IReadable input) { Tools = ""; Comments = ""; Copyright = ""; TargetProduct = ""; Author = ""; DataRom = ""; CreationDate = ""; BankName = ""; SoundEngine = ""; var id = input.Read8BitChars(4); var size = input.ReadInt32LE(); if (id.ToLower() != "list") throw new Exception("Invalid soundfont. Could not find INFO LIST chunk."); var readTo = input.Position + size; id = input.Read8BitChars(4); if (id.ToLower() != "info") throw new Exception("Invalid soundfont. The LIST chunk is not of type INFO."); while (input.Position < readTo) { id = input.Read8BitChars(4); size = input.ReadInt32LE(); switch (id.ToLower()) { case "ifil": SfVersionMajor = input.ReadInt16LE(); SfVersionMinor = input.ReadInt16LE(); break; case "isng": SoundEngine = input.Read8BitStringLength(size); break; case "inam": BankName = input.Read8BitStringLength(size); break; case "irom": DataRom = input.Read8BitStringLength(size); break; case "iver": RomVersionMajor = input.ReadInt16LE(); RomVersionMinor = input.ReadInt16LE(); break; case "icrd": CreationDate = input.Read8BitStringLength(size); break; case "ieng": Author = input.Read8BitStringLength(size); break; case "iprd": TargetProduct = input.Read8BitStringLength(size); break; case "icop": Copyright = input.Read8BitStringLength(size); break; case "icmt": Comments = input.Read8BitStringLength(size); break; case "isft": Tools = input.Read8BitStringLength(size); break; default: throw new Exception("Invalid soundfont. The Chunk: " + id + " was not expected."); } } }
/// <summary> /// Reads an integer as size, and then the string itself /// </summary> /// <returns></returns> public static string GpReadStringInt(this IReadable data) { return(data.GpReadString(data.ReadInt32LE())); }
/// <summary> /// Reads an integer as size, and then the string itself /// </summary> /// <returns></returns> public static string GpReadStringInt(this IReadable data, string encoding) { return(data.GpReadString(data.ReadInt32LE(), encoding)); }