Example #1
0
  public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    MidiEvent event = null;
    try {
      ShortMessage a = new ShortMessage();
      a.setMessage(comd, chan, one, two);
      event = new MidiEvent(a, tick);

    } catch (Exception e) {
    }
    return event;
  }
  public MidiEvent eventErzeugen(int comd, int chan, int one, int two, int tick) {
    MidiEvent event = null;
    try {
      ShortMessage a = new ShortMessage();
      a.setMessage(comd, chan, one, two);
      event = new MidiEvent(a, tick);

    } catch (Exception e) {
    }
    return event;
  } // Methode eventErzeugen schließen
Example #3
0
 public static MidiEvent addNote(int cmmnd, int chnl, int d1, int d2, int tck) {
   MidiEvent ev = null;
   try {
     ShortMessage msg = new ShortMessage();
     msg.setMessage(cmmnd, chnl, d1, d2);
     ev = new MidiEvent(msg, tck);
   } catch (Exception ex) {
   }
   ;
   return ev;
 }
Example #4
0
 public boolean createEvent(int type, int num, long tick) {
   ShortMessage message = new ShortMessage();
   try {
     message.setMessage(type + cc.num, num, cc.velocity);
     MidiEvent event = new MidiEvent(message, tick);
     track.add(event);
     return true;
   } catch (Exception ex) {
     ex.printStackTrace();
     return false;
   }
 }
  private void loadNotes() {
    int program = 0;
    HashMap<Integer, Float> lastTimeNote = new HashMap<Integer, Float>();
    HashMap<Float, Integer> secondsNotes = new HashMap<Float, Integer>();
    this.notas = new ArrayList<MIDINote>();
    for (Track track : sequencia.getTracks()) {
      for (int c = 0; c < track.size(); ++c) {
        MidiEvent event = track.get(c);
        MidiMessage msg = event.getMessage();
        if (msg instanceof ShortMessage) {
          ShortMessage shortmsg = (ShortMessage) msg;
          if (shortmsg.getCommand() == ShortMessage.PROGRAM_CHANGE) {
            program = shortmsg.getData1();
          } else {
            // }else if(program>=25 && program <= 40){
            // else if(program== 30){
            if (shortmsg.getCommand() == ShortMessage.NOTE_ON) {
              MIDINote midiNote = new MIDINote(event, sequencia, tempoProcessor, program);
              // tocador.start();
              int noteChord = midiNote.getChord();
              float noteSecond = midiNote.getSecond();
              if (!lastTimeNote.containsKey(noteChord)) {
                lastTimeNote.put(noteChord, 0.0f);
              }
              if (noteSecond - lastTimeNote.get(noteChord).floatValue() <= this.interval) {
                continue;
              }
              lastTimeNote.put(noteChord, noteSecond);
              // System.out.println("Play chord "+noteChord+" in "+noteSecond+" seconds");
              notas.add(midiNote);
              if (!secondsNotes.containsKey(noteSecond)) {
                secondsNotes.put(noteSecond, 1);
              }
              secondsNotes.put(noteSecond, secondsNotes.get(noteSecond).intValue() + 1);
            }
          }
        }
      }
    }

    // System.out.println("tamanho da pista "+notas.size()+" e track "+maxNote);
    for (float second : secondsNotes.keySet()) {
      int repeated = secondsNotes.get(second).intValue();
      if (repeated > maxNote) {
        this.maxNote = repeated;
      }
    }
    this.notesLength = secondsNotes.size();
    // GameEngine.getInstance().setFramesPerSecond((int)(((tocador.getMicrosecondLength()/1000000)/(notas.size()*1.0))*4000));
    // System.out.println("(int)(("+sequencia.getMicrosecondLength()+"/1000000)/"+notas.size()+"="+(int)((sequencia.getMicrosecondLength()/1000000)/notas.size()))
  }
Example #6
0
  /**
   * Create the MidiEvent for a note, given the data.
   *
   * @param command The command value for the ShortMessage
   * @param note The MIDI value for the note to be played
   * @param eventTime When this event should occur
   * @param velocity The velocity of this note
   * @return The MidiEvent for the note
   */
  private MidiEvent createNoteEvent(int command, int note, int eventTime, int velocity) {
    // Create the message and set its parameters to the ones given.
    ShortMessage message = new ShortMessage();
    try {
      message.setMessage(command, 0, note, velocity);
    } catch (InvalidMidiDataException ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }

    // Create the MidiEvent and return it.
    return new MidiEvent(message, eventTime);
  }
  private MidiEvent newProgramMessage(long tick, int channel, int programNumber) {
    ShortMessage message = new ShortMessage();
    try {
      message.setMessage(
          ShortMessage.PROGRAM_CHANGE, // command
          channel,
          programNumber,
          0); // not used
    } catch (InvalidMidiDataException ex) {
      throw new RuntimeException(ex);
    }

    return new MidiEvent(message, tick);
  }
Example #8
0
 public String debugString() {
   String dbgs = "(submit: ";
   dbgs += super.debugString();
   dbgs += getSourceAddr().debugString();
   dbgs += " ";
   dbgs += getDestAddr().debugString();
   dbgs += " ";
   dbgs += "(esmClass: " + this.getEsmClass() + ", protocolId: " + this.getProtocolId() + ")";
   dbgs += " ";
   dbgs += shortMessage.debugString();
   dbgs += " ";
   dbgs += debugStringOptional();
   dbgs += ") ";
   return dbgs;
 }
Example #9
0
 public void setBody(ByteBuffer buffer)
     throws NotEnoughDataInByteBufferException, TerminatingZeroNotFoundException, PDUException {
   setServiceType(buffer.removeCString());
   sourceAddr.setData(buffer); // ?
   destAddr.setData(buffer); // ?
   setEsmClass(buffer.removeByte());
   setProtocolId(buffer.removeByte());
   setPriorityFlag(buffer.removeByte());
   setScheduleDeliveryTime(buffer.removeCString());
   setValidityPeriod(buffer.removeCString());
   setRegisteredDelivery(buffer.removeByte());
   setReplaceIfPresentFlag(buffer.removeByte());
   setDataCoding(buffer.removeByte());
   setSmDefaultMsgId(buffer.removeByte());
   setSmLength(decodeUnsigned(buffer.removeByte()));
   shortMessage.setData(buffer.removeBuffer(getSmLength()));
 }
Example #10
0
 public ByteBuffer getBody() {
   ByteBuffer buffer = new ByteBuffer();
   buffer.appendCString(getServiceType());
   buffer.appendBuffer(getSourceAddr().getData());
   buffer.appendBuffer(getDestAddr().getData());
   buffer.appendByte(getEsmClass());
   buffer.appendByte(getProtocolId());
   buffer.appendByte(getPriorityFlag());
   buffer.appendCString(getScheduleDeliveryTime());
   buffer.appendCString(getValidityPeriod());
   buffer.appendByte(getRegisteredDelivery());
   buffer.appendByte(getReplaceIfPresentFlag());
   buffer.appendByte(getDataCoding());
   buffer.appendByte(getSmDefaultMsgId());
   buffer.appendByte(encodeUnsigned(getSmLength()));
   buffer.appendBuffer(shortMessage.getData());
   return buffer;
 }
Example #11
0
  public void parseSequence(boolean[][] grid) {
    ShortMessage on;
    this.grid = grid;
    ShortMessage off;
    int tickLength = 16;
    try {
      ShortMessage sm = new ShortMessage();
      sm.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);
      track.add(new MidiEvent(sm, 0));
      for (boolean[] aGrid : grid) {
        for (int col = 0; col < aGrid.length; col++) {
          if (aGrid[col]) {
            if (other == 1) {
              other = 0;
            } else {
              other = 1;
            }
            off = new ShortMessage();
            off.setMessage(
                ShortMessage.NOTE_OFF,
                0,
                scale[(grid[0].length - col - 1 + other) % scale.length],
                velocity);
            on = new ShortMessage();
            on.setMessage(
                ShortMessage.NOTE_ON,
                0,
                scale[(grid[0].length - col - 1 + other) % scale.length],
                velocity);
            track.add(new MidiEvent(on, ticks));
            track.add(new MidiEvent(off, ticks + tickLength));
          }
        }
        ticks += tickLength;
      }

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
  }
  public void play(int instrument, int note) {

    try {

      Sequencer player = MidiSystem.getSequencer();
      player.open();

      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      // MidiEvent event = null;

      ShortMessage first = new ShortMessage();
      first.setMessage(192, 1, instrument, 0);
      MidiEvent changeInstrument = new MidiEvent(first, 1);
      track.add(changeInstrument);

      ShortMessage a = new ShortMessage();
      a.setMessage(144, 1, note, 100);
      MidiEvent noteOn = new MidiEvent(a, 1);
      track.add(noteOn);

      ShortMessage b = new ShortMessage();
      b.setMessage(128, 1, note, 100);
      MidiEvent noteOff = new MidiEvent(b, 16);
      track.add(noteOff);
      player.setSequence(seq);
      player.start();

      Thread.sleep(2000);
      player.close();
      System.exit(0);

    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
  } // close play
Example #13
0
 private MidiEvent createBellNoteOff(int bell, long millis) throws InvalidMidiDataException {
   ShortMessage msg = new ShortMessage();
   msg.setMessage(ShortMessage.NOTE_OFF, 0, PITCH_SCALE[fNBells - bell] + fPitchIncrease, 100);
   return new MidiEvent(msg, millis * 1000);
 }
 /** We assume that we are holding the "this" monitor */
 private void addStopEvent(int note) throws InvalidMidiDataException {
   ShortMessage message = new ShortMessage();
   message.setMessage(ShortMessage.NOTE_OFF, 0, note, 0);
   track.add(new MidiEvent(message, pos));
 }
Example #15
0
 public ByteBuffer getShortMessageData() {
   return shortMessage.getData();
 }
Example #16
0
 public String getShortMessage(String encoding) throws UnsupportedEncodingException {
   return shortMessage.getMessage(encoding);
 }
Example #17
0
 public String getShortMessage() {
   return shortMessage.getMessage();
 }
Example #18
0
 public void setShortMessage(String value, String encoding)
     throws WrongLengthOfStringException, UnsupportedEncodingException {
   shortMessage.setMessage(value, encoding);
   setSmLength((short) shortMessage.getLength());
 }
Example #19
0
 public void setShortMessage(String value) throws WrongLengthOfStringException {
   shortMessage.setMessage(value);
   setSmLength((short) shortMessage.getLength());
 }
Example #20
0
 public void setShortMessageData(ByteBuffer value)
     throws PDUException, NotEnoughDataInByteBufferException, TerminatingZeroNotFoundException {
   shortMessage.setData(value);
   setSmLength((short) shortMessage.getLength());
 }
  private InputStream writeTrack(Track track, int type)
      throws IOException, InvalidMidiDataException {
    int bytesWritten = 0;
    int lastBytesWritten = 0;
    int size = track.size();
    PipedOutputStream thpos = new PipedOutputStream();
    DataOutputStream thdos = new DataOutputStream(thpos);
    PipedInputStream thpis = new PipedInputStream(thpos);

    ByteArrayOutputStream tdbos = new ByteArrayOutputStream();
    tddos = new DataOutputStream(tdbos);
    ByteArrayInputStream tdbis = null;

    SequenceInputStream fStream = null;

    long currentTick = 0;
    long deltaTick = 0;
    long eventTick = 0;
    int runningStatus = -1;

    // -----------------------------
    // Write each event in the track
    // -----------------------------
    for (int i = 0; i < size; i++) {
      MidiEvent event = track.get(i);

      int status;
      int eventtype;
      int metatype;
      int data1, data2;
      int length;
      byte data[] = null;
      ShortMessage shortMessage = null;
      MetaMessage metaMessage = null;
      SysexMessage sysexMessage = null;

      // get the tick
      // $$jb: this gets easier if we change all system-wide time to delta ticks
      eventTick = event.getTick();
      deltaTick = event.getTick() - currentTick;
      currentTick = event.getTick();

      // get the status byte
      status = event.getMessage().getStatus();
      eventtype = getType(status);

      switch (eventtype) {
        case ONE_BYTE:
          shortMessage = (ShortMessage) event.getMessage();
          data1 = shortMessage.getData1();
          bytesWritten += writeVarInt(deltaTick);

          if (status != runningStatus) {
            runningStatus = status;
            tddos.writeByte(status);
            bytesWritten += 1;
          }
          tddos.writeByte(data1);
          bytesWritten += 1;
          break;

        case TWO_BYTE:
          shortMessage = (ShortMessage) event.getMessage();
          data1 = shortMessage.getData1();
          data2 = shortMessage.getData2();

          bytesWritten += writeVarInt(deltaTick);
          if (status != runningStatus) {
            runningStatus = status;
            tddos.writeByte(status);
            bytesWritten += 1;
          }
          tddos.writeByte(data1);
          bytesWritten += 1;
          tddos.writeByte(data2);
          bytesWritten += 1;
          break;

        case SYSEX:
          sysexMessage = (SysexMessage) event.getMessage();
          length = sysexMessage.getLength();
          data = sysexMessage.getMessage();
          bytesWritten += writeVarInt(deltaTick);

          // $$jb: 04.08.99: always write status for sysex
          runningStatus = status;
          tddos.writeByte(data[0]);
          bytesWritten += 1;

          // $$jb: 10.18.99: we don't maintain length in
          // the message data for SysEx (it is not transmitted
          // over the line), so write the calculated length
          // minus the status byte
          bytesWritten += writeVarInt((data.length - 1));

          // $$jb: 10.18.99: now write the rest of the
          // message
          tddos.write(data, 1, (data.length - 1));
          bytesWritten += (data.length - 1);
          break;

        case META:
          metaMessage = (MetaMessage) event.getMessage();
          length = metaMessage.getLength();
          data = metaMessage.getMessage();
          bytesWritten += writeVarInt(deltaTick);

          // $$jb: 10.18.99: getMessage() returns the
          // entire valid midi message for a file,
          // including the status byte and the var-length-int
          // length value, so we can just write the data
          // here.  note that we must _always_ write the
          // status byte, regardless of runningStatus.
          runningStatus = status;
          tddos.write(data, 0, data.length);
          bytesWritten += data.length;
          break;

        case IGNORE:
          // ignore this event
          break;

        case ERROR:
          // ignore this event
          break;

        default:
          throw new InvalidMidiDataException("internal file writer error");
      }
    }
    // ---------------------------------
    // End write each event in the track
    // ---------------------------------

    // Build Track header now that we know length
    thdos.writeInt(MTrk_MAGIC);
    thdos.writeInt(bytesWritten);
    bytesWritten += 8;

    // Now sequence them
    tdbis = new ByteArrayInputStream(tdbos.toByteArray());
    fStream = new SequenceInputStream(thpis, tdbis);
    thdos.close();
    tddos.close();

    return fStream;
  }
  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;
  }
Example #23
0
 /**
  * Doesn't appear to work!
  *
  * @param bank
  * @param millis
  * @return
  * @throws InvalidMidiDataException
  */
 private MidiEvent createBankChange(int bank, long millis) throws InvalidMidiDataException {
   ShortMessage msg = new ShortMessage();
   msg.setMessage(ShortMessage.CONTROL_CHANGE, 0, 0, bank);
   return new MidiEvent(msg, millis * 1000);
 }
Example #24
0
 private MidiEvent createProgramChange(int instrument, long millis)
     throws InvalidMidiDataException {
   ShortMessage msg = new ShortMessage();
   msg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instrument, 0);
   return new MidiEvent(msg, millis * 1000);
 }