Spray - Audio file segmenting

This tutorial shows how the audio can be used in a creative way. The audio file is granulated and the grains are spread across the stereo spectrum in a steady sweeping pattern controlled by a sine function.

The output file will sound similar this:

Click here to view source.

Lets have a closer look.
import jm.JMC;
import jm.music.data.*;
import jm.util.*;
import jm.audio.*;
import jm.audio.synth.*;
The import statements must bring in the jMusic audio classes as well as the music classes.

public class Spray implements JMC {

public static void main(String[] args) {
new Spray();
}

public Spray() {
Score score = new Score();
Part p = new Part("Spray", 0, 0);
Phrase phr = new Phrase();
String fileName = "Welcome.au";
// get file size
int numb = (int)(Math.random() * 10) + 1;
File f = new File(fileName);
double fileSize = (double)(f.length() - 32)/44100/2/2;

// make instrument stuff
Instrument[] ensemble = new Instrument[1];
ensemble[0] = new SimpleSampleInst(fileName, FRQ[C4]);
// set start time in file
double st = 0.0;
// the length of each note (audio segment)
double dur = 0.1;
// the start panning position (left)
double pan = 0.0;
// the factor which sets the number of steps
// between panning changes
double panSpeed = 4.0;
The attributes are declared in this next section, and the file size is calculated. Note that the formula assumes a stereo file at a 44100 sample rate.

		// iteratie through many notes
for(int i=0; i<100; i++) {
// check to see if the panning position
//should be updated this time through
pan = Math.sin((0.01 * panSpeed * (double)i)%3.14);
System.out.println((0.01 * panSpeed * (double)i)%3.14);
// create a note
Note n = new Note(C4 + (int)(Math.random() * 7), dur,
(int)(Math.random() * 100 + 27));
n.setPan(pan);
n.setDuration(dur * 1.5);
// set the sample read point for this note
n.setSampleStartTime(st);
// add the note to the phrase
phr.addNote(n);
// shift the sample read point forward
st += dur;
// check to see if the sample read point will be past
// the end of the sample. If so reset it.
if ((st + dur) > fileSize) st = 0.0;
}
100 notes are created and added to the phrase. On each iteration the panSpeed value is checked to see if the panning position should be updated or not. Each note has a random pitch with in an octave, a random dynamic, and the duration is slightly longer than the rhythmValue to compensate for shortening due to resampling for notes above C4.

The SampleStartTime is the duration through the sound file that the note should start reading. Error checking is done to make sure the next note will not read past the end of the file.

To provide variety in the panning, the speed is shifted up and down by step.

		// pack the phrase into the jMusic score structure
p.addPhrase(phr);
score.addPart(p);
// display the score on screen
View.print(score);
View.show(score);
// render the score as an audio file
Write.au(score, "TestSpray.au", ensemble);
}

}
The phrase is packed into a score in the normal way, and the score is then written to an AU file. During this processes each note in turn is rendered. With 100 notes this can take a little while.


jMusic Tutorial Index