简体   繁体   中英

Java MIDI Sequencer Latency

I've been working on editing some code in the Head First Java book to allow a MIDI sequencer to dynamically update notes.

This works by having a grid 16 x 16 of JCheckboxes that contains actionListeners looking out for user input. Each time a change is detected, the sequencer stops playback, deletes the current track, re-builds the track (by detecting which boxes are checked) and then resumes playback from the current playback position.

This works, but causes a slight delay with each change. Does anybody have any ideas how this could be approached?

public void buildTrackAndStart() {
    int[] trackList = null;

    sequence.deleteTrack(track);
    track = sequence.createTrack();

    for (int i = 0; i < 16; i++) {
        trackList = new int[16];

        int key = instruments[i];

        for (int j = 0; j < 16; j++ ) {
            JCheckBox jc = (JCheckBox) checkboxList.get(j + (16*i));
            if ( jc.isSelected()) {
                trackList[j] = key;
            } else {
                trackList[j] = 0;
            }
        } // close inner loop

        makeTracks(trackList);
        track.add(makeEvent(176,1,127,0,16));
    } // close outer

    track.add(makeEvent(192,9,1,0,15));
    try {
        sequencer.setSequence(sequence);
        sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
        sequencer.start();
        sequencer.setTempoInBPM(120);
    } catch(Exception e) {e.printStackTrace();}
} // close buildTrackAndStart method

You need to add events on the fly while the Java sequencer is playing. It is difficult to do using the default Java sequencer because its behaviour in this case is unspecified, see the JavaSound FAQ 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM