package jmsltutorial; import java.util.Enumeration; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.util.LinearInterpolator; /** Calculate the mean pitch of all notes in the copy buffer.
Set the pitch of each according to the position in the buffer: early notes changed minimally, later notes attracted maximally to mean. @author Nick Didkovsky, copyright 2000 Nick Didkovsky, all right reserved */ public class ConvergeToMeanTransform extends UnaryCopyBufferTransform { public ConvergeToMeanTransform() { setName("Converge To Mean"); } public void operate(CopyBuffer copyBuffer) { // First calculate the mean pitch double pitchSum = 0; double pitchCount = 0; for (Enumeration e = copyBuffer.elements(); e.hasMoreElements();) { Note note = (Note) e.nextElement(); if (!note.isRest()) { pitchSum += note.getPitchData(); pitchCount++; } } double mean = pitchSum / pitchCount; // Now make an interpolator to provide a weighting for every note, starts at 1, down to 0 LinearInterpolator weightInterpolator = new LinearInterpolator(0, 1.0, pitchCount, 0); // now make a second pass, and scale each note according to its position in the copy buffer,: // early minimally, late maximally int pitchPosition = 0; for (Enumeration e = copyBuffer.elements(); e.hasMoreElements();) { Note note = (Note) e.nextElement(); if (!note.isRest()) { double weight = weightInterpolator.interp(pitchPosition); pitchPosition++; double newPitch = weight * note.getPitchData() + (1 - weight) * mean; note.setPitchData(newPitch); // NoteFactory.updateFromPitch(note); NoteFactory.update(note); } } } }