From: Einar Pehrson Date: Tue, 13 May 2003 10:48:34 +0000 (+0000) Subject: Fixed a play thread that almost works X-Git-Url: https://ruin.nu/git/?p=moosique.git;a=commitdiff_plain;h=5380690b9fc56b683d15765382669d79c50d3414 Fixed a play thread that almost works --- diff --git a/MooGUI.java b/MooGUI.java index 17af222..065267e 100644 --- a/MooGUI.java +++ b/MooGUI.java @@ -114,9 +114,9 @@ public class MooGUI extends JFrame { * Calls on the main view to update the track views, * and on the toolbar to update the progress indicator. */ - public void update(){ - view.update(); - toolbar.updateProgInd(); + public void update(long tickPosition){ + view.update(tickPosition); + toolbar.updateProgInd(tickPosition); } class MooGUICloser extends WindowAdapter { diff --git a/MooNoteElement.java b/MooNoteElement.java index dd31c86..3b08887 100644 --- a/MooNoteElement.java +++ b/MooNoteElement.java @@ -20,7 +20,7 @@ public class MooNoteElement extends JPanel { * @param mn the note that will be graphically represented * @param rows the number of rows that the note will occupy */ - public MooNoteElement (MooNote mn, int rows) { + public MooNoteElement (MooNote mn) { note = mn; columns = mn.getDuration() / 24; } diff --git a/MooToolbar.java b/MooToolbar.java index eb8ca43..54b90db 100644 --- a/MooToolbar.java +++ b/MooToolbar.java @@ -69,14 +69,14 @@ public class MooToolbar extends JToolBar { /** * Updates the progress indicator. + * @param tickPosition the tick position to visualize */ - public void updateProgInd() { - long pos = Moosique.getSequencer().getTickPosition(); + public void updateProgInd(long tickPosition) { int ticksPerBeat = Moosique.getSequence().getResolution(); int beatsPerMeasure = 4; - long measures = pos / (beatsPerMeasure * ticksPerBeat); - long beats = (pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat; - long ticks = pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat; + long measures = tickPosition / (beatsPerMeasure * ticksPerBeat); + long beats = (tickPosition - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat; + long ticks = tickPosition - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat; measureValue.setText(Long.toString(measures)); beatsValue.setText(Long.toString(beats)); ticksValue.setText(Long.toString(ticks)); @@ -113,27 +113,21 @@ public class MooToolbar extends JToolBar { class MooMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (((JButton)e.getSource()).getToolTipText() == "Play") { - Moosique.play(); playpause.setIcon(pauseIcon); playpause.setToolTipText("Pause"); - while(Moosique.getSequencer().isRunning()) { - Moosique.getGUI().update(); - } + Moosique.play(); } else if (((JButton)e.getSource()).getToolTipText() == "Pause") { - Moosique.pause(); playpause.setIcon(playIcon); playpause.setToolTipText("Resume"); + Moosique.pause(); } else if (((JButton)e.getSource()).getToolTipText() == "Resume") { - Moosique.resume(); playpause.setIcon(pauseIcon); playpause.setToolTipText("Pause"); - while(Moosique.getSequencer().isRunning()) { - Moosique.getGUI().update(); - } + Moosique.resume(); } else if (((JButton)e.getSource()).getToolTipText() == "Stop") { - Moosique.stop(); playpause.setIcon(playIcon); playpause.setToolTipText("Play"); + Moosique.stop(); } } diff --git a/MooTrackView.java b/MooTrackView.java index 8138442..c7850fe 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -59,7 +59,7 @@ public class MooTrackView extends JPanel implements ActionListener { /** * Updates the track view. */ - public void update() { + public void update(long tickPosition) { repaint(); } diff --git a/MooView.java b/MooView.java index 8223eca..a9679ff 100644 --- a/MooView.java +++ b/MooView.java @@ -74,10 +74,10 @@ public class MooView extends JPanel { /** * Calls on each track view to update itself. */ - public void update() { + public void update(long tickPosition) { Component[] comps = getComponents(); for (int i = 0; i < comps.length; i++) { - if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(); + if(comps[i] instanceof MooTrackView) ((MooTrackView)comps[i]).update(tickPosition); } } diff --git a/Moosique.java b/Moosique.java index bc821ba..b4c4259 100644 --- a/Moosique.java +++ b/Moosique.java @@ -23,6 +23,7 @@ public class Moosique { private static String filename, fileArg; private static long position; private static boolean makeGUI = true, isEdited; + private static Thread player; /** * Starts the application. @@ -151,7 +152,8 @@ public class Moosique { */ public static void play() { sequencer.setTickPosition(position); - sequencer.start(); + resume(); + } /** @@ -159,6 +161,7 @@ public class Moosique { */ public static void pause() { sequencer.stop(); + player.destroy(); } /** @@ -166,6 +169,17 @@ public class Moosique { */ public static void resume() { sequencer.start(); + + // Disables input to volatile components + // gui.disable(); + + // Creates the visualization thread and starts it. + player = new Thread () { + public void run() { + gui.update(sequencer.getTickPosition()); + } + }; + player.start(); } /** @@ -174,6 +188,8 @@ public class Moosique { public static void stop() { sequencer.stop(); sequencer.setTickPosition(position); + player.destroy(); + gui.update((long)0); } /**