Example #1
0
  /**
   * Constructs a new Test.
   *
   * @throws Exception if it feels like it.
   */
  public Test() throws Exception {

    String MIDI_NAME = "USB Uno MIDI  In";
    MidiDevice dev = getMidiDevice(MIDI_NAME);
    dev.open();
    MyReceiver rec = new MyReceiver();
    dev.getTransmitter().setReceiver(rec);
    Thread.sleep(20000);
    dev.close();
  }
  private void init() throws MidiUnavailableException {
    try {
      if (!(device.isOpen())) {
        device.open();
      }

      this.transmitter = device.getTransmitter();
      this.mrftd = new MidiReceiverForTransmitterDevice();
    } catch (MidiUnavailableException e) {
      device.close();
      throw e;
    }
  }
Example #3
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;
    }
  }
  /** 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();
      }
    }
  }
Example #5
0
  public void close() {
    if (input != null) {
      input.close();
    }
    input = null;

    if (cl != null) {
      cl.stateChanged(e);
    }
  }
  /** 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());
    }
  }
  /*
   * Change the color of the correct pad on both Launchpads
   * It returns boolean to ensure the signal was sent correctly before doing
   * the console output in the above methods (or prints an error if it fails)
   */
  private boolean setColor(int pad, boolean isOn) {
    boolean success = true;

    // Set and send message (which contains the color) to the real Launchpad
    ShortMessage message = new ShortMessage();
    try {
      if (isOn) {
        message.setMessage(144, pad, colorIndexes[colorIndex]);
      } else {
        message.setMessage(144, pad, colorIndexes[OFF]);
      }
    } catch (InvalidMidiDataException e1) {
      e1.printStackTrace();
      success = false;
    }

    // Gets here if Launchpad is not connected
    // TODO: Output error message if Launchpad is connected but this still fails (might be possible)
    try {
      out.getReceiver().send(message, -1);
    } catch (MidiUnavailableException | NullPointerException e) {
      //			e.printStackTrace();
      //			success = false;
    }

    // Send the pad color to the screen
    try {
      if (isOn) {
        launchpad.setColor(pad, colorValues[colorIndex]);
      } else {
        launchpad.setColor(pad, colorValues[OFF]);
      }
    } catch (NullPointerException e) {
      if (isOn) System.out.println("\t*\tError:\t\t<\tPad " + pad + " cannot be set\t\t>");
      success = false;
    }

    return success; // Made it
  }
Example #8
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;
        }
      }
    }
  }
 public void close() {
   transmitter.close();
   device.close();
   //        this.mrftd.close();
   //        this.transmitter.close();
 }
 public void stopListening() {
   //        this.transmitter.setReceiver(this.defaultReceiver);
   device.close();
   //        this.transmitter.close();
   //        this.mrftd.close();
 }
Example #11
0
 public static boolean allowsTransmitters(MidiDevice device) {
   return probe(device.getMaxTransmitters()) || probe(device.getTransmitters());
 }
Example #12
0
 public static boolean allowsReceivers(MidiDevice device) {
   return probe(device.getMaxReceivers()) || probe(device.getReceivers());
 }
Example #13
0
 public static boolean hasAvailableTransmitters(MidiDevice device) {
   return probe(device.getMaxTransmitters());
 }
Example #14
0
 public static boolean hasAvailableReceivers(MidiDevice device) {
   return probe(device.getMaxReceivers());
 }
Example #15
0
 public boolean isOpen() {
   return input != null && input.isOpen();
 }
Example #16
0
 public String getName() {
   javax.sound.midi.MidiDevice.Info info = device.getDeviceInfo();
   return info.getName() + " " + info.getVendor();
 }
Example #17
0
 MidiOutput(javax.sound.midi.MidiDevice device) throws MidiUnavailableException {
   this.device = device;
   if (!device.isOpen()) device.open();
   receiver = device.getReceiver();
 }
Example #18
0
 /**
  * Close the device associated with this output. This will close other outputs connected to this
  * device as well.
  */
 public void closeMidi() {
   device.close();
 }
Example #19
0
 private void checkOpen(MidiDevice device, boolean desiredState) {
   if (device.isOpen() != desiredState) {
     out("device should be " + getStateString(desiredState) + ", but isn't!");
     failed = true;
   }
 }
Example #20
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");
    }
  }