Tutorial
Instruments - Using the instrument name constants
Reload page to restart example
There is an interface of SoundCipher constants for all the General MIDI sound bank instruments. These are the instruments (sounds) that are included with Java in the JavaSound soundbank. The General MIDI standard specifies categories for most orchestral and popular music instruments including keyboards, guitars, basses, drums, woodwinds, strings, brass, and so on. In many cases you should be able to 'guess' the instrument name and it will be available. But check the SoundCipher documentation for the full list. As with other SoundCipher constants the format for specifying the instrument names is:

[soundcipher instance].[instrument name in upper case]

for example:

sc.BASSOON

The following program is a demonstration of how to use and change the instruments. It's way more complicated than necessary (sorry) but hopefully there are a number of other interesting aspects to it as well, such as employing several SoundCipher instances at once.

The program creates three parallel and somewhat independent descending musical lines. It uses a pentatonic scale (defined by another constant) to keep all pitch material coherent. The descending lines reset to the a higher pitch as required so the descending process continues indefinitely. The instruments can be changed while the program is running by rolling over the graphical interface with the mouse. This allows the user to choose between two sets of instruments; a woodwind set and the string set. Here's the code.

code

Some of the technical details will now be described.

Arrays are used to contain values for each of three parts. There are arrays for sets of SoundCipher instances, pitches of each part, and instruments that will be used.

The setup() method does the GUI interface work, and sets a low frame rate that controls note playback tempo.

The draw() method monitors mouse position and triggers redraw, instrument change when required, and sometimes plays the notes in each part.

The play() method is called by draw() and is passed an argument for part 0, 1 or 2, a value that is used to access values within the various arrays. The play command ends with a check to see if the current pitch is below a threshold and, if so, resets it to a higher pitch so the descending process restarts. Otherwise the pitch is decremented each iteration.

That's it, for new stuff.

The modeQuantize() method is taken from the Scales tutorial and rounds a pitch up to the nearest scale (mode) degree. The scale or mode  and key is passed in as an argument and the sc.PENTATONIC scale constant is used in this case and the key is always C (indicated by value 0).