Пример #1
0
        /// <summary>
        /// Create a Midi Short event
        /// </summary>
        /// <param name="status">the status byte for this message</param>
        /// <param name="channel">the midi channel (0 - 15)</param>
        /// <param name="d1">the first data byte for this message</param>
        /// <param name="d2">the second data byte for this message</param>
        /// <param name="tick">the position of the event in the sequence</param>
        /// <returns>the created Midi Short event</returns>
        public static MidiEvent CreateShortEvent(int status, int channel, int d1, int d2, long tick)
        {
            var sm = new ShortMessage();

            sm.SetMessage(status, channel, d1, d2);
            var ev = new MidiEvent(sm, tick);

            return(ev);
        }
Пример #2
0
        /// <summary>
        /// Create a Midi Short event
        /// </summary>
        /// <param name="status">the status byte of the Short event</param>
        /// <param name="tick">the position of the event in the sequence</param>
        /// <returns>the created Midi Short event</returns>
        public static MidiEvent CreateShortEvent(int status, long tick)
        {
            var sm = new ShortMessage();

            sm.SetMessage(status);
            var ev = new MidiEvent(sm, tick);

            return(ev);
        }
Пример #3
0
        /// <summary>
        /// Get the MIDI Sequence found in this input stream.
        /// <see cref="gnu.sound.midi.spi.MidiFileReader#GetSequence(Stream)"/>
        /// </summary>
        public override Sequence GetSequence(Stream stream)
        {
            // Good midi spec:
            // http://www.somascape.org/midi/tech/mfile.html
            // http://www.ccarh.org/courses/253/handout/smf/

            var din = new MidiDataInputStream(stream);
            var mff = (ExtendedMidiFileFormat)GetMidiFileFormat(din.BaseStream);

            var seq = new Sequence(mff.DivisionType, mff.Resolution, 0, mff.MidiFileType);

            int ntracks = mff.NumberOfTracks;

            while (ntracks-- > 0)
            {
                Track track = seq.CreateTrack();
                int   Mtrk  = din.ReadInt32();
                if (Mtrk != 0x4D54726B)                 // "MTrk"
                {
                    throw new InvalidMidiDataException("Invalid MIDI track header.");
                }
                int length = din.ReadInt32();

                int runningStatus = -1;
                int click         = 0;

                // Set this to true when we've hit an End of Track meta event.
                bool done = false;

                // Read all events.
                while (!done)
                {
                    MidiMessage mm;
                    int         deltaTime = din.ReadVariableLengthInt();
                    click += deltaTime;

                    // in Java bytes are signed (-128, +127)
                    // where in C# it's not (0, 255).
                    int statusByte = din.ReadByte();

                    if (statusByte < (int)MidiHelper.MidiEventType.SystemExclusive)
                    {
                        ShortMessage shortMessage;
                        switch (statusByte & 0xF0)
                        {
                        case (int)MidiHelper.MidiEventType.NoteOff:
                        case (int)MidiHelper.MidiEventType.NoteOn:
                        case (int)MidiHelper.MidiEventType.AfterTouchPoly:
                        case (int)MidiHelper.MidiEventType.ControlChange:
                        case (int)MidiHelper.MidiEventType.PitchBend:
                        case (int)MidiHelper.MidiEventType.SongPosition:
                            shortMessage = new ShortMessage();
                            shortMessage.SetMessage(statusByte, din.ReadByte(), din.ReadByte());
                            runningStatus = statusByte;
                            break;

                        case (int)MidiHelper.MidiEventType.ProgramChange:
                        case (int)MidiHelper.MidiEventType.AfterTouchChannel:
                        case (int)MidiHelper.MidiEventType.SongSelect:
                        case (int)MidiHelper.MidiEventType.BusSelect:
                            shortMessage = new ShortMessage();
                            shortMessage.SetMessage(statusByte, din.ReadByte(), 0);
                            runningStatus = statusByte;
                            break;

                        case (int)MidiHelper.MidiEventType.TuneRequest:
                        case (int)MidiHelper.MidiEventType.EndOfExclusive:
                        case (int)MidiHelper.MidiEventType.Clock:
                        case (int)MidiHelper.MidiEventType.Start:
                        case (int)MidiHelper.MidiEventType.Continue:
                        case (int)MidiHelper.MidiEventType.Stop:
                        case (int)MidiHelper.MidiEventType.ActiveSensing:
                        case (int)MidiHelper.MidiEventType.SystemReset:
                            shortMessage = new ShortMessage();
                            shortMessage.SetMessage(statusByte, 0, 0);
                            runningStatus = statusByte;
                            break;

                        default:
                            if (runningStatus != -1)
                            {
                                switch (runningStatus & 0xF0)
                                {
                                case (int)MidiHelper.MidiEventType.NoteOff:
                                case (int)MidiHelper.MidiEventType.NoteOn:
                                case (int)MidiHelper.MidiEventType.AfterTouchPoly:
                                case (int)MidiHelper.MidiEventType.ControlChange:
                                case (int)MidiHelper.MidiEventType.PitchBend:
                                case (int)MidiHelper.MidiEventType.SongPosition:
                                    shortMessage = new ShortMessage();
                                    shortMessage.SetMessage(runningStatus, statusByte, din.ReadByte());
                                    break;

                                case (int)MidiHelper.MidiEventType.ProgramChange:
                                case (int)MidiHelper.MidiEventType.AfterTouchChannel:
                                case (int)MidiHelper.MidiEventType.SongSelect:
                                case (int)MidiHelper.MidiEventType.BusSelect:
                                    shortMessage = new ShortMessage();
                                    shortMessage.SetMessage(runningStatus, statusByte, 0);
                                    continue;

                                case (int)MidiHelper.MidiEventType.TuneRequest:
                                case (int)MidiHelper.MidiEventType.EndOfExclusive:
                                case (int)MidiHelper.MidiEventType.Clock:
                                case (int)MidiHelper.MidiEventType.Start:
                                case (int)MidiHelper.MidiEventType.Continue:
                                case (int)MidiHelper.MidiEventType.Stop:
                                case (int)MidiHelper.MidiEventType.ActiveSensing:
                                case (int)MidiHelper.MidiEventType.SystemReset:
                                    shortMessage = new ShortMessage();
                                    shortMessage.SetMessage(runningStatus, 0, 0);
                                    continue;

                                default:
                                    throw new InvalidMidiDataException("Invalid Short MIDI Event: " + statusByte);
                                }
                            }
                            else
                            {
                                throw new InvalidMidiDataException("Invalid Short MIDI Event: " + statusByte);
                            }

                            break;
                        }
                        mm = shortMessage;
                    }
                    else if (statusByte == (int)MidiHelper.MidiEventType.SystemExclusive ||
                             statusByte == (int)MidiHelper.MidiEventType.EndOfExclusive)
                    {
                        // System Exclusive event
                        int sysexLength  = din.ReadVariableLengthInt();
                        var sysexData    = din.ReadBytes(sysexLength);
                        var sysexMessage = new SysexMessage();
                        sysexMessage.SetMessage(statusByte, sysexData, sysexLength);
                        mm            = sysexMessage;
                        runningStatus = -1;
                    }
                    else if (statusByte == (int)MidiHelper.META)
                    {
                        // Meta Message
                        byte metaType    = din.ReadByte();
                        int  metaLength  = din.ReadVariableLengthInt();
                        var  metaData    = din.ReadBytes(metaLength);
                        var  metaMessage = new MetaMessage();
                        metaMessage.SetMessage(metaType, metaData, metaLength);
                        mm = metaMessage;

                        // End of Track
                        if (metaType == (byte)MidiHelper.MetaEventType.EndOfTrack)
                        {
                            done = true;
                        }

                        runningStatus = -1;
                    }
                    else
                    {
                        throw new InvalidMidiDataException("Invalid status byte: " + statusByte);
                    }

                    track.Add(new MidiEvent(mm, click));
                }
            }

            return(seq);
        }