package jmsltestsuite; import java.awt.FontFormatException; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import com.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.score.musicglyphrenderers.BravuraMusicGlyphRenderer; /** * JMSL Score lets you add custom music glyph renderers, that might use a real music font for * example. Here we test two such renderers, one that uses Sonata music font for most glyphs, but * gives the priority to another renderer for microtonal accidentals (Accidentals font by Matthew * Hindson, see http://www.hindson.com.au/wordpress/free-fonts-available-for-download/ * * The order you add MusicGlyphRenderers to JMSL determines the priority. The first renderer to * successfully render a glyph returns true and stops the chain. If they all fall through or if none * exist, JMSL uses line drawing routines. * * * * Test SonataMusicGlyphRenderer and AccidentalsMusicGlyphRenderer */ public class BravuraMusicGlyphRendererTest implements MusicGlyphRendererProxyListener { PrintWriter log; public BravuraMusicGlyphRendererTest() { try { log = new PrintWriter(new FileWriter(new File("logs/MusicGlyphProxyOutput.txt")), true); } catch (IOException e) { e.printStackTrace(); } } public void notifyRenderMusicGlyph(String msg) { if (log != null) { log.println(msg); } else { System.out.println("\t\t\t" + msg); } } public static void main(String[] args) { JMSL.setViewFactory(new ViewFactorySwing()); BravuraMusicGlyphRendererTest musicGlyphRendererTest = new BravuraMusicGlyphRendererTest(); boolean USE_LINE_DRAWING = false; boolean USE_PROXY = false; Score score = new Score(3, 1400, 800); if (USE_PROXY && !USE_LINE_DRAWING) { System.out.println("Using proxy renderer"); MusicGlyphRendererProxy proxyRenderer = new MusicGlyphRendererProxy(); proxyRenderer.addMusicGlyphRendererProxyListener(musicGlyphRendererTest); score.addMusicGlyphRenderer(proxyRenderer); } else if (!USE_LINE_DRAWING) { try { MusicGlyphRenderer bravuraMusicGlyphRenderer = new BravuraMusicGlyphRenderer(); // bravuraMusicGlyphRenderer.printAvailableSystemFonts(); if (bravuraMusicGlyphRenderer.isFontFound()) { score.addMusicGlyphRenderer(bravuraMusicGlyphRenderer); } else { // try loading from a otf file File otfDir = new File("SMuFL_OpenType_fonts"); File bravuraFile = new File(otfDir, "Bravura.otf"); File lelandFile = new File(otfDir, "Leland.otf"); File petalumaFile = new File(otfDir, "Petaluma.otf"); File otfFile = bravuraFile; if (otfFile.exists()) { bravuraMusicGlyphRenderer.createBaseFont(otfFile); score.addMusicGlyphRenderer(bravuraMusicGlyphRenderer); score.setName(otfFile.getName()); } else { System.out.println( "Bravura font not found on your system, and cannot locate Bravura.otf . Get it from www.smufl.org"); } } } catch (FontFormatException e1) { System.err.println("FontFormatException, music font cannot be loaded"); e1.printStackTrace(); } catch (IOException e1) { System.err.println("IOException, music font cannot be loaded"); e1.printStackTrace(); } } score.getScoreLayoutManager().setZoom(1); score.addMeasure(3, 4); score.addMeasure(13, 8); score.addMeasure(4, 4); double dur = .25; score.addNote(dur, 60, 0.5, 0.8).setBeamedOut(true); score.addNote(dur, 63 - 12, 0.5, 0.8).setAccPref(Note.ACC_PREFER_FLAT); score.addNote(dur, 66, 0.5, 0.8).setAccPref(Note.ACC_PREFER_FLAT); score.addNote(dur, 67, 0.5, 0.8).setAccPref(Note.ACC_PREFER_FLAT); dur = .5; Note n = score.addNote(dur, 12 + 61, 0.5, 0.8); n.setBeamedOut(true); score.addNote(dur, 12 + 63, 0.5, 0.8); Note noteToAddGraceNoteTo = score.addNote(dur, 24 + 66, 0.5, 0.8); Note graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 81, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); graceNote.addInterval(88); score.addNote(dur, 24 + 68, 0.5, 0.8); dur = 1; score.addNote(dur, 60.5, 0.5, 0.8).setAccPref(Note.ACC_PREFER_FLAT); noteToAddGraceNoteTo = score.addNote(dur, 61.5, 0.5, 0.8); graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 60, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); graceNote.setBeamedOut(true); graceNote.addInterval(78); graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 70, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); graceNote.addInterval(78); noteToAddGraceNoteTo = score.addNote(dur, 60.5, 0.5, 0.8); graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 60, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 70, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); score.addNote(dur, 61.5, 0.5, 0.8).addMark(Note.MARK_TRILL); noteToAddGraceNoteTo = score.addNote(dur, 63.5, 0.5, 0.8); noteToAddGraceNoteTo.addMark(Note.MARK_TRILL); graceNote = noteToAddGraceNoteTo.addGraceNote(0.5, 81, 0.5, 0.4, true, false, false, false, false, Note.MARK_NONE); graceNote.addInterval(88); score.addNote(1.5, 0, 0, 0.4); Note note = score.addNote(0.5, 60, 0.6, 0.4); note.addMark(Note.MARK_ACCIACCATURA); note = score.addNote(0.5, 72, 0.6, 0.4); note.addMark(Note.MARK_ACCIACCATURA); note = score.addNote(0.5, 60, 0.6, 0.4); note.addMark(Note.MARK_ACCIACCATURA); note.setBeamedOut(true); score.addNote(0.5, 72, 0.6, 0.4); note = score.addNote(0.5, 72, 0.6, 0.4); note.addMark(Note.MARK_ACCIACCATURA); note.setBeamedOut(true); score.addNote(0.5, 78, 0.6, 0.4); // SECOND STAFF score.setCurrentMeasureNumber(0); score.setCurrentStaffNumber(1); dur = 0.5; int[] dynamics = { Note.DYNAMIC_PPP, Note.DYNAMIC_PP, Note.DYNAMIC_P, Note.DYNAMIC_MP, Note.DYNAMIC_MF, Note.DYNAMIC_F, Note.DYNAMIC_FF, Note.DYNAMIC_FFF }; for (int i = 0; i < dynamics.length; i++) { score.addNote(dur, 60, 0.5, dur * 0.8).setDynamic(dynamics[i]); // score.addNote(dur, 72, 0.5, dur * 0.8).setDynamic(dynamics[i]); } dur = 2; score.addNote(dur, 12 + 61, 0.5, 0.8).addMark(Note.MARK_TRILL_FLAT); score.addNote(dur, 61, 0.5, 0.8).addMark(Note.MARK_TRILL_SHARP); dur = 4; n = score.addNote(dur, 41, 0.5, 0.8); n.addMark(Note.MARK_TRILL_NATURAL); n.setNoteheadColor(java.awt.Color.green); // System.out.println("CORE DUR OF WHOLE NOTE=" + n.getCoreDuration()); // THIRD STAFF score.setCurrentMeasureNumber(0); score.setCurrentStaffNumber(2); double restDur = 1.0; for (int i = 0; i < 7; i++) { score.addNote(restDur, 0, 0, 0); if (i == 6) { score.addNote(restDur, 0, 0, 0); } restDur /= 2; } for (int i = 1; i < 20; i++) { int mark = i; int[] pitches = { 60, 72 }; for (int pitchIndex = 0; pitchIndex < pitches.length; pitchIndex++) { n = score.addNote(1, pitches[pitchIndex], 0.5, 0.2); n.addMark(mark); n.setText(Note.markToString(i)); } } score.addNote(0.25, 60, 0.5, 0.2); score.addNote(0.25, 72, 0.5, 0.2); score.getMeasure(1).getStaff(0).setClef(Clef.ALTO_CLEF); score.getMeasure(0).getStaff(1).setClef(Clef.TENOR_CLEF); score.getMeasure(1).getStaff(1).setClef(Clef.BASS_CLEF); score.getMeasure(2).getStaff(1).setClef(Clef.PERCUSSION_CLEF); ScoreFrame scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { JMSL.closeMusicDevices(); System.exit(0); } }); scoreFrame.setVisible(true); } }