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(); } }