gpt4 book ai didi

Java Midi Sequencer - 使用播放头进行音符播放

转载 作者:行者123 更新时间:2023-12-02 00:27:17 25 4
gpt4 key购买 nike

我目前实现了一项功能,允许用户播放他们演奏过的音符。

这是代码:

public void playNotes() throws InvalidMidiDataException,
MidiUnavailableException {

ArrayList<KeyPress> keyPressList = keyPressListCollection.getKeyPressList();
double bpm = keyPressListCollection.getMetronome().getBpm();
double millisPerTick = (60000.0 / (bpm * 16.0));
double ticksPerMilli = 1 / millisPerTick;

if (keyPressListCollection.isPlaying()) {
sequencer.stop();
sequencer.close();
}

sequencer = MidiSystem.getSequencer();
sequencer.open();
Sequence sequence = new Sequence(Sequence.PPQ, 16);
Track track = sequence.createTrack();

for (int i = 0; i < keyPressList.size(); i++) {
KeyPress keyPress = keyPressList.get(i);

ShortMessage noteOnMsg = new ShortMessage();
noteOnMsg.setMessage(ShortMessage.NOTE_ON, 0, keyPress.getKeyNumber(), keyPress.getVelocity());

ShortMessage noteOffMsg = new ShortMessage();
noteOffMsg.setMessage(ShortMessage.NOTE_OFF, 0, keyPress.getKeyNumber(), 0);

track.add(new MidiEvent(noteOnMsg, (long) (keyPress.getStartTime() * ticksPerMilli)));
track.add(new MidiEvent(noteOffMsg, (long) (keyPress.getEndTime() * ticksPerMilli)));

}

keyPressListCollection.setPlaying(true);
sequencer.setSequence(sequence);
sequencer.setTempoInBPM((float) bpm);
sequencer.start();

这工作正常,并在我的数组列表中播放音符。不过,我希望能够直观地向用户显示正在播放的音符或播放的位置。

问题是,如何为排序器处理的每个 MidiEvent 进行方法调用?

最佳答案

I want to be able to visually show the user .. or the position of which the playback is at.

使用 JProgressBar 。根据从 Sequencer 方法获取的值设置最大和当前位置 getMicrosecondLength() & getMicrosecondPosition() .

关于Java Midi Sequencer - 使用播放头进行音符播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com