Audio Drum Kit

This example creates a simple one bar drum pattern, but more importantly writes it out to an Audio file!

The SimpleSampleInst class is used to create audio instruments for each drum sound. These are arranged in an instrument array called drumKit. This array is used, at the bottom of the code, when the Write.au() method is called for the score. Each Part in the jMusic score uses a different instrument in the 'drumKit' array, as specified by the third constructor argument when the part is instantiated.

The final Audio file will sound like this:

Click here to view source.

Lets have a closer look.
import jm.JMC;
import jm.util.*;
import jm.music.data.*;
import jm.music.tools.*;
import jm.audio.*;
As well as the jm.music classes, the jm.audio classes are imported. The audio.data folder contains input and output code, the audio.synth folder contains synthesis tools not used in the sample-based example, and the audio.inst class contains the instrument classes including the simpleSampleInst class used in this tutorial.

public class AudioDrums implements JMC {
	public static void main(String[] args) {
 
The class is declared, then the main method. Structurally this class is very basic, everything happens in the main method.

SimpleSampleInst kickInst = new SimpleSampleInst("Kick.au", FRQ[36]);
SimpleSampleInst snareInst = new SimpleSampleInst("Snare.au", FRQ[38]);
SimpleSampleInst hatsInst = new SimpleSampleInst("Hats.au", FRQ[42]);
Instrument[] drumKit = {kickInst, snareInst, hatsInst};
First audio priority is to declare instruments and then an instrument array. Three instances of the simpleSampleInst class are declared, one for each part of the drum kit. The drumKit array holds all the instruments to be used by this score. The simpleSampleInst takes two arguments, the name of the audio file it should use and the frequency to be used as the base pitch of that file. The JMC supports conversions of MIDI notes to frequency using the FRQ[mp] format where mp is the MIDI pitch number.

		Score s = new Score("title");
		Part kickPart = new Part("Kick", 0, 9);
		Part snarePart = new Part("Snare", 1, 9);
		Part hatsPart = new Part("Hats", 2, 9);
		Phrase kickPhrase = new Phrase();
		Phrase snarePhrase = new Phrase();
		Phrase hatsPhrase = new Phrase();
 
The jMusic score is created in the 'normal' way. This is the nice thing about jMusic, the same score file can be used to render audio or MIDI versions of the score. Importantly, the third argument to the part indicates which instrument in the instrument array is to be used by the part.

		// kick 
		for(int i=0;i<2;i++) {
			Note n = new Note(36,2.0);
			kickPhrase.addNote(n);
		}
 
A bass drum phrase of two minims (half-notes) is created.

		//snare
		for(int i=0;i<2;i++) {
			Note r = new Note(REST,1.0);
			snarePhrase.addNote(r);
			Note n = new Note(38,1.0);
			snarePhrase.addNote(n);
		}
 
A snare drum phrase of two crotchet (quarter-notes) rest then note pairs.

		//hats
		for(int i=0;i<16;i++) {
			Note n = new Note(42,0.25, (int)(Math.random()*127));
			hatsPhrase.addNote(n);
		}
The hihats introduce a random dynamic to give them some life.

		//pack into s score
		kickPart.addPhrase(kickPhrase);
		snarePart.addPhrase(snarePhrase);
		hatsPart.addPhrase(hatsPhrase);
		
		s.addPart(kickPart);
		s.addPart(snarePart);
		s.addPart(hatsPart);
 
The phrases are packed into a their respective parts and then into the score.

		//write a MIDI file of the score
		Write.midi(s, "AudioDrums.mid");
		
		//write an audio file of the score using the drumKit instruments
		Write.au(s, "AudioDrums.au", drumKit);
	}
}
 
A MIDI file is written, then an audio file. Notice that the syntax for audio rendering is just like MIDI or jm files.

The only additional components to make this score audio enabled were:

1. The declaration of audio instruments and an instrument array.

2. The careful declaration of parts to include, as a second argument, an appropriate index to the instrument array.

3. Calling the Write.au() method once the score had been constructed.

This class uses four drum samples. You can use your own of course, please do, but to get going quickly download ours. There is one for hihat, open hihat, kick, and snare.

 

jMusic Tutorial Index