/* * Created by Nick on Jan 10, 2005 * */ package jmslexamples.jsyn2; import java.awt.Point; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.Limits; import com.softsynth.jmsl.MusicJob; import com.softsynth.jmsl.ParallelCollection; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; import com.softsynth.jmsl.jsyn2.unitvoices.FilteredSawtoothBL; import com.softsynth.jmsl.jsyn2.unitvoices.RingModBell; import jmslexamples.AnimationTextDemo; /** * Extends AnimationApplet in jmslexamples substituting MusicJob "scripts" that * move the characters around while also making sound. See definition of * CharacterScriptWithSound at bottom of this file. * * Upgraded to JSyn2 Dec 2016 * * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * nick@didkovsky.com * */ public class AnimationTextDemoWithSound extends AnimationTextDemo { private JMSLMixerContainer mixer; private Instrument ins1; private Instrument ins2; private CharacterScriptWithSound script1; private CharacterScriptWithSound script2; /** Overridden so we can make sound */ protected void initJMSL() { super.initJMSL(); JSynMusicDevice.instance().open(); } /** Overridden so we can make sound */ protected void buildScripts() { System.out.println("buildScripts() with sound"); // a script to move character 1 around script1 = new CharacterScriptWithSound(character1); script1.setRepeats(Integer.MAX_VALUE); script1.setRepeatPause(1.0); // a script to move character 2 around (note it's a different repeat // pause than character 1) script2 = new CharacterScriptWithSound(character2); script2.setRepeats(Integer.MAX_VALUE); script2.setRepeatPause(0.25); } /** Overridden so we can make sound */ protected void buildCartoon() { cartoon = new ParallelCollection(); cartoon.add(animation); cartoon.add(script1); cartoon.add(script2); } public void start() { super.start(); buildMixer(); buildInstruments(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } /** Make a couple of JSyn instruments and hand them to the scripts */ private void buildInstruments() { ins1 = new JSynUnitVoiceInstrument(4, FilteredSawtoothBL.class.getName()); ins2 = new JSynUnitVoiceInstrument(4, RingModBell.class.getName()); mixer.addInstrument(ins1, 0.1, 0.5); mixer.addInstrument(ins2, 0.9, 0.5); script1.setInstrument(ins1); script2.setInstrument(ins2); } public static void main(String[] args) { AnimationTextDemoWithSound demo = new AnimationTextDemoWithSound(); demo.start(); demo.pack(); demo.setVisible(true); demo.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { demo.stop();System.exit(0); } }); } } /** Updates x,y position of character over time */ class CharacterScriptWithSound extends MusicJob { Point character; public CharacterScriptWithSound(Point character) { this.character = character; } public double repeat(double playTime) { int xDelta = JMSLRandom.choose(-2, 3); int yDelta = JMSLRandom.choose(-2, 3); character.x += xDelta; character.y += yDelta; if (getInstrument() != null) { double pitch = scaleXToPitch(character.x); double amplitude = scaleYToAmp(character.y); double[] data = { getRepeatPause(), pitch, amplitude, getRepeatPause() * 1.2 }; getInstrument().play(playTime, 1.0, data); } return playTime; } private double scaleYToAmp(int y) { double amp = y / 200.0; return Limits.clipTo(amp, 0.0, 1.0); } private double scaleXToPitch(int x) { double pitch = x / 200.0 * 120; return Limits.clipTo(pitch, 0, 120); } }