/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import com.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.MusicShape; import com.softsynth.jmsl.SequentialCollection; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; /** * Use a SequentialCollection to play ABABB "song form" with a JSyn UnitVoice * instrument * * Upgraded to JSyn2 API Dec 6, 2016 * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * nick@didkovsky.com * */ public class JSynSong extends JFrame { JMSLMixerContainer mixer; Instrument instrument; SequentialCollection mySong; public void start() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildSong(); launchSong(); } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.2); } private void initMusicDevices() { JSynMusicDevice.instance().open(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { String unitVoiceClassName = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.class.getName(); int unitVoicePreset = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.PRESET_BRASS; int polyphony = 8; instrument = new JSynUnitVoiceInstrument(polyphony, unitVoiceClassName, unitVoicePreset); mixer.addInstrument(instrument); } protected void buildSong() { MusicShape verse = new MusicShape(4); verse.setInstrument(instrument); verse.add(0.25, 60, 0.6, 0.5); verse.add(0.25, 60.5, 0.6, 0.25); verse.add(0.75, 65, 0.6, 0.2); verse.setRepeats(4); MusicShape chorus = new MusicShape(4); chorus.setInstrument(instrument); chorus.add(0, 50, 0.5, 0.75); chorus.add(1.0, 59, 0.5, 0.75); chorus.add(0, 53, 0.5, 0.75); chorus.add(1.0, 57, 0.5, 0.75); chorus.setRepeats(2); MusicShape rest = new MusicShape(4); rest.add(2.0, 0, 0, 0); mySong = new SequentialCollection(); mySong.add(verse); mySong.add(chorus); mySong.add(verse); mySong.add(chorus); mySong.add(rest); mySong.add(chorus); } private void launchSong() { mySong.launch(JMSL.now()); } public void stop() { mySong.finishAll(); try { mySong.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { JSynSong demo = new JSynSong(); demo.add(new JLabel("There is no GUI for this example", JLabel.CENTER)); demo.setSize(800, 200); demo.setVisible(true); demo.start(); demo.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { demo.stop(); System.exit(0); } }); } }