title: Java MIDI: Connecting MIDI Keyboard to Default Synthesizer
body: <p>I gotta a MIDI Keyboard. 
  All I want is an example of how I can setup the default software synthesizer to play sounds as I play the Keyboard.</p>
  
  <pre><code>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);
              }
          }
      }
  }
  </code></pre>
  
  <p>By running the code above, I get the following output:</p>
  
  <blockquote>
    <p>run:</p>
    
    <p>Device:  Name: Gervill Vendor: OpenJDK Version: 1.0 Description:
    Software MIDI Synthesizer Transmitters: 0 Receivers: -1</p>
    
    <p>Device:  Name: Oxygen 49 Vendor: M-Audio Version: Unknown version
    Description: Oxygen 49 Transmitters: -1 Receivers: 0</p>
    
    <p>Device:  Name: Oxygen 49 Vendor: M-Audio Version: Unknown version
    Description: Oxygen 49 Transmitters: 0 Receivers: -1</p>
    
    <p>Device:  Name: Real Time Sequencer Vendor: Oracle Corporation Version:
    Version 1.0 Description: Software sequencer Transmitters: -1
    Receivers: -1 BUILD SUCCESSFUL (total time: 2 seconds)</p>
  </blockquote>
  
  <p>But when I run the code below, no sound is played when I hit the keys.</p>
  
  <pre><code>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 &amp;&amp; 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);
              }
          }
  
      }
  
  
  }
  </code></pre>
  
  <p>It seems there is more to do than connecting a Synthesizer.receiver to a InputDevice.transmitter as I got from the docs <a href="https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html" rel="nofollow">https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html</a>.</p>