/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import java.io.File; import java.io.IOException; import com.softsynth.jmsl.DefaultMusicClock; import com.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.midi.MidiIOFactory; import com.softsynth.jmsl.midi.MidiIO_JavaSound; import com.softsynth.jmsl.score.Measure; import com.softsynth.jmsl.score.Note; import com.softsynth.jmsl.score.Orchestra; import com.softsynth.jmsl.score.Score; import com.softsynth.jmsl.score.ScoreFrame; import com.softsynth.jmsl.score.midi.MidiScoreInstrument; /** * Build a simple Score with a JavaSound Midi orchestra * * Also demonstrates JMSL's support for high quality SoundFonts (sf2) files. You can download the * FluidR3 SoundFont at http://www.algomusic.com/SoundFonts/ * * If the Java installation that is running this applet does not have a JavaSound MIDI soundbank, * your classes folder (ie your applet's codebase) must contain a folder called javasound containing * a soundbank called soundbank-min.gm (or call subfolder and the soundbank whatever you want and * change the name in the initJavaSoundMidi() method below * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: nick@didkovsky.com * */ public class MIDIScore { // change this if you have a preferred MIDI sound font! Otherwise leave it // as is and it will // fail and fall through to default /** * FluidR3 SoundFont is default, see https://sourceforge.net/p/fluidsynth/wiki/SoundFont/ */ String LOCATION_OF_SOUNDFONT = "F:/jwork/SoundFonts/FluidR3/FluidR3 GM2-2.SF2"; Score score; ScoreFrame scoreFrame; Orchestra orchestra; public void go() { initJMSL(); initMusicDevices(); buildScore(); buildOrchestra(); buildScoreFrame(); } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } private void initMusicDevices() { JMSL.clock = new DefaultMusicClock(); initJavaSoundMidi(); } private void initJavaSoundMidi() { try { JMSL.midi = new MidiIOFactory().getMidiIO(MidiIOFactory.MidiIO_JAVASOUND); // Gervill only available after Java 1.7 String javaSpecStr = System.getProperty("java.specification.version"); double javaSpecVersion = javaSpecStr != null ? Double.parseDouble(javaSpecStr) : 0; String requestedDeviceName = javaSpecVersion < 1.7 ? "Java Sound Synthesizer" : "Gervill"; // you could change this to a soundbank.gm location if pre-Gervill, or just leave it and // JavaSound will ignore ((MidiIO_JavaSound) JMSL.midi).setSoundbankFile(new File(LOCATION_OF_SOUNDFONT)); JMSL.midi.setOutDevice(requestedDeviceName); JMSL.midi.open(); } catch (IOException e2) { e2.printStackTrace(); } } private void buildOrchestra() { orchestra = new Orchestra(); // use MidiScoreInstrument, a subclass of MidiInstrument which uses // amplitude 0..1 for // velocity, for compatibility with JSyn instruments in the same score Instrument ins = new MidiScoreInstrument(1); // midi ch 1 orchestra.addInstrument(ins); score.setOrchestra(orchestra); } void buildMeasure() { Measure m = score.addMeasure(5, 4); m.setTempo(120); // duration (where 1.0 = qtr), pitch (where 60 = middle C), amp, sustain for (int i = 0; i < 2; i++) { score.addNote(1.0, 60, 0.6, 0.8); score.addNote(1.0, 62, 0.6, 0.8); Note n = score.addNote(0.50, 63, 0.6, 0.8); n.setBeamedOut(true); score.addNote(0.50, 66, 0.6, 0.8); score.addNote(2.0, 72, 0.6, 0.8); } } void buildScore() { score = new Score(1, 800, 600, "JMSL Score using MIDI"); buildMeasure(); } void buildScoreFrame() { scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.setVisible(true); } public static void main(String[] args) { final MIDIScore midiScore = new MIDIScore(); midiScore.go(); } }