コード例 #1
0
        public byte[] ToMIDI()
        {
            RIFF riff = new RIFF("MIDI");

            byte[] mthd = new byte [6];
            BEBitConv.ToInt16(mthd, 0, 0);
            BEBitConv.ToInt16(mthd, 2, 1);
            BEBitConv.ToInt16(mthd, 4, resolution);
            riff.Items.Add(new RIFFData("MThd", mthd));
            byte[] mtrk = new byte [15 + data.Length];
            mtrk[0] = 0;
            mtrk[1] = 0xFF;
            mtrk[2] = 0x51;
            mtrk[3] = 0x03;
            MIDIConv.To3BE(mtrk, 4, tempo);
            mtrk[7]  = 0;
            mtrk[8]  = 0xFF;
            mtrk[9]  = 0x58;
            mtrk[10] = 0x04;
            BEBitConv.ToInt16(mtrk, 11, rhythm);
            mtrk[13] = 0x18;
            mtrk[14] = 0x08;
            data.CopyTo(mtrk, 15);
            riff.Items.Add(new RIFFData("MTrk", mtrk));
            return(riff.SaveBody(Endianness.BigEndian));
        }
コード例 #2
0
        public byte[] Save()
        {
            int length = 6;

            foreach (SEQ seq in seqs)
            {
                length += 13;
                length += seq.Data.Length;
            }
            byte[] data = new byte [length];
            BEBitConv.ToInt32(data, 0, Magic);
            BEBitConv.ToInt16(data, 4, Version);
            int offset = 6;

            for (int i = 0; i < seqs.Count; i++)
            {
                SEQ seq = seqs[i];
                BEBitConv.ToInt16(data, offset, (short)i);
                BEBitConv.ToInt16(data, offset + 2, seq.Resolution);
                MIDIConv.To3BE(data, offset + 4, seq.Tempo);
                BEBitConv.ToInt16(data, offset + 7, seq.Rhythm);
                BEBitConv.ToInt32(data, offset + 9, seq.Data.Length);
                offset += 13;
                seq.Data.CopyTo(data, offset);
                offset += seq.Data.Length;
            }
            return(data);
        }
コード例 #3
0
        public static SEQ Load(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            // All SEP/SEQ stuff is big-endian, like MIDI
            if (data.Length < 15)
            {
                ErrorManager.SignalError("SEQ: Data is too short");
            }
            int magic   = BEBitConv.FromInt32(data, 0);
            int version = BEBitConv.FromInt32(data, 4);

            if (magic != Magic)
            {
                ErrorManager.SignalIgnorableError("SEQ: Magic number is wrong");
            }
            if (version != Version)
            {
                ErrorManager.SignalIgnorableError("SEQ: Version number is wrong");
            }
            short resolution = BEBitConv.FromInt16(data, 8);
            int   tempo      = MIDIConv.From3BE(data, 10);
            short rhythm     = BEBitConv.FromInt16(data, 13);

            byte[] scoredata = new byte [data.Length - 15];
            Array.Copy(data, 15, scoredata, 0, scoredata.Length);
            return(new SEQ(resolution, tempo, rhythm, scoredata));
        }
コード例 #4
0
 public byte[] Save()
 {
     byte[] result = new byte [15 + data.Length];
     BEBitConv.ToInt32(result, 0, Magic);
     BEBitConv.ToInt32(result, 4, Version);
     BEBitConv.ToInt16(result, 8, resolution);
     MIDIConv.To3BE(result, 10, tempo);
     BEBitConv.ToInt16(result, 13, rhythm);
     data.CopyTo(result, 15);
     return(result);
 }
コード例 #5
0
        public static short FromInt16(Endianness endianness, byte[] str, int offset)
        {
            switch (endianness)
            {
            case Endianness.LittleEndian:
                return(BitConv.FromInt16(str, offset));

            case Endianness.BigEndian:
                return(BEBitConv.FromInt16(str, offset));

            default:
                throw new ArgumentException("Endianness is invalid.");
            }
        }
コード例 #6
0
        public static void ToInt32(Endianness endianness, byte[] str, int offset, int value)
        {
            switch (endianness)
            {
            case Endianness.LittleEndian:
                BitConv.ToInt32(str, offset, value);
                break;

            case Endianness.BigEndian:
                BEBitConv.ToInt32(str, offset, value);
                break;

            default:
                throw new ArgumentException("Endianness is invalid.");
            }
        }
コード例 #7
0
        public static SEP Load(byte[] data, int seqcount)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (seqcount < 0)
            {
                throw new ArgumentOutOfRangeException("seqcount");
            }
            // All SEP/SEQ stuff is big-endian, like MIDI
            if (data.Length < 6)
            {
                ErrorManager.SignalError("SEP: Data is too short");
            }
            int   magic   = BEBitConv.FromInt32(data, 0);
            short version = BEBitConv.FromInt16(data, 4);

            if (magic != Magic)
            {
                ErrorManager.SignalIgnorableError("SEP: Magic number is wrong");
            }
            if (version != Version)
            {
                ErrorManager.SignalIgnorableError("SEP: Version number is wrong");
            }
            int offset = 6;

            SEQ[] seqs = new SEQ [seqcount];
            for (int i = 0; i < seqcount; i++)
            {
                if (data.Length < offset + 13)
                {
                    ErrorManager.SignalError("SEP: Data is too short");
                }
                short seqid      = BEBitConv.FromInt16(data, offset);
                short resolution = BEBitConv.FromInt16(data, offset + 2);
                // tempo is 3 (yes, three) bytes
                int   tempo  = MIDIConv.From3BE(data, offset + 4);
                short rhythm = BEBitConv.FromInt16(data, offset + 7);
                int   length = BEBitConv.FromInt32(data, offset + 9);
                if (seqid != i)
                {
                    ErrorManager.SignalIgnorableError("SEP: Track number is wrong");
                }
                if (length < 0)
                {
                    ErrorManager.SignalError("SEP: Track length is negative");
                }
                offset += 13;
                if (data.Length < offset + length)
                {
                    ErrorManager.SignalError("SEP: Data is too short");
                }
                byte[] seqdata = new byte [length];
                Array.Copy(data, offset, seqdata, 0, length);
                seqs[i] = new SEQ(resolution, tempo, rhythm, seqdata);
                offset += length;
            }
            return(new SEP(seqs));
        }