[jmsl] Navigating Sequential Sequence

jmsl at music.columbia.edu jmsl at music.columbia.edu
Mon Apr 27 08:30:17 EDT 2009


Hi,

I'm trying to implement function to start, stop, next, previous for a 
sequential collection.
1. should be able to move the pointer (currentSequence) and launch the 
collection from where the pointer is.
2. Also, should be able to move the pointer while playing. The function 
should sequenceCol.finishAll() right away, then resume from where the 
pointer is.
3. When the playback reaches to the end, the pointer should come back to 
where it started.
The way I'm doing doesn't seem to work. Is there a simpler way? Here's the 
code.
Thanks,

Chi

private SequentialCollection sequenceCol;
private SequenceBehavior sequenceBehavior;
private int currentSequence, startedSequence;
private JCheckBoxMenuItem sequencePlayCB;

private void buildCollection() {
sequenceCol = new SequentialCollection();
sequenceCol.setRepeats(Integer.MAX_VALUE);
sequenceBehavior = new SequenceBehavior();
sequenceCol.setBehavior(sequenceBehavior);
sequenceCol.addStartPlayable(new SequencePlayable());
sequenceCol.addRepeatPlayable(new SequencePlayable());
}

privat void buildGui() {
m.add(sequencePlayCB = new JCheckBoxMenuItem("Play Sequence"));
sequencePlayCB.setMnemonic(KeyEvent.VK_Q);
sequencePlayCB.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0));
sequencePlayCB.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (sequencePlayCB.getState()) {
playSequence();
} else {
stopSequence();
}
}
});

m.add(mi = new JMenuItem("Previous Sequence"));
mi.setMnemonic(KeyEvent.VK_V);
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 0));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
prevSequence();
}
});

m.add(mi = new JMenuItem("Next Sequence"));
mi.setMnemonic(KeyEvent.VK_N);
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, 0));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nextSequence();
}
});

m.add(mi = new JMenuItem("Add Sequence"));
mi.setMnemonic(KeyEvent.VK_D);
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addSequence();
}
});

m.add(mi = new JMenuItem("Remove Sequence"));
mi.setMnemonic(KeyEvent.VK_R);
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeSequence();
}
});

}

private void playSequence() {
if (sequenceCol.size() > 0) {
startedSequence=currentSequence; // Record where it started
sequenceCol.launch(JMSL.now());
}
}

private void stopSequence() {
sequenceCol.finishAll();
currentSequence=startedSequence; // go back to where it started
sequenceBehavior.setStart(currentSequence);
}

private void prevSequence() {
if (currentSequence>0) {
currentSequence--;
sequenceBehavior.setStart(currentSequence);
// If playing, stop, move, resume
if (sequencePlayCB.getState()) {
sequenceCol.finishAll();
try {sequenceCol.waitForDone();}
catch (InterruptedException e) {e.printStackTrace();}
sequenceCol.launch(JMSL.now());
}
}
}

private void nextSequence() {
if (currentSequence<sequenceCol.size()-1) {
currentSequence++;
sequenceBehavior.setStart(currentSequence);
// If playing, stop, move, resume.
if (sequencePlayCB.getState()) {
sequenceCol.finishAll();
try {sequenceCol.waitForDone();}
catch (InterruptedException e) {e.printStackTrace();}
sequenceCol.launch(JMSL.now());
}
}
}

private void addSequence() {
ParallelCollection pc = new ParallelCollection();
for (int i = 0; i < 21; i++)
if (ms[i] != null && playingCol.contains(ms[i]))
pc.add((AutoMS)ms[i].clone());
sequenceCol.add(pc);
}

private void removeSequence() {
if (sequenceCol.size() > 0) {
if (currentSequence >= sequenceCol.size()-1) currentSequence =
sequenceCol.size()-1;
sequenceCol.remove(currentSequence);
}
}

class SequencePlayable implements Playable {
public double play(double playTime, Composable thing) throws 
InterruptedException {
if ( currentSequence < sequenceCol.size() ) {
sequenceBehavior.setStart(currentSequence);
currentSequence++;
return playTime;
}
if ( currentSequence >= sequenceCol.size() ) {
stopSequence();
}
return playTime;
}
}

class SequenceBehavior implements Behavior {
private int start = 0;
public Composable choose(SequentialCollection collection) {
return collection.get(start);
}

public void setStart(int start) {
this.start = start;
}
}




More information about the jmsl mailing list