package jmslexamples.jsyn2;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import javax.swing.JFrame;
import com.softsynth.jmsl.JMSL;
import com.softsynth.jmsl.JMSLRandom;
import com.softsynth.jmsl.MusicShape;
import com.softsynth.jmsl.jsyn2.JSynMusicDevice;
import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument;
import com.softsynth.jmsl.score.Note;
import com.softsynth.jmsl.score.Orchestra;
import com.softsynth.jmsl.score.Score;
import com.softsynth.jmsl.score.ScoreFrame;
import com.softsynth.jmsl.util.Oof;
import com.softsynth.jmsl.view.MusicShapeEditor;
/**
* This demo generates pitches using JMSL's 1/F sequence generator. It loads a
* MusicShape with this data first, and displays it in a MusicShapeEditor. It
* adds notes to a JScore by pulling the data from the MusicShape.
*
*
* JSyn2 version Dec 2016
*
* @author Nick Didkovsky, copyright 2000 Nick Didkovsky
*/
public class JScoreOofDemo extends JFrame {
Score score;
ScoreFrame scoreFrame;
MusicShape oofShape;
/**
* Build a MusicShape of 32nd notes whose pitches are generated by 1/F sequence.
* 1/F sequence ranges from 0..127. Here we scale it to half that range,
* starting 2 octaves below middle C
*/
void buildOofShape() {
oofShape = new MusicShape(4);
oofShape.useStandardDimensionNameSpace();
Oof oof = new Oof(7);
// duration is set at 32nd notes
double dur = 1 / 8.0;
for (int i = 0; i < 64; i++) {
double pitch = (oof.next() / 2.0) + 36;
double amplitude = JMSLRandom.choose(0.5, 1.0);
double hold = dur * 0.8;
oofShape.add(dur, pitch, amplitude, hold);
}
}
/**
* call score.addNote() for each element in MusicShape, to add algorithmically
* generated data to notated score
*/
void buildOofScore() {
score.addMeasure();
int kount = 0;
for (Enumeration e = oofShape.elements(); e.hasMoreElements();) {
Note n = score.addNote((double[]) e.nextElement());
n.setBeamedOut(kount % 8 != 7);
kount++;
}
}
public void init() {
}
Orchestra orch;
public void start() {
initMusicDevices();
initJMSL();
buildScore();
buildOrchestra();
buildShapeEditor();
buildScoreFrame();
}
private void buildScoreFrame() {
// score.setSystemWrap(false);
scoreFrame = new ScoreFrame("JMSL Score JSyn Demo by Nick Didkovsky, copyright 2016 Nick Didkovsky");
scoreFrame.addScore(score);
score.getScoreLayoutManager().setZoom(0.5);
score.getControlPanel().setZoom(0.5);
scoreFrame.setVisible(true);
}
private void buildShapeEditor() {
MusicShapeEditor se = new MusicShapeEditor();
se.addMusicShape(oofShape);
add("Center", se.getComponent());
}
private void buildScore() {
int numStaves = 1;
score = new Score(numStaves, "1/F Demo");
score.setComposer("Nick Didkovsky");
score.setCopyright("(c) 2016 Nick Didkovsky");
buildOofShape();
oofShape.print();
buildOofScore();
}
private void buildOrchestra() {
orch = new Orchestra();
JSynUnitVoiceInstrument ins = new JSynUnitVoiceInstrument(2,
com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.class.getName(),
com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.PRESET_WOODDRUM);
orch.addInstrument(ins, "Wood");
// build a mixer gui with a SynthMixer
// orch.buildMixer();
score.setOrchestra(orch);
}
private void initMusicDevices() {
JSynMusicDevice.instance().open();
}
private void initJMSL() {
JMSL.clock.setAdvance(1.0);
}
public ScoreFrame getScoreFrame() {
return scoreFrame;
}
public void stop() {
removeAll();
scoreFrame.setVisible(false);
scoreFrame.dispose();
}
public static void main(String args[]) {
JScoreOofDemo demo = new JScoreOofDemo();
demo.start();
demo.pack();
demo.setVisible(true);
demo.getScoreFrame().addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JMSL.closeMusicDevices();
System.exit(0);
}
});
}
}