Music Editing Program in C# and .NET




Figure 1 - Music Maker Form Showing Jingle Bells

I was just involved in writing a musical play in New York and we needed some sheet music to give to the director. I just had the notes in letters so I needed to transpose them to music.  I then thought to myself, what better way to do this than by using the power of C# and Visual Studio.NET!  This program will create music from a file of letter-coded notes.  It will also print  and print preview the music. The tough thing about writing a music program is how to initially design it. Once the design was thought out, the program fell into place fairly easily. Below is the design in UML:

Figure 2 - This design was reverse engineered using WithClass 2000 UML Design Tool

Basically, the design is a hierarchy of collections of Pages->Staffs->Measures->Notes.  The notes are read in using the StreamReader class and parsed into a Measure using the NoteReader Class.

A Sample Music File is shown below:

Title:Jingle Bells
Signature1:4
Signature2:4
E E Ea/E E Ea/E G Ce De Ea
F F Fa/Fe Fe E Ea/Ee Ee E D/D E Da/Gw
E E Ea/E E Ea/E G Ce De Ea
F F Fa/Fe Fe E Ea/Ee Ee G Ga/F D Ca
 

The rules for writing music are as follows:

  1. Keywords Title, Signature1 and Signature2 must end in a colon and followed by the value.
  2. Measures are separated by a forward slash  /
  3. Notes are described as letters C D E F G A B along with descriptor letters in lower case separated by a single space.
  4. Octaves - MusicMaker covers three octaves.  A lower octave note contains a lower case  l,  A higher octave note contains an h,  m is  the default.
  5. Sharps and flats - You can add a sharp to a note with a lower case s,  a flat with a lower case f.
  6. Timing is as follows  sixteenth - x,  eighth - e, quarter(default), half - a, three-quarter - t, whole - w
  7. Descriptor letters can be placed in any order next to the note.

Once you've created your file in notepad or any simple text editor, just save it as a .notes file (e.g.  mysong.notes).

The jingle bells song sample is included in the download.  Now on to our C# lesson.

In this article we will discuss an important structure in C# called an enumeration.  Enumerations are used to define your own set of values in a common collection.  Enumerations are great because you can use very descriptive words for values in the set.  Below is the enumeration declarations for a note in this program:

// chromatic enumeration for flats, sharps, naturals
public enum Chromatic
{
Flat,
Sharp,
Natural
};
// Timing for notes enumeration
public enum Timing
{
sixteenth,
eighth,
quarter,
half,
third,
whole
};
// pitch of note ranging from C to B
public enum Pitch
{
C,
D,
E,
F,
G,
A,
B
}
// octave of note divided into 3 octaves (low, middle, high)
public enum Octave
{
low,
middle,
high
}
 

All enumerations can be converted to integers by coercing them, and all enumerations start at 0, (unless forcefully assigned) and increment by 1.  For example low = 0, middle = 1, high = 2 in the above declaration. 

If the enumeration were like this:

public enum Octave
{
low=1,
middle,
high
}

Then the starting element in the enumeration sequence is 1 instead of 0, so the values would be low = 1, middle=2, high = 3.

To declare an enumeration field and initialize it, you just do the following:

public Octave TheOctave = Octave.middle;

To get the Octave value as an integer, coerce it:

int n = (int)TheOctave;

This is a fairly simple music editor, but has the potential for adding many powerful features (including playing the music through the sound card. Perhaps some adventurous programmer will take it a step further using the DirectSound or DirectMusic API.  In the next phase we will probably include a way to add words for the song in between the staffs.


Similar Articles