javax.sound.Midi, пример того, как включить Meta Event Type? - PullRequest
0 голосов
/ 20 октября 2019

Я проверял этот вопрос Завершить вывод Java Midi

На основании этих документов https://www.csie.ntu.edu.tw/~r92092/ref/midi/

Я хочу включить в контролируемый режим некоторые дополнительные события ...

Вот код Midi:

import javax.sound.midi.*;

class TestMidiSound {

  public static MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    ShortMessage a = new ShortMessage();
    try {
      a.setMessage(comd, chan, one, two);
    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
    return new MidiEvent(a, tick);
  }

  public static void main(String[] args) {

    try {

      final Sequencer seq = MidiSystem.getSequencer();
      seq.open();

      int[] events = {125};

      seq.addControllerEventListener(new ControllerEventListener() {
        @Override
        public void controlChange(ShortMessage event) {
          System.out.println(
              "event.getCommand():" + event.getCommand()
              + "\tevent.getChannel():" + event.getChannel()
              + "\tevent.getData1():" + event.getData1()
              + "\tevent.getData2():" + event.getData2()
              + "\tevent.toString():" + event.toString());
        }
      }, events);
      Sequence s = new Sequence(Sequence.PPQ, 4);

      Track t = s.createTrack();

      for (int i = 5; i < 60; i += 4) {
        t.add(MySound.makeEvent(/*144*/ShortMessage.NOTE_ON, 1 /*channel*/, i/*pitch*/, 100 /*velocity*/, i /*tick*/));

        // No will be controlled
        t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 127 /*[Channel Mode Message] Poly Mode On (+ mono off, +all notes off)*/, 0, i/*tick*/));

        t.add(MySound.makeEvent(/*176*/ShortMessage.CONTROL_CHANGE, 1/*channel*/, 125 /*[Channel Mode Message] Omni Mode On (+ all notes off)*/, 0, i + 2/*tick*/));
        t.add(MySound.makeEvent(/*128*/ShortMessage.NOTE_OFF, 1/*channel*/, i/*pitch*/, 100/*velocity*/, i + 2/*tick*/));
      }

      seq.setSequence(s);
      seq.addMetaEventListener(new MetaEventListener() {
        @Override
        public void meta(MetaMessage metaMsg) {
          if (metaMsg.getType() == 0x2F) {
            seq.close();
          }
        }
      });
      seq.setTempoInBPM(220);
      seq.start();

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (MidiUnavailableException e) {
      e.printStackTrace();
    }
  }
}

Здесь мои события

  • Время Signature
  • Key Signature
  • Set Tempo
  • Префикс Midi-порта
  • Префикс Midi-канала
  • Смещение SMPTE

Как включить Тип мета-события .

1 Ответ

1 голос
/ 20 октября 2019
  private static MidiEvent createMetaMessageMidiEvent(int type, byte[] data, int length, long instant) {
    MetaMessage metaMessage = new MetaMessage();
    try {
      metaMessage.setMessage(type, data, length);
    } catch (InvalidMidiDataException e) {
    }
    return new MidiEvent(metaMessage, instant);
  }

Теперь вы можете вставить события ...

  long instant = 0L;

  byte[] bytesSetTempo = {0x0F, 0x42, 0x40};//1'000.000
  byte[] bytesTimeSignature = {0x05, 0x03, 0x18, 0x16};//'Time Signature/numerator: '05' = 5, denominator pow: '03' -> Pow(2, 3) = 8, MIDI Clocks :'18' = 24, number 1/32 notes per 24 MIDI clocks:'16' = 16'

  String trackNamePiano = new String("Track Name:Piano");
  byte[] bytesKeySignaturePiano = {0x02, 0x00};//'02' = 2 sharps, key: '00' = major key'
  trackPiano.add(createMetaMessageMidiEvent(0x03, trackNamePiano.getBytes(), trackNamePiano.length(), instant));
  trackPiano.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
  trackPiano.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
  trackPiano.add(createMetaMessageMidiEvent(0x59, bytesKeySignaturePiano, bytesKeySignaturePiano.length, instant));

  String trackNameViolin = new String("Track Name:Violin");
  byte[] bytesKeySignatureViolin = {(byte) 0xFE, 0x01};//'FE' = 2 flats, key: '01' = minor key'
  trackViolin.add(createMetaMessageMidiEvent(0x03, trackNameViolin.getBytes(), trackNameViolin.length(), instant));
  trackViolin.add(createMetaMessageMidiEvent(0x58, bytesTimeSignature, bytesTimeSignature.length, instant));
  trackViolin.add(createMetaMessageMidiEvent(0x51, bytesSetTempo, bytesSetTempo.length, instant));
  trackViolin.add(createMetaMessageMidiEvent(0x59, bytesKeySignatureViolin, bytesKeySignatureViolin.length, instant));
...