/* * Created by Nick on Apr 11, 2005 * */ package jmslexamples.jsyn; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.*; import com.softsynth.jmsl.jsyn.circuits.BrassSynthNote; import com.softsynth.jmsl.util.TuningET; /** * * Use a DataTranslator to convert data from frequencies to pitches so * JSynInsFromClassName can use a MusicShape where frequnecies are specified. * * This way you can think in terms of frequencies if that's what makes sense for your composition * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class FrequencyDataTranslatorDemo { JMSLMixerContainer mixer; JSynInsFromClassName ins; MusicShape musicShape; public FrequencyDataTranslatorDemo() { initJMSLJSyn(); buildMixer(); buildInstrument(); buildMusicShape(); } public void initJMSLJSyn() { JMSL.clock.setAdvance(0.1); JSynMusicDevice.instance().open(); } void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } void buildInstrument() { ins = new JSynInsFromClassName(8, BrassSynthNote.class.getName()); mixer.addInstrument(ins); } public void buildMusicShape() { musicShape = new MusicShape(4); musicShape.setDataTranslator(new FrequencyDataTranslator()); musicShape.useStandardDimensionNameSpace(); musicShape.setDimensionName(1, "frequency"); musicShape.setDefault(1, 440); musicShape.setLimits(1, 20, 3000); double baseFrequency = 440; musicShape.add(1.0, baseFrequency, 0.5, 10); musicShape.add(1.0, baseFrequency * 81 / 64, 0.5, 9); // Pythagorean major third musicShape.add(1.0, baseFrequency * 3 / 2, 0.5, 8); musicShape.add(7.0, baseFrequency * 16 / 9, 0.5, 7); // Pythagorean minor 7th musicShape.setInstrument(ins); musicShape.setRepeats(10); } public MusicShape getMusicShape() { return musicShape; } public static void main(String[] args) { Frame f = new Frame("Close to quit"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { JMSL.closeMusicDevices(); System.exit(0); } }); f.setSize(320, 200); // Amiga res for max cross platform f.setVisible(true); FrequencyDataTranslatorDemo demo = new FrequencyDataTranslatorDemo(); demo.getMusicShape().launch(JMSL.now() + 3); } } class FrequencyDataTranslator implements DataTranslator { public FrequencyDataTranslator() { } /** * data[1] contains frequency. translate to pitch using the tuning from the * instrument. This way the instrument will simply convert back. */ public double[] translate(MusicJob job, double[] data) { // make a copy of the incoming double[] double[] dataCopy = new double[data.length]; System.arraycopy(data, 0, dataCopy, 0, data.length); // this will fail if instrument is not of type TunedSynthNoteInstrument // and tuning is not equal tempered // Note that the tuning "falls out" of this process and is completely arbitrary. // it just needs to be the same when used here to go from freq to pitch as it is in the instrument // when it goes from pitch back to freq TuningET tuning = (TuningET) ((TunedSynthNoteInstrument) job.getInstrument()).getTuning(); double pitch = tuning.getPitch(dataCopy[1]); dataCopy[1] = pitch; return dataCopy; } }