/* * Created on Nov 11, 2012 by Nick * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import com.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.Composable; import com.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.MusicShape; import com.softsynth.jmsl.Playable; import com.softsynth.jmsl.SequentialCollection; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; public class JSyn2Song extends JFrame { JMSLMixerContainer mixer; Instrument instrument; SequentialCollection mySong; public void start() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildSong(); launchSong(); } private void initJMSL() { JMSL.setViewFactory(new ViewFactorySwing()); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } private void initMusicDevices() { JSynMusicDevice.instance().open(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { // instrument = new JSynUnitVoiceInstrument(8, jmslexamples.simple.jsyn2.SimpleUnitVoice.class.getName()); instrument = new JSynUnitVoiceInstrument(8, com.softsynth.jmsl.jsyn2.unitvoices.AutoPanningSawtoothBL.class.getName()); mixer.addInstrument(instrument); if (instrument.getNumOutputs() == 2) { mixer.panAmpChange(0, 0, .6); mixer.panAmpChange(1, 1, .6); } add(mixer.getPanAmpControlPanel()); } 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); rest.addStartPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { System.out.println("Just resting for a moment"); return time; } }); rest.addStopPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { System.out.println("Ok back to the tune"); return time; } }); mySong = new SequentialCollection(); mySong.add(verse); mySong.add(chorus); mySong.add(verse); mySong.add(chorus); mySong.add(rest); mySong.add(chorus); mySong.setRepeats(4); mySong.setRepeatPause(2); mySong.addRepeatPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { MusicShape s1 = (MusicShape) ((SequentialCollection) parent).getChild(0); s1.transpose(JMSLRandom.choose(1, 4), 0, s1.size() - 1, 1); MusicShape s2 = (MusicShape) ((SequentialCollection) parent).getChild(1); s2.transpose(-JMSLRandom.choose(1, 4), 0, s2.size() - 1, 1); return time; } }); } 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) { JSyn2Song demo = new JSyn2Song(); demo.setSize(500, 250); demo.setVisible(true); demo.start(); demo.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { demo.stop(); System.exit(0); } }); } }