Пример #1
0
 /**
  * Send a SYSEX MIDI message on this output
  *
  * @param msg Bytes of the sysex message, have to contain 0xF0 at the beginning and 0xF7 at the
  *     end
  * @return 1 on success, 0 on error
  */
 public int sendSysex(byte[] msg) {
   javax.sound.midi.SysexMessage msg2 = new javax.sound.midi.SysexMessage();
   try {
     msg2.setMessage(msg, msg.length);
     receiver.send(msg2, 0);
     return 1;
   } catch (InvalidMidiDataException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     return 0;
   }
 }
  private static MidiEvent readEvent(
      DataInputStream dataInputStream,
      long[] alRemainingBytes,
      int[] anRunningStatusByte,
      long lTicks)
      throws InvalidMidiDataException, IOException {
    int nStatusByte = readUnsignedByte(dataInputStream, alRemainingBytes);
    // TDebug.out("status byte: " + nStatusByte);
    MidiMessage message = null;
    boolean bRunningStatusApplies = false;
    int nSavedByte = 0;
    if (nStatusByte < 0x80) {
      if (anRunningStatusByte[0] != -1) {
        bRunningStatusApplies = true;
        nSavedByte = nStatusByte;
        nStatusByte = anRunningStatusByte[0];
      } else {
        throw new InvalidMidiDataException("corrupt MIDI file: status byte missing");
      }
    }
    switch (getType(nStatusByte)) {
      case STATUS_ONE_BYTE:
        int nByte = 0;
        if (bRunningStatusApplies) {
          nByte = nSavedByte;
        } else {
          nByte = readUnsignedByte(dataInputStream, alRemainingBytes);
          anRunningStatusByte[0] = nStatusByte;
        }
        ShortMessage shortMessage1 = new ShortMessage();
        shortMessage1.setMessage(nStatusByte, nByte, 0);
        message = shortMessage1;
        break;

      case STATUS_TWO_BYTES:
        int nByte1 = 0;
        if (bRunningStatusApplies) {
          nByte1 = nSavedByte;
        } else {
          nByte1 = readUnsignedByte(dataInputStream, alRemainingBytes);
          anRunningStatusByte[0] = nStatusByte;
        }
        int nByte2 = readUnsignedByte(dataInputStream, alRemainingBytes);
        ShortMessage shortMessage2 = new ShortMessage();
        shortMessage2.setMessage(nStatusByte, nByte1, nByte2);
        message = shortMessage2;
        break;

      case STATUS_SYSEX:
        if (CANCEL_RUNNING_STATUS_ON_META_AND_SYSEX) {
          anRunningStatusByte[0] = -1;
        }
        int nSysexDataLength = (int) readVariableLengthQuantity(dataInputStream, alRemainingBytes);
        byte[] abSysexData = new byte[nSysexDataLength];
        for (int i = 0; i < nSysexDataLength; i++) {
          int nDataByte = readUnsignedByte(dataInputStream, alRemainingBytes);
          abSysexData[i] = (byte) nDataByte;
        }
        SysexMessage sysexMessage = new SysexMessage();
        sysexMessage.setMessage(nStatusByte, abSysexData, nSysexDataLength);
        message = sysexMessage;
        break;

      case STATUS_META:
        if (CANCEL_RUNNING_STATUS_ON_META_AND_SYSEX) {
          anRunningStatusByte[0] = -1;
        }
        int nTypeByte = readUnsignedByte(dataInputStream, alRemainingBytes);
        int nMetaDataLength = (int) readVariableLengthQuantity(dataInputStream, alRemainingBytes);
        byte[] abMetaData = new byte[nMetaDataLength];
        for (int i = 0; i < nMetaDataLength; i++) {
          int nDataByte = readUnsignedByte(dataInputStream, alRemainingBytes);
          abMetaData[i] = (byte) nDataByte;
        }
        MetaMessage metaMessage = new MetaMessage();
        metaMessage.setMessage(nTypeByte, abMetaData, nMetaDataLength);
        message = metaMessage;
        break;
      default:
    }
    MidiEvent event = new MidiEvent(message, lTicks);
    return event;
  }