Using Excel VBA and MIDI to Play Scales

In this post, I can going to demonstrate how you can use VBA to interact with the MIDI capabilities of your PC to play a simple musical scale. In music, scales consist of a series of notes, that are either a semitone or wholetone (in pitch) apart from each other. In this example, we are going to play a C major diatonic scale. This will consist of 8 notes starting with C and finishing with the same note an octave higher.

C D E F G A B C

For a major scale, the interval between the 3rd and 4th notes (E & F) and the 7th and 8th notes (B & C) is a semitone. For the rest of the notes, the interval is going to be a wholetone. This will be an important thing to note, when it comes to coding the scale.

For the solution, I have created two classes called csNote and csMidi:

The class csNote, is going to be used to model each musical note within the scale. Note objects derived from this class, will have all the attributes required for the note to be played by the csMidi class. The Note class will also allow us to specify instructions in musical language, so that they can be translated into MIDI language for the csMidi class to understand. The csMidi class will encapsulate all the lower level code required for playing MIDI sounds on our PC. The code below shows how the two classes are used to play the scale:

A Key of C is defined as well as a starting octave (this is the pitch range for the note) for the scale. The code makes 8 note iterations for the scale. With each iteration it moves the note either a wholetone or semitone up from the previous note until it reaches the last note. That is all that is required to play the scale! The code for both of the classes is presented below:

Note Class (csNote)


Midi Class (csMidi)

 

Thank you for reading this post. Please take time to read the disclaimer about content found on this site.

Share :Facebooktwittergoogle_plusredditpinterestmail