2012-02-29 20 views
6

我正在試圖將我鋼琴的midiport連接到我的電腦。 我已經閱讀了我能找到的所有內容,但不知何故,我錯過了一些東西,所以我希望這裏有人能幫助我。我正在試着這樣做一個星期,現在它變得令人沮喪。在Java中訪問MidiDevice

public class MidiDeviceGetter { 

    public MidiDeviceGetter() {} 

    public static void listTransmitterDevices() throws MidiUnavailableException { 
     MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); 
     for (int i = 0; i < infos.length; i++) { 
     MidiDevice device = MidiSystem.getMidiDevice(infos[i]); 
     if (device.getMaxTransmitters() != 0) 
      System.out.println(device.getDeviceInfo().getName().toString() 
        + " has transmitters"); 
     } 
    } 

    // should get me my USB MIDI Interface. There are two of them but only one 
    // has Transmitters so the if statement should get me the one i want 
    public static MidiDevice getInputDevice() throws MidiUnavailableException { 
     MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); 
     for (int i = 0; i < infos.length; i++) { 
     MidiDevice device = MidiSystem.getMidiDevice(infos[i]); 
     if (device.getMaxTransmitters() != 0 
       && device.getDeviceInfo().getName().contains("USB")) { 
      System.out.println(device.getDeviceInfo().getName().toString() 
        + " was chosen"); 
      return device; 
     } 
     } 
     return null; 
    } 

    public static void main(String[] args) throws MidiUnavailableException, 
     IOException { 
     MidiDevice inputDevice; 

     // MidiDeviceGetter.listTransmitterDevices(); 
     inputDevice = MidiDeviceGetter.getInputDevice(); 

     // just to make sure that i got the right one 
     System.out.println(inputDevice.getDeviceInfo().getName().toString()); 
     System.out.println(inputDevice.getMaxTransmitters()); 

     // opening the device 
     System.out.println("open inputDevice: " 
      + inputDevice.getDeviceInfo().toString()); 
     inputDevice.open(); 
     System.out.println("connect Transmitter to Receiver"); 

     // Creating a Dumpreceiver and setting up the Midi wiring 
     Receiver r = new DumpReceiver(System.out); 
     Transmitter t = inputDevice.getTransmitter(); 
     t.setReceiver(r); 

     System.out.println("connected."); 
     System.out.println("running..."); 
     System.in.read(); 
     // at this point the console should print out at least something, as the 
     // send method of the receiver should be called when i hit a key on my 
     // keyboard 
     System.out.println("close inputDevice: " 
      + inputDevice.getDeviceInfo().toString()); 
     inputDevice.close(); 
     System.out.println(("Received " + ((DumpReceiver) r).seCount 
      + " sysex messages with a total of " 
      + ((DumpReceiver) r).seByteCount + " bytes")); 
     System.out.println(("Received " + ((DumpReceiver) r).smCount 
      + " short messages with a total of " 
      + ((DumpReceiver) r).smByteCount + " bytes")); 
     System.out.println(("Received a total of " 
        + (((DumpReceiver) r).smByteCount + 
         ((DumpReceiver) r).seByteCount) + " bytes")); 
    } 
} 

那麼,這就是我到目前爲止。我只是想讓鋼琴連接起來,所以我可以從那裏走得更遠,但正如我所說,我無法讓它工作。

爲了測試我從 http://www.jsresources.org/examples/DumpReceiver.java.html拿到了DumpReceiver類。

我非常感謝任何幫助,謝謝。

P.S .:對不起,我的英語不是母語。

EDIT1:根據評論:

我希望PROGRAMM打印的東西在控制檯時,我打而 System.in()正在運行的關鍵,因爲在接收器的發送(的MidiMessage,長)方法最後一行是Prinstream.print(midimsg)。我是對的,認爲如果在接收器連接的發射器上播放音符時,總是調用Receiver接口類的發送方法,是不是我? 如果只有這樣做不起作用,我可以弄明白,但也有接收器的一些成員變量應該保存被擊中的鍵的數量,但這些成員變量總是爲空。所以我的主要問題是,發送方法從未被調用過。 我希望我明確了我的主要問題是什麼。編輯2:如果你進入了整個「用java編寫的midi程序」 - 你看不到任何重大錯誤,那麼請告訴我。我發現我無法在Steinbergs Cubase中獲得任何Midisignals。也許這次,問題不在屏幕前。

+0

*「我無法讓它工作」*可能是在罷工,或睏倦,或只是簡單的懶惰。首先,談判。對於第二個,給它一個好的晚上休息,並在早上嘗試。對於第三,嘗試胡蘿蔔和棒的方法。對於其他任何事情,請嘗試描述1)你期望發生什麼2)實際發生了什麼,以及用途3)爲什麼你期望(1)發生。此外,爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-02-29 14:11:30

回答

7

好吧我想通了。我張貼的代碼完全正確並且正在工作。問題是,我的USB MIDI接口的MIDI IN插頭屬於我鋼琴的MIDI OUT插頭。猜猜我現在要把我的頭撞到牆上幾個小時。

感謝您的閱讀。