public MidiConnector() throws MidiUnavailableException {
   infos = MidiSystem.getMidiDeviceInfo();
   for (int i = 0; i < infos.length; i++) {
     devices.add(MidiSystem.getMidiDevice(infos[i]));
     List<Transmitter> transmitters = devices.get(i).getTransmitters();
     for (int j = 0; j < transmitters.size(); j++) {
       // create a new receiver
       transmitters
           .get(j)
           .setReceiver(new MidiInputReceiver(devices.get(i).getDeviceInfo().toString()));
     }
   }
 }
Пример #2
0
 public static MidiDevice getMidiDevice(MidiDevice.Info deviceInfo)
     throws MidiUnavailableException {
   try {
     return MidiSystem.getMidiDevice(deviceInfo);
   } catch (IllegalArgumentException e) {
     // the device does not exist anymore -
     // this seems to be a bug because MidiUnavailableException should
     // be thrown instead
     MidiUnavailableException m = new MidiUnavailableException();
     m.initCause(e);
     throw m;
   }
 }
Пример #3
0
 /** Returns true if at least one MIDI (port) device is correctly installed on the system. */
 public static boolean isMidiInstalled() {
   boolean result = false;
   MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
   for (int i = 0; i < devices.length; i++) {
     try {
       MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
       result = !(device instanceof Sequencer) && !(device instanceof Synthesizer);
     } catch (Exception e1) {
       System.err.println(e1);
     }
     if (result) break;
   }
   return result;
 }
Пример #4
0
  public static MidiDevice getMidiDevice(String name) {

    MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();

    for (Info element : info) {
      if (element.getName().equals(name)) {
        try {
          return MidiSystem.getMidiDevice(element);
        } catch (MidiUnavailableException e) {
          log.error("{}", e);
        }
      }
    }

    return null;
  }
Пример #5
0
  public void open() throws MidiUnavailableException {

    if (input != null && input.isOpen()) {
      return;
    }

    try {
      input = MidiSystem.getMidiDevice(info);
      input.open();
      cl.stateChanged(e);
    } catch (MidiUnavailableException ex) {
      input = null;
      throw ex;
    } catch (IllegalArgumentException ex) {
      input = null;
    }
  }
Пример #6
0
  /** Method to get all available midi ports and add them to the corresponding device list. */
  private void getAvailablePorts() {
    javax.sound.midi.MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    for (int i = 0; i < infos.length; i++) {
      try {
        javax.sound.midi.MidiDevice theDevice = MidiSystem.getMidiDevice(infos[i]);

        if (theDevice instanceof javax.sound.midi.Sequencer) {
          // Ignore this device as it's a sequencer
        } else if (theDevice.getMaxReceivers() != 0) {
          midiOutDevices.add(theDevice);
        } else if (theDevice.getMaxTransmitters() != 0) {
          midiInputDevices.add(theDevice);
        }
      } catch (MidiUnavailableException e) {
        e.printStackTrace();
      }
    }
  }
  /** Fill MIDI devices comboboxes. */
  public void updateMidiDevices(MidiDevice.Info[] midiDeviceInfo) {
    _midiInComboBox.removeAllItems();
    _midiOutComboBox.removeAllItems();

    try {
      int n = midiDeviceInfo.length;
      for (int i = 0; i < n; i++) {
        MidiDevice midiDevice = MidiSystem.getMidiDevice(midiDeviceInfo[i]);

        if (midiDevice.getMaxTransmitters() != 0) {
          _midiInComboBox.addItem(midiDeviceInfo[i]);
        }
        if (midiDevice.getMaxReceivers() != 0) {
          _midiOutComboBox.addItem(midiDeviceInfo[i]);
        }
      }

      _midiInComboBox.setSelectedIndex(-1);
      _midiOutComboBox.setSelectedIndex(-1);
    } catch (MidiUnavailableException exception) {
      System.out.println("MIDI device unavailable: " + exception.getMessage());
    }
  }
Пример #8
0
 public DeviceThatWillTransmitMidi(MidiDevice.Info info) throws MidiUnavailableException {
   this.device = MidiSystem.getMidiDevice(info);
   init();
 }
Пример #9
0
  public static void main(String[] args) throws Exception {
    boolean failed = false;
    out("#4616517: Receiver.send() does not work properly");
    if (!isMidiInstalled()) {
      out("Soundcard does not exist or sound drivers not installed!");
      out("This test requires sound drivers for execution.");
      return;
    }
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    MidiDevice outDevice = null;
    MidiDevice inDevice = null;
    for (int i = 0; i < infos.length; i++) {
      MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
      if (!(device instanceof Synthesizer) && !(device instanceof Sequencer)) {
        if (device.getMaxReceivers() != 0) {
          outDevice = device;
        }
        if (device.getMaxTransmitters() != 0) {
          inDevice = device;
        }
      }
    }
    if (outDevice != null) {
      // set the default provider properties
      System.setProperty(Receiver.class.getName(), "#" + outDevice.getDeviceInfo().getName());
    }
    if (inDevice != null) {
      System.setProperty(Transmitter.class.getName(), "#" + inDevice.getDeviceInfo().getName());
    }
    out("Using MIDI OUT Device: " + outDevice);
    out("Using MIDI IN Device: " + inDevice);

    isTestExecuted = false;
    if (outDevice != null) {
      isTestExecuted = true;
      TestHelper testHelper = new ReceiverTestHelper(outDevice);
      try {
        doTest("Receiver", testHelper);
        failed |= testHelper.hasFailed();
      } catch (Exception e) {
        out("Exception occured, cannot test!");
        isTestExecuted = false;
      }
    }

    if (inDevice != null) {
      isTestExecuted = true;
      TestHelper testHelper = new TransmitterTestHelper(inDevice);
      try {
        doTest("Transmitter", testHelper);
        failed |= testHelper.hasFailed();
      } catch (Exception e) {
        out("Exception occured, cannot test!");
        isTestExecuted = false;
      }
    }

    isTestPassed = !failed;

    if (isTestExecuted) {
      if (isTestPassed) {
        out("Test PASSED.");
      } else {
        throw new Exception("Test FAILED.");
      }
    } else {
      out("Test NOT FAILED");
    }
  }
Пример #10
0
  public void play(String name, String version, String vendor, String desc) throws Exception {
    Info port = null;
    for (Info info : MidiSystem.getMidiDeviceInfo()) {
      if (name == null && version == null && vendor == null && desc == null) {
        System.err.println(
            "At least one of name, version, vendor, or description must be specified.");
        break;
      }
      if (name != null && !info.getName().equals(name)) {
        continue;
      }
      if (vendor != null && !info.getVendor().equals(vendor)) {
        continue;
      }
      if (version != null && !info.getVersion().equals(version)) {
        continue;
      }
      if (desc != null && !info.getDescription().equals(desc)) {
        continue;
      }
      try (MidiDevice dev = MidiSystem.getMidiDevice(info)) {
        Receiver r = null;
        try {
          r = dev.getReceiver();
        } catch (Exception e) {
          r = null;
        }
        if (r == null) {
          continue;
        }
      }
      if (port != null) {
        System.err.println(
            "Multiple MIDI ports match the given parameters. Please be more specific.");
        port = null;
        break;
      }
      port = info;
      continue;
    }
    if (port == null) {
      System.err.println("Unable to locate MIDI port");
      System.err.println("Available ports:");
      for (Info info : MidiSystem.getMidiDeviceInfo()) {
        try (MidiDevice dev = MidiSystem.getMidiDevice(info)) {
          Receiver r = null;
          try {
            r = dev.getReceiver();
          } catch (Exception e) {
            r = null;
          }
          if (r == null) {
            continue;
          }
        }

        System.out.println(
            "Device [name="
                + info.getName()
                + ", version="
                + info.getVersion()
                + ", vendor="
                + info.getVendor()
                + ", desc="
                + info.getDescription()
                + "]");
      }
      return;
    }

    System.out.println(
        "Using device [name="
            + port.getName()
            + ", version="
            + port.getVersion()
            + ", vendor="
            + port.getVendor()
            + ", desc="
            + port.getDescription()
            + "]");

    try (MidiDevice dev = MidiSystem.getMidiDevice(port)) {
      int channel = CHANNEL_RAW_STEREO;
      dev.open();

      try (Receiver rx = dev.getReceiver()) {
        rxCleanup = rx;
        try (DataInputStream dis = new DataInputStream(new FileInputStream(ymz))) {
          resetController(rx, channel, registers);
          long startTime = System.nanoTime();

          // read a buffer
          long prevClock = 0;
          while (true) {
            long clock = dis.readLong();
            byte register = dis.readByte();
            byte value = dis.readByte();
            if (prevClock != clock) {
              sendAll(rx, channel, registers);
              prevClock = clock;
            }
            sleepUntil(startTime, clock);
            System.out.println(
                "Clock: " + clock + " register: " + register + ", value: " + (value & 0xff));
            if (register < 0 || register > 13) {
              System.err.println("Invalid register " + register + " found, ignoring.");
              continue;
            }
            registers[register] = value;
          }
        } catch (EOFException e) {
          System.err.println("EOF reached");
        } finally {
          resetController(rx, channel, registers);
          rxCleanup = null;
        }
      }
    }
  }