简体   繁体   English

Java MIDI 音序器延迟

[英]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.我一直在编辑 Head First Java 书中的一些代码,以允许 MIDI 音序器动态更新音符。

This works by having a grid 16 x 16 of JCheckboxes that contains actionListeners looking out for user input.这通过有一个 16 x 16 的 JCheckboxes 网格来工作,其中包含用于查找用户输入的 actionListeners。 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.您需要在 Java 音序器播放时动态添加事件。 It is difficult to do using the default Java sequencer because its behaviour in this case is unspecified, see the JavaSound FAQ 1很难使用默认的 Java 音序器,因为它在这种情况下的行为是未指定的,请参阅 JavaSound FAQ 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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