예제 #1
0
        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.");

        }
예제 #2
0
        /// <summary>
        /// Reads a byte as size and the string itself.
        /// Additionally it is ensured the specified amount of bytes is read.
        /// </summary>
        /// <param name="data">the data to read from.</param>
        /// <param name="length">the amount of bytes to read</param>
        /// <returns></returns>
        public static string GpReadStringByteLength(this IReadable data, int length)
        {
            var stringLength = data.ReadByte();
            var s            = data.GpReadString(stringLength);

            if (stringLength < length)
            {
                data.Skip(length - stringLength);
            }
            return(s);
        }
예제 #3
0
        public static Color GpReadColor(this IReadable data, bool readAlpha = false)
        {
            byte r = (byte)data.ReadByte();
            byte g = (byte)data.ReadByte();
            byte b = (byte)data.ReadByte();
            byte a = 255;

            if (readAlpha)
            {
                a = (byte)data.ReadByte();
            }
            else
            {
                data.Skip(1);
            }
            return(new Color(r, g, b, a));
        }
예제 #4
0
        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.");
            }
        }
예제 #5
0
 /// <summary>
 ///  Skips an integer (4byte) and reads a string using
 ///  a bytesize
 /// </summary>
 /// <returns></returns>
 public static string GpReadStringIntUnused(this IReadable data)
 {
     data.Skip(4);
     return(data.GpReadString(data.ReadByte()));
 }