Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

C# MIDI Toolkit

, 18 Apr 2007 MIT
A toolkit for creating MIDI applications with C#.
csharpmiditoolkitv4_demo.zip
CSharpMidiToolkitV4_demo
LSCollections.dll
MidiToolkitDemo
Properties
Settings.settings
MidiWatcher
Properties
Settings.settings
Multimedia.dll
Multimedia.Midi
Clocks
Device Classes
InputDevice Class
OutputDevice Classes
Messages
Message Builders
Multimedia.Midi.csproj.user
Processing
Sequencing
Midi File Player
Track Classes
UI
StateMachineToolkit.dll
csharpmiditoolkitv5_demo.zip
CSharpMidiToolkitV5_demo
MidiWatcher
Properties
Settings.settings
Sanford.Collections.dll
Sanford.Multimedia.dll
Sanford.Multimedia.Midi
Clocks
Device Classes
InputDevice Class
OutputDevice Classes
Messages
EventArgs
Message Builders
Processing
Sanford.Multimedia.Midi.csproj.user
Sequencing
Track Classes
UI
Sanford.Multimedia.Timers.dll
Sanford.Threading.dll
SequencerDemo
Properties
Settings.settings
miditoolkit_demo.zip
CSharpMidiToolkit
MidiToolkitDemo
App.ico
MidiToolkitDemo.csproj.user
Multimedia
Multimedia.csproj.user
Multimedia.Midi
Devices
Messages
Message Adaptors
MIDI File
Multimedia.MIDI.csproj.user
Sequencing
Synchronization
UI
miditoolkit_src.zip
MidiToolkit_src
Devices
Messages
MIDI File
Sequencing
TickGenerators
UI
/*
 * Created by: Leslie Sanford
 * 
 * Contact: jabberdabber@hotmail.com
 * 
 * Last modified: 05/08/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 ICloneable Members

        /// <summary>
        /// Creates a clone of the Midi message.
        /// </summary>
        /// <returns>
        /// A clone of the Midi message.
        /// </returns>
        public abstract object Clone();

        #endregion

        #region IMidiMessage Members
        
        /// <summary>
        /// Accepts an IMidiMessageVisitor.
        /// </summary>
        /// <param name="visitor">
        /// The IMidiMessageVisitor to accept.
        /// </param>
        public abstract void Accept(IMidiMessageVisitor visitor);

        /// <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

Share

About the Author

Leslie Sanford

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 18 Apr 2007
Article Copyright 2004 by Leslie Sanford
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid