/** * equalTemperedBentPitchFactory.java * * Generates a bentPitch object which is part of an equal tempered scale. * * * * Given the base MIDI pitch, number of divisions per octave, * and step position, * this class creates a bentPitch object with the base MIDI pitch * and two status bytes set correctly * * This module is part of Karl Brown's MIDI programming project. * */ package MidiApps; import java.io.*; import java.util.*; import javax.sound.midi.*; /** * * @author Karl Brown * @version 1.0 * last updated 2/16/2003 */ public class equalTemperedBentPitchFactory extends bentPitchFactory { //This constant assumes MIDI instrument's pitch bend range //is set to the normal value of two semitones up and down. private static int HALF_STEP_PITCH_BEND = 0x1000; private short divisionsPerOctave; private short stepPosition; private byte baseMidiPitch; private double octaveFraction; private double totalHalfsteps; private short wholeHalfsteps; private double fractionalHalfsteps; public void setBaseMidiPitch (byte pitch) { baseMidiPitch = pitch; } protected void setDivisions(short div) { divisionsPerOctave = div; } protected void setStepPosition (short pos) { stepPosition = pos; } public byte getBaseMidiPitch() { return baseMidiPitch; } public short getDivisionsPerOctave() { return divisionsPerOctave; } public short getStepPosition() { return stepPosition; } /** * * calculate bend amount relative to pitch bend center * from bend amount relative to pitch bend center * It is assumed that the MIDI instrument playing the output * has a pitch bend range of two semitones up and down. * */ public void calculateBendAmount() throws Exception { //calculate portion of octave represented by the interval, //where one half step on an ordinary keyboard is one twelfth of an octave octaveFraction = (double)( (double)getStepPosition() / (double)getDivisionsPerOctave() ); //translate the fraction of an octave into whole and fractional half steps totalHalfsteps = octaveFraction * 12; wholeHalfsteps = (short)totalHalfsteps; fractionalHalfsteps = (double)((double)totalHalfsteps - (double)wholeHalfsteps ); //set the pitch value to which the note on / off event status byte will be set. setMidiPitchOut( (byte)( wholeHalfsteps + (short)getBaseMidiPitch() ) ); //set the pitch bend amount, from which the pitch bend event status bytes will be calulated setBendAmount((int)(fractionalHalfsteps * (double)HALF_STEP_PITCH_BEND)); /* tr.trace("step position: " + getStepPosition() + " divisions: " + getDivisionsPerOctave() + " octave fraction: " + octaveFraction); tr.trace("total half steps: " + totalHalfsteps + " whole half steps " + wholeHalfsteps + " fractional half steps: " + fractionalHalfsteps); tr.trace("bend amount: " + getBendAmount() + " midi pitch out " + getMidiPitchOut()); */ } }