Esempio n. 1
0
        public void Write(Stream stream)
        {
            UnityEngine.Debug.Log("Write " + this);
            // masterChunk
            using (var masterStream = new MemoryStream()) {
                StreamHelperLe.WriteString(masterStream, "WAVE");

                // formatChunk
                using (var formatStream = new MemoryStream()) {
                    StreamHelperLe.WriteUInt16(formatStream, FormatTag);
                    StreamHelperLe.WriteUInt16(formatStream, Channels);
                    StreamHelperLe.WriteUInt32(formatStream, SamplePerSec);
                    StreamHelperLe.WriteUInt32(formatStream, AvgBytesPerSec);
                    StreamHelperLe.WriteUInt16(formatStream, BlockAlign);
                    StreamHelperLe.WriteUInt16(formatStream, BitsPerSample);

                    var formatChunk = new RiffChunk("fmt ", formatStream.ToArray());
//					UnityEngine.Debug.Log(formatChunk.Size);
                    formatChunk.Write(masterStream);
                }

                // dataChunk
                var dataChunk = new RiffChunk("data", Data);
//				UnityEngine.Debug.Log(dataChunk.Size);
                dataChunk.Write(masterStream);

                var masterChunk = new RiffChunk("RIFF", masterStream.ToArray());
//				UnityEngine.Debug.Log(masterChunk.Size);
                masterChunk.Write(stream);
            }
        }
Esempio n. 2
0
        public Generator(Stream stream)
        {
            Gen = (GeneratorType)StreamHelperLe.ReadUInt16(stream);

            AmoutLo = (byte)stream.ReadByte();
            AmoutHi = (byte)stream.ReadByte();
        }
Esempio n. 3
0
 public Modulator(Stream stream)
 {
     ModSrc    = new ModulatorType(stream);
     ModDest   = (GeneratorType)StreamHelperLe.ReadUInt16(stream);
     ModAmount = StreamHelperLe.ReadInt16(stream);
     ModAmtSrc = new ModulatorType(stream);
     ModTrans  = (TransformType)StreamHelperLe.ReadUInt16(stream);
 }
Esempio n. 4
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[]");
                }
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
 }
Esempio n. 7
0
        void BuildSamples(Stream stream)
        {
            if ((BitsPerSample & 0x7) > 0)
            {
                throw new FileFormatException("WaveFile.BitsPerSample", BitsPerSample, "[multiple of 8]");
            }

            var bytesPerSample = BitsPerSample >> 3;
            var sampleLength   = Data.Length / bytesPerSample;

            Samples = new double[sampleLength];

            switch (bytesPerSample)
            {
            case 1:
                for (int i = 0; i < sampleLength; i++)
                {
                    Samples[i] = (double)stream.ReadByte() / 0x7F - 1.0;
                }
                break;

            case 2:
                for (int i = 0; i < sampleLength; i++)
                {
                    Samples[i] = (double)StreamHelperLe.ReadInt16(stream) / 0x7FFF;
                }
                break;

            case 3:
                for (int i = 0; i < sampleLength; i++)
                {
                    Samples[i] = (double)StreamHelperLe.ReadInt24(stream) / 0x7FFFFF;
                }
                break;

            case 4:
                for (int i = 0; i < sampleLength; i++)
                {
                    Samples[i] = (double)StreamHelperLe.ReadInt32(stream) / 0x7FFFFFFF;
                }
                break;
            }

            if (stream.Position < stream.Length)
            {
                throw new FileFormatException("WaveFile.stream.Position", stream.Position, stream.Length);
            }
        }
Esempio n. 8
0
        public ModulatorType(System.IO.Stream stream)
        {
            var data = StreamHelperLe.ReadUInt16(stream);

            Type      = (SourceType)(data >> 10);
            Polarity  = (SourcePolarityType)((data >> 9) & 0x01);
            Direction = (SourceDirectionType)((data >> 8) & 0x01);

            if (((data >> 7) & 0x01) == 0)
            {
                Source = (ControllerSourceType)(data & 0x3F);
            }
            else
            {
                Source     = ControllerSourceType.MidiController;
                MidiSource = (MidiControllerType)(data & 0x3F);
            }
        }
Esempio n. 9
0
        public void BuildData()
        {
            var bytesPerSample = BitsPerSample >> 3;
            var sampleLength   = Samples.Length;

            using (var dataStream = new MemoryStream()) {
                switch (bytesPerSample)
                {
                case 1:
                    for (int i = 0; i < sampleLength; i++)
                    {
                        dataStream.WriteByte((byte)System.Math.Round((Samples[i] + 1.0) * 0x7F));
                    }
                    break;

                case 2:
                    for (int i = 0; i < sampleLength; i++)
                    {
                        StreamHelperLe.WriteInt16(dataStream, (short)System.Math.Round(Samples[i] * 0x7FFF));
                    }
                    break;

                case 3:
                    for (int i = 0; i < sampleLength; i++)
                    {
                        StreamHelperLe.WriteInt24(dataStream, (int)System.Math.Round(Samples[i] * 0x7FFFFF));
                    }
                    break;

                case 4:
                    for (int i = 0; i < sampleLength; i++)
                    {
                        StreamHelperLe.WriteInt32(dataStream, (int)System.Math.Round(Samples[i] * 0x7FFFFFFF));
                    }
                    break;
                }

                Data = dataStream.ToArray();
            }
        }
Esempio n. 10
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();
        }
Esempio n. 11
0
 public Bag(Stream stream)
 {
     GenNdx = StreamHelperLe.ReadUInt16(stream);
     ModNdx = StreamHelperLe.ReadUInt16(stream);
 }
Esempio n. 12
0
 public InstrumentHeader(Stream stream)
 {
     InstName   = StreamHelperLe.ReadString(stream, 20);
     InstBagNdx = StreamHelperLe.ReadUInt16(stream);
 }