Ejemplo n.º 1
0
        internal static InstrumentInfo[] ReadFromChunk(BinaryReader reader, int size)
        {
            if (size % 22 != 0)
            {
                throw new InvalidDataException("The instrument list is invalid.");
            }

            var count = size / 22;

            var instruments = new InstrumentInfo[count];

            for (var i = 0; i < count; i++)
            {
                var instrument = new InstrumentInfo();
                instrument.name           = reader.ReadFixedLengthString(20);
                instrument.zoneStartIndex = reader.ReadUInt16();

                instruments[i] = instrument;
            }

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

            return(instruments);
        }
Ejemplo n.º 2
0
        private Instrument(InstrumentInfo info, Zone[] zones, SampleHeader[] samples)
        {
            this.name = info.Name;

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

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

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

            regions = InstrumentRegion.Create(this, zoneSpan, samples);
        }
        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);
        }