How To Play MIDI Instruments

VisualStudio.HowToPlayMIDIInstruments History

Hide minor edits - Show changes to output

Added line 3:
* -or- DLL library download: [[http://www.nicolaimarquardt.com/ta/code/Toub.Sound.Midi.dll | MIDI DLL file]].
January 16, 2006, at 09:14 PM by 207.236.235.30 -
Changed lines 31-32 from:
''MidiPlayer.Play( new NoteOn( 0, 1, "C4", 127 ) );''
to:
(:source lang=csharp tabwidth=2 :) [=
MidiPlayer.Play( new NoteOn( 0, 1, "C4", 127 ) );
=]
Changed lines 37-38 from:
''MidiPlayer.Play( new NoteOff( 0, 1, "C4", 127 ) );''
to:
(:source lang=csharp tabwidth=2 :) [=
MidiPlayer.Play( new NoteOff( 0, 1, "C4", 127 ) );
=]
Changed lines 53-54 from:
''MidiPlayer.Play( new NoteOn( 0, GeneralMidiPercussion.AcousticSnare, 127 ) );''
to:
(:source lang=csharp tabwidth=2 :) [=
MidiPlayer
.Play( new NoteOn( 0, GeneralMidiPercussion.AcousticSnare, 127 ) );
=]
Changed lines 67-68 from:
''MidiPlayer.Play( new ProgramChange(0, 1, GeneralMidiInstruments.StringEnsemble1) );''
to:
(:source lang=csharp tabwidth=2 :) [=
MidiPlayer
.Play( new ProgramChange(0, 1, GeneralMidiInstruments.StringEnsemble1) );
=]
January 12, 2006, at 01:37 AM by 207.236.235.30 -
Changed lines 33-39 from:
Let's take a look at the parameters:
to:
Keep in mind this turns a note on for an indefinite amount of time! You won't notice this with a piano as it has a decay that eventually makes the sound fade out, however for sustained instruments such as strings, you'll just have a continuous drone. To turn notes back off again, use the following code:

''MidiPlayer.Play( new NoteOff( 0, 1, "C4", 127 ) );''

The constructor for this object takes exactly the same parameters as the NoteOn object. As long as the channel and note match something that is currently playing, the note will be switched off again.

Now let
's take a look at the parameters:
Changed lines 59-61 from:
''Coming soon...''
to:
The Instrument Preset is also referred to as a Channel's ''Program'', thus we use a ''ProgramChange'' object to set the instrument preset for a particular channel. For instance, to set Channel 1 to the StringEnsemble1 instrument, we use the following code:

''MidiPlayer.Play( new ProgramChange(0, 1, GeneralMidiInstruments.StringEnsemble1) );''

All new NoteOn events to Channel 1 will now sound like Strings instead of piano. You can play around for hours trying out all the different instrument presets now!
January 11, 2006, at 10:08 PM by 207.236.235.30 -
Added lines 56-58:
!Reference Links
* [[http://www.sfu.ca/sca/Manuals/247/midi/GeneralMIDI+GS.html#Table1|Instrument Preset Reference]]
* [[http://www.musicmarkup.info/midi/control.html|Controller Reference]]
January 11, 2006, at 10:03 PM by 207.236.235.30 -
Changed lines 51-52 from:
There are [[http://www.sfu.ca/sca/Manuals/247/midi/GeneralMIDI+GS.html#Table1|128 different instruments]] to choose from. Luckily these instruments are listed in the ''GeneralMidiInstruments'' enumeration.
to:
There are %newwin% [[http://www.sfu.ca/sca/Manuals/247/midi/GeneralMIDI+GS.html#Table1|128 different instruments]] to choose from. Luckily these instruments are listed in the ''GeneralMidiInstruments'' enumeration.
January 11, 2006, at 10:03 PM by 207.236.235.30 -
Added lines 51-52:
There are [[http://www.sfu.ca/sca/Manuals/247/midi/GeneralMIDI+GS.html#Table1|128 different instruments]] to choose from. Luckily these instruments are listed in the ''GeneralMidiInstruments'' enumeration.
January 11, 2006, at 09:59 PM by 207.236.235.30 -
Changed line 18 from:
!Playing a MIDI file
to:
!Playing a MIDI File
January 11, 2006, at 09:58 PM by 207.236.235.30 -
Added lines 18-22:
!Playing a MIDI file
The ''MidiPlayer'' class has the function ''Play''. One override takes a string argument which is used to specify the path of the MIDI file to open and immediately begin playing. For example:

''MidiPlayer.Play("mymusic.mid");''
Changed lines 24-25 from:
The ''MidiPlayer'' class has the function ''Play'' which takes an argument of type ''MidiEvent''.
to:
The ''MidiPlayer'' class has the function ''Play'' which takes an argument of type ''MidiEvent''. MidiEvent is a subclass for several types of events that can be sent to the MIDI Device - of particular interest is the ''NoteOn'' event, which turns a particular note on, thus producing a sound.
January 11, 2006, at 09:44 PM by 207.236.235.30 -
Changed lines 46-48 from:
You can change the instrument preset for a channel as follows:
to:
''Coming soon...''
January 11, 2006, at 09:44 PM by 207.236.235.30 -
Changed line 30 from:
# '''Channel''' parameter - can range from 1 to 15. (Oddly enough channel 10 still produces a melodic note, rather than being percussion as it should.)
to:
# '''Channel''' parameter - can range from 0 to 15. (Oddly enough channel 10 still produces a melodic note, rather than being percussion as it should.)
Added lines 45-48:
!Setting the Instrument Preset
You can change the instrument preset for a channel as follows:
January 11, 2006, at 09:37 PM by 207.236.235.30 -
Added lines 40-44:
The parameters are similar to before:
# '''Delta Time''' - again, leave it as 0 for now.
# '''Percussion Preset''' - use the GeneralMidiPercussion enumeration to select the percussion instrument to play.
# '''Velocity''' - the force of the instrument, from 0 to 127.
January 11, 2006, at 09:35 PM by 207.236.235.30 -
Changed line 2 from:
* Download the [[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]] toolkit.
to:
* Download the %newwin% [[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]] toolkit.
January 11, 2006, at 09:35 PM by 207.236.235.30 -
Changed lines 7-9 from:
to:
* Right click on the ''References'' item for your project in the ''Solution Explorer'' and choose ''Add Reference...''. You will be brought to a dialog box.
* Click on the ''Projects'' tab and select ''Toub.Sound.Midi'' from the list. Click the ''Select'' button to add it to the list below, and then click '''OK'''.
January 11, 2006, at 09:33 PM by 207.236.235.30 -
Changed line 1 from:
!Setup!
to:
!Setup
Changed line 8 from:
!Examples!
to:
!Examples
Added lines 22-32:
The default instrument for any track is instrument preset 0, which corresponds to the Grand Piano. If we play a melodic note without setting the instrument preset, we will get a note played on the Grand Piano. To play a note to a given channel, use the following code:

''MidiPlayer.Play( new NoteOn( 0, 1, "C4", 127 ) );''

Let's take a look at the parameters:
# '''Delta Time''' parameter - Suffice it to say that this parameter will remain at 0 for now.
# '''Channel''' parameter - can range from 1 to 15. (Oddly enough channel 10 still produces a melodic note, rather than being percussion as it should.)
# '''Note''' parameter - is a string to represent a note in traditional musical notation. Values can range from "C0" to "G10", where "C3" is considered middle-C (basically an anchor point for the most commonly used range of 8 notes). Enumerations go as follows (where ''n'' represents a number): C0, ... Cn, C#n, Dn, D#n, En, Fn, F#n, Gn, G#n, An+1, A#n+1, Bn+1, B#n+1, Cn+1, ..., G10. As you can see, the note lettering iterates from A to G, with #s (sharps) between. E does not have a # version, but don't ask me why, that's just how it is in music.
# '''Velocity''' parameter - is an indication of how hard the note is played, where 0 is completely silent, and 127 is the hardest possible. Usually 100 is a reasonable value to be heard without using excessive force.
January 11, 2006, at 09:14 PM by 207.236.235.30 -
Changed lines 20-21 from:
MIDI files consist of one or more ''tracks'', each of which can contain any number of notes to be played by a designated instrument. These notes aren't restricted to having a single one playing at a time - notes in the same track can overlap to create chords. Melodic tracks are assigned a channel number from 1-15 with the exception of Channel 10, which is reserved for percussion (see the next section for more information). More than one track can be mapped to the same channel, however this results in both tracks having the same instrument preset.
to:
The MIDI device has 15 channels with which to output sound. Other than Channel 10, all channels are used to output melodic sound. Each channel is assigned an ''Instrument Preset'', which allows you to choose from 127 instruments, including Pianos, Strings, Woodwinds, Bass, Electronic, and Sound Effects. MIDI files consist of one or more ''tracks'' that map to a channel. Each track can contain any number of notes to be played. These notes aren't restricted to having a single one playing at a time - notes in the same track can overlap to create chords. More than one track can be mapped to the same channel, however this results in both tracks having the same instrument preset. This is usually only done to keep two counterpoints logically separated - for instance, the notes for two harmonizing trumpets.
January 11, 2006, at 09:07 PM by 207.236.235.30 -
Changed lines 20-21 from:
MIDI files consist of one or more ''tracks'', each of which can contain any number of notes to be played by a designated instrument. These notes aren't restricted to having a single one playing at a time - notes in the same track can overlap to create chords.
to:
MIDI files consist of one or more ''tracks'', each of which can contain any number of notes to be played by a designated instrument. These notes aren't restricted to having a single one playing at a time - notes in the same track can overlap to create chords. Melodic tracks are assigned a channel number from 1-15 with the exception of Channel 10, which is reserved for percussion (see the next section for more information). More than one track can be mapped to the same channel, however this results in both tracks having the same instrument preset.
January 11, 2006, at 09:01 PM by 207.236.235.30 -
Changed lines 19-22 from:
!! Percussion
to:
!! Melodic Tracks
MIDI files consist of one or more ''tracks'', each of which can contain any number of notes to be played by a designated instrument. These notes aren't restricted to having a single one playing at a time - notes in the same track can overlap to create chords.

!! The Percussion Track
January 11, 2006, at 08:54 PM by 207.236.235.30 -
Changed lines 20-23 from:
The percussion track
to:
MIDI contains a single track known as the "percussion track". It is an instrument where different note pitches map to a whole array of percussion instruments. MIDI Channel 10 is reserved for percussion, however it is possible to have multiple percussion presets much in the same way that one has instrument presets on any other channel. This toolkit has an enumeration called ''GeneralMidiPercussion'' that lists all the standard percussion instruments. To play a sound on the percussion track, use the following code:

''MidiPlayer.Play( new NoteOn( 0, GeneralMidiPercussion.AcousticSnare, 127 ) );''
January 11, 2006, at 08:48 PM by 207.236.235.30 -
Added lines 10-20:

!Initialization/Destruction
The ''MidiPlayer'' class is a static object that functions as the main interface between the you and the MIDI device.
* When your program starts up, you must call ''MidiPlayer.OpenMidi();'' to initialize the device.
* When your program shuts down, you must call ''MidiPlayer.CloseMidi();'' to free the device resources.

!Playing Sounds in Real Time
The ''MidiPlayer'' class has the function ''Play'' which takes an argument of type ''MidiEvent''.

!! Percussion
The percussion track
January 11, 2006, at 05:54 PM by 207.236.235.30 -
Changed lines 5-7 from:
* In the ""Solution Explorer"" window, right click on the solution and select ""Add > Existing Project"".
* Browse to the location you unzipped the MIDI toolkit and select the Toub.Sound.Midi.csproj file. Click """OPEN""". The MIDI toolkit project is added to your solution.
to:
* In the ''Solution Explorer'' window, right click on the solution and select ''Add > Existing Project''.
* Browse to the location you unzipped the MIDI toolkit and select the Toub.Sound.Midi.csproj file. Click '''OPEN'''. The MIDI toolkit project is added to your solution.
Changed line 9 from:
The toolkit comes with several example projects that demonstrate how to use some of the basic and advanced features of the toolkit. You may switch demos by right-clicking on the demo project in the ""Solution Explorer"" and choosing ""Set as Startup Project"".
to:
The toolkit comes with several example projects that demonstrate how to use some of the basic and advanced features of the toolkit. You may switch demos by right-clicking on the demo project in the ''Solution Explorer'' and choosing ''Set as Startup Project''.
January 11, 2006, at 05:53 PM by 207.236.235.30 -
Changed lines 5-7 from:
* In the !!Solution Explorer!! window, right click on the solution and select !!Add!! > !!Existing Project!!.
* Browse to the location you unzipped the MIDI toolkit and select the Toub.Sound.Midi.csproj file. Click !!!OPEN!!!. The MIDI toolkit project is added to your solution.
to:
* In the ""Solution Explorer"" window, right click on the solution and select ""Add > Existing Project"".
* Browse to the location you unzipped the MIDI toolkit and select the Toub.Sound.Midi.csproj file. Click """OPEN""". The MIDI toolkit project is added to your solution.
Changed line 9 from:
The toolkit comes with several example projects that demonstrate how to use some of the basic and advanced features of the toolkit. You may switch demos by right-clicking on the demo project in the !!Solution Explorer!! and choosing !!Set as Startup Project!!.
to:
The toolkit comes with several example projects that demonstrate how to use some of the basic and advanced features of the toolkit. You may switch demos by right-clicking on the demo project in the ""Solution Explorer"" and choosing ""Set as Startup Project"".
January 11, 2006, at 05:52 PM by 207.236.235.30 -
Changed lines 1-3 from:
This how-to utilizes the MIDI for .NET v2.0.4 library, which is freely available for download from the following URL:

* [[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2
.0.4]]
to:
!Setup!
* Download
the [[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]] toolkit.
* Extract the contents of
the ZIP file to your !!Visual Studio Projects!! folder, or wherever you keep a repository of your Visual Studio projects.
* Start a new Visual Studio project or open the existing one to which you wish to add MIDI playback functionality
.
* In the !!Solution Explorer!! window, right click on the solution and select !!Add!! > !!Existing Project!!.
* Browse to the location you unzipped the MIDI toolkit and select the Toub.Sound.Midi.csproj file. Click !!!OPEN!!!. The MIDI toolkit project is added to your solution.

!Examples!
The toolkit comes with several example projects that demonstrate how to use some of the basic and advanced features of the toolkit. You may switch demos by right-clicking on the demo project in the !!Solution Explorer!! and choosing !!Set as Startup Project!!.
January 11, 2006, at 05:40 PM by 207.236.235.30 -
Changed line 3 from:
[[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]]
to:
* [[http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]]
January 11, 2006, at 05:40 PM by 207.236.235.30 -
Changed lines 1-3 from:
This how-to utilizes the [[MIDI for .NET v2.0.4|http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c]] library, which is freely available for download.
to:
This how-to utilizes the MIDI for .NET v2.0.4 library, which is freely available for download from the following URL:

[[http:
//www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c|MIDI for .NET v2.0.4]]
January 11, 2006, at 05:39 PM by 207.236.235.30 -
Added line 1:
This how-to utilizes the [[MIDI for .NET v2.0.4|http://www.gotdotnet.com/community/usersamples/details.aspx?sampleguid=89cde290-5580-40bf-90d2-5754b2e8137c]] library, which is freely available for download.