//Track currTrack = sequence.createTrack();
currentTime = phrase.getStartTime();
long phraseTick = (long)(currentTime * m_ppqn * trackTempoRatio);
MidiEvent evt;
if (phrase.getInstrument() != NO_INSTRUMENT) instrument = phrase.getInstrument();
evt = createProgramChangeEvent(currChannel, instrument, phraseTick);
currTrack.add(evt);
m_tempoHistory.push(new Float(m_currentTempo));
tempo = new Float(phrase.getTempo()).floatValue();
if (tempo != Phrase.DEFAULT_TEMPO) {
m_currentTempo = tempo;
//System.out.println("jMusic MidiSynth notification: Phrase TempoEvent (BPM) = " + tempo);
}
elementTempoRatio = m_masterTempo/m_currentTempo;
double lastPanPosition = -1.0;
int offSetTime = 0;
/// Each note
Enumeration notes = phrase.getNoteList().elements();
while(notes.hasMoreElements()) {
Note note = (Note) notes.nextElement();
// deal with offset
offSetTime = (int)(note.getOffset() * m_ppqn * elementTempoRatio);
//handle frequency pitch types
int pitch = -1;
if(note.getPitchType() == Note.MIDI_PITCH) {
pitch = note.getPitch();
} else {
pitch = Note.freqToMidiPitch(note.getFrequency());
}
int dynamic = note.getDynamic();
if (pitch == Note.REST) {
phraseTick += note.getRhythmValue() * m_ppqn * elementTempoRatio;
continue;
}
long onTick = (long)(phraseTick);
// pan
if (note.getPan() != lastPanPosition) {
evt = createCChangeEvent(currChannel, 10, (int)(note.getPan()*127), onTick);
currTrack.add(evt);
lastPanPosition = note.getPan();
}
evt = createNoteOnEvent(currChannel, pitch, dynamic, onTick + offSetTime);
currTrack.add(evt);
long offTick = (long)(phraseTick + note.getDuration() * m_ppqn * elementTempoRatio);
evt = createNoteOffEvent(currChannel, pitch, dynamic, offTick + offSetTime);
currTrack.add(evt);
phraseTick += note.getRhythmValue() * m_ppqn * elementTempoRatio;
// TODO: Should this be ticks since we have tempo stuff
// to worry about
//System.out.println("offtick = " + offTick + " ppq = " +
// m_ppqn + " score length = " + score.getEndTime() +
// " length * ppq = " + (m_ppqn * score.getEndTime()));
if ((double)offTick > longestTime) {
longestTime = (double)offTick;
longestTrack = currTrack;
//longestRatio = trackTempoRatio;
}
}
Float d = (Float)m_tempoHistory.pop();
m_currentTempo = d.floatValue();
} // while(phrases.hasMoreElements())
Float d = (Float)m_tempoHistory.pop();
m_currentTempo = d.floatValue();
} // while(parts.hasMoreElements())
// add a meta event to indicate the end of the sequence.
if (longestTime > 0.0 && longestTrack != null) {
MetaMessage msg = new MetaMessage();
byte [] data = new byte[0];
msg.setMessage(StopType, data, 0);
MidiEvent evt = new MidiEvent(msg, (long)longestTime); //+ 100 if you want leave some space for reverb tail
longestTrack.add(evt);
}
return sequence;
}