JMSL Tutorial: JScore
Implementing your own Binary Copy Buffer Transform, part 1



You can design your own BinaryCopyBufferTransform by extending BinaryCopyBufferTransform and overriding
public void operate(CopyBuffer fromBuffer1, CopyBuffer fromBuffer2, CopyBuffer toBuffer)
The operate() method receives two aux copy buffers, used as sources, and a copy buffer to write results to. Recall that copy buffers are subclasses of java.util.Vector

Zipper Transform Example

Let us examine the source code for the Zipper Interleave Transform below. Notice the constructor assigns a unique name to the transform. This name will show up in the ScoreFrame's menu when it is added later.

Notice also that the source clears any Notes that may be in the copy buffer from earlier operations with a call to:
toBuffer.removeAllElements();
The strategy is simple: while there are elements left in either of the auxiliary copy buffers, continue to interleave them. Check for an empty buffer, however, before calling nextElement() on either buffer's enumeration, as we cannot assume aux buffer 1 has the same number of notes as aux buffer 2.
package com.softsynth.jmsl.score;
import java.util.*;
public class ZipperTransform extends BinaryCopyBufferTransform {
	
	public ZipperTransform() {
		name = "Zipper Interleave";
	}

/** Operate on CopyBuffer source1 & source2, sending result to CopyBuffer result.  Don't forget to result.removeAllElements() first!

*/
	public void operate(CopyBuffer fromBuffer1, CopyBuffer fromBuffer2, CopyBuffer toBuffer) {
		toBuffer.removeAllElements();
		Enumeration e1=fromBuffer1.elements();
		Enumeration e2=fromBuffer2.elements();
		
		while (e1.hasMoreElements() || e2.hasMoreElements()) {
			if (e1.hasMoreElements()) toBuffer.addElement(e1.nextElement());
			if (e2.hasMoreElements()) toBuffer.addElement(e2.nextElement());
		}
	}
}
Previous Tutorial Index Tutorial Contents Next


  (C) 2000 Nick Didkovsky and Phil Burk, All Rights Reserved
  JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.