Esempio n. 1
0
 /** return true if the passed message is Meta End Of Track */
 public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
   // first check if it is a META message at all
   if (midiMsg.getLength() != 3 || midiMsg.getStatus() != MetaMessage.META) {
     return false;
   }
   // now get message and check for end of track
   byte[] msg = midiMsg.getMessage();
   return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
 }
Esempio n. 2
0
 /** return if the given message is a meta tempo message */
 public static boolean isMetaTempo(MidiMessage midiMsg) {
   // first check if it is a META message at all
   if (midiMsg.getLength() != 6 || midiMsg.getStatus() != MetaMessage.META) {
     return false;
   }
   // now get message and check for tempo
   byte[] msg = midiMsg.getMessage();
   // meta type must be 0x51, and data length must be 3
   return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
 }
Esempio n. 3
0
 /**
  * parses this message for a META tempo message and returns the tempo in MPQ, or -1 if this isn't
  * a tempo message
  */
 public static int getTempoMPQ(MidiMessage midiMsg) {
   // first check if it is a META message at all
   if (midiMsg.getLength() != 6 || midiMsg.getStatus() != MetaMessage.META) {
     return -1;
   }
   byte[] msg = midiMsg.getMessage();
   if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
     return -1;
   }
   int tempo = (msg[5] & 0xFF) | ((msg[4] & 0xFF) << 8) | ((msg[3] & 0xFF) << 16);
   return tempo;
 }
  public static void assertNoteEventEqual(
      MidiEvent noteEvent,
      long tick,
      byte byte1,
      byte byte2,
      byte byte3,
      boolean ignoreTickOffset) {
    int tickOffset = (ignoreTickOffset ? 0 : MidiNote.MIDI_SEQUENCE_START_SILENCE_TICK_OFFSET);

    assertEquals(tick, noteEvent.getTick() - tickOffset);
    javax.sound.midi.MidiMessage msg = noteEvent.getMessage();
    assertEquals(byte1, msg.getMessage()[0]);
    assertEquals(byte2, msg.getMessage()[1]);
    assertEquals(byte3, msg.getMessage()[2]);
  }
Esempio n. 5
0
 public void send(MidiMessage message, long timeStamp) {
   try {
     if (this.sequencer.isRunning()) {
       parseMessage(message.getMessage());
     }
   } catch (MidiPlayerException e) {
     e.printStackTrace();
   }
 }
Esempio n. 6
0
    /** {@inheritDoc} */
    public void send(MidiMessage midi, long time) {
      byte[] msg = midi.getMessage();
      int len = midi.getLength();
      if (len <= 1) {
        return;
      }
      StringBuilder out = new StringBuilder("Status: ");
      out.append(msg[0]);
      out.append(" Data: [");
      for (int i = 1; i < len; i++) {
        out.append(msg[i]);
        if (i == len - 1) {
          out.append("");
        } else {
          out.append(',');
        }
      }
      out.append(']');

      log.debug(out.toString());
    }
 /** TODO: */
 public void setMessage(int nType, byte[] abData, int nDataLength)
     throws InvalidMidiDataException {
   if (nType > 127) {
     throw new InvalidMidiDataException("type must not exceed 127");
   }
   byte[] abLength = MidiUtils.getVariableLengthQuantity(nDataLength);
   int nCompleteLength = 2 + nDataLength;
   byte[] abCompleteData = new byte[nCompleteLength];
   abCompleteData[0] = (byte) META;
   abCompleteData[1] = (byte) nType;
   System.arraycopy(abData, 0, abCompleteData, 2, nDataLength);
   super.setMessage(abCompleteData, nCompleteLength);
 }
Esempio n. 8
0
  void callPlug(MidiInput _input, final MidiMessage msg) {
    try {
      if ((msg.getStatus() & 0xF0) != getStatus() && getStatus() != -1) return;
      if (msg instanceof ShortMessage) {
        ShortMessage smsg = (ShortMessage) msg;
        if (smsg.getChannel() != channel && channel != -1) return;
      }
      MidiEvent event = null;
      if (msg instanceof MidiEvent) {
        event = (MidiEvent) msg;
      } else {
        event = MidiEvent.create(msg);
      }

      if (event != null) {
        event.setInput(_input);
        if (paramClass.isInstance(event)) method.invoke(getObject(), new Object[] {event});
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException("Error on calling plug: " + methodName);
    }
  }
Esempio n. 9
0
  public void _testGPWithPlayer(MidiSongDefinition sd, int usq) throws Exception {
    MidiSongDefinition testFile = SongArchive.testFileSongDefinition();
    try {
      GPInputStream gpis = new GPInputStream(sd.getGpFileName());
      GPSong gpsong = (GPSong) gpis.readObject();
      gpis.close();

      int tempoGPSong = gpsong.getTempo();
      // OLD assertEquals((int)(60*1000*1000/usq),tempoGPSong);
      assertEquals((60 * 1000 * 1000 / usq), tempoGPSong);

      Song song = GPAdaptor.makeSong(gpsong);

      Tempo tempoSong = song.getTempo();
      assertEquals(usq, (int) tempoSong.getUSQ());

      MasterPlayer player = new MasterPlayer();
      player.setSoundPlayer(new MidiFiler(testFile.getMidiFileName()));
      Performance performance = player.arrange(song, null);

      Tempo tempoPerformance = performance.getTempo();
      assertEquals(usq, (int) tempoPerformance.getUSQ());

      // a performance is really a sequence. So make sure there
      // is a tempo event (meta 0x51) on track 0.

      // make sure as well there is exactly ONE tempo event at timestamp 0
      Sequence sequence = (Sequence) performance;
      Track[] midiTracks = sequence.getTracks();
      Track tempoMap = midiTracks[0];

      Tempo tempoInMIDI = new Tempo();
      for (int i = 0; i < tempoMap.size(); i++) {
        MidiEvent me = tempoMap.get(i);
        long tick = me.getTick();
        if (tick > 0) break;
        MidiMessage mm = me.getMessage();
        if (mm.getStatus() == MetaMessage.META) {
          MetaMessage meta = (MetaMessage) mm;
          if (meta.getType() == 0x51) {
            byte[] data = meta.getData();
            tempoInMIDI.setUSQ(
                ((data[0] & 0x00FF) << 16) | ((data[1] & 0x00FF) << 8) | ((data[2] & 0x00FF)));
            break;
          }
        }
      }
      assertEquals(usq, (int) tempoInMIDI.getUSQ());

      MidiOutputStream mos = new MidiOutputStream(new FileOutputStream(testFile.getMidiFileName()));
      mos.write(performance);
      mos.close();

      compareMIDIFiles(
          sd.getMidiFileName(), testFile.getMidiFileName(), sd.getChannels(), sd.getEventRemap());
    } catch (FileNotFoundException e) {
      fail("file not found exception");
    } catch (GPFormatException e) {
      fail("gp format exception");
    } catch (IOException e) {
      fail("ioexception");
    } catch (CodecFormatException e) {
      fail("codec format exception");
    } catch (InvalidMidiDataException e) {
      fail("invalid midi data exception");
    }
  }
Esempio n. 10
0
 public final synchronized void send(MidiMessage var1, long var2) {
   byte[] var4;
   if ((var4 = var1.getMessage()).length < 3 || !this.b(var4[0], var4[1], var4[2], var2)) {
     this.c.send(var1, var2);
   }
 }
Esempio n. 11
0
  // Is called by Java MIDI libraries for each MIDI message encountered.
  public void send(MidiMessage message, long timeStamp) {
    if (message.getStatus() > 127 && message.getStatus() < 144) { // Note OFF
      // Convert the MIDI channel being used to the controller pin on the
      // Arduino by multipying by 2.
      byte pin = (byte) (2 * (message.getStatus() - 127));

      System.out.println("Got note OFF on pin: " + (pin & 0xFF));
      mb.sendEvent(pin, 0);
      currentPeriod[message.getStatus() - 128] = 0;
    } else if (message.getStatus() > 143 && message.getStatus() < 160) { // Note ON
      // Convert the MIDI channel being used to the controller pin on the
      // Arduino by multipying by 2.
      byte pin = (byte) (2 * (message.getStatus() - 143));

      // Get note number from MIDI message, and look up the period.
      // NOTE: Java bytes range from -128 to 127, but we need to make them
      // 0-255 to use for lookups.  & 0xFF does the trick.

      // After looking up the period, devide by (the Arduino resolution * 2).
      // The Arduino's timer will only tick once per X microseconds based on the
      // resolution.  And each tick will only turn the pin on or off.  So a full
      // on-off cycle (one step on the floppy) is two periods.
      int period = microPeriods[(message.getMessage()[1] & 0xff)] / (ARDUINO_RESOLUTION * 2);

      System.out.println("Got note ON on pin: " + (pin & 0xFF) + " with period " + period);
      System.out.println(message.getLength() + " " + message.getMessage()[message.getLength() - 1]);

      // Zero velocity events turn off the pin.
      if (message.getMessage()[2] == 0) {
        mb.sendEvent(pin, 0);
        currentPeriod[message.getStatus() - 144] = 0;
      } else {
        mb.sendEvent(pin, period);
        currentPeriod[message.getStatus() - 144] = period;
      }
    } else if (message.getStatus() > 223 && message.getStatus() < 240) { // Pitch bends
      // Only proceed if the note is on (otherwise, no pitch bending)
      if (currentPeriod[message.getStatus() - 224] != 0) {
        // Convert the MIDI channel being used to the controller pin on the
        // Arduino by multipying by 2.
        byte pin = (byte) (2 * (message.getStatus() - 223));

        double pitchBend =
            ((message.getMessage()[2] & 0xff) << 8) + (message.getMessage()[1] & 0xff);

        int period =
            (int)
                (currentPeriod[message.getStatus() - 224]
                    / Math.pow(2.0, (pitchBend - 8192) / 8192));
        System.out.println(currentPeriod[message.getStatus() - 224] + "-" + period);
        mb.sendEvent(pin, period);
      }
    }
  }