/* * Created by Nick on Jan 10, 2005 * */ package jmslexamples.jsyn; import java.awt.Point; import jmslexamples.AnimationApplet; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.*; import com.softsynth.jsyn.circuits.*; /** * 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 * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class AnimationAppletWithSound extends AnimationApplet { 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(0.3); // 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.15); } /** Overridden so we can make sound */ protected void buildCartoon() { cartoon = new ParallelCollection(); cartoon.add(animation); cartoon.add(script1); cartoon.add(script2); } public void start() { synchronized (JMSL.class) { 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 JSynInsFromClassName(4, FilteredSawtoothBL.class.getName()); ins2 = new JSynInsFromClassName(4, RingModBell.class.getName()); mixer.addInstrument(ins1, 0.3, 0.5); mixer.addInstrument(ins2, 0.7, 0.5); script1.setInstrument(ins1); script2.setInstrument(ins2); } } /** 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); } }