/* * Created by Nick on Nov 12, 2004 * */ package jmslexamples.jsyn; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.JSynInsFromClassName; import com.softsynth.jmsl.jsyn.JSynMusicDevice; import com.softsynth.jsyn.circuits.RingModBell; /** * Use a PlayLurker to listen in on elements of a MusicShape being played. For * each element played, PlayLurker generates and launches a MusicShape based on * the pitch * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class PlayLurkingArpeggiatorExample extends java.applet.Applet implements ActionListener { JMSLMixerContainer mixer; Button launchButton; Button finishButton; MusicShape melodyMusicShape; public void init() { JMSLRandom.randomize(); } public void start() { MusicDevice dev = JSynMusicDevice.instance(); dev.open(); // melody JSynInsFromClassName ins = new JSynInsFromClassName(8, RingModBell.class.getName()); melodyMusicShape = new MusicShape(ins.getDimensionNameSpace()); melodyMusicShape.setInstrument(ins); melodyMusicShape.prefab(); melodyMusicShape.setRepeats(Integer.MAX_VALUE); melodyMusicShape.setTimeStretch(3); // arpeggiating PlayLurker ArpeggiatingPlayLurker lurker = new ArpeggiatingPlayLurker(); melodyMusicShape.addPlayLurker(lurker); lurker.setInstrument(new JSynInsFromClassName(32, RingModBell.class.getName())); // pan instruments L/R mixer = new JMSLMixerContainer(); mixer.start(); mixer.addInstrument(ins, 0, 0.8); mixer.addInstrument(lurker.getInstrument(), 1, 0.8); add(launchButton = new Button("Launch")); add(finishButton = new Button("Finish")); launchButton.addActionListener(this); finishButton.addActionListener(this); } public void stop() { melodyMusicShape.finishAll(); JMSL.closeMusicDevices(); } public void actionPerformed(ActionEvent ev) { Object source = ev.getSource(); if (source == launchButton) { melodyMusicShape.launch(JMSL.now()); launchButton.setEnabled(false); finishButton.setEnabled(true); } if (source == finishButton) { melodyMusicShape.finishAll(); launchButton.setEnabled(true); finishButton.setEnabled(false); } } } class ArpeggiatingPlayLurker implements PlayLurker { private Instrument instrument; /** Pull the pitch from the MusicShape's element and arpeggiate it */ public void notifyPlayLurker(double playTime, MusicJob list, int index) { MusicShape s = (MusicShape) list; double[] data = s.get(index); double pitch = data[1]; double amp = data[2]; MusicShape gesture = new MusicShape(4); int numArps = JMSLRandom.choose(2, 8); double dur = JMSLRandom.choose(0.1, 0.15); gesture.setStartDelay(dur); int direction = JMSLRandom.choose() < 0.5 ? 1 : -1; for (int i = 0; i <= numArps; i++) { pitch += 12.0 / numArps * direction; gesture.add(dur, pitch, amp - (amp / numArps * i), dur * 0.8); } gesture.setInstrument(instrument); gesture.launch(playTime); } public Instrument getInstrument() { return instrument; } public void setInstrument(Instrument instrument) { this.instrument = instrument; } }