/* * 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.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.*; import com.softsynth.jmsl.view.MidiInstrumentEditDialog; /** * Just play a MusicShape with a Midi instrument. * * This demo opens a Midi Device Editor. It also opens Midi Instrument Editor which you can play * with while the MusicShape is performing; * * 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 MIDISimple extends JFrame { // 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; MidiInstrument instrument; MusicShape musicShape; public void go() { initJavaSoundMidi(); buildMixer(); buildInstrument(); buildSong(); launchSong(); openMidiInstrumentEditor(); } private void openMidiInstrumentEditor() { MidiInstrumentEditDialog d = new MidiInstrumentEditDialog(instrument); d.setVisible(true); } 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(); } /** create a Midi instrument that sends on channel 1 and uses program change */ private void buildInstrument() { instrument = new MidiInstrument(1); // midi channel 1 instrument.setProgram(10); // midi program change mixer.addInstrument(instrument); } void buildSong() { musicShape = new MusicShape(instrument.getDimensionNameSpace()); musicShape.setInstrument(instrument); musicShape.add(0.5, 60, 64, 1.5); musicShape.add(0.5, 62, 64, 1.5); musicShape.add(0.5, 64, 64, 1.5); musicShape.add(0.5, 66, 64, 1.5); musicShape.add(0.5, 68, 64, 1.5); musicShape.setRepeats(1000); } private void launchSong() { musicShape.launch(JMSL.now()); } public static void main(String[] args) { JMSL.setViewFactory(new ViewFactorySwing()); MIDISimple midiSimple = new MIDISimple(); midiSimple.setTitle("Play MIDI with a MusicShape using JavaSound MIDI Gervill and a SoundFont"); midiSimple.setSize(400, 200); midiSimple.setVisible(true); midiSimple.add(new JLabel("You can edit the Midi instrument while MusicShape is playing")); midiSimple.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); midiSimple.go(); } }