/* * Created on Nov 2, 2007 by Nick Didkovsky * */ package jmsltestsuite; import java.io.IOException; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.*; /** * Create a piece using two MusicShapes, a MusicJob, and three MidiInstruments playing three * different programs on three different channels. Play the hierarchy silently in non-realtime and * log to MidiFile. * * @author Nick Didkovsky, (c) 2005 Nick Didkovsky, nick@didkovsky.com * */ public class MusicShapeToMIDIFile { public static void main(String[] args) { JMSL.clock = new NonRealTimeMusicClock(); JMSL.clock.setAdvance(0.1); JMSL.midi = MidiIO_JavaSound.instance(); JMSL.midi.open(); JMSL.midi.setMidiLogging(true); JMSL.midi.setQuiet(true); Instrument ins1 = new MidiInstrument(1); // channel, program change MidiInstrument ins2 = new MidiInstrument(2, 68); MidiInstrument ins3 = new MidiInstrument(2, 12); MusicShape s1 = new MusicShape(ins1.getDimensionNameSpace()); s1.add(0.0, 64, 100, 2.0); s1.add(0.0, 66, 90, 2.5); s1.add(1.5, 69, 80, 1.5); s1.add(0.0, 72, 70, 2.5); s1.add(1.5, 75, 100, 2.0); s1.setInstrument(ins1); s1.setRepeats(2); s1.setName("Piano"); MusicShape s2 = new MusicShape(ins2.getDimensionNameSpace()); s2.add(0.3333, 72, 70, 0.33); s2.add(0.3333, 74, 80, 0.33); s2.add(0.3333, 77, 70, 0.33); s2.add(0.6666, 80, 85, 0.66); s2.add(0.3333, 77, 70, 0.33); s2.setInstrument(ins2); s2.setRepeats(3); s2.setName("Oboe"); s2.addRepeatPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { MusicShape s = (MusicShape) parent; s.scramble(0, s.size() - 1, 1); // scramble pitches each repeat return time; } }); // this MusicJob will create its own midi data every time it repeats and plays a note MusicJob j = new MusicJob() { public double repeat(double playTime) { double duration = JMSLRandom.choose(0.125, 0.250); double pitch = JMSLRandom.choose(50, 90); double vel = JMSLRandom.choose(30, 80); double hold = duration * 1.2; double[] data = { duration, pitch, vel, hold }; return getInstrument().play(playTime, 1, data); } }; j.setInstrument(ins3); j.setRepeats(20); j.setStartDelay(3); ParallelCollection col = new ParallelCollection(); col.add(s1); col.add(s2); col.add(j); col.addStopPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { try { JMSL.midi.writeMidiFile("MidiLoggerNonRealTimeTest.MID"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } return time; } }); col.launch(JMSL.now()); } }