IMPORTANT: Any Note's pitch can be changed algorithmically with
note.setPitchData(double newPitch), but you must make a call to
NoteFactory.updateFromPitch(note) to ensure that the new pitch data
is properly reflected by the Note's graphic properties.
Similarly, any note's duration can be changed algorithmically with
note.setDurationData(double newDur), followed by a call to NoteFactory.updateFromDur(note)
The following transform, suggested by Phil Burk, converges the pitches of a melody to the original melody's mean pitch. Two passes through the copy buffer are required: one to calculate the mean pitch, and a second one to change the pitch of each note and draw it closer to the mean. So the first note of the melody will be unchanged, the last will equal the mean pitch, and all notes in between will converge closer and closer to the mean.
package jmsltutorial; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.util.*; import java.util.*; 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); } } } }
scoreFrame.addUnaryCopyBufferTransform(new ConvergeToMeanTransform());
Note: If you cannot easily draw a rectangle around the melody without
missing one or two of the Notes, click once on the first Note, then Shift-Click
on the last, which will select the entire range.
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.