[英]Java MIDI - getting data from piano?
我繼承了一個Java項目, 該項目使用舊的C ++ dll從連接到計算機的鋼琴接收MIDI數據 。
既然Java內置了對MIDI設備的支持,我想擺脫傳統的C ++ DLL並且只使用純Java。 Java是否支持從連接到計算機的鋼琴接收數據? 我搜索谷歌的例子無濟於事。
如果你想用Java(javax.sound.midi。*)只用MIDI api錄音,這很容易完成。 這不是復制和粘貼的代碼,但它應該可以幫助您開始編程自己的MIDI錄音機,這實際上非常簡單。
第一步是定義輸入和輸出MidiDevice。 因此,首先您必須找到IO可能性列表並制作一個GUI,您可以在其中選擇用於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,您應該在finally語句中打開的所有內容上調用close方法。
但這只是代碼應該是什么樣子的核心。 一旦調用方法sequencer.startRecording()
它就會將所有內容記錄到Sequence seq
中。
然后,您想要停止錄制,並能夠將序列作為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"));
}
Track類(一個序列可以有多個軌道)也包含實際的輸入數據,您可以通過get方法輕松訪問這些數據。 Track類由MidiEvents組成。 例如,Track是:
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 Ticks表示,因此你可以通過增加或減少每秒的滴答數來輕松改變速度。
這里最難的是MidiEvents用字節代碼表示,因此你必須使用一個參考字節代碼表來告訴你哪個字節代表什么動作。 這應該可以幫助您: http : //www.onicos.com/staff/iz/formats/midi-event.html
是的,JavaSound API可用於從MIDI設備讀取MIDI數據。
JFugue是一個使用JavaSound API的音樂編程Java 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.