/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import java.net.URL; import javax.swing.JFrame; import javax.swing.JLabel; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.*; /** * Use a MusicJob to play ten MIDI notes with random pitch, duration, vel, and * hold time * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * nick@didkovsky.com * */ public class TenRandomMidiNotes { // change this if you have a preferred MIDI sound font! Otherwise leave it as is // and it will // fail and fall through to default String LOCATION_OF_SOUNDFONT = "F:/jwork/SoundFonts/FluidR3/FluidR3 GM2-2.SF2"; JMSLMixerContainer mixer; Instrument midiInstrument; MusicJob randomMusicJob; public void go() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildRandomMusicJob(); launchRandomMusicJob(); } 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 buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { midiInstrument = new MidiInstrument(1); mixer.addInstrument(midiInstrument); } void buildRandomMusicJob() { randomMusicJob = new MusicJob() { public double repeat(double playTime) { double randomDuration = JMSLRandom.choose(0.1, 5.0); double randomPitch = JMSLRandom.choose(60, 72); double randomVel = JMSLRandom.choose(60, 90); double randomHold = JMSLRandom.choose(0.2, 4.0); double[] data = { randomDuration, randomPitch, randomVel, randomHold }; // FYI System.out.print((getRepeatCount() + 1) + ") "); JMSL.printDoubleArray(data); return getInstrument().play(playTime, 1, data); } }; randomMusicJob.setRepeats(10); randomMusicJob.setInstrument(midiInstrument); } private void launchRandomMusicJob() { randomMusicJob.launch(JMSL.now()); } public void cleanup() { randomMusicJob.finishAll(); try { randomMusicJob.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { final TenRandomMidiNotes tenRandomMIDINotes = new TenRandomMidiNotes(); tenRandomMIDINotes.go(); JFrame jf = new JFrame("10 Random MIDI notes using JavaSound MIDI"); jf.add(tenRandomMIDINotes.mixer.getPanAmpControlPanel()); jf.pack(); jf.setVisible(true); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { tenRandomMIDINotes.cleanup(); System.exit(0); } }); } }