예제 #1
0
        internal static PresetInfo[] ReadFromChunk(BinaryReader reader, int size)
        {
            if (size % 38 != 0)
            {
                throw new InvalidDataException("The preset list is invalid.");
            }

            var count = size / 38;

            var presets = new PresetInfo[count];

            for (var i = 0; i < count; i++)
            {
                var preset = new PresetInfo();
                preset.name           = reader.ReadFixedLengthString(20);
                preset.patchNumber    = reader.ReadUInt16();
                preset.bankNumber     = reader.ReadUInt16();
                preset.zoneStartIndex = reader.ReadUInt16();
                preset.library        = reader.ReadInt32();
                preset.genre          = reader.ReadInt32();
                preset.morphology     = reader.ReadInt32();

                presets[i] = preset;
            }

            for (var i = 0; i < count - 1; i++)
            {
                presets[i].zoneEndIndex = presets[i + 1].zoneStartIndex - 1;
            }

            return(presets);
        }
예제 #2
0
        private Preset(PresetInfo info, Zone[] zones, Instrument[] instruments)
        {
            this.name        = info.Name;
            this.patchNumber = info.PatchNumber;
            this.bankNumber  = info.BankNumber;
            this.library     = info.Library;
            this.genre       = info.Genre;
            this.morphology  = info.Morphology;

            var zoneCount = info.ZoneEndIndex - info.ZoneStartIndex + 1;

            if (zoneCount <= 0)
            {
                throw new InvalidDataException($"The preset '{info.Name}' has no zone.");
            }

            var zoneSpan = zones.AsSpan(info.ZoneStartIndex, zoneCount);

            regions = PresetRegion.Create(this, zoneSpan, instruments);
        }
        internal SoundFontParameters(BinaryReader reader)
        {
            var chunkId = reader.ReadFourCC();

            if (chunkId != "LIST")
            {
                throw new InvalidDataException("The LIST chunk was not found.");
            }

            var end = reader.BaseStream.Position + reader.ReadInt32();

            var listType = reader.ReadFourCC();

            if (listType != "pdta")
            {
                throw new InvalidDataException($"The type of the LIST chunk must be 'pdta', but was '{listType}'.");
            }

            PresetInfo[]     presetInfos          = null;
            ZoneInfo[]       presetBag            = null;
            Generator[]      presetGenerators     = null;
            InstrumentInfo[] instrumentInfos      = null;
            ZoneInfo[]       instrumentBag        = null;
            Generator[]      instrumentGenerators = null;

            while (reader.BaseStream.Position < end)
            {
                var id   = reader.ReadFourCC();
                var size = reader.ReadInt32();

                switch (id)
                {
                case "phdr":
                    presetInfos = PresetInfo.ReadFromChunk(reader, size);
                    break;

                case "pbag":
                    presetBag = ZoneInfo.ReadFromChunk(reader, size);
                    break;

                case "pmod":
                    Modulator.DiscardData(reader, size);
                    break;

                case "pgen":
                    presetGenerators = Generator.ReadFromChunk(reader, size);
                    break;

                case "inst":
                    instrumentInfos = InstrumentInfo.ReadFromChunk(reader, size);
                    break;

                case "ibag":
                    instrumentBag = ZoneInfo.ReadFromChunk(reader, size);
                    break;

                case "imod":
                    Modulator.DiscardData(reader, size);
                    break;

                case "igen":
                    instrumentGenerators = Generator.ReadFromChunk(reader, size);
                    break;

                case "shdr":
                    sampleHeaders = SampleHeader.ReadFromChunk(reader, size);
                    break;

                default:
                    throw new InvalidDataException($"The INFO list contains an unknown ID '{id}'.");
                }
            }

            if (presetInfos == null)
            {
                throw new InvalidDataException("The PHDR sub-chunk was not found.");
            }
            if (presetBag == null)
            {
                throw new InvalidDataException("The PBAG sub-chunk was not found.");
            }
            if (presetGenerators == null)
            {
                throw new InvalidDataException("The PGEN sub-chunk was not found.");
            }
            if (instrumentInfos == null)
            {
                throw new InvalidDataException("The INST sub-chunk was not found.");
            }
            if (instrumentBag == null)
            {
                throw new InvalidDataException("The IBAG sub-chunk was not found.");
            }
            if (instrumentGenerators == null)
            {
                throw new InvalidDataException("The IGEN sub-chunk was not found.");
            }
            if (sampleHeaders == null)
            {
                throw new InvalidDataException("The SHDR sub-chunk was not found.");
            }

            var instrumentZones = Zone.Create(instrumentBag, instrumentGenerators);

            instruments = Instrument.Create(instrumentInfos, instrumentZones, sampleHeaders);

            var presetZones = Zone.Create(presetBag, presetGenerators);

            presets = Preset.Create(presetInfos, presetZones, instruments);
        }