Ejemplo n.º 1
0
  /**
   * Saves the MIDI data from a pattern into a file.
   *
   * @param pattern the pattern to save
   * @param file the File to save the pattern to. Should include file extension, such as .mid
   */
  public void saveMidi(Pattern pattern, File file) throws IOException {
    Sequence sequence = getSequence(pattern);

    int[] writers = MidiSystem.getMidiFileTypes(sequence);
    if (writers.length == 0) return;

    MidiSystem.write(sequence, writers[0], file);
  }
Ejemplo n.º 2
0
  /**
   * Exports to a file.
   *
   * @param outputFileName The output file name
   * @throws InvalidFileFormatException If the file name doesn't end in .mid or .midi
   */
  public void exportToFile(String outputFileName) throws InvalidFileFormatException {
    // Check for a valid file format.
    if (!outputFileName.endsWith(".mid") && !outputFileName.endsWith(".midi")) {
      String msg = "File names must end in .mid or .midi";
      throw new InvalidFileFormatException(msg);
    }

    // Find a supported file type, and export the file.
    int[] types = MidiSystem.getMidiFileTypes(sequence);
    try {
      File outputFile = new File(outputFileName);
      MidiSystem.write(sequence, types[0], outputFile);
    } catch (IOException ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }
Ejemplo n.º 3
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;
   }
 }
  private void process() throws InvalidMidiDataException, IOException, JAXBException {

    Sequence orchestraSequence = MidiSystem.getSequence(inputFile);

    Sequence masterSequence = new Sequence(Sequence.PPQ, resolution);

    //  the orchestra tracks
    for (int i = 0; i < orchestraSequence.getTracks().length; i++) {
      masterSequence =
          TrackMerger.process(
              masterSequence, orchestraSequence, new int[] {i}, -1, null, loggingHandler); //
    }

    // This is a hack....to make the track 0 as long as the whole sequence
    double rawSeqLen = masterSequence.getTickLength();
    double quarterLen = masterSequence.getResolution();
    double barLen = 4 * quarterLen;
    long fullSeqLen = (long) (barLen * (Math.ceil((rawSeqLen + quarterLen) / barLen)));
    masterSequence.getTracks()[0].add(newEndOfTrackMessage(fullSeqLen));

    // add track 5; the metronome track
    masterSequence = MetronomeCreator.process(masterSequence, 0, loggingHandler);

    System.out.println("** Track 0 length " + masterSequence.getTracks()[0].ticks());
    System.out.println("** Sequence length " + masterSequence.getTickLength());

    ChannelCleaner sequenceImporter = new ChannelCleaner(masterSequence, loggingHandler);
    masterSequence = sequenceImporter.getResult();

    // Write the file to disk
    MidiSystem.write(masterSequence, 1, outputMidiFile);
    System.out.println("############ Midi file is: " + outputMidiFile.getCanonicalPath());

    // ----------------------------------------------------------------------------------------
    // create the appropriate song object
    Song songObject = new Song();
    songObject.setName(description);

    MasterTrack mastertrack = songObject.createMastertrack();
    mastertrack.setSequencefile(outputMidiFile.getName());
    mastertrack.setName(sequenceImporter.getTrackName(0));
    mastertrack.setMidiTrackIndex(0);
    mastertrack.setMidiChannel(sequenceImporter.getChannel(0));

    // create a super track that will collect all orchestra tracs
    MidiSynthesizerTrack orchestraSuperTrack = new MidiSynthesizerTrack();
    orchestraSuperTrack.setName("Orchester");
    mastertrack.addSubtrack(orchestraSuperTrack);
    BuiltinSynthesizer OrchestraSynt = new BuiltinSynthesizer();
    OrchestraSynt.setSoundbankfile("../Schickardt.sf2");
    orchestraSuperTrack.setSynthesizer(OrchestraSynt);

    // create a super track that will collect the voices tracs
    MidiSynthesizerTrack voicesSuperTrack = new MidiSynthesizerTrack();
    voicesSuperTrack.setName("Voices");
    mastertrack.addSubtrack(voicesSuperTrack);
    BuiltinSynthesizer voicesSynt = new BuiltinSynthesizer();
    voicesSynt.setSoundbankfile("../StringPiano.sf2");
    voicesSuperTrack.setSynthesizer(voicesSynt);

    // link all the orchestra tracks
    int orchestraBase = 1; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    int orchestraEnd = 2; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    for (int i = orchestraBase; i <= orchestraEnd; i++) {
      MidiTrack songTrack = new MidiTrack();
      songTrack.setName(sequenceImporter.getTrackName(i));
      songTrack.setMidiTrackIndex(i);
      songTrack.setMidiChannel(sequenceImporter.getChannel(i));
      songTrack.setInstrumentDescription(sequenceImporter.getInstrumentDescription(i));
      orchestraSuperTrack.addSubtrack(songTrack);
    }

    MidiTrack newSongTrack;
    int voiceBase = 3;
    // -- Diskant
    newSongTrack = new MidiTrack();
    newSongTrack.setName("Flute 1");
    newSongTrack.setMidiTrackIndex(voiceBase);
    newSongTrack.setMidiChannel(0);
    newSongTrack.setInstrumentDescription("Piano");
    newSongTrack.setMute(true);
    voicesSuperTrack.addSubtrack(newSongTrack);
    // -- Bass
    voiceBase++; // 4
    newSongTrack = new MidiTrack();
    newSongTrack.setName("Flute 2");
    newSongTrack.setMidiTrackIndex(voiceBase);
    newSongTrack.setMidiChannel(1);
    newSongTrack.setInstrumentDescription("Piano");
    newSongTrack.setMute(true);
    voicesSuperTrack.addSubtrack(newSongTrack);

    // -- Metronome
    voiceBase++; // 5
    newSongTrack = new MidiTrack();
    newSongTrack.setName("Metronome");
    newSongTrack.setMidiTrackIndex(voiceBase);
    newSongTrack.setMidiChannel(9);
    newSongTrack.setInstrumentDescription("Metronome");
    newSongTrack.setMute(true);
    voicesSuperTrack.addSubtrack(newSongTrack);

    songObject.marshal(new FileOutputStream(outputSongFile));
    System.out.println("############ Song file is: " + outputSongFile.getCanonicalPath());
  }
Ejemplo n.º 5
0
 /**
  * Scrive il brano in un file midi.
  *
  * @param path il percorso del file.
  * @throws IOException se ci sono errori di I/O.
  */
 public void scrivi(final String path) throws IOException {
   MidiSystem.write(sequence, 1, new File(path));
 }