Click here to Skip to main content
15,889,839 members
Articles / Programming Languages / C#

C# MIDI Toolkit

Rate me:
Please Sign up or sign in to vote.
4.95/5 (177 votes)
18 Apr 2007MIT18 min read 3.2M   41.8K   303  
A toolkit for creating MIDI applications with C#.
/*
 * Created by: Leslie Sanford
 * 
 * Contact: jabberdabber@hotmail.com
 * 
 * Last modified: 08/05/2004
 */

using System;

namespace Multimedia.Midi
{
    /// <summary>
    /// The base class for all MIDI short messages.
    /// </summary>
    /// <remarks>
    /// Short messages are any MIDI message except for system exclusive 
    /// messages. This includes all channel, system common, and system 
    /// realtime messages.
    /// </remarks>
	public abstract class ShortMessage : IMidiMessage
	{
        #region ShortMessage Members

        #region Constants

        //
        // Bit manipulation constants.
        //

        private const int StatusMask = 16776960;
        private const int Data1Mask = 16711935;
        private const int Data2Mask = 65535;
        private const int Data1Shift = 8;
        private const int Data2Shift = 16;

        /// <summary>
        /// Maximum value allowed for any status byte.
        /// </summary>
        public const int StatusValueMax = 255;

        /// <summary>
        /// Maximum value allowed for any data byte.
        /// </summary> 
        public const int DataValueMax = 127;

        #endregion

        #region Fields

        // The short message packed as an integer.
        private int message = 0;

        #endregion

        #region Construction

        /// <summary>
        /// Initializes a new instance of the ShortMessage class.
        /// </summary>
		protected ShortMessage()
		{
		}

        #endregion

        #region Methods

        /// <summary>
        /// Unpacks the status value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The status value of the message.
        /// </returns>
        public static int UnpackStatus(int message)
        {
            return message & ~StatusMask;
        }

        /// <summary>
        /// Unpacks the first data value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The first data value of the message.
        /// </returns>
        public static int UnpackData1(int message)
        {
            return (message & ~Data1Mask) >> Data1Shift;
        }

        /// <summary>
        /// Unpacks the second data value from a packed MIDI short message.
        /// </summary>
        /// <param name="message">
        /// The packed MIDI short message.
        /// </param>
        /// <returns>
        /// The second data value of the message.
        /// </returns>
        public static int UnpackData2(int message)
        {
            return (message & ~Data2Mask) >> Data2Shift;
        }

        /// <summary>
        /// Sets the status value.
        /// </summary>
        /// <param name="status">
        /// The new status value.
        /// </param>
        protected void SetStatus(int status)
        {
            // Enforce preconditions.
            if(status < 0 || status > StatusValueMax)
                throw new ArgumentOutOfRangeException("status", status,
                    "Status value out of range.");

            // Replace the current status value with the new one.
            message &= StatusMask;
            message |= status;
        }

        /// <summary>
        /// Gets the first data value.
        /// </summary>
        /// <returns>
        /// The first data value.
        /// </returns>
        protected int GetData1()
        {
            return (message & ~Data1Mask) >> Data1Shift;
        }

        /// <summary>
        /// Sets the first data value.
        /// </summary>
        /// <param name="data1">
        /// The first data value.
        /// </param>
        protected void SetData1(int data1)
        {
            // Enforce preconditions.
            if(data1 < 0 || data1 > DataValueMax)
                throw new ArgumentOutOfRangeException("data1", data1,
                    "First data byte for short message out of range.");

            // Replace current data value with the new one.
            message &= Data1Mask;
            message |= data1 << Data1Shift;
        }

        /// <summary>
        /// Gets the second data value.
        /// </summary>
        /// <returns>
        /// The second data value.
        /// </returns>
        protected int GetData2()
        {
            return (message & ~Data2Mask) >> Data2Shift;
        }

        /// <summary>
        /// Sets the second data value.
        /// </summary>
        /// <param name="data2">
        /// The second data value.
        /// </param>
        protected void SetData2(int data2)
        {
            // Enforce preconditions.
            if(data2 < 0 || data2 > DataValueMax)
                throw new ArgumentOutOfRangeException("data2", data2,
                    "Second data byte for short message out of range.");
            
            // Replace current data value with the new one.
            message &= Data2Mask;
            message |= data2 << Data2Shift;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the message as a packed integer.
        /// </summary>
        public int Message
        {
            get
            {
                return message;
            }
        }       

        #endregion

        #endregion        

        #region IMidiMessage Members

        /// <summary>
        /// Accepts a MIDI message visitor.
        /// </summary>
        /// <param name="visitor">
        /// The visitor to accept.
        /// </param>
        public abstract void Accept(MidiMessageVisitor visitor);

        /// <summary>
        /// Creates a deep copy of this message.
        /// </summary>
        /// <returns>
        /// A deep copy of this message.
        /// </returns>
        public abstract object Clone();

        /// <summary>
        /// Gets the Midi message's status value.
        /// </summary>
        public int Status
        {
            get
            {
                return message & ~StatusMask;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Aside from dabbling in BASIC on his old Atari 1040ST years ago, Leslie's programming experience didn't really begin until he discovered the Internet in the late 90s. There he found a treasure trove of information about two of his favorite interests: MIDI and sound synthesis.

After spending a good deal of time calculating formulas he found on the Internet for creating new sounds by hand, he decided that an easier way would be to program the computer to do the work for him. This led him to learn C. He discovered that beyond using programming as a tool for synthesizing sound, he loved programming in and of itself.

Eventually he taught himself C++ and C#, and along the way he immersed himself in the ideas of object oriented programming. Like many of us, he gotten bitten by the design patterns bug and a copy of GOF is never far from his hands.

Now his primary interest is in creating a complete MIDI toolkit using the C# language. He hopes to create something that will become an indispensable tool for those wanting to write MIDI applications for the .NET framework.

Besides programming, his other interests are photography and playing his Les Paul guitars.

Comments and Discussions