Java MIDI - ControllerEventListener (How to change the instrument)

别说谁变了你拦得住时间么 提交于 2020-06-01 06:58:05

问题


I'm working on a program which generates and plays MIDI Events. I've implemented the ControllerEventListener Interface for I want to print messages every time one of the 15 notes is played.

The problem is, that I can't change the instrument the notes are played by anymore (by that I've to say that my computer don't uses the default Piano, but a drum or something similar if I add the ControllerEvent to the track) I already looked up the involved methods in the Java API specifications, but I found nothing to solve my problem, just as little I found on this Site or by googling. What I also did, was to try to change the instrument of the whole channel (by adding a Program Change message), but it was just the same as before.

The code is the following:

import javax.sound.midi.*;

public class MusikPlayer implements ControllerEventListener {
    public static void main(String[] args) {
        MusikPlayer mini = new MusikPlayer();
        mini.go();
    }

    public void go() {
        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();

            int[] wishedEvents = {127};
            sequencer.addControllerEventListener(this, wishedEvents);

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

            for (int i = 5; i < 61; i+=4) {
                track.add(generateEvent(144, 1, i, 100, i));
                track.add(generateEvent(128, 1, i, 100, i+2));
                track.add(generateEvent(176, 1, 127, 0, i));
            }

            sequencer.setSequence(seq);
            sequencer.setTempoInBPM(220);
            sequencer.start();
            Thread.sleep(5000);
            sequencer.close();

        } catch (Exception e) {e.printStackTrace();}
    }

    public void controlChange(ShortMessage event) {
        System.out.println("la");
    }

    public MidiEvent generateEvent(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;
    }
}

My Question now is: How can I change the Instrument without a change of my event handling procedure?


回答1:


Check this code

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.sound.midi.*;

public class TestMidi {

  private static final int TYPE_SINGLE_TRACK = 0;
  private static final int TYPE_PARALLEL_TRACKS = 1;
  private static final int TYPE_SERIAL_TRACKS = 2;

  public static void main(String[] args) {

    String filename = TestMidi.class.getName() + "_" + new SimpleDateFormat("yyyyMMdd-HHmmss.SSSS").format(new Date()) + ".mid";
    System.out.println("filename:" + filename);
    File midiOutputFile = new File(filename);
    System.out.println("midiOutputFile.getAbsolutePath():" + midiOutputFile.getAbsolutePath());
    Sequence sequence = null;
    Synthesizer synthesizer = null;
    final Sequencer sequencer;
    List<Instrument> listInstrument;
    List<MidiChannel> listMidiChannel;
    Track trackPiano;
    Track trackViolin;
    try {
      synthesizer = MidiSystem.getSynthesizer();
      synthesizer.open();
      synthesizer.loadAllInstruments(synthesizer.getDefaultSoundbank());
      listInstrument = Arrays.asList(synthesizer.getLoadedInstruments());
      listMidiChannel = Arrays.asList(synthesizer.getChannels());
      listInstrument
          .stream()
          .map(instrument -> instrument.getPatch())
          .forEach(patch -> System.out.println("patch.getBank():" + patch.getBank()
          + "\tpatch.getProgram():" + patch.getProgram()
          + "\tlistInstrument.get(patch.getProgram()).getName():" + listInstrument.get(patch.getProgram()).getName()));

      Instrument instrumentPiano = listInstrument
          .stream()
          .filter(instrument -> instrument.getName().equals("Acoustic Grand Piano"))
          .findFirst()
          .get();
      Instrument instrumentViolin = listInstrument
          .stream()
          .filter(instrument -> instrument.getName().equals("Violin"))
          .findFirst()
          .get();

      int channelPiano = 0;
      int channelViolin = 1;

      listMidiChannel.get(channelPiano).programChange(instrumentPiano.getPatch().getProgram());
      listMidiChannel.get(channelViolin).programChange(instrumentViolin.getPatch().getProgram());

      sequence = new Sequence(Sequence.PPQ, 10); //10 pulses per quarter note
      trackPiano = sequence.createTrack();
      trackViolin = sequence.createTrack();
      long instant = 0L;
      trackPiano.add(createMidiEvent(ShortMessage.PROGRAM_CHANGE, channelPiano, instrumentPiano.getPatch().getProgram(), 0, instant * sequence.getResolution() / 500));
      trackViolin.add(createMidiEvent(ShortMessage.PROGRAM_CHANGE, channelViolin, instrumentViolin.getPatch().getProgram(), 0, instant * sequence.getResolution() / 500));

      instant = 250L; //250milliSeconds -> 1/4 Second
      int pitchViolin = 63;
      int valueViolin = 127;
      trackViolin.add(createMidiEvent(ShortMessage.NOTE_ON, channelViolin, pitchViolin, valueViolin, instant * sequence.getResolution() / 500));
      instant = 500L; //500milliSeconds -> 1/2 Second
      int pitchPiano = 60;
      int valuePiano = 64;
      trackPiano.add(createMidiEvent(ShortMessage.NOTE_ON, channelPiano, pitchPiano, valuePiano, instant * sequence.getResolution() / 500));
      instant = 1000L; //1000milliSeconds -> 1 Second
      trackPiano.add(createMidiEvent(ShortMessage.NOTE_OFF, channelPiano, pitchPiano, valuePiano, instant * sequence.getResolution() / 500));
      instant = 1250L; //1250milliSeconds

      trackViolin.add(createMidiEvent(ShortMessage.NOTE_OFF, channelViolin, pitchViolin, valueViolin, instant * sequence.getResolution() / 500));

      instant = 1500L; //1500milliSeconds
      pitchPiano = 66;
      trackPiano.add(createMidiEvent(ShortMessage.NOTE_ON, channelPiano, pitchPiano, valuePiano, instant * sequence.getResolution() / 500));
      instant = 1000L; //500milliSeconds
      trackPiano.add(createMidiEvent(ShortMessage.NOTE_OFF, channelPiano, pitchPiano, valuePiano, instant * sequence.getResolution() / 500));

      sequencer = MidiSystem.getSequencer();
      try {
        sequencer.open();
        sequencer.setSequence(sequence);
      } catch (Exception ex) {
        Logger.getLogger(TestMidi.class.getName()).log(Level.SEVERE, null, ex);
      }
      sequencer.start();

      Integer midiFileType;
      if (sequence.getTracks().length == 1) {
        midiFileType = TYPE_SINGLE_TRACK;
      } else {
        midiFileType = TYPE_PARALLEL_TRACKS;
      }

      int[] arrayMidiFileTypes = MidiSystem.getMidiFileTypes(sequence);
      if (arrayMidiFileTypes.length > 0) {
        MidiSystem.write(sequence, arrayMidiFileTypes[0], midiOutputFile);
      }

      sequencer.addMetaEventListener(new MetaEventListener() {
        @Override
        public void meta(MetaMessage metaMsg) {
          if (metaMsg.getType() == 0x2F) {
            sequencer.close();
          }
        }
      });

      /*
      while (sequencer.isRunning()) {
        try {
          Thread.sleep(100);
        } catch (InterruptedException ex) {
          Logger.getLogger(TestMidi.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      sequencer.close();
       */
    } catch (InvalidMidiDataException e) {
      Logger.getLogger(TestMidi.class.getName()).log(Level.SEVERE, null, e);

    } catch (MidiUnavailableException ex) {
      Logger.getLogger(TestMidi.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      Logger.getLogger(TestMidi.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  private static MidiEvent createMidiEvent(int command, int channel, int data1, int data2, long instant) {
    ShortMessage shortMessage = new ShortMessage();
    try {
      shortMessage.setMessage(
          command,
          channel,
          data1,
          data2);
    } catch (InvalidMidiDataException e) {
    }
    return new MidiEvent(shortMessage, instant);
  }

}


来源:https://stackoverflow.com/questions/55228242/java-midi-controllereventlistener-how-to-change-the-instrument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!