/* * Created by Nick on April 24, 2012 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JApplet; import javax.swing.JLabel; import com.didkovsky.portview.swing.ViewFactorySwing; import com.jsyn.swing.JAppletFrame; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.util.LinearInterpolator; /** * Build a simple Score with an orchestra of pure Java JSyn instruments. Also * shows quarter tones. * * @author Nick Didkovsky, (c) 2012 All rights reserved, Email: * nick@didkovsky.com * */ public class JSyn2Score { Score score; ScoreFrame scoreFrame; Orchestra orchestra; public void start() { initJMSL(); initMusicDevices(); buildScore(); buildOrchestra(); assignRandomPanning(); buildScoreFrame(); } private void initJMSL() { JMSL.setViewFactory(new ViewFactorySwing()); NoteFactory.useQuarterToneAccidentals(true); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } private void initMusicDevices() { JMSL.clock = new DefaultMusicClock(); initJSyn2MusicDevice(); } private void initJSyn2MusicDevice() { MusicDevice dev = JSynMusicDevice.instance(); dev.edit(new java.awt.Frame()); dev.open(); } private void buildOrchestra() { orchestra = new Orchestra(); // use 4 voice polyphonic stereo JSyn instrument Instrument ins = new JSynUnitVoiceInstrument(4, com.jsyn.instruments.WaveShapingVoice.class.getName()); orchestra.addInstrument(ins, "WaveShapingVoice"); ins = new JSynUnitVoiceInstrument(4, com.jsyn.instruments.SubtractiveSynthVoice.class.getName()); orchestra.addInstrument(ins, "SubtractiveSynthVoice"); ins = new JSynUnitVoiceInstrument(4, com.jsyn.instruments.DrumWoodFM.class.getName()); orchestra.addInstrument(ins, "DrumWoodFM"); score.setOrchestra(orchestra); } /** * build measures with a whole note chord. Transpose up a quarter tone each * measure */ void buildPiece() { Measure m = score.addMeasure(4, 4); m.setTempo(120); score.getMeasure(0).getStaff(2).setClef(Clef.BASS_CLEF); int numMeasures = 33; /* add notes to top staff */ // duration (where 1.0 = qtr), pitch (where 60 = middle C), amp, sustain for (int i = 0; i < numMeasures; i++) { double quarterTone = i * 0.5; // 0 first loop, 0.5 second loop, // etc double pitch = 60 + quarterTone; // duration 4 == whole note, pitch, amplitude, hold time Note n = score.addNote(4.0, pitch, 0.6, 3.9); n.addInterval(pitch + 7); } /* add notes to next staff */ score.rewind(); score.setCurrentStaffNumber(1); for (int i = 0; i < numMeasures * 4; i++) { double duration = 1.0; double pitch = JMSLRandom.choose(12.0) + 72; double amp = JMSLRandom.choose(0.5, 0.9); score.addNote(duration, pitch, amp, duration * 1.3); } /* add notes to next staff */ score.rewind(); score.setCurrentStaffNumber(2); double lastPitch = -1; int numNotes = numMeasures * 8; /* * increase the likelihood of a rhythmic hiccough over the course of the piece */ LinearInterpolator probabilityThresholdInterpolator = new LinearInterpolator(0, 0, numNotes, 1.0); for (int i = 0; i < numNotes; i++) { double duration = 0.5; double pitch = JMSLRandom.choose(12) + 48; if (pitch == lastPitch) { pitch += JMSLRandom.choose() > 0.5 ? 12 : -12; } lastPitch = pitch; Note lastAddedNote = null; double amp = 0.8; double octaveProb = probabilityThresholdInterpolator.interp(i); if (JMSLRandom.choose() < octaveProb) { duration /= 2; Note n = score.addNote(duration, pitch, amp, duration * 1.3); lastAddedNote = score.addNote(duration, pitch + 12, amp, duration * 1.3); n.setBeamedOut(true); } else { lastAddedNote = score.addNote(duration, pitch, amp, duration * 1.3); } if (i % 2 == 0) { lastAddedNote.setBeamedOut(true); } } } private void assignRandomPanning() { // pan randomly int numFaders = score.getOrchestra().getJMSLMixerContainer().getNumFaders(); for (int i = 0; i < numFaders; i++) { score.getOrchestra().getJMSLMixerContainer().panAmpChange(i, JMSLRandom.choose(), 0.5); } // also reduce amp of first fader (waveshapingvoice is loud) score.getOrchestra().getJMSLMixerContainer().panAmpChange(0, JMSLRandom.choose(), 0.2); } void buildScore() { score = new Score(3, 800, 400); buildPiece(); } void buildScoreFrame() { scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.setVisible(true); } public static void main(String[] args) { JSyn2Score demo = new JSyn2Score(); demo.start(); } }