/* * 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 javax.swing.JFrame; import javax.swing.JLabel; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.*; /** * Use a SequentialCollection to ABAB "song form" with Midi piano * * SoundFont support! As of Java 1.7 uses Gervill JavaSound. This demo uses FluidR3 soundfont. If * you don't have it, it will use Java's default sounds. You can download the FluidR3 SoundFont at * http://www.algomusic.com/SoundFonts/ * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: nick@didkovsky.com * */ public class MIDISong { // change this if you have a preferred MIDI sound font! // If this file is not found, MidiIO_JavaSound will try to load a default soundbank. // You can download the FluidR3 SoundFont at http://www.algomusic.com/SoundFonts/ String LOCATION_OF_SOUNDFONT = "F:/jwork/SoundFonts/FluidR3/FluidR3 GM2-2.SF2"; JMSLMixerContainer mixer; Instrument instrument; SequentialCollection mySong; public void go() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildSong(); launchSong(); } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.5); } 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() { instrument = new MidiInstrument(1); mixer.addInstrument(instrument); } void buildSong() { MusicShape verse = new MusicShape(4); verse.setInstrument(instrument); verse.add(0.25, 60, 80, 0.5); verse.add(0.25, 62, 80, 0.25); verse.add(0.75, 65, 80, 0.2); verse.setRepeats(4); MusicShape chorus = new MusicShape(4); chorus.setInstrument(instrument); chorus.add(0, 50, 90, 0.75); chorus.add(1.0, 59, 90, 0.75); chorus.add(0, 53, 90, 0.75); chorus.add(1.0, 57, 90, 0.75); chorus.setRepeats(2); mySong = new SequentialCollection(); verse.addStartPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { System.out.println("Sending control change to turn sustain pedal ON in the verse"); JMSL.midi.control(time, 1, 64, 70); return time; } }); chorus.addStartPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { System.out.println("Sending control change to turn sustain pedal OFF in the chorus"); JMSL.midi.control(time, 1, 64, 40); return time; } }); mySong.add(verse); mySong.add(chorus); mySong.add(verse); mySong.add(chorus); } private void launchSong() { mySong.launch(JMSL.now() + 3); } public void cleanup() { mySong.finishAll(); try { mySong.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { final MIDISong midiSong = new MIDISong(); midiSong.go(); JFrame jf = new JFrame("MIDI Song using JavaSound MIDI"); jf.setSize(400, 200); jf.setVisible(true); jf.add(new JLabel("There is no GUI for this demo")); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { midiSong.cleanup(); System.exit(0); } }); } }