jMusic Constants

In this tutorial you will be introduced to some key words which jMusic uses to refer to musical attributes. Constants allow your code to be more human readable and 'musical'. For example, you can specify a pitch using the constant C4 rather than the number 60. Behind the scenes Java translates the constant C4 as 60.

The constants are setup in the class called JMC and to use them you must import that class. You will notice that most jMusic code has this line toward the start:

import jm.JMC;

Secondly, when the class is declared it needs to implement the JMC class so the constants can be used. Therefore class declarations in jMusic tend to look like this;

public class AudioScale implements JMC{

There are currently jMusic constants for pitch, rhythmic values, dynamics, and MIDI program changes. In this tutorial we will examine each in turn.


Pitch

Note constants are written in pitch-class/octave notation, such that middle C is C4, the D above it is D4 and the B below it is B3.

Accidents are indicated with S for sharp and F for flat. e.g., C sharp above middle C is CS4 or DF4.

The pitch constants are converted into integer numbers for absolute pitch as used in MIDI. C4 (middle C) = 60. The range is from G9 - CN1 (C negative one).

Here is an example of the pitch constants being used:

Note n = new Note(C4, CROTCHET);

Pitch can also be specified as frequency for jMusic audio output. Middle C, for example, becomes a frequency of 261.63. The constants convert equally tempered pitches to frequency. The syntax is FRQ[n] where n is the MIDI note number to be converted.

Here is an example of the frequency constants being used:

SimpleSampleInst kickInst = new SimpleSampleInst("Kick.au", FRQ[36]);

MIDI and frequency constants can be combined, for example, FRQ[C4].

A rest (silence) is considered a special case of pitch in jMusic. The constant REST in place of a pitch value will indicate that a 'note' with no sound (i.e., a rest) should be created.


Rhythmic Value

Full names of abbreviations for English and American terms are provided for most common rhythmic values.

jMusic is based on a beat pulse where one beat is a value of 1.0 and all other rhythms are relative to that. The beat has several constants that equal it: CROTCHET, C, QUARTER_NOTE, and QN.

Similarly, half a beat can be: QUAVER, Q, EIGHTH_NOTE, or EN.

Dotted rhythm values follow the pattern: DOTTED_CROTCHET, DC, CD, DOTTED_QUARTER_NOTE, or DQN.

Triplets follow the pattern: QUAVER_TRIPLET, QT, EIGHTH_NOTE_TRIPLET, ENT.

Common rhythmic values from 4 beats to 1/12th of a beat have rhythmic value constants. See the JMC documentation or source code for a complete list.

Here is an example of the rhythmic value constants being used:

Note n = new Note(C4, CROTCHET);


Dynamic

jMusic allows integers from 0-127 to indicate the dynamic (loudness) of a note. The constants, shown below, specify some set levels using abbreviations for the common Italian terms for dynamics, pianissimo etc.

SILENT = 0,

PPP = 10,

PP = 25,

P = 50,

MP = 60,

MF = 70,

F = 85,

FF = 100,

FFF = 120;

Here is an example of the rhythmic value constants being used:

Note n = new Note(C4, CROTCHET, MF);


Panning

There are jMusic constants for panning notes across the stereo image.jMusic supports more than two (stereo) outputs but the constants only support the normal two channel output. Here are a few of the constants which relate the panning of a note:

PAN_CENTRE = 0.5,

PAN_LEFT = 0.0,

PAN_RIGHT = 1.0;

Here is an example of the panning constants being used:

Note n = new Note(C4, CROTCHET, MF, PAN_LEFT);


Duration Articulation

In many cases jMusic notes will have a duration which relates to the rhythmicValue, by default this is 85% of the rhythm value. So a crotchet note will have a rhythmicValue of 1.0 and a duration of 0.85.

To specify a duration differently a duration argument needs to be passed to the Note constructor. This value need not have anything relationship with the rhythmicValue but in music it often does. The duration constants are designed to be multiplied by the rhythmic Duration (CROTCHET * STACCATO) that way they can be applied to any rhythmicValue. There are a few common Italian terms for articulation defined as constants for the duration of a note:

STACCATO = 0.2,

LEGATO = 0.95,

SOSTENUTO = 1.2,

TENUTO = 1.0;

Here is an example of the rhythmic value constants being used:

Note n = new Note(C4, CROTCHET, MF, PAN_CENTRE, CROTCHET * LEGATO);


Timbre (MIDI Program Change)

The General MIDI set of program changes are included as jMusic constants, each constant equates to a integer number from 1-127. These will only be relevant when playing back MIDI files generated from jMusic on General MIDI equipment (such as PC sound cards or QuickTime Musical Instruments). Most orchestral and rock band instruments are part of the GM set and you should be lucky enough to guess the name in most cases as several alternate wordings are provided form many instruments.

Here are a few:

PIANO = 0,

HONKYTONK = 3,

EPIANO = 4, ELECTRIC_PIANO = 4, ELPIANO = 4,

. . .

Here is an example of the timbre constants being used:

Part p2 = new Part("Chords",1, GUITAR);

A list of all the jMusic contants can be found in the JMC documentation or at this jMusic instrument list page.


Scale

The JMC has a few scale constants including major, harmonic minor and pentatonic. These are stored as an array of integer values which correspond to the chromatic scale degrees with C as a root; C =0, D = 2, E = 4, F = 5, and so on. These arrays are within one octave, so ranges greater than that need to extrapolate by multiplying by a factor of 12, or getting the modulus 12 (i.e., %12) of a pitch, if required.

Here is what the major scale constant looks like:

public static final int[] MAJOR_SCALE = {0, 2, 4, 5, 7, 9, 11};

There is a useful Note method called isScale() which returns true or false if the note is a scale note of the specified scale.

Here is an example of the isScale() method using the scale constant:

Note n = new Note((int)(Math.random()*126 + 1), CROTCHET);
if (n.isScale(DORIAN_SCALE) == false) n.setPitch(n.getPitch() -1);

In this example a note with a random pitch is created. The second line checks to see if it is a note of the dorian mode, and if not lowers its pitch one semitone so that it will be.

Scale constants include those listed here, some are the same scale by different names:

  • MAJOR_SCALE
  • MINOR_SCALE
  • HARMONIC_MINOR_SCALE
  • MELODIC_MINOR_SCALE
  • NATURAL_MINOR_SCALE
  • DIATONIC_MINOR_SCALE
  • AOLEAN_SCALE
  • DORIAN_SCALE
  • LYDIAN_SCALE
  • MIXOLYDIAN_SCALE
  • PENTATONIC_SCALE
  • BLUES_SCALE
  • TURKISH_SCALE
  • INDIAN_SCALE

jMusic Tutorial Index