/* * Created by Nick on Jan 4, 2005 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import com.jsyn.swing.JAppletFrame; 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.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; /** * * Just play one pitched sound once. * * Upgraded to JSyn2 API Dec 6, 2016 by ND * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * nick@didkovsky.com * */ public class OneSoundOnce extends JFrame { JMSLMixerContainer mixer; Instrument instrument; MusicShape musicShape; public void start() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildMusicShape(); launchMusicShape(); } private void initJMSL() { 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() { String unitVoiceClassName = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.class.getName(); int unitVoicePreset = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.PRESET_CLARINET; int polyphony = 4; instrument = new JSynUnitVoiceInstrument(polyphony, unitVoiceClassName, unitVoicePreset); mixer.addInstrument(instrument); } private void buildMusicShape() { // 4 dimensional MusicShape: duration, pitch, amplitude, sustain time ("hold") musicShape = new MusicShape(4); musicShape.setInstrument(instrument); // 1 second, middle C, 0.6 amp, hold 0.95 sec musicShape.add(1, 60, 0.6, 0.95); // wait up to 3 seconds before making the sound musicShape.setStartDelay(JMSLRandom.choose(3.0)); } private void launchMusicShape() { musicShape.launch(JMSL.now()); } public void stop() { musicShape.finishAll(); try { musicShape.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { OneSoundOnce demo = new OneSoundOnce(); 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); } }); } }