JScore, algorithmic transformations

You have seen how to add algorithmically generated notes to a JScore.

You can also transform notes that are already in a JScore. Examples that ship with JMSL include:
Scramble and Retrograde operate on whatever is in the copy buffer. They are examples of Unary Transforms.

Zipper Interleave zips two melodies together, alternating notes. It operates on two auxiliary copy buffers, and is an example of a Binary Transform.

JScore provides an API to write your custom transforms. The transform subclasses UnaryCopyBufferTransform or BinaryCopyBufferTransform, and overrides operate().

Your custom transform is then added to ScoreFrame's menu with addUnaryCopyBufferTransform() and addBinaryCopyBufferTransform()

Example:

public class ScrambleTransform extends UnaryCopyBufferTransform {
	
	public ScrambleTransform() {
		setName("Scramble");	// this name shows up in the menu
	}
		

/** Implement this method to do whatever you want to CopyBuffer passed to operate().  */
public void operate(CopyBuffer copyBuffer) {
	for (int i=0; i < copyBuffer.size(); i++) {
		Object temp = copyBuffer.elementAt(i);
		int swapIndex = com.softsynth.jmsl.JMSLRandom.choose(copyBuffer.size());
		copyBuffer.setElementAt(copyBuffer.elementAt(swapIndex), i);
		copyBuffer.setElementAt(temp, swapIndex);
	}
}

}

Adding an instance of this class to the ScoreFrame menu is done like so:
scoreFrame.addUnaryCopyBufferTransform(new ScrambleTransform(), -1);	
// -1 means, no menu shortcut key
The next example shows a transform that calculates the mean pitch of the notes in the copy buffer. Over the range of notes, its pulls each note closer and closer to the mean.