Realtime jMusic instruments

The audio side of jMusic includes an instrument architecture. The focus of this was originally on rendering to audio files from instruments, and thus all the audio tutorials are about instruments that render. Real time instruments vary only slightly from rendering instruments in that they send the audio output to Java's audio output rather than to a file.

In practice this amounts to leaving our the sampleOut step in the audio object chain for real time instruments. Let's compare the createChain() methods for PluckInst and RTPluckInst.

PluckInst (rendereing version)

public void createChain(){
Pluck plk = new Pluck(this, this.sampleRate, this.channels, this.feedback);
Volume vol = new Volume(plk);
StereoPan span = new StereoPan(vol);
Envelope env = new Envelope(span, new double[] {0.0, 1.0, 0.9, 1.0, 1.0, 0.0});
SampleOut sout = new SampleOut(env);
}

RTPluckInst (realtime version)

public void createChain(){
Pluck plk = new Pluck(this, this.sampleRate, this.channels, this.feedback);
Volume vol = new Volume(plk);
StereoPan span = new StereoPan(vol);
Envelope env = new Envelope(span, new double[] {0.0, 1.0, 0.9, 1.0, 1.0, 0.0});
}

So, in order to create a realtime version of any existing jMusic instrument, simply create a version without the SampleOut object at the end of the chain.

Because the difference is so slight, it seems a pain to have two instrument files to maintain etc... So let's look at a clever way to have one instrument that can be used for either real time or rendered activities, depending on the context.

Fortunatley, there is a variable in the super class of Instruments that indicates if if a realtime or rendered activity is taking place. This "output" variable is set to the constant RENDER in non-realtime circumstances automatically. We can simply check the status of this flag and include the SampleOut audio object when rendering is true. As shown below.

public void createChain(){
Pluck plk = new Pluck(this, this.sampleRate, this.channels, this.feedback);
Volume vol = new Volume(plk);
StereoPan span = new StereoPan(vol);
Envelope env = new Envelope(span, new double[] {0.0, 1.0, 0.9, 1.0, 1.0, 0.0});
SampleOut sout;
if(output == RENDER) sout = new SampleOut(env);
}

This is the way most of the jMusic instruments included with the jMusic download (i.e. in the inst directory) operate. We advise doing the same for your own audio instuments.

Cheers, and happy instrument making.



© 2007 Andew R Brown