Example #1
0
        public void Read(Stream stream)
        {
            var masterChunk = new RiffChunk(stream);

            if (masterChunk.Id != "RIFF")
            {
                throw new FileFormatException("WaveFile.masterChunk.Id", masterChunk.Id, "RIFF");
            }

            // masterChunk
            using (var masterStream = masterChunk.GetStream()) {
                var WaveId = StreamHelperLe.ReadString(masterStream, 4);
                if (WaveId != "WAVE")
                {
                    throw new FileFormatException("WaveFile.WaveId", WaveId, "WAVE");
                }

                // formatChunk
                var formatChunk = new RiffChunk(masterStream);
                if (formatChunk.Id != "fmt ")
                {
                    throw new FileFormatException("WaveFile.formatChunk.Id", formatChunk.Id, "fmt ");
                }
                using (var formatStream = formatChunk.GetStream()) {
                    FormatTag      = StreamHelperLe.ReadUInt16(formatStream);
                    Channels       = StreamHelperLe.ReadUInt16(formatStream);
                    SamplePerSec   = StreamHelperLe.ReadUInt32(formatStream);
                    AvgBytesPerSec = StreamHelperLe.ReadUInt32(formatStream);
                    BlockAlign     = StreamHelperLe.ReadUInt16(formatStream);
                    BitsPerSample  = StreamHelperLe.ReadUInt16(formatStream);
                }

                if (Format != WaveFileFormat.MicrosoftPcm)
                {
                    throw new FileFormatException("WaveFile.Format", Format, WaveFileFormat.MicrosoftPcm);
                }

                // dataChunk
                while (masterStream.Position < masterStream.Length)
                {
                    var chunk = new RiffChunk(masterStream);
                    if (chunk.Id == "data")
                    {
                        Data = chunk.Data;
                        using (var dataStream = chunk.GetStream()) {
                            BuildSamples(dataStream);
                        }
                        break;
                    }
                }

                if (Data == null)
                {
                    throw new FileFormatException("WaveFile.WaveData", "null", "byte[]");
                }
            }
        }
Example #2
0
        public PresetHeader(Stream stream)
        {
            PresetName     = StreamHelperLe.ReadString(stream, 20);
            Preset         = StreamHelperLe.ReadUInt16(stream);
            Bank           = StreamHelperLe.ReadUInt16(stream);
            PresetBagIndex = StreamHelperLe.ReadUInt16(stream);

            Library    = StreamHelperLe.ReadUInt32(stream);
            Genre      = StreamHelperLe.ReadUInt32(stream);
            Morphology = StreamHelperLe.ReadUInt32(stream);
        }
Example #3
0
 public SampleHeader(Stream stream)
 {
     SampleName     = StreamHelperLe.ReadString(stream, 20);
     Start          = StreamHelperLe.ReadUInt32(stream);
     End            = StreamHelperLe.ReadUInt32(stream);
     Startloop      = StreamHelperLe.ReadUInt32(stream);
     Endloop        = StreamHelperLe.ReadUInt32(stream);
     SampleRate     = StreamHelperLe.ReadUInt32(stream);
     OriginalKey    = (byte)stream.ReadByte();
     Correction     = (sbyte)stream.ReadByte();
     SampleLink     = StreamHelperLe.ReadUInt16(stream);
     SampleLinkType = (SampleLinkType)StreamHelperLe.ReadUInt16(stream);
 }
Example #4
0
        public Sf2File(Stream stream)
        {
            // <SFBK-form> ; SoundFont 2 RIFF File Format
            var sfbkChunk = new RiffChunk(stream);

            using (var sfbkStream = sfbkChunk.GetStream()) {
                // <SFBK-form> -> RIFF (‘sfbk’ ; RIFF form header
                var sfbkId = StreamHelperLe.ReadString(sfbkStream, 4);
                if (sfbkId != "sfbk")
                {
                    throw new FileFormatException("Sf2.sfbkId", sfbkId, "sfbk");
                }

                // <INFO-list> ; Supplemental Information
                var infoChunk = new RiffChunk(sfbkStream);
                using (var infoStream = infoChunk.GetStream()) {
                    // <INFO-list> -> LIST (‘INFO’
                    var infoId = StreamHelperLe.ReadString(infoStream, 4);
                    if (infoId != "INFO")
                    {
                        throw new FileFormatException("Sf2.infoChunk.infoId", infoId, "INFO");
                    }

                    while (infoStream.Position < infoStream.Length)
                    {
                        var chunk = new RiffChunk(infoStream);
                        switch (chunk.Id)
                        {
                        case "ifil":
                            // <ifil-ck> ; Refers to the version of the Sound Font RIFF file
                            Version = BitConverter.ToUInt16(chunk.Data, 0) + "." + BitConverter.ToUInt16(chunk.Data, 2);
                            break;

                        case "isng":
                            // <isng-ck> ; Refers to the target Sound Engine
                            TargetSoundEngine = Encoding.UTF8.GetString(chunk.Data);
                            break;

                        case "INAM":
                            // <INAM-ck> ; Refers to the Sound Font Bank Name
                            Name = chunk.GetString();
                            break;

                        case "ICRD":
                            // [<ICRD-ck>] ; Refers to the Date of Creation of the Bank
                            DateOfCreation = chunk.GetString();
                            break;

                        case "IENG":
                            // [<IENG-ck>] ; Sound Designers and Engineers for the Bank
                            Enginners = chunk.GetString();
                            break;

                        case "IPRD":
                            // [<IPRD-ck>] ; Product for which the Bank was intended
                            Product = chunk.GetString();
                            break;

                        case "ICOP":
                            // [<ICOP-ck>] ; Contains any Copyright message
                            Copyright = chunk.GetString();
                            break;

                        case "ICMT":
                            // [<ICMT-ck>] ; Contains any Comments on the Bank
                            Comments = chunk.GetString();
                            break;

                        case "ISFT":
                            // [<ISFT-ck>] ; The SoundFont tools used to create and alter the bank
                            Tools = chunk.GetString();
                            break;
                        }
                    }
                }

                // <sdta-list> ; The Sample Binary Data
                var sdtaChunk = new RiffChunk(sfbkStream);
                using (var sdtaStream = sdtaChunk.GetStream()) {
                    // <sdta-ck> -> LIST (‘sdta’
                    var sdtaId = StreamHelperLe.ReadString(sdtaStream, 4);
                    if (sdtaId != "sdta")
                    {
                        throw new FileFormatException("Sf2.sdtaChunk.sdtaId", sdtaId, "sdta");
                    }

                    // [<smpl-ck>] ; The Digital Audio Samples for the upper 16 bits
                    if (sdtaStream.Position < sdtaStream.Length)
                    {
                        var smplChunk = new RiffChunk(sdtaStream);
                        SampleData = new short[smplChunk.Size / 2];
                        for (int i = 0; i < smplChunk.Size; i += 2)
                        {
                            SampleData[i / 2] = BitConverter.ToInt16(smplChunk.Data, i);
                        }

                        // [<sm24-ck>] ; The Digital Audio Samples for the lower 8 bits
                        if (sdtaStream.Position < sdtaStream.Length)
                        {
                            var sm24Chunk = new RiffChunk(sdtaStream);
                            SampleData24 = sm24Chunk.Data;
                        }
                    }
                }

                // <pdta-list> ; The Preset, Instrument, and Sample Header data
                var pdtaChunk = new RiffChunk(sfbkStream);
                using (var pdtaStream = pdtaChunk.GetStream()) {
                    // <pdta-ck> -> LIST (‘pdta’
                    var pdtaId = StreamHelperLe.ReadString(pdtaStream, 4);
                    if (pdtaId != "pdta")
                    {
                        throw new FileFormatException("Sf2.pdtaChunk.pdtaId", pdtaId, "pdta");
                    }

                    // <phdr-ck> ; The Preset Headers
                    var phdrChunk = new RiffChunk(pdtaStream);
                    using (var phdrStream = phdrChunk.GetStream()) {
                        var list = new List <PresetHeader>();
                        while (phdrStream.Position < phdrStream.Length)
                        {
                            list.Add(new PresetHeader(phdrStream));
                        }
                        PresetHeaders = list.ToArray();
                    }

                    // <pbag-ck> ; The Preset Index list
                    var pbagChunk = new RiffChunk(pdtaStream);
                    using (var pbagStream = pbagChunk.GetStream()) {
                        var list = new List <Bag>();
                        while (pbagStream.Position < pbagStream.Length)
                        {
                            list.Add(new Bag(pbagStream));
                        }
                        PresetBags = list.ToArray();
                    }

                    // <pmod-ck> ; The Preset Modulator list
                    var pmodChunk = new RiffChunk(pdtaStream);
                    using (var pmodStream = pmodChunk.GetStream()) {
                        var list = new List <Modulator>();
                        while (pmodStream.Position < pmodStream.Length)
                        {
                            list.Add(new Modulator(pmodStream));
                        }
                        PresetModulators = list.ToArray();
                    }

                    // <pgen-ck> ; The Preset Generator list
                    var pgenChunk = new RiffChunk(pdtaStream);
                    using (var pgenStream = pgenChunk.GetStream()) {
                        var list = new List <Generator>();
                        while (pgenStream.Position < pgenStream.Length)
                        {
                            list.Add(new Generator(pgenStream));
                        }
                        PresetGenerators = list.ToArray();
                    }

                    // <inst-ck> ; The Instrument Names and Indices
                    var instChunk = new RiffChunk(pdtaStream);
                    using (var instStream = instChunk.GetStream()) {
                        var list = new List <InstrumentHeader>();
                        while (instStream.Position < instStream.Length)
                        {
                            list.Add(new InstrumentHeader(instStream));
                        }
                        InstrumentHeaders = list.ToArray();
                    }

                    // <ibag-ck> ; The Instrument Index list
                    var ibagChunk = new RiffChunk(pdtaStream);
                    using (var ibagStream = ibagChunk.GetStream()) {
                        var list = new List <Bag>();
                        while (ibagStream.Position < ibagStream.Length)
                        {
                            list.Add(new Bag(ibagStream));
                        }
                        InstrumentBags = list.ToArray();
                    }

                    // <imod-ck> ; The Instrument Modulator list
                    var imodChunk = new RiffChunk(pdtaStream);
                    using (var imodStream = imodChunk.GetStream()) {
                        var list = new List <Modulator>();
                        while (imodStream.Position < imodStream.Length)
                        {
                            list.Add(new Modulator(imodStream));
                        }
                        InstrumentModulators = list.ToArray();
                    }

                    // <igen-ck> ; The Instrument Generator list
                    var igenChunk = new RiffChunk(pdtaStream);
                    using (var igenStream = igenChunk.GetStream()) {
                        var list = new List <Generator>();
                        while (igenStream.Position < igenStream.Length)
                        {
                            list.Add(new Generator(igenStream));
                        }
                        InstrumentGenerators = list.ToArray();
                    }

                    // <shdr-ck> ; The Sample Headers
                    var shdrChunk = new RiffChunk(pdtaStream);
                    using (var shdrStream = shdrChunk.GetStream()) {
                        var list = new List <SampleHeader>();
                        while (shdrStream.Position < shdrStream.Length)
                        {
                            list.Add(new SampleHeader(shdrStream));
                        }
                        SampleHeaders = list.ToArray();
                    }
                }
            }

            Compile();
        }
Example #5
0
 public InstrumentHeader(Stream stream)
 {
     InstName   = StreamHelperLe.ReadString(stream, 20);
     InstBagNdx = StreamHelperLe.ReadUInt16(stream);
 }