Beispiel #1
0
 public boolean saveMidiFile(File file) {
   try {
     int[] fileTypes = MidiSystem.getMidiFileTypes(sequence);
     if (fileTypes.length == 0) {
       System.out.println("Can't save sequence");
       return false;
     } else {
       if (MidiSystem.write(sequence, fileTypes[0], file) == -1) {
         throw new IOException("Problems writing to file");
       }
       return true;
     }
   } catch (SecurityException ex) {
     ex.printStackTrace();
     return false;
   } catch (Exception ex) {
     ex.printStackTrace();
     return false;
   }
 }
Beispiel #2
0
  public boolean open() {

    try {
      if (synthesizer == null) {
        if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
          System.out.println("getSynthesizer() failed!");
          return false;
        }
      }
      synthesizer.open();
      sequencer = MidiSystem.getSequencer();
      sequencer.addMetaEventListener(new ProcessMeta());

      sequence = new Sequence(Sequence.PPQ, 10);
    } catch (Exception ex) {
      System.out.println("midi exception 1 ");
      ex.printStackTrace();
      return false;
    }

    System.out.println("midi opening");

    Soundbank sb = synthesizer.getDefaultSoundbank();
    if (sb != null) {
      instruments = synthesizer.getDefaultSoundbank().getInstruments();
      synthesizer.loadInstrument(instruments[0]);
    }
    MidiChannel midiChannels[] = synthesizer.getChannels();
    numChannels = midiChannels.length;
    channels = new ChannelData[midiChannels.length];
    if (channels.length == 0) return false;
    for (int i = 0; i < channels.length; i++) {
      channels[i] = new ChannelData(midiChannels[i], i);
    }
    cc = channels[0];
    return true;
  }