From 53dfc0da74479d5c2e5eba4ff2ac0fa2b81224fb Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Sun, 3 Oct 2004 09:00:36 +0000 Subject: [PATCH] reindent/format --- src/java/org/lwjgl/test/fmod3/NetTest.java | 418 ++++++++++----------- 1 file changed, 207 insertions(+), 211 deletions(-) diff --git a/src/java/org/lwjgl/test/fmod3/NetTest.java b/src/java/org/lwjgl/test/fmod3/NetTest.java index 99783666..7aedab71 100644 --- a/src/java/org/lwjgl/test/fmod3/NetTest.java +++ b/src/java/org/lwjgl/test/fmod3/NetTest.java @@ -67,47 +67,41 @@ import org.lwjgl.fmod3.FSoundStream; * @version $Revision$ */ public class NetTest { - - /** Main frame */ - private Frame frame; - - /** Whether we're initialized */ - private boolean initialized; - - /** Audio thread */ - private Thread fmodThread; - - /** Whether we're running */ - private volatile boolean running; - - /** Channel we'll be playing on */ - private int channel = -1; - - /** Canvas with fancy stuff */ - public static Canvas spectrumCanvas; - - /** Buffer containing the spectrum (512 floats) */ - public static FloatBuffer spectrum; - - /** List of known urls (monkeyradio, di streams) */ - public static String[] urls = new String[] { - "http://207.200.96.227:8038/", - "http://205.188.234.65:8020", - "http://64.236.34.67:80/stream/1006", - "http://64.236.34.67:80/stream/1003", - "http://205.188.234.65:8026", - "http://64.236.34.67:80/stream/2004", - "http://64.236.34.67:80/stream/1019", - "http://64.236.34.67:80/stream/2005"}; - - /** - * Creates a new NetTest - * @param frame parent frame - */ + + /** Main frame */ + private Frame frame; + + /** Whether we're initialized */ + private boolean initialized; + + /** Audio thread */ + private Thread fmodThread; + + /** Whether we're running */ + private volatile boolean running; + + /** Channel we'll be playing on */ + private int channel = -1; + + /** Canvas with fancy stuff */ + public static Canvas spectrumCanvas; + + /** Buffer containing the spectrum (512 floats) */ + public static FloatBuffer spectrum; + + /** List of known urls (monkeyradio, di streams) */ + public static String[] urls = new String[] { "http://207.200.96.227:8038/", "http://205.188.234.65:8020", + "http://64.236.34.67:80/stream/1006", "http://64.236.34.67:80/stream/1003", "http://205.188.234.65:8026", + "http://64.236.34.67:80/stream/2004", "http://64.236.34.67:80/stream/1019", "http://64.236.34.67:80/stream/2005"}; + + /** + * Creates a new NetTest + * @param frame parent frame + */ public NetTest(Frame frame) { this.frame = frame; } - + /** * Disposes NetTest */ @@ -118,14 +112,14 @@ public class NetTest { FMOD.destroy(); } } - + /** * Plays the supplied URL - * - * @param url URK to play + * + * @param url URK to play */ protected void play(final String url) { - // do initialize if needed + // do initialize if needed if (!initialized) { frame.setTitle("Initializing..."); if (!initialize()) { @@ -133,54 +127,55 @@ public class NetTest { return; } } - - // if already running, stop + + // if already running, stop if (fmodThread != null) { stop(); } - - // actual audi thread that starts playing + + // actual audi thread that starts playing fmodThread = new Thread() { + public void run() { - // Open the url and prepare to play - // ============================================ + // Open the url and prepare to play + // ============================================ frame.setTitle("Opening [" + url + "]"); running = true; FSoundStream stream = FSound.FSOUND_Stream_Open(url, FSound.FSOUND_NORMAL | FSound.FSOUND_NONBLOCKING, 0, 0); - // -------------------------------------------- - - // With a stream in hand, loop untill we're told to stop - // ============================================ + // -------------------------------------------- + + // With a stream in hand, loop untill we're told to stop + // ============================================ if (stream != null) { IntBuffer status = BufferUtils.createIntBuffer(4); while (running) { - - // get channel, if we haven't got one already + + // get channel, if we haven't got one already if (channel < 0) { channel = FSound.FSOUND_Stream_PlayEx(FSound.FSOUND_FREE, stream, null, false); } - - // query open state of stream + + // query open state of stream int openstate = FSound.FSOUND_Stream_GetOpenState(stream); if ((openstate == -1) || (openstate == -3)) { error("failed to open stream!: " + FSound.FSOUND_Stream_Net_GetLastServerStatus()); break; } - - // get status of stream + + // get status of stream FSound.FSOUND_Stream_Net_GetStatus(stream, status); - + frame.setTitle("Playing [state: " + getNameFor(status.get(0)) + ", buffer: " + status.get(1) - + ", bitrate: " + status.get(2) + "]"); - - // repaint spectrum + + ", bitrate: " + status.get(2) + "]"); + + // repaint spectrum spectrumCanvas.repaint(); - - // dont use all resources! + + // dont use all resources! pause(25); } - - // exiting. spin on stop/close since we're non-blocking + + // exiting. spin on stop/close since we're non-blocking while (!FSound.FSOUND_Stream_Stop(stream)) { pause(10); } @@ -191,15 +186,15 @@ public class NetTest { } else { error("Unable to play: " + url + ". Error: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError())); } - // -------------------------------------------- - - // we're done, nuke leftovers + // -------------------------------------------- + + // we're done, nuke leftovers spectrumCanvas.repaint(); } }; fmodThread.start(); } - + /** * @return enum name for supplied stream open state */ @@ -218,17 +213,17 @@ public class NetTest { } return ""; } - + /** * Stops the playing */ protected void stop() { - // update title + // update title if (frame.isVisible()) { frame.setTitle("LWJGL Fmod streaming player: stopping..."); } - - // nuke the thread, but wait for it to finish! + + // nuke the thread, but wait for it to finish! running = false; if (fmodThread != null) { try { @@ -237,21 +232,21 @@ public class NetTest { } fmodThread = null; } - - // update title + + // update title if (frame.isVisible()) { frame.setTitle("LWJGL Fmod streaming player"); } } - - /** - * Initializes the test - * - * @return true if initialization was successfull - */ + + /** + * Initializes the test + * + * @return true if initialization was successfull + */ protected boolean initialize() { if (!initialized) { - // create FMOD first! + // create FMOD first! try { FMOD.create(); } catch (FMODException fmode) { @@ -259,28 +254,28 @@ public class NetTest { fmode.printStackTrace(); return false; } - - // initialize fsound + + // initialize fsound if (!FSound.FSOUND_Init(44100, 32, 0)) { error("Failed to initialize FMOD: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError())); return false; } - - // get fft unit and activate it (else, we can't get the spectrum) + + // get fft unit and activate it (else, we can't get the spectrum) FSound.FSOUND_DSP_SetActive(FSound.FSOUND_DSP_GetFFTUnit(), true); - - // get the spectrum - spectrum = FSound.FSOUND_DSP_GetSpectrum(); - + + // get the spectrum + spectrum = FSound.FSOUND_DSP_GetSpectrum(); + // setup buffers FSound.FSOUND_Stream_SetBufferSize(100); FSound.FSOUND_Stream_Net_SetBufferProperties(64000, 95, 95); - + initialized = true; } return initialized; } - + /** * @param string */ @@ -288,23 +283,24 @@ public class NetTest { final Dialog dialog = new Dialog(frame, "Error", true); dialog.add(new Label(string)); dialog.pack(); - + dialog.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { dialog.dispose(); } }); - + // setup dialog int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - dialog.getWidth()) / 2; int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - dialog.getHeight()) / 2; dialog.setLocation(x, y); dialog.setVisible(true); } - + /** - * Pause calling thread for i milliseconds - * + * Pause calling thread for i milliseconds + * * @param i how long time to pause */ private static void pause(long i) { @@ -313,135 +309,135 @@ public class NetTest { } catch (InterruptedException inte) { } } - - /** - * Executes the NetTest - * @param args - */ - public static void main(String[] args) { - - // UI and instance stuff - final Frame frame = new Frame("LWJGL Fmod streaming player"); - final Choice choice; - final Button btnPlay; - final Button btnStop; - final boolean playing = false; - final NetTest netTest = new NetTest(frame); - - // main panel - frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS)); - final Panel panel[] = { new Panel(), new Panel()}; - panel[0].setLayout(new FlowLayout()); - panel[0].add(new Label("URL:")); - panel[0].add(choice = new Choice()); - panel[0].add(btnPlay = new Button("Play")); - panel[0].add(btnStop = new Button("Stop")); - panel[1].add(spectrumCanvas = netTest.new SpectrumCanvas()); - panel[0].setBackground(new Color(0x99, 0x99, 0x99)); - panel[1].setBackground(new Color(0x99, 0x99, 0x99)); - - // add list of known urls - for(int i=0; i - * @version $Revision$ - */ - class SpectrumCanvas extends Canvas { - - /** Offscreen image for that flickerless feel! (TM) */ - Image bufferImage; - - /** Graphics context for buffer */ - Graphics2D bufferGraphics; - - /** Background color */ - Color bgColor = new Color(0x99, 0x99, 0x99); - - /** Gradient paint */ - GradientPaint gp; - - /** - * Called to paint the canvas - */ + + /** + * Executes the NetTest + * @param args + */ + public static void main(String[] args) { + + // UI and instance stuff + final Frame frame = new Frame("LWJGL Fmod streaming player"); + final Choice choice; + final Button btnPlay; + final Button btnStop; + final boolean playing = false; + final NetTest netTest = new NetTest(frame); + + // main panel + frame.setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS)); + final Panel panel[] = { new Panel(), new Panel()}; + panel[0].setLayout(new FlowLayout()); + panel[0].add(new Label("URL:")); + panel[0].add(choice = new Choice()); + panel[0].add(btnPlay = new Button("Play")); + panel[0].add(btnStop = new Button("Stop")); + panel[1].add(spectrumCanvas = netTest.new SpectrumCanvas()); + panel[0].setBackground(new Color(0x99, 0x99, 0x99)); + panel[1].setBackground(new Color(0x99, 0x99, 0x99)); + + // add list of known urls + for (int i = 0; i < urls.length; i++) { + choice.add(urls[i]); + } + + // add spectrum panel, set size and pack + frame.add(panel[0]); + spectrumCanvas.setSize(512, 100); + frame.pack(); + + // Add listeners + // ===================================================== + frame.addWindowListener(new WindowAdapter() { + + public void windowClosing(WindowEvent e) { + frame.dispose(); + netTest.dispose(); + } + }); + + btnPlay.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + frame.add(panel[1]); + frame.pack(); + netTest.play(choice.getSelectedItem()); + } + }); + + btnStop.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + frame.remove(panel[1]); + frame.pack(); + netTest.stop(); + } + }); + // ----------------------------------------------------- + + // setup frame + int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - frame.getWidth()) / 2; + int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - frame.getHeight()) / 2; + frame.setLocation(x, y); + frame.setResizable(false); + frame.setVisible(true); + } + + /** + * Simplish spectrum + * @author Brian Matzon + * @version $Revision$ + */ + class SpectrumCanvas extends Canvas { + + /** Offscreen image for that flickerless feel! (TM) */ + Image bufferImage; + + /** Graphics context for buffer */ + Graphics2D bufferGraphics; + + /** Background color */ + Color bgColor = new Color(0x99, 0x99, 0x99); + + /** Gradient paint */ + GradientPaint gp; + + /** + * Called to paint the canvas + */ public void paint(Graphics g) { // create offscreen if (bufferImage == null) { - gp = new GradientPaint(0, 0, Color.RED, 0, getHeight(), Color.GREEN); + gp = new GradientPaint(0, 0, Color.RED, 0, getHeight(), Color.GREEN); bufferImage = createImage(getWidth(), getHeight()); bufferGraphics = (Graphics2D) bufferImage.getGraphics(); } - + // clear old bufferGraphics.setColor(bgColor); bufferGraphics.fillRect(0, 0, getWidth(), getHeight()); - - // if we have a spectrum, draw it + + // if we have a spectrum, draw it if (spectrum != null && NetTest.this.fmodThread != null) { - int x = 0; - - // for each spectrum value, draw a line according to its - // value (times 4, since we want higher frequencies to show up) - bufferGraphics.setPaint(gp); + int x = 0; + + // for each spectrum value, draw a line according to its + // value (times 4, since we want higher frequencies to show up) + bufferGraphics.setPaint(gp); for (int i = 0; i < 256; i++) { - int height = (int) (getHeight() * 4.0 * spectrum.get(i)); + int height = (int) (getHeight() * 4.0 * spectrum.get(i)); bufferGraphics.fillRect(x, getHeight() - height, 2, height); - x+=2; + x += 2; } } - - // map offscreen onscreen + + // map offscreen onscreen g.drawImage(bufferImage, 0, 0, this); } - - /** - * Just draw the sucker instead of default update - */ + + /** + * Just draw the sucker instead of default update + */ public void update(Graphics g) { paint(g); }