/* * Created on Sep 19, 2006 by Nick * */ package jmsltestsuite; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.JFrame; import com.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.*; import com.softsynth.jmsl.score.*; import com.sun.image.codec.jpeg.*; /** * Draw on top of a rendered score repeatedly, save each resulting image as jpeg image sequence * * Using new callback via scorecanvaslistener to add drawing on top of score. Nick Didkovsky 9/20/06 * * @author jason, Nick Didkovsky * */ public class DrawOnScore2 extends JFrame { public static final int WIDTH = 320; public static final int HEIGHT = 240; private Score[] score; public DrawOnScore2() { super("ScorePainterRenderTester2"); // initialize GUI Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(2, 2)); initializeScore(); for (int i = 0; i < score.length; i++) { getContentPane().add(score[i].getScoreCanvas().getComponent()); } setSize(WIDTH * 2, HEIGHT * 2); pack(); MusicJob scoreGeneratingMusicJob = new ScoreGenMusicJob() { public double repeat(double time) throws InterruptedException { addMeasureWithNotes(); renderScore(time); return (time + 2.00); } }; for (int i = 0; i < score.length; i++) { score[i].getScoreCanvas().addScoreCanvasListener((ScoreCanvasListener) scoreGeneratingMusicJob); } scoreGeneratingMusicJob.setRepeats(Integer.MAX_VALUE); scoreGeneratingMusicJob.launch(JMSL.now()); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } private void initializeScore() { // every score gets its own canvas Score.useSharedCanvas(false); synchronized (JMSL.class) { JMSL.clock = new DefaultMusicClock(); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } score = new Score[4]; for (int i = 0; i < 4; i++) { score[i] = new Score(1, WIDTH, HEIGHT); } } // add some random notes to the score so there's something to render private void addMeasureWithNotes() { for (int inst = 0; inst < 4; inst++) { Measure m = score[inst].addMeasure(4, 4); score[inst].setCurrentMeasureNumber(m.getMeasureIndex()); for (int i = 0; i < 16; i++) { double pitch = 60 + (m.getMeasureIndex() * i) % 24; if (i == 0) pitch += JMSLRandom.choose(12); Note note = score[inst].addNote(0.25, pitch, 64, 0.25); note.setBeamedOut(true); } } } private void renderScore(double time) { for (int inst = 0; inst < 4; inst++) { score[inst].render(score[inst].size() - 1, time, true); } } public static void encodeJpeg(BufferedImage bi, float quality, OutputStream out) throws IOException { JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi); param.setQuality(quality, false); encoder.setJPEGEncodeParam(param); encoder.encode(bi); } public static void main(String[] args) { JMSL.setViewFactory(new ViewFactorySwing()); new DrawOnScore2(); } } /** * This MusicJob will get a callback from ScoreCanvas just before the offscreen displayed score * image is about to be displayed to the Graphics context. This gives you a chance to add graphics * on top. Here we also save as a jpeg. */ class ScoreGenMusicJob extends MusicJob implements ScoreCanvasListener { public void scoreCanvasClicked(ScoreCanvas canvas, Point p, MouseEvent e) { } public void scoreCanvasRectangleSelected(ScoreCanvas canvas, Point p1, Point p2) { } public void scoreCanvasReady(ScoreCanvas canvas) { } public void scoreCanvasKeyTyped(ScoreCanvas canvas, KeyEvent keyEvent) { } public void scoreCanvasKeyReleased(ScoreCanvas canvas, KeyEvent keyEvent) { } /** Do some drawing over a rendered score page. Save as a JPEG */ public void scoreCanvasDisplayedImageReady(Score score, ScoreCanvas canvas, Image displayedImage) { Graphics g = displayedImage.getGraphics(); g.drawString("Superimposed, " + score.getName() + ", repeat count=" + getRepeatCount(), 30, 200); saveJpeg(score, displayedImage); } /** * @param displayedImage */ private void saveJpeg(Score score, Image displayedImage) { String jpegFileName = score.getName() + "_img-" + getRepeatCount() + ".jpg"; try { FileOutputStream out = new FileOutputStream(jpegFileName); DrawOnScore2.encodeJpeg((BufferedImage) displayedImage, 0.7f, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 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 } }