/* * Created on Sep 3, 2006 by Nick * */ package jmslexamples; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.geom.Line2D; import com.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.score.Measure; 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.ScoreCanvasListener; import com.softsynth.jmsl.score.ScoreFrame; /** * * Use stemless notes to generate a demo of proportional notation. Use a series of 64th notes and * make some of them hidden rests. Make all notes stemless. * * Draw on top of a score using callback to create a light gray grid * * @author Nick Didkovsky, (c) 2009 Nick Didkovsky, nick@didkovsky.com * */ public class ProportionalNotation implements ScoreCanvasListener { int width = 800; int height = 800; Score score; ScoreFrame scoreFrame; public static void main(String[] args) { ProportionalNotation propNotation = new ProportionalNotation(); propNotation.start(); } public void start() { JMSL.setViewFactory(new ViewFactorySwing()); JMSLRandom.randomize(); Score.useSharedCanvas(false); createScore(); scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.setVisible(true); } void createScore() { // setTitle("Proportional Notation in JMSL"); score = new Score(1, width, height, "Proportional Notation Example"); score.getScoreCanvas().addScoreCanvasListener(this); Measure m = score.addMeasure(4, 4); score.setClefsVisible(false); score.setMeasureNumbersVisible(false); score.setInstrumentNamesVisible(false); score.setTempoVisible(false); score.setTimeSignaturesVisible(false); score.setStaffNumbersVisible(false); addStemlessNotes(8); // # measures // getContentPane(). // add(score.getScoreCanvas().getComponent()); } int[] intervals = { 0, 2, 2, 1, 2, 2, 2, 1 }; int accum(int index) { int accum = 0; for (int i = 0; i < index; i++) { accum += intervals[i]; } return accum; } void addStemlessNotes(int numMeasures) { for (int m = 0; m < numMeasures; m++) { int numNotes = 16 * 4; for (int i = 0; i < numNotes; i++) { double duration = 1 / 16.0; int intervalIndex = JMSLRandom.choose(intervals.length); int interval = accum(intervalIndex); double pitch = interval + NoteFactory.MIDDLE_C; if (JMSLRandom.choose() < 0.5) { pitch += 12; } // System.out.println("interval=" + interval + ", intervalIndex=" + intervalIndex + // ", pitch=" + pitch); double amp = JMSLRandom.choose(); double hold = duration * 0.8; Note note = score.addNote(duration, pitch, amp, hold); note.setBeamedOut(false); note.setStemVisible(false); boolean invisibleRest = JMSLRandom.choose() < 0.75; if (invisibleRest ) { note.setPitchData(0); // make it a rest note.setVisible(false); } } // we want proportional notation to start where t=0 is the graphic beginning of measure, without any layout margin score.getMeasure(m).setMeasureLeftMargin(0); } } public void scoreCanvasClicked(ScoreCanvas canvas, Point p, MouseEvent e) { // TODO Auto-generated method stub } public void scoreCanvasRectangleSelected(ScoreCanvas canvas, Point p1, Point p2) { System.out.println("score rectangle selected " + p1 + ", " + p2); } public void scoreCanvasKeyTyped(ScoreCanvas canvas, KeyEvent keyEvent) { // TODO Auto-generated method stub } public void scoreCanvasKeyReleased(ScoreCanvas canvas, KeyEvent keyEvent) { // TODO Auto-generated method stub } /** * Called immediately before ScoreCanvas draws the completed offscreen display image to the * canvas's graphics context. You may add drawing on top of the score by drawing in this method */ public void scoreCanvasDisplayedImageReady(Score score, ScoreCanvas canvas, Image displayedImage) { Color c = new Color(200, 200, 200); Graphics gr = displayedImage.getGraphics(); gr.setColor(c); int scoreWidth = score.getLayoutWidth() - (int) score.getMeasure(0).getDrawingAnchor().getX(); int xInterval = scoreWidth / 10; double xMargin = score.getMeasure(0).getDrawingAnchor().getX(); int y = score.getLayoutHeight(); for (int i = 0; i < 10; i++) { double x = i * xInterval + xMargin; drawDashedLine(gr, x, 30, x, y - 10); ((Graphics2D) gr).drawString(i + " sec", (float) x, (float) 15); } } void drawDashedLine(Graphics g, double x1, double y1, double x2, double y2) { int dashLength = 10; for (int i = 0; i < (y2 - y1) / (dashLength * 2); i++) { double yStart = i * dashLength * 2 + y1; ((Graphics2D) g).draw(new Line2D.Double(x1, yStart, x2, yStart + dashLength)); } } public void scoreCanvasReady(ScoreCanvas canvas) { } // public static void main(String[] args) { // JMSL.setViewFactory(new ViewFactorySwing()); // JMSLRandom.randomize(); // // Score.useSharedCanvas(false); // ProportionalNotation propNotation = new ProportionalNotation(); // // propNotation.createScore(); // propNotation.pack(); // propNotation.setVisible(true); // // propNotation.addWindowListener(new WindowAdapter() { // public void windowClosing(WindowEvent e) { // System.exit(0); // } // }); // } public void scoreCanvasDragged(ScoreCanvas canvas, Point p, MouseEvent e) { // TODO Auto-generated method stub } public void scoreCanvasReleased(ScoreCanvas canvas, Point p, MouseEvent e) { // TODO Auto-generated method stub } public void scoreCanvasPressed(ScoreCanvas canvas, Point p, MouseEvent e) { // TODO Auto-generated method stub } public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }