A Simple Drum Machine

This demo will start you on the track to rock or techno with jMusic, and generally introduce techniques for multi-part ostinati which might also be used for minimalist, African, or Gamelan music.

To hear the result play the MIDI file below.

Click here to view source

Lets have a closer look.
import jm.JMC;
import jm.music.data.*;
import jm.music.tools.*;
import jm.util.*;
Lines 1-4 import useful packages that we need to use in the program. The first import statement gets the class containing the jMusic constant values. The rest of the statements import the other jMusic classes required by this class. The tools package has the Mod class, the util package contains the Write class.

public final class Kit implements JMC{
public static void main(String[] args){
Score pattern1 = new Score("JMDemo - Kit");
Part drums = new Part("Drum Kit", 0, 9);
Phrase phrBD = new Phrase(0.0);
Phrase phrSD = new Phrase(0.0);
Phrase phrHH = new Phrase(0.0);

//Let us know things have started System.out.println("Creating drum patterns . . .");

This section declares the class and sets class variables for most levels of the jMusic data structure. Phrases will hold a pattern for each drum, the 'drum' part will hold those phrases and, although there is only one instrument in this demo, the music in jMusic always needs to be in a score format to create a MIDI file. The part is set to the 10th MIDI channel (i.e. channel 9, counting from 0) for GM playback of drum and percussion sounds. The number 0.0, which is an argument to the Phrase constructor, tells the new phrases that they will start at the beginning of the piece. The final println statement simply keeps us informed of the program's progress as it runs by printing to the standard output.

         // make bass drum
         for(short i=0;i<4;i++){
                  Note note = new Note(36, C);
                  phrBD.addNote(note);
                  Note rest = new Note(REST, C); //rest
                  phrBD.addNote(rest);
         }

The first phrase to be created is the bass drum. It consists of four crotchet note - crotchet rest pairs. The General MIDI (GM) drum kit uses note number 36 for bass drum. Each note and rest is added to the bass drum phrase (phrBD) by calling the 'addNote' method in the Phrase class.

         // make snare drum
         for(short i=0;i<4;i++){
                  Note rest = new Note(REST, C); //rest
                  phrSD.addNote(rest);
                  Note note = new Note(38, C);
                  phrSD.addNote(note);
         }
		
         // make hats
         for(short i=0;i<15;i++){
                  Note note = new Note(42, Q);
                  phrHH.addNote(note);
         }
         Note note = new Note(46, Q); // open hi hat
         phrHH.addNote(note);

A similar process creates the snare and hi hat parts. The snare plays on the opposite beats to the bass drum. The hi hat is normally closed (note 42 in GM) but an open hi hat is added to the end of the phrase just for some small aesthetic interest.

         // loop the drum pattern for 16 bars
         Mod.repeat(phrBD, 7);
         Mod.repeat(phrSD, 7);
         Mod.repeat(phrHH, 7);

Mod.repeat() is used to create loops of each phrase. In this case the same number of repeats are used for each phrase and each phrase is the same length. However, interesting effects could be created by 'phasing' patterns of different lengths to create continually varying rhythmic combinations.

         // add phrases to the instrument (part)
         drums.addPhrase(phrBD);
         drums.addPhrase(phrSD);
         drums.addPhrase(phrHH)
These lines add each phrase in turn to the 'drums' instrument. In sequencer terms, then are merged onto the one track.

         // add the drum part to a score.
         pattern1.addPart(drums);
A score is constructed from the single drum pattern with this statement.

         Write.midi(pattern1, "Kit.mid");
As with other jMusic demo files, this code creates a MIDI file from the score. Once created the kit.mid file can be played by any MIDI file player and will sound correctly using a General MIDI sound source, such as most PC sound cards, or QuickTime.



jMusic Tutorial Index