/* * Created on May 14, 2004 * */ package jmsltutorial; import java.awt.*; 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.jmsl.util.WeightedIntegerSequence; /** * Plays a MusicJob which consults a WeightedIntegerSequence to play a random walk melody. * TextFields allow the user to set the relative weights of choosing intervals -1, 0, 1
* * Thanks to Larry Polansky for suggesting WeightedIntegerSequence. * * @author Nick Didkovsky, email: didkovn@mail.rockefeller.edu, (c) 2004 Nick Didkovsky, all rights reserved. * */ public class WeightedIntegerToot extends java.applet.Applet implements ActionListener { double[] weights = { 1, 1, 1 }; WeightedIntegerSequence weightedIntegerSequence; WeightedMelodyMusicJob job; Button startButton; Button stopButton; Button applyButton; ArrayEditorPanel arrayEditorPanel; JMSLMixerContainer mixer; public void start() { JMSLRandom.randomize(); JMSL.clock.setAdvance(0.1); weightedIntegerSequence = new WeightedIntegerSequence(weights); job = new WeightedMelodyMusicJob(); job.setWeightedIntegerSequence(weightedIntegerSequence); job.setRepeats(Integer.MAX_VALUE); JSynMusicDevice.instance().open(); JSynInsFromClassName ins = new JSynInsFromClassName(4, "com.softsynth.jsyn.circuits.FilteredSawtoothBL"); mixer = new JMSLMixerContainer(); mixer.start(); mixer.addInstrument(ins); job.setInstrument(ins); buildGUI(); } void buildGUI() { setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new GridLayout(0, 1)); p.add(startButton = new Button("START")); p.add(stopButton = new Button("STOP")); p.add(applyButton = new Button("APPLY WEIGHTS")); add(BorderLayout.NORTH, p); arrayEditorPanel = new ArrayEditorPanel(); arrayEditorPanel.setArray(weightedIntegerSequence.getWeights()); add(BorderLayout.SOUTH, arrayEditorPanel); startButton.addActionListener(this); stopButton.addActionListener(this); applyButton.addActionListener(this); } public void stop() { removeAll(); job.finishAll(); JMSL.closeMusicDevices(); } public void actionPerformed(ActionEvent ev) { Object source = ev.getSource(); if (source == startButton) { startButton.setEnabled(false); stopButton.setEnabled(true); job.launch(JMSL.now()); } if (source == stopButton) { stopButton.setEnabled(false); startButton.setEnabled(true); job.finish(); } if (source == applyButton) { arrayEditorPanel.setArrayFromTextFields(); } } } class WeightedMelodyMusicJob extends MusicJob { WeightedIntegerSequence weightedIntegerSequence; double pitch = 60; double duration = 0.25; public WeightedIntegerSequence getWeightedIntegerSequence() { return weightedIntegerSequence; } public void setWeightedIntegerSequence(WeightedIntegerSequence sequence) { weightedIntegerSequence = sequence; } public double repeat(double playTime) { int interval = weightedIntegerSequence.next(); // 0, 1, 2 int change = interval - 1; // -1, 0, 1 pitch += change; //System.out.println(pitch); double[] data = { duration, pitch, 0.6, duration * 0.8 }; return getInstrument().play(playTime, 1, data); } }