package jmsltutorial; import java.awt.Button; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.SynthNoteAllPortsInstrument; /** PlayToot02.java * Play a sequence of MusicShapes with the same JSyn instrument * * Generate 7 MusicShapes, each 3 elements long, with random melodies and synth control params. * Play them with uniform random behavior (ie choose a different one each repeat of player) * * Uses two instruments, and provide a button that, when clicked, switches Instruments * * @author Nick Didkovsky, (C) 2003 Nick Didkovsky, All Rights Reserved, didkovn@mail.rockefeller.edu */ public class PlayToot02 extends java.applet.Applet implements ActionListener { Player myPlayer; SynthNoteAllPortsInstrument instrument1; SynthNoteAllPortsInstrument instrument2; JMSLMixerContainer mixer; Button startButton; Button stopButton; Button switchInstrumentsButton; public void init() { JMSLRandom.randomize(); } /* When applet starts up, do our building */ public void start() { buildInstrumentsAndMixer(); buildPlayer(); buildGUI(); /* Synchronize Java display. */ getParent().validate(); getToolkit().sync(); } private void buildPlayer() { myPlayer = new Player(instrument1); myPlayer.setBehavior(new UniformRandomBehavior()); myPlayer.setRepeats(10000); } private void buildGUI() { setLayout(new FlowLayout()); startButton = new Button("Start"); stopButton = new Button("Stop"); switchInstrumentsButton = new Button("Switch Instrument"); startButton.addActionListener(this); stopButton.addActionListener(this); switchInstrumentsButton.addActionListener(this); add(startButton); add(stopButton); add(switchInstrumentsButton); } private void buildInstrumentsAndMixer() { instrument1 = new SynthNoteAllPortsInstrument(); MusicDevice dev = instrument1.getMusicDevice(); dev.open(); instrument1.setMaxVoices(8); instrument1.setSynthNoteClassName("com.softsynth.jsyn.circuits.FilteredSawtoothBL"); instrument1.buildFromAttributes(); mixer = new JMSLMixerContainer(); mixer.start(); instrument2 = new SynthNoteAllPortsInstrument(8, "com.softsynth.jmsl.jsyn.circuits.BrassSynthNote"); mixer.addInstrument(instrument1); mixer.addInstrument(instrument2); } private void buildMusicShapes() { myPlayer.removeAll(); for (int i = 0; i < 7; i++) { MusicShape ms = new MusicShape(instrument1.getDimensionNameSpace()); ms.prefab(3); // little 3-element shapes myPlayer.add(ms); } } /* Shut down the Collection when applet stops */ public void stop() { removeAll(); mixer.stop(); myPlayer.finishAll(); JMSL.closeMusicDevices(); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) { myPlayer.finishAll(); buildMusicShapes(); myPlayer.launch(JMSL.now()); } if (source == stopButton) myPlayer.finishAll(); if (source == switchInstrumentsButton) { if (myPlayer.getInstrument() == instrument1) { myPlayer.setInstrument(instrument2); } else { myPlayer.setInstrument(instrument1); } } } /* Can be run as either an application or as an applet. */ public static void main(String args[]) { PlayToot02 applet = new PlayToot02(); com.softsynth.jsyn.AppletFrame frame = new com.softsynth.jsyn.AppletFrame("JMSL Tutorial", applet); frame.setSize(200, 200); frame.show(); frame.test(); } }