Esempio n. 1
0
  // ParserListener methods
  public void noteEvent(Note note) {
    if (layer >= staves) {
      return;
    }
    // System.out.println(note.getMusicString() + " " + note.getMillisDuration() + " " +
    // note.getDecimalDuration());
    Vector<Chord> currChords = chords[layer];
    Iterator<NotePanel> currNote = currNotes[layer];

    if (!currNote.hasNext()) {
      System.err.println("Received noteEvent, but no PostScript notes are left");
      return;
    }

    if (note.getMillisDuration() > 0) {
      NotePanel notePanel = currNote.next();
      // time the last chord ended
      long tempTime = 0;
      for (int i = currChords.size() - 1; i >= 0; --i) {
        if (!currChords.get(i).isTie()) {
          tempTime = currChords.get(i).getTime() + currChords.get(i).getDuration();
          break;
        }
      }

      if (notePanel.isTie) {
        Chord chord = new Chord();
        // for each note in the last chord, set the next note as a tied note
        for (int i = 0; i < currChords.lastElement().size(); ++i) {
          notePanel.setTie(true).setTime(Math.min(tempTime, time - 1)).setTempo(tempo);
          chord.addNote(notePanel);
          notePanel = currNote.next();
        }
        currChords.add(chord);
      }

      while (notePanel.isRest) {
        notePanel
            .setTime(Math.min(tempTime, time - 1)) // hack, in case the rest should be trimmed
            .setTempo(tempo);
        tempTime += notePanel.getDuration();
        Chord chord = new Chord(notePanel);
        currChords.add(chord);
        // System.out.println("REST: " + notePanel.getMusicString() + " " +
        // notePanel.getDuration());
        notePanel = currNote.next();
      }

      notePanel.setNote(note).setTime(time).setTempo(tempo);
      if (currChords.isEmpty() || currChords.lastElement().getTime() != time) {
        Chord chord = new Chord(notePanel);
        currChords.add(chord);
      } else {
        currChords.lastElement().addNote(notePanel);
      }
    }
  }
Esempio n. 2
0
  private void parseMidi() {
    try {
      // Allow for alternate file extensions of midi files.
      File midiFile = new File("data/out/" + name + ".midi");
      File altFile = new File("data/out/" + name + ".mid");

      if (!midiFile.exists() && altFile.exists()) {
        midiFile = altFile;
      }

      // parse midi
      Sequence sequence = MidiSystem.getSequence(midiFile);
      resolution = sequence.getResolution();

      for (int i = 0; i < staves; ++i) {
        currNotes[i] = notes[i].iterator();
      }

      MidiParser parser = new MidiParser();
      parser.addParserListener(this);
      parser.parse(sequence);

      // initialize any rests trailing at the end
      for (int i = 0; i < staves; ++i) {
        Vector<Chord> currChords = chords[i];
        Iterator<NotePanel> currNote = currNotes[i];
        long tempTime = 0;
        for (int j = currChords.size() - 1; j >= 0; --j) {
          if (!currChords.get(j).isTie()) {
            tempTime = currChords.get(j).getTime() + currChords.get(j).getDuration();
            break;
          }
        }
        while (currNote.hasNext()) {
          NotePanel notePanel = currNote.next();
          if (notePanel.getNote() != null) {
            notePanel.setTime(tempTime).setTempo(tempo);

            tempTime += notePanel.getDuration();
            Chord chord = new Chord(notePanel);
            currChords.add(chord);
          }
        }
      }

      // attach staff lines
      for (int layer = 0; layer < staves; ++layer) {
        NotePanel prevNote = null;
        for (Chord chord : chords[layer]) {
          for (NotePanel note : chord.notes) {
            double staffLine = 0.0;
            for (double line : staffLines.get(layer).get(note.page - 1)) {
              if (staffLine < 0.0001 || Math.abs(staffLine - note.y) > Math.abs(line - note.y)) {
                staffLine = line;
              }
            }
            if (prevNote != null) {
              if (Math.abs(staffLine - prevNote.staffLine) > 0.001) {
                // either reached the end of the line, or the note is really high/low
                // make a hacky guess if the note is really high/low,
                // and if so, set its staffline equal to the previous note's
                if (prevNote.x < note.x + 1.0) {
                  note.setStaffLine(prevNote.staffLine);
                  prevNote = note;
                  continue;
                }
              }
            }
            note.setStaffLine(staffLine);
            prevNote = note;
          }
        }
      }

    } catch (Exception e) {
      System.err.println("Parsing the score from Lilypond's output has failed. Error: ");
      e.printStackTrace();
    }
  }