/* * Created by Nick on April 24, 2012 * */ package jmslexamples.jsyn2; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import com.didkovsky.portview.swing.ViewFactorySwing; import com.softsynth.jmsl.DefaultMusicClock; import com.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.MusicDevice; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; import com.softsynth.jmsl.score.Clef; import com.softsynth.jmsl.score.Measure; import com.softsynth.jmsl.score.Note; import com.softsynth.jmsl.score.NoteFactory; import com.softsynth.jmsl.score.Orchestra; import com.softsynth.jmsl.score.Score; import com.softsynth.jmsl.score.ScoreFrame; import com.softsynth.jmsl.score.SelectionBuffer; import com.softsynth.jmsl.util.LinearInterpolator; import jmslexamples.jsyn2.unitvoices.FMVoice; /** * This is a popular demo. Lots of love on our socials. * * Build a simple Score with an orchestra of pure Java JSyn instruments with the * use of presets. Also shows quarter tones. * * @author Nick Didkovsky, (c) 2012 All rights reserved, Email: * nick@didkovsky.com * */ public class JSyn2FMScore extends JFrame { Score score; ScoreFrame scoreFrame; Orchestra orchestra; public void build() { 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.2); } private void initMusicDevices() { JMSL.clock = new DefaultMusicClock(); initJSyn2MusicDevice(); } private void initJSyn2MusicDevice() { MusicDevice dev = JSynMusicDevice.instance(); dev.edit(new java.awt.Frame()); // ((JSynMusicDevice)dev).setInputDeviceID(-1); ((JSynMusicDevice) dev).setNumInputChannels(0); dev.open(); } private void buildOrchestra() { orchestra = new Orchestra(); // use 4 voice polyphonic stereo JSyn2 instrument, note use of presets! Instrument ins = new JSynUnitVoiceInstrument(4, FMVoice.class.getName(), FMVoice.PRESET_BELL); orchestra.addInstrument(ins, "FM Bell"); ins = new JSynUnitVoiceInstrument(4, FMVoice.class.getName(), FMVoice.PRESET_WOODDRUM); orchestra.addInstrument(ins, "FM Wood"); ins = new JSynUnitVoiceInstrument(4, FMVoice.class.getName(), FMVoice.PRESET_CLARINET); orchestra.addInstrument(ins, "FM Clarinet"); ins = new JSynUnitVoiceInstrument(4, FMVoice.class.getName(), FMVoice.PRESET_BRASS); orchestra.addInstrument(ins, "FM Brass"); 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); int numMeasures = 33; score.getMeasure(0).getStaff(3).setClef(Clef.BASS_CLEF); /* 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); 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); } } /* add notes to next staff */ score.rewind(); score.setCurrentStaffNumber(2); for (int i = 0; i < numMeasures * 2; i++) { double duration = 2.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(3); for (int i = 0; i < numMeasures * 3; i++) { double duration = 4.0 / 3.0; double pitch = JMSLRandom.choose(12.0) + 32; double amp = JMSLRandom.choose(0.5, 0.9); Note n = score.addNote(duration, pitch, amp, duration * 1.3); if (i % 3 != 2) { n.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(4, 800, 600); buildPiece(); } void buildScoreFrame() { scoreFrame = new ScoreFrame(); scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.setVisible(true); } public void stop() { if (scoreFrame != null) { scoreFrame.setVisible(false); scoreFrame.dispose(); Score.deleteCanvas(); SelectionBuffer.disposeEditFrame(); scoreFrame = null; score = null; } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { JSyn2FMScore jf = new JSyn2FMScore(); jf.setTitle("Score using JSyn"); jf.add(new JLabel("A Score will open in its own window.", JLabel.CENTER)); jf.setSize(500, 200); jf.setVisible(true); jf.build(); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { JMSL.closeMusicDevices(); System.exit(0); } }); } }