[jmsl] Insertion index for Track...?

jmsl at music.columbia.edu jmsl at music.columbia.edu
Sat Nov 1 09:09:47 EDT 2008


Hi Peter

When the mouse is clicked with the intention of inserting a note, 
EditManager enumerates through all notes in the current layout and finds 
the closest note.  Notes which are close but not in the currently active 
track are scored as very far away
Something like
if (note.getTrack().getTrackIndex() != score.getCurrentTrackNumber()) {
    distance = Integer.MAX_VALUE;
}
By the end of the enumeration a temporary variable "closestNote" is 
inspected and if its distance from the mouse click is less than some 
threshold ("chord attractor radius"), the new note becomes part of a 
chord that is built on closestNote
Else if the closestNote does not qualify as being close enough, the new 
note gets inserted into staff with the insertion index calculated by:
int insertionIndex = staff.getInsertionIndex(location, 
editStateProvider.getActiveTrackIndex());

Thanks
Nick

jmsl at music.columbia.edu wrote:
> Hi Nick,
>
> How does EditManager choose where in the track to insert to?  I've 
> found methods for finding the nearest note to a clicked point, but 
> that is dependent on spacing, and a sparsely spaced measure would not 
> work well with this.  Is there a convenience method that I'm missing 
> that does this?  (I'm writing my own EditManager)
>
> thanks,
> Peter McCulloch
>
>
> package com.petermcculloch.megalo.scratch;
>
> import java.awt.BorderLayout;
> import java.awt.Point;
> import java.awt.event.MouseEvent;
> import java.awt.event.WindowAdapter;
> import java.awt.event.WindowEvent;
> import java.util.Vector;
>
> import javax.swing.JFrame;
>
> import com.softsynth.jmsl.JMSL;
> import com.softsynth.jmsl.score.EditManager;
> import com.softsynth.jmsl.score.EditStateProvider;
> import com.softsynth.jmsl.score.Note;
> import com.softsynth.jmsl.score.NoteFactory;
> import com.softsynth.jmsl.score.Score;
> import com.softsynth.jmsl.score.ScoreCanvas;
> import com.softsynth.jmsl.score.Staff;
>
> /**
>  * @author peter
>  *
>  */
> public class TestAddInterval extends EditManager {
>
>     Score    score;
>
>     /**
>      * @param score
>      * @param esp
>      */
>     public TestAddInterval(Score score, EditStateProvider esp) {
>         super(score, esp);
>         this.score = score;
>     }
>
>     /*
>      * (non-Javadoc)
>      *
>      * @see
>      * 
> com.softsynth.jmsl.score.EditManager#scoreCanvasClicked(com.softsynth
>      * .jmsl.score.ScoreCanvas, java.awt.Point, 
> java.awt.event.MouseEvent)
>      */
>     @SuppressWarnings("unchecked")
>     @Override
>     public void scoreCanvasClicked(ScoreCanvas canvas, Point p, 
> MouseEvent evt) {
>
>         Staff staff = findSelectedStaff(p);
>         score.setSelectedStaff(staff);
>         Note n = findClosestNote(p, EditManager.CURSOR_MODE);
>
>         if (n != null) {
>             int level = staff.getNearestLevel(p);
>             Note insNote =
>                 
> NoteFactory.makeNote(score.getEditManager().getEditStateProvider().getDuration(), 
>
>                     0, 0, Note.ACC_FLAT, level, 
> n.getScore().getMeasure(n.getMeasureIndex())
>                         .getStaff(n.getStaffIndex()).getClef());
>             n.addInterval(insNote);
>         } else {
>
>             int level = staff.getNearestLevel(p);
>             Vector<Note> v =
>                 
> staff.getTrack(score.getEditManager().getEditStateProvider().getActiveTrackIndex()) 
>
>                     .getChildren();
>             boolean found = false;
>
>             // Is there a prettier way of doing this? It works, but...
>             for (Note iNote : v) {
>                 if (iNote.getDrawingAnchor().x > p.x) {
>                     if (!found) {
>                         System.out.println("CHANGING INSERTION INDEX 
> TO:\t" + iNote.getNoteIndex());
>                         staff.getTrack(
>                             
> score.getEditManager().getEditStateProvider().getActiveTrackIndex())
>                             .setInsertionIndex(iNote.getNoteIndex());
>                         staff.setInsIndexSetByHand(true);
>                         found = true;
>                     }
>                 }
>             }
>             Note insNote =
>                 
> NoteFactory.makeNote(score.getEditManager().getEditStateProvider().getDuration(), 
>
>                     
> score.getEditManager().getEditStateProvider().getTuplet(), score
>                         
> .getEditManager().getEditStateProvider().getDots(), 
> score.getEditManager()
>                         .getEditStateProvider().getAccidental(), 
> level, staff.getClef());
>             
> staff.getTrack(score.getEditManager().getEditStateProvider().getActiveTrackIndex()) 
>
>                 .insertNote(insNote);
>
>         }
>         score.render();
>
>         // super.scoreCanvasClicked(canvas, p, evt);
>     }
>
>     public static void main(String[] args) {
>         // JMSL.setViewFactory(new ViewFactorySwing());
>
>         Score score = new Score(2);
>         score.addMeasures(10);
>
>         TestAddInterval manager =
>             new TestAddInterval(score, 
> score.getEditManager().getEditStateProvider());
>         score.getScoreCanvas().removeAllScoreCanvasListeners();
>         score.getScoreCanvas().addScoreCanvasListener(manager);
>
>         JFrame frame = new JFrame();
>         frame.setLayout(new BorderLayout());
>
>         frame.add(score.getScoreCanvas().getComponent());
>
>         frame.addWindowStateListener(new WindowAdapter() {
>             @Override
>             public void windowClosed(WindowEvent e) {
>                 JMSL.closeMusicDevices();
>                 System.exit(0);
>             }
>         });
>
>         frame.pack();
>
>         frame.setVisible(true);
>
>     }
>
> }
>
> _______________________________________________
> jmsl mailing list
> jmsl at music.columbia.edu
> http://music.columbia.edu/mailman/listinfo/jmsl


More information about the jmsl mailing list