我继承所使用旧C ++ DLL以从接收的MIDI数据钢琴连接到计算机。

现在Java已经内置了对MIDI设备的支持,我想摆脱传统C ++的dll,只是使用纯Java。 是否Java支持从连接到计算机的钢琴接收数据?我搜索谷歌的例子无济于事。

有帮助吗?

解决方案

是,则JavaSound API可以被用于从一个MIDI设备读取的MIDI数据。

JFugue 是为音乐节目的Java API可以使用JavaSound API,并且可以帮助简化您的互动JavaSound。在JFugue 5.x中,示例代码来捕捉MIDI数据10秒钟从一个MIDI设备如下:

MidiDevice device = /* specify a MIDI device */
MusicTransmitterToSequence transmitter = new MusicTransmitterToSequence(device); 
transmitter.listenForMillis(10000); 
Sequence music = transmitter.getSequence();

您还可以启动和停止收听设备:

MidiDevice device = /* specify a MIDI device */
MusicTransmitterToSequence transmitter = new MusicTransmitterToSequence(device); 
transmitter.startListening(); 
// Do stuff
transmitter.stopListening(); 
Sequence music = transmitter.getSequence();

其他提示

如果您希望只用MIDI API被Java记录(javax.sound.midi中。*),这是很容易做到。这不是代码复制并粘贴,但它应该帮助你开始编程自己的MIDI录音,这是很容易的实际。

第一步是定义输入和输出MidiDevice的。所以,首先你必须找到的IO可能性列表和使图形用户界面中,你可以选择输入和输出设备为您的MIDI录制和播放。

Info[] infos = MidiSystem.getMidiDeviceInfo();
for(int i=0;i<infos.length;i++)
{
    System.out.println(infos[i].getName() + " - " + infos[i].getDescription());
}

因此,有你的MIDI设备的列表。接下来,你要选择一个MIDI设备,例如你可以选择的相关信息数组中的索引。

MidiDevice inputDevice = MidiSystem.getMidiDevice(infos[x]);
MidiDevice outputDevice = MidiSystem.getMidiDevice(infos[y]);

您也将要指定一些全局:序器,发射器和接收器

Sequencer sequencer = MidiSystem.getSequencer();
Transmitter transmitter;
Receiver receiver;

现在有一个记录按钮,您要使用。

// Open a connection to your input device
inputDevice.open();
// Open a connection to the default sequencer (as specified by MidiSystem)
sequencer.open();
// Get the transmitter class from your input device
transmitter = inputDevice.getTransmitter();
// Get the receiver class from your sequencer
receiver = sequencer.getReceiver();
// Bind the transmitter to the receiver so the receiver gets input from the transmitter
transmitter.setReceiver(receiver);

// Create a new sequence
Sequence seq = new Sequence(Sequence.PPQ, 24);
// And of course a track to record the input on
Track currentTrack = seq.createTrack();
// Do some sequencer settings
sequencer.setSequence(seq);
sequencer.setTickPosition(0);
sequencer.recordEnable(currentTrack, -1);
// And start recording
sequencer.startRecording();

谨防,这个代码可以抛出MidiUnavailableExceptions,你应该呼吁所有你在最后声明中已经打开了的东西close方法。

但是,这是的代码应该是什么样子只是核心。它只要你调用该方法seq记录一切的序列sequencer.startRecording()

然后要停止录制,并能够序列作为MIDI保存到文件,或做一个播放。例如,当您按下停止录制按钮什么的,这可能是代码。

// Stop recording
if(sequencer.isRecording())
{
    // Tell sequencer to stop recording
    sequencer.stopRecording();

    // Retrieve the sequence containing the stuff you played on the MIDI instrument
    Sequence tmp = sequencer.getSequence();

    // Save to file
    MidiSystem.write(tmp, 0, new File("MyMidiFile.mid"));
}

另外,根据跟踪类(一个序列可以有多个音轨)包含实际输入的数据,这可以很容易地通过一个get方法访问。轨道类由MidiEvents的。例如,该轨道是:

MidiEvent 0: The C key is pressed
MidiEvent 1: The D key is pressed
MidiEvent 2: The C key of MidiEvent 0 is released
MidiEvent 3: The sustain pedal is pressed
etc...

和每个的MidiEvent具有一定的时间戳,这是在MIDI蜱表达,由此可以很容易地通过增加或减少每秒滴答数改变速度。

这里的最困难的问题是,MidiEvents在字节代码中表达,因此,你将不得不使用参考字节码片,它告诉你什么字节表示什么行动。这应该让你开始认为: http://www.onicos.com /staff/iz/formats/midi-event.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top