gpt4 book ai didi

java - 在 Java 中从 Receiver 获取 midi 消息

转载 作者:行者123 更新时间:2023-12-01 20:16:45 26 4
gpt4 key购买 nike

我正在尝试使用接收器,以便在播放时提取 MIDI 事件信息。我想听演奏的音符并同步它们是一些事件。我已阅读文档,但不确定如何实际使用发射器/接收器。如果有人可以帮助我指明如何从接收器中获取 midi 事件的正确方向,我将非常感激。

编辑:下面提供的可能的重复内容并不能解释接收器的实际工作原理。它提供了有关实现的源代码,但作为初学者,源代码对我来说太高级了,无法理解。

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

public static Sequencer sequencer;
public static Sequence sequence;
public static Receiver synthRcvr;
public static Transmitter seqTrans;

public static void main(String[] args) {

try {
sequencer = MidiSystem.getSequencer();
sequence = MidiSystem.getSequence(new File("test.midi"));

seqTrans = sequencer.getTransmitter();
synthRcvr = sequencer.getReceiver();
seqTrans.setReceiver(synthRcvr);

sequencer.open();
sequencer.setSequence(sequence);

sequencer.start();
} catch (IOException | MidiUnavailableException | InvalidMidiDataException e) {
System.out.println(e);
}
}

最佳答案

这是收听所有 MIDI 消息的解决方案。

import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class Main {

public static Receiver synthRcvr = new CustomReceiver();
public static Transmitter seqTrans;
public static Synthesizer synth;

public static void main(String[] args) {

try {
sequencer = MidiSystem.getSequencer();
sequence = MidiSystem.getSequence(new File("test.midi"));

Sequencer sequencer = MidiSystem.getSequencer();
seqTrans = sequencer.getTransmitter();
seqTrans.setReceiver(synthRcvr);

sequencer.open();
sequencer.setSequence(sequence);

sequencer.start();
} catch (IOException | MidiUnavailableException | InvalidMidiDataException e) {
System.out.println(e);
}
}

这是实现接收器的类:

import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;

public class CustomReceiver implements Receiver {

public CustomReceiver() {

}

@Override
public void send(MidiMessage message, long timeStamp) {
// Process midi messages here
}

@Override
public void close() {

}
}

关于java - 在 Java 中从 Receiver 获取 midi 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670358/

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