Click here to Skip to main content
15,860,861 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.1M   41.7K   303  
A toolkit for creating MIDI applications with C#.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;

namespace Multimedia.Midi.UI
{
    /// <summary>
    /// Represents a dialog box for selecting a Midi input device.
    /// </summary>
    public class MidiInDeviceDialog : System.Windows.Forms.Form
    {
        #region MidiInDeviceDialog Members

        #region Fields

        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.ComboBox cboInDevices;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        #endregion

        #region Construction

        /// <summary>
        /// Initializes a new instance of the MidiInDeviceDlg.
        /// </summary>
        public MidiInDeviceDialog()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            // Initialize ComboBox.
            InitializeComboBox();
            
            // If there are any input devices available, set the selected 
            // device ID to the first device.
            if(InputDevice.DeviceCount > 0)
            {
                SelectedDeviceId = 0;
            }
        }

        /// <summary>
        /// Initializes a new instance of the MidiInDeviceDlg with the 
        /// specified intput device Id.
        /// </summary>
        public MidiInDeviceDialog(int deviceId)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            // Initialize ComboBox.
            InitializeComboBox();

            // Set the selected device Id.
            SelectedDeviceId = deviceId;            
        }

        #endregion

        #region Methods

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnOK = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.cboInDevices = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.Location = new System.Drawing.Point(32, 72);
            this.btnOK.Name = "btnOK";
            this.btnOK.TabIndex = 0;
            this.btnOK.Text = "OK";
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(144, 72);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "Cancel";
            // 
            // cboInDevices
            // 
            this.cboInDevices.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboInDevices.Location = new System.Drawing.Point(16, 24);
            this.cboInDevices.Name = "cboInDevices";
            this.cboInDevices.Size = new System.Drawing.Size(216, 21);
            this.cboInDevices.TabIndex = 0;
            // 
            // MidiInDeviceDialog
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 110);
            this.Controls.Add(this.cboInDevices);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOK);
            this.Name = "MidiInDeviceDialog";
            this.Text = "Input Devices";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// Initializes ComboBox.
        /// </summary>
        private void InitializeComboBox()
        {
            // The number of input devices available.
            int deviceCount = InputDevice.DeviceCount;

            // The capabilities of an input device.
            MidiInCaps caps;

            // For converting ASCII byte text to a string.
            ASCIIEncoding encoder = new ASCIIEncoding();

            // Add input device names to the combo box.
            for(int i = 0; i < deviceCount; i++)
            {
                caps = InputDevice.GetCapabilities(i);
                cboInDevices.Items.Add(encoder.GetString(caps.name));
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the selected device ID.
        /// </summary>
        public int SelectedDeviceId
        {
            get
            {
                return cboInDevices.SelectedIndex;
            }
            set
            {
                // Enforce preconditions.
                if(value < 0 || value >= cboInDevices.Items.Count)
                    throw new ArgumentOutOfRangeException("SelectedDeviceId",
                        value, "Selected device ID out of range.");

                // Set the combo box to the selected device.
                cboInDevices.SelectedIndex = value;
            }
        }

        #endregion

        #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