/* * Created on Jun 28, 2009 by Nick * */ package jmsltestsuite; import java.awt.Color; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Enumeration; import java.util.Vector; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.score.transforms.TransposeGraceNotesTransform; import com.softsynth.jmsl.score.util.GraceNoteMaxRollbackCalculator; public class GraceNoteTest { private void addNoteToSelectionBuffer(Score score, Note note) { if (!score.getSelectionBuffer().contains(note)) { score.getSelectionBuffer().addElement(note); } } public Note getNote(Score score, int measureIndex, int staffIndex, int trackIndex, int noteIndex, int intervalIndex, int graceNoteIndex, int graceNoteIntervalIndex) { Note note = null; if (measureIndex < score.size() && measureIndex >= 0) { Measure m = score.getMeasure(measureIndex); if (staffIndex < m.size() && staffIndex >= 0) { Staff s = m.getStaff(staffIndex); if (trackIndex < s.size() && trackIndex >= 0) { Track t = s.getTrack(trackIndex); if (noteIndex < t.size() && noteIndex >= 0) { note = t.getNote(noteIndex); if (intervalIndex != -1) { if (intervalIndex < note.getIntervals().size()) { note = (Note) note.getIntervals().elementAt(intervalIndex); } else { note = null; } } if (note != null && graceNoteIndex != -1) { if (graceNoteIndex < note.getGraceNotes().size()) { note = (Note) note.getGraceNotes().elementAt(graceNoteIndex); } else { note = null; } if (note != null && graceNoteIntervalIndex != -1) { if (graceNoteIntervalIndex < note.getIntervals().size()) { note = (Note) note.getIntervals().elementAt(graceNoteIntervalIndex); } else { note = null; } } } } } } } return note; } public static void main(String[] args) { System.out.println("JMSL version=" + JMSL.JMSL_VERSION); int w = 1300; int h = 600; int numStaves = 1; Score score = new Score(numStaves, w, h); Measure measure = score.addMeasure(); measure.setTempo(30); Note note0 = score.addNote(1, 60, 0.5, 0.8); Note graceNote = note0.addGraceNote(0.5, 60, 0.5, 0.4, false, false, false, false, true, Note.MARK_NONE); graceNote.addInterval(72); graceNote = note0.addGraceNote(0.5, 60, 0.5, 0.4, false, false, false, false, true, Note.MARK_NONE); note0.setGraceNoteSeparationScaler(1); note0 = score.addNote(1, 60, 0.5, 0.8); graceNote = note0.addGraceNote(0.5, 60, 0.5, 0.4, false, false, false, false, true, Note.MARK_NONE); graceNote.addInterval(72); graceNote = note0.addGraceNote(0.5, 60, 0.5, 0.4, false, false, false, false, true, Note.MARK_NONE); note0.setGraceNoteSeparationScaler(2); graceNote.setSelected(true); new GraceNoteTest().addNoteToSelectionBuffer(score, graceNote); Vector graceNotes = note0.getGraceNotes(); for (int i = 0; i < graceNotes.size(); i++) { Note gn = (Note) graceNotes.elementAt(i); System.out.println(i + ") " + gn.getPitchData() + ", # intervals: " + gn.getIntervals().size()); } Note n = new GraceNoteTest().getNote(score, 0, 0, 0, 1, -1, 0, 0); if (n != null) { System.out.println("Looked up note " + n.getPitchData() + ", isInterval? " + n.isInterval() + ", isGracenote?" + n.isGraceNote()); n.setNoteheadColor(Color.red); } else { System.out.println("no such note"); } /* * Note note1 = score.addNote(1, 69, 0.5, 0.8); // note1.setTiedOut(true); // * score.addNote(1, 72, 0.5, 0.8); // * * note1.addGraceNote(0.5, 70, 0.5, 0.4, false, true, true, false, false, Note.MARK_ACCENT); * Note gn = note1.addGraceNote(0.5, 58, 0.5, 0.4, false, false, false, false, false, * Note.MARK_NONE); * * * score.getSelectionBuffer().deselectAndClear(); gn.setSelected(true); * * score.getSelectionBuffer().addElement(gn); * * Note note2 = score.addNote(1, 58, 0.5, 0.8); */ ScoreFrame scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); NotePropertiesTransform t1 = new NotePropertiesTransform() { public void operate(Score score, SelectionBuffer selectionBuffer) { Score.copyBuffer.removeAllElements(); for (Enumeration e = selectionBuffer.elements(); e.hasMoreElements();) { Note note = (Note) e.nextElement(); for (int i = 0; i < note.getGraceNotes().size(); i++) { Note graceNote = (Note) note.getGraceNotes().elementAt(i); graceNote.setIsGraceNote(false); note.getTrack().insertNote(graceNote, note.getTrack().indexOf(note)); } note.removeAllGraceNotes(); } score.setDirty(true, getName()); } }; t1.setName("Detach Grace Notes"); scoreFrame.addNotePropertiesTransform(t1); NotePropertiesTransform t2 = new NotePropertiesTransform() { public void operate(Score score, SelectionBuffer selectionBuffer) { Note lastNote = null; for (Enumeration e = selectionBuffer.elements(); e.hasMoreElements();) { lastNote = (Note) e.nextElement(); } Note noteToWhichGraceNotesWillBeAttached = lastNote.nextNote(); if (noteToWhichGraceNotesWillBeAttached != null) for (Enumeration e = selectionBuffer.elements(); e.hasMoreElements();) { Note graceNote = (Note) e.nextElement(); noteToWhichGraceNotesWillBeAttached.addGraceNote(graceNote); graceNote.setIsGraceNote(true); graceNote.getTrack().deleteNote(graceNote); } score.setDirty(true, getName()); } }; t2.setName("Make Grace Notes"); scoreFrame.addNotePropertiesTransform(t2); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(1, true)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(-1, true)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(12, true)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(-12, true)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(1, false)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(-1, false)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(12, false)); scoreFrame.addNotePropertiesTransform(new TransposeGraceNotesTransform(-12, false)); scoreFrame.setVisible(true); GraceNoteMaxRollbackCalculator graceNoteMaxRollbackCalculator = new GraceNoteMaxRollbackCalculator(); graceNoteMaxRollbackCalculator.calcMaxGraceNoteRollback(score); double maxRollback = graceNoteMaxRollbackCalculator.getMaxRollback(); System.out.println("max grace note rollback for this score=" + maxRollback); } /** * @param note */ private static Note createGraceNote(Note note, double pitch) { Note graceNote = NoteFactory.makeNote(0.5, pitch, 0.5, 0.8); graceNote.setTrack(note.getTrack()); NoteFactory.updateFromPitch(graceNote); note.addGraceNote(graceNote); graceNote.setIsGraceNote(true); // graceNote.setTiedOut(true); return graceNote; } }