/** * Try a JMSL Score applet with JavaSound MIDI. Only works if soundbank.gm * installed properly and if browser is using Sun Java plugin v1.2 or greater. * * The path to the soundbank should be (jre/)lib/audio/soundbank.gm. You may * have to make the "audio" directory manually and install the soundbank * manually. You can download a soundbank of your choice at * http://java.sun.com/products/java-media/sound/soundbanks.html More * instructions are there. * * @author Nick Didkovsky, didkovn@mail.rockefeller.edu, (C) 2003 Nick * Didkovsky, All Rights Reserved * */ package jmsltutorial; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.MidiIO_JavaSound; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.score.midi.MidiScoreInstrument; public class JScoreTootJavaSoundMIDI extends java.applet.Applet { Score score; ScoreFrame scoreFrame; Orchestra orch; Button toFrontButton; public void init() { JMSL.setIsApplet(true); setBackground(Color.yellow); add(new Label("Your JMSL score will open in a new window")); } /* * When applet starts up, init JavaSound MIDI, build an orchestra, add notes * to a score, display it */ public void start() { synchronized (JMSL.class) { buildCleanClock(); buildCleanScheduler(); initJavaSoundMIDI(); // 3 staves, width, height score = new Score(3, 800, 400); score.addMeasure(4, 4); score.setOrchestra(new Orchestra()); scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.addInstrument(new MidiScoreInstrument(1)); scoreFrame.addInstrument(new MidiScoreInstrument(2)); scoreFrame.addInstrument(new MidiScoreInstrument(3)); // score.getOrchestra().getJMSLMixerContainer().panAmpChange(0, 0, // 0.75); // score.getOrchestra().getJMSLMixerContainer().panAmpChange(1, 0.5, // 0.75); // score.getOrchestra().getJMSLMixerContainer().panAmpChange(2, 1.0, // 0.75); generateNotes(); setupShowScore(); add(toFrontButton = new Button("Score to Front")); toFrontButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setupShowScore(); } }); } } private void setupShowScore() { scoreFrame.setSize(900, 600); scoreFrame.setVisible(true); score.render(); scoreFrame.toFront(); } private void buildCleanClock() { JMSL.clock = new DefaultMusicClock(); JMSL.clock.setAdvance(0.1); } /** * Create a new EventScheduler each time this applet starts, so old threads * don't hang around */ private void buildCleanScheduler() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); } private void generateNotes() { for (int i = 0; i < 10; i++) { Note note = score.addNote(4.0 / 5.0, 60 + i * 2, 0.5, 0.8); note.setBeamedOut(true); } score.rewind(); score.setCurrentStaffNumber(1); for (int i = 0; i < 18; i++) { Note note = score.addNote(4.0 / 9.0, 48 + i * 2, 0.5, 0.8); note.setBeamedOut(true); } score.rewind(); score.setCurrentStaffNumber(2); for (int i = 0; i < 14; i++) { Note note = score.addNote(4.0 / 7.0, 54 + i * 2, 0.5, 0.8); note.setBeamedOut(true); } } /** Set JMSL.midi to the JavaSound Midi MusicDevice singleton */ private void initJavaSoundMIDI() { JMSL.midi = MidiIO_JavaSound.instance(); try { ((MidiIO_JavaSound) JMSL.midi).setSoundbankURL(new URL(getCodeBase(), "javasound/soundbank-min.gm")); } catch (MalformedURLException e) { e.printStackTrace(); } JMSL.midi.edit(new Frame()); JMSL.midi.open(); } /* When applet stops, free resources */ public void stop() { synchronized (JMSL.class) { if (scoreFrame != null) { scoreFrame.setVisible(false); scoreFrame.dispose(); Score.deleteCanvas(); SelectionBuffer.disposeEditFrame(); scoreFrame = null; score = null; } removeAll(); JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } }