2016-06-01 54 views
0

我得到一個MIDI鍵盤。 我只想要一個例子,說明如何在播放鍵盤時設置默認軟件合成器來播放聲音。Java MIDI:將MIDI鍵盤連接到默認合成器

package cleffsgame; 

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.sound.midi.MidiDevice; 
import javax.sound.midi.MidiSystem; 
import javax.sound.midi.MidiUnavailableException; 

public class CheckDevices { 

    public static void main(String[] args) { 
     MidiDevice device; 
     // display each device's properties 
     for (MidiDevice.Info info: MidiSystem.getMidiDeviceInfo()) { 

      try { 
       device = MidiSystem.getMidiDevice(info); 

       System.out.println("\nDevice: "); 
       System.out.println("Name: " + device.getDeviceInfo().getName()); 
       System.out.println("Vendor: " + device.getDeviceInfo().getVendor()); 
       System.out.println("Version: " + device.getDeviceInfo().getVersion()); 
       System.out.println("Description: " + device.getDeviceInfo().getDescription()); 
       System.out.println("Transmitters: " + device.getMaxTransmitters()); 
       System.out.println("Receivers: " + device.getMaxReceivers()); 

      } catch (MidiUnavailableException ex) { 
       Logger.getLogger(CheckDevices.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

通過運行上面的代碼,我得到以下輸出:

運行:

設備:名稱:Gervill供應商:OpenJDK的版本:1.0說明: 軟件MIDI合成器變送器: 0接收器:-1

裝置:名稱:氧氣49銷售商:M-Audio版本:未知版本 描述:氧氣49發射器:-1接收器:0

設備:名稱:Oxygen 49廠商:M-Audio的版本:版本未知 說明:氧氣49個變送器:0接收器:-1

設備:名稱:實時測序供應商:Oracle公司版本: 版本1.0描述:軟件定序變送器:-1 接收機:-1 BUILD SUCCESSFUL(總時間:2秒)

但是當我運行下面的代碼,當我打鍵沒有播放聲音。

package cleffsgame; 

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.sound.midi.MidiDevice; 
import javax.sound.midi.MidiSystem; 
import javax.sound.midi.MidiUnavailableException; 
import javax.sound.midi.Receiver; 
import javax.sound.midi.Synthesizer; 
import javax.sound.midi.Transmitter; 

/** 
* 
* @author umberto 
*/ 
public class Test { 

    public static void main(String[] args) { 
     MidiDevice inputDevice = null, synthDevice = null; 
     Transmitter transmitter = null; 
     Synthesizer synthesizer = null; 
     Receiver receiver = null; 

     try { 
      inputDevice = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[1]); 
      synthDevice = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[0]); 

     } catch (MidiUnavailableException ex) { 
      Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     // goodDevice must be Oxygen 49 transmitter (MIDI Input) 
     if (inputDevice != null && synthDevice != null) { 
      try { 
       transmitter = inputDevice.getTransmitter(); 
       System.out.println("Transmitter: " + inputDevice.getDeviceInfo()); 
       System.out.println(String.format("T/R: %s/%s", inputDevice.getMaxTransmitters(), inputDevice.getMaxReceivers())); 

       receiver = synthDevice.getReceiver(); 
       System.out.println("Receiver: " + synthDevice.getDeviceInfo()); 
       System.out.println(String.format("T/R: %s/%s", synthDevice.getMaxTransmitters(), synthDevice.getMaxReceivers())); 

       transmitter.setReceiver(receiver); 
       System.out.println("GoodDevice is open... check sound\n"); 
       inputDevice.open(); 
       synthDevice.open(); 

      } catch (Exception ex) { 
       Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

    } 


} 

似乎有更多的事情要做比Synthesizer.receiver連接到InputDevice.transmitter因爲我從文檔https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html了。

+0

你不需要爲合成器指定一個音色文件嗎? – Ishmael

+0

宇得了一個什麼? – gpasch

+0

@Ishmael如果可以,請檢查下面的代碼。在操縱它們之前,我打開了kb設備和合成器。我也裝了一個樂器,但是根本沒有聲音。 如果你可以寫一些你好世界snnipet,它會真正幫助,因爲我從來沒有與MIDI工作過。 –

回答

0

在獲取發射器/接收器之前打開設備可能是個好主意。

但實際的問題是,該程序立即退出,所以設備沒有打開一段有用的時間。

+0

你認爲下面的代碼會起作用嗎? ' public static void main(String [] args){ Synthesizer s = MS.getSynthesizer(); s.open(); Instrument [] inst = s.getAvailableInstruments(); s.loadInstrument(inst [0]); Receiver r = s.getReceiver(); MidiDevice kb = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[1]); kb.open(); Transmitter t = kb.getTransmitter(); t.setReceiver(r); //保持活動狀態 JFrame frame = new JFrame(「Test」); frame.add(...); } ' –

+0

對不起,關於上面未格式化的代碼。 無論如何,我試圖打開midi鍵盤和合成器之前調用其他方法,但我沒有任何聲音。 我的鍵盤在Logic Pro和Virtual Midi Piano Kb等其他應用程序中工作得很好。 我需要一些關於從MIDI鍵盤獲取聲音的Hello World代碼。 –

+0

那麼,你的程序保持打開狀態嗎? –