예제 #1
0
        /// <summary>
        /// Determines the type of message received and triggers the correct
        /// event in response.
        /// </summary>
        /// <param name="message">
        /// The short Midi message received.
        /// </param>
        /// <param name="timeStamp">
        /// Number of milliseconds that have passed since the input device
        /// began recording.
        /// </param>
        private void DispatchShortMessage(int message, int timeStamp)
        {
            // Unpack status value.
            int status = ShortMessage.UnpackStatus(message);

            // If a channel message was received.
            if (ChannelMessage.IsChannelMessage(status))
            {
                // If anyone is listening for channel messages.
                if (ChannelMessageReceived != null)
                {
                    // Create channel message.
                    ChannelMessage msg = new ChannelMessage(message);

                    // Create channel message event argument.
                    ChannelMessageEventArgs e =
                        new ChannelMessageEventArgs(msg, timeStamp);

                    // Trigger channel message received event.
                    ChannelMessageReceived(this, e);
                }
            }
            // Else if a system common message was received
            else if (SysCommonMessage.IsSysCommonMessage(status))
            {
                // If anyone is listening for system common messages
                if (SysCommonReceived != null)
                {
                    // Create system common message.
                    SysCommonMessage msg = new SysCommonMessage(message);

                    // Create system common event argument.
                    SysCommonEventArgs e = new SysCommonEventArgs(msg, timeStamp);

                    // Trigger system common received event.
                    SysCommonReceived(this, e);
                }
            }
            // Else if a system realtime message was received
            else if (SysRealtimeMessage.IsSysRealtimeMessage(status))
            {
                // If anyone is listening for system realtime messages
                if (SysRealtimeReceived != null)
                {
                    // Create system realtime message.
                    SysRealtimeMessage msg = new SysRealtimeMessage(message);

                    // Create system realtime event argument.
                    SysRealtimeEventArgs e = new SysRealtimeEventArgs(msg, timeStamp);

                    // Trigger system realtime received event.
                    SysRealtimeReceived(this, e);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the ChannelMessage class with a
        /// channel message packed into an integer.
        /// </summary>
        /// <param name="message">
        /// The packed channel message to use for initialization.
        /// </param>
        /// <exception cref="ArgumentException">
        /// Thrown if the message does not represent a channel message.
        /// </exception>
        public ChannelMessage(int message)
        {
            // Get status byte.
            int status = UnpackStatus(message);

            // Enforce preconditions.
            if (!ChannelMessage.IsChannelMessage(status))
            {
                throw new ArgumentException("Message is not a channel message.",
                                            "message");
            }

            //
            // Initialize properties.
            //

            Command     = (ChannelCommand)(message & ~CommandMask);
            MidiChannel = message & ~ChannelMask;
            SetData1(ShortMessage.UnpackData1(message));
            SetData2(ShortMessage.UnpackData2(message));
        }
        /// <summary>
        /// Reads the data for the next track from the Midi file.
        /// </summary>
        /// <param name="trackNum">
        /// The track number.
        /// </param>
        private void ReadNextTrack(int trackNum)
        {
            MetaType metaType      = MetaType.TrackName;
            int      status        = 0;
            int      runningStatus = 0;

            // Read length of track.
            binReader.ReadBytes(LengthByteCount);

            // Continue reading Midi events until the end of the track.
            while (metaType != MetaType.EndOfTrack)
            {
                // Next Midi message in track.
                IMidiMessage msg = null;

                // Ticks for next Midi event.
                int ticks = ReadVariableLengthQuantity();

                // Read status byte for the next Midi message.
                status = binReader.ReadByte();

                // If this is a status byte.
                if ((status & StatusFlag) == StatusFlag)
                {
                    // If the next Midi message is a channel message.
                    if (ChannelMessage.IsChannelMessage(status))
                    {
                        // Read channel message from the Midi file.
                        msg = ReadChannelMessage(status);

                        // Update running status.
                        runningStatus = status;
                    }
                    // Else if the next Midi message is a meta message.
                    else if (MetaMessage.IsMetaMessage(status))
                    {
                        // Read the type of meta message.
                        metaType = (MetaType)binReader.ReadByte();

                        // Read the length of the meta message data.
                        int length = ReadVariableLengthQuantity();

                        // Read the meta message data.
                        byte[] data = binReader.ReadBytes(length);

                        // Create meta message.
                        msg = new MetaMessage(metaType, data);
                    }
                    // Else if the next Midi message is a system exclusive
                    // message.
                    else if (SysExMessage.IsSysExMessage(status))
                    {
                        // The type of system exclusive message.
                        SysExType type = (SysExType)status;

                        // Read the length of the system exclusive data.
                        int length = ReadVariableLengthQuantity();

                        // Read the system exclusive data.
                        byte[] data = binReader.ReadBytes(length);

                        // Create system exclusive message.
                        msg = new SysExMessage(type, data);
                    }
                }
                // Assumes running status.
                else
                {
                    // Create channel message.
                    msg = ReadChannelMessage(runningStatus, status);
                }

                // Create the next Midi event and store it in the specified
                // track.
                MidiEvent e = new MidiEvent(msg, ticks);
                tracks[trackNum].Add(e);
            }
        }