/* * Created by Nick on Nov 28, 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.JSynMusicDevice; import com.softsynth.jmsl.jsyn.SynthNoteAllPortsInstrument; import com.softsynth.jmsl.score.*; import com.softsynth.jmsl.score.transcribe.*; import com.softsynth.jmsl.view.MusicShapeEditor; import com.softsynth.jsyn.circuits.FilteredSawtoothBL; /** * Edit a MusicShape in the MusicShapeEditor, then transcribe it into a notated Score. This uses a SynthNoteAllPortsInstrument * demonstrating that timbral dimensions higher than dimension 3 are preserved during transcription. * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class TranscribeToot05 extends java.applet.Applet implements ActionListener { Instrument insForMusicShape; Instrument insForScore; JMSLMixerContainer mixer; MusicShapeEditor musicShapeEditor; MusicShape musicShape; Score score; ScoreFrame scoreFrame; Button startButton; Button stopButton; Button transcribeButton; Transcriber transcriber; public void init() { JMSLRandom.randomize(); JMSL.setIsApplet(true); } public void start() { synchronized (JMSL.class) { initializeJMSL(); openMusicDevices(); buildInstruments(); buildMusicShape(); buildMixer(); buildMusicShapeEditor(); buildGUI(); scoreFrame = new ScoreFrame(); transcriber = new Transcriber(); } } private void initializeJMSL() { JMSL.clock.setAdvance(0.1); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); } private void openMusicDevices() { JSynMusicDevice.instance().open(); // we don't want someone to reinitialize JSyn after this so we prevent the editor from coming up JSynMusicDevice.instance().setEditEnabled(false); } private void buildInstruments() { insForMusicShape = new SynthNoteAllPortsInstrument(8, FilteredSawtoothBL.class.getName()); insForScore = new SynthNoteAllPortsInstrument(8, FilteredSawtoothBL.class.getName()); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); mixer.addInstrument(insForMusicShape); } private void buildMusicShape() { musicShape = new MusicShape(insForMusicShape.getDimensionNameSpace()); musicShape.prefab(); musicShape.setInstrument(insForMusicShape); // cute trick to change state of buttons when musicShape finishes on its own musicShape.addStopPlayable(new Playable() { public double play(double playTime, Composable parent) throws InterruptedException { startButton.setEnabled(true); stopButton.setEnabled(false); return playTime; } }); } private void buildMusicShapeEditor() { musicShapeEditor = new MusicShapeEditor(); musicShapeEditor.addMusicShape(musicShape); } private void buildGUI() { setLayout(new BorderLayout()); Panel buttonPanel = new Panel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(startButton = new Button("Start")); buttonPanel.add(stopButton = new Button("Stop")); stopButton.setEnabled(false); buttonPanel.add(transcribeButton = new Button("Transcribe")); startButton.addActionListener(this); stopButton.addActionListener(this); transcribeButton.addActionListener(this); add(BorderLayout.SOUTH, buttonPanel); add(BorderLayout.NORTH, mixer.getPanAmpControlPanel()); add(BorderLayout.CENTER, musicShapeEditor.getComponent()); } public void stop() { synchronized (JMSL.class) { musicShape.finish(); try { musicShape.waitForDone(); } catch (InterruptedException e) { } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); scoreFrame.setVisible(false); scoreFrame.dispose(); Score.deleteCanvas(); SelectionBuffer.disposeEditFrame(); removeAll(); } } private void transcribe() { BeatDivisionSchemeList.defaultSetup(); // protect the original MusicShape by cloning it MusicShape shapeToTranscribe = (MusicShape) musicShape.clone(); // convert durations to absolute times shapeToTranscribe.integrate(0); Orchestra orch = new Orchestra(); orch.addInstrument(insForScore); score = new Score(1, 800, 500); score.setOrchestra(orch); score.addMeasure(); transcriber.setScore(score); transcriber.setSourceMusicShape(shapeToTranscribe); score.rewind(); score.setCurrentStaffNumber(0); try { transcriber.transcribe(); } catch (SearchPathListExpansionException e) { e.printStackTrace(); } catch (ElementMissedException e) { e.printStackTrace(); } scoreFrame.addScore(score); scoreFrame.pack(); scoreFrame.setVisible(true); } public void actionPerformed(ActionEvent ev) { Object source = ev.getSource(); if (source == startButton) { startButton.setEnabled(false); stopButton.setEnabled(true); musicShape.launch(JMSL.now()); } if (source == stopButton) { startButton.setEnabled(true); stopButton.setEnabled(false); musicShape.finish(); } if (source == transcribeButton) { transcribe(); } } }