> Audio > Musical Data > Realtime audio in jMusic | |||||||
Realtime audio in jMusicThis tutorial provides an introduction to real time audio
with jMusic. When producing audio in realtime, data is played back as
soon as it's created. The architecture of realtime audio in jMusic varies considerably
from rendered audio in that the audio engine deals directly with notes,
and they need The tutorial is in two parts. First, the RTTest class is
presented which sets up the instruments and the RTline. Second, the
RealtimeMelody class is presented. This class is an extension of the
RTLine class. Each realtime piece must create extensions of the RTLine
class of each musical line (phrase). Here is the source for these two files: in addition you will need the RTPluckInst class availible from the jMusic instruments page, or directly from here: Lets have a closer look at the RTTest class
As well as the familiar JMC class, we import a number of
jMusic audio classes. The RTLine class is in the jm.music package because
of its similarity to the other music data classes (Note, phrase, score
etc.).
The class is short and so all the workings are in the main method. The first four lines of the main(0 method set up the audio
specifications for the music. The samplerate in this case is the same
as CD quality audio, it will produce a monophonic audio stream, the
control rate specifies how frequenctly adjustments to the sound are
calculated (shorter = more precission), and the buffer size is calulated
from those previous settings. The buffer size is the amount of sample
data computed at a time - it effects the latency of the audio system.
In practice it is good to have as small a control rate as possible to
minimise the buffer size.
An instrument to use for the music is decalred - the RTPluckInst. We only have one line in this piece so one instrument will suffice. realtime instruments in jMusic are slightly different from rendered instruments in that they don't write a file to disk. it is usually better to have specific realtime and non-realtime versions of an instrument. The instrument is added to an instrument array. This seems superfluous when we only have one instrument, but in cases where there are several this makes more sense. An array of RTLines is decalared. in this case it only contains one line and uses our RealtimeMelosy class (see below). Notice that the RTLine takes the instruments, controlrate, and buffersize as arguments. Each jMusic realtime audio piece has an RTMixer object.
Its function is to combine all of the RTLines together and send the
mix to the audio output.
The RealtimeMelody class has the job of providing notes as the music proceeds. It gets much of its functionality by extending the RTLine class. Each realtime audio program in jMusic must contain one or more classes that extend the RTLine class in this way. The class imports the appropriate jMusic packages. There are two clasess that are critical, the constructor and the getNote() method. The constructor takes the necessary arguments allowing the appropriate audio data to be calulated. these are passed to the super class - RTLine - for use. The getNote() method simply generates one note. When this
has been rendered the method will be called again, and so on, until
we quit the application. |
|||||||