Search code examples
javamidijavasound

Sequencer receiver timestamp always outputs -1


I am trying to use a receiver to output the timestamp from a sequencer that is reading a midi. When I try to output the timestamp to the console, it outputs -1. Code that runs the midi:

public class MidiReadAndPlay {

    public static Receiver synthRcvr = new CustomReceiver();
    public static Transmitter seqTrans;
    public static Sequencer sequencer;
    public static Sequence sequence;
    public static Synthesizer synth;

    public static void main(String[] args) throws Exception {


        sequencer = MidiSystem.getSequencer();
        sequence = MidiSystem.getSequence(new File("File.mid"));

        seqTrans = sequencer.getTransmitter();
        seqTrans.setReceiver(synthRcvr);
        sequencer.open(); 
        sequencer.setSequence(sequence);

        sequencer.start();



    }

}

Receiver code:

public class CustomReceiver implements Receiver {

    public CustomReceiver() {

    }
    public static final int NOTE_ON = 0x90;
    public static final int NOTE_OFF = 0x80;
    public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

    @Override
    public void send(MidiMessage message, long timeStamp) {
        System.out.println(timeStamp);
        if (message instanceof ShortMessage) {
            ShortMessage sm = (ShortMessage) message;
            System.out.print("Channel: " + sm.getChannel() + " ");
            if (sm.getCommand() == NOTE_ON) {
                int key = sm.getData1();
                int octave = (key / 12)-1;
                int note = key % 12;
                String noteName = NOTE_NAMES[note];
                int velocity = sm.getData2();
                System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
            } else if (sm.getCommand() == NOTE_OFF) {
                int key = sm.getData1();
                int octave = (key / 12)-1;
                int note = key % 12;
                String noteName = NOTE_NAMES[note];
                int velocity = sm.getData2();
                System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
            } else {
                System.out.println("Command:" + sm.getCommand());
            }
        } else {
            System.out.println("Other message: " + message.getClass());
        }
    }

    @Override
    public void close() {

    }
}

The send method should output the timestamp but it doesn't. It just outputs -1. Is there a way to fix this or am I doing something wrong? I am trying to get the time stamp of when a note is played. Thanks.

EDIT

I made this solution but it doesn't always work.

int milliseconds = Math.toIntExact(MidiReadAndPlay.sequencer.getMicrosecondPosition()) / 1000;
int ppq = MidiReadAndPlay.sequence.getResolution();
int bpm = Math.round(MidiReadAndPlay.sequencer.getTempoInBPM());
int msPerTick = 60000 / (bpm * ppq);
int tick = milliseconds/msPerTick;
System.out.println("milliseconds: " + milliseconds + ", ppq: " + ppq + ", bpm: " + bpm + ", msPerTick: " + msPerTick + ", tick: " + tick);

Sometimes the ticks are really large, usually in tens of thousands. For one midi, the ppq is 480 and the bpm is 120. I think there may be an error with the formula.

EDIT 2

I found this function to find the tick and I feel like this is better than the solution I wrote above.

int tick = Math.toIntExact(sequencer.getTickPosition());

I feel like its outputting the ticks to high because a 3 minute midi ends at 176000 ticks. Is it supposed to be that high?


Solution

  • The timestamp in the Send function does not work. So I just used:

    int tick = Math.toIntExact(sequencer.getTickPosition());
    

    which worked well