From 1e83aad48d4a454c69b13167f075a280a4ac61c0 Mon Sep 17 00:00:00 2001 From: Brian Matzon Date: Sun, 3 Oct 2004 02:47:10 +0000 Subject: [PATCH] got boredm spiced it up --- src/java/org/lwjgl/test/fmod3/NetTest.java | 575 +++++++++++++-------- 1 file changed, 361 insertions(+), 214 deletions(-) diff --git a/src/java/org/lwjgl/test/fmod3/NetTest.java b/src/java/org/lwjgl/test/fmod3/NetTest.java index 78a72512..99783666 100644 --- a/src/java/org/lwjgl/test/fmod3/NetTest.java +++ b/src/java/org/lwjgl/test/fmod3/NetTest.java @@ -32,20 +32,28 @@ package org.lwjgl.test.fmod3; import java.awt.Button; +import java.awt.Canvas; +import java.awt.Choice; import java.awt.Color; import java.awt.Dialog; import java.awt.FlowLayout; import java.awt.Frame; +import java.awt.GradientPaint; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; import java.awt.Label; import java.awt.Panel; -import java.awt.TextField; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.nio.FloatBuffer; import java.nio.IntBuffer; +import javax.swing.BoxLayout; + import org.lwjgl.BufferUtils; import org.lwjgl.fmod3.FMOD; import org.lwjgl.fmod3.FMODException; @@ -60,57 +68,315 @@ import org.lwjgl.fmod3.FSoundStream; */ public class NetTest { - private Frame frame; - private boolean initialized; - private Thread fmodThread; - private volatile boolean running; - private int channel = -1; - private boolean paused = true; + /** 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; - public NetTest(Frame frame) { - this.frame = frame; - } + /** 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 + */ + private void dispose() { + if (initialized) { + stop(); + FSound.FSOUND_Close(); + FMOD.destroy(); + } + } + + /** + * Plays the supplied URL + * + * @param url URK to play + */ + protected void play(final String url) { + // do initialize if needed + if (!initialized) { + frame.setTitle("Initializing..."); + if (!initialize()) { + frame.setTitle("LWJGL Fmod streaming player"); + return; + } + } + + // if already running, stop + if (fmodThread != null) { + stop(); + } + + // actual audi thread that starts playing + fmodThread = new Thread() { + public void run() { + // 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 + // ============================================ + if (stream != null) { + IntBuffer status = BufferUtils.createIntBuffer(4); + while (running) { + + // 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 + 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 + 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 + spectrumCanvas.repaint(); + + // dont use all resources! + pause(25); + } + + // exiting. spin on stop/close since we're non-blocking + while (!FSound.FSOUND_Stream_Stop(stream)) { + pause(10); + } + while (!FSound.FSOUND_Stream_Close(stream)) { + pause(10); + } + channel = -1; + } else { + error("Unable to play: " + url + ". Error: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError())); + } + // -------------------------------------------- + + // we're done, nuke leftovers + spectrumCanvas.repaint(); + } + }; + fmodThread.start(); + } + + /** + * @return enum name for supplied stream open state + */ + protected String getNameFor(int i) { + switch (i) { + case FSound.FSOUND_STREAM_NET_NOTCONNECTED: + return "FSOUND_STREAM_NET_NOTCONNECTED"; + case FSound.FSOUND_STREAM_NET_CONNECTING: + return "FSOUND_STREAM_NET_CONNECTING"; + case FSound.FSOUND_STREAM_NET_BUFFERING: + return "FSOUND_STREAM_NET_BUFFERING"; + case FSound.FSOUND_STREAM_NET_READY: + return "FSOUND_STREAM_NET_READY"; + case FSound.FSOUND_STREAM_NET_ERROR: + return "FSOUND_STREAM_NET_ERROR"; + } + return ""; + } + + /** + * Stops the playing + */ + protected void stop() { + // update title + if (frame.isVisible()) { + frame.setTitle("LWJGL Fmod streaming player: stopping..."); + } + + // nuke the thread, but wait for it to finish! + running = false; + if (fmodThread != null) { + try { + fmodThread.join(); + } catch (InterruptedException inte) { + } + fmodThread = null; + } + + // update title + if (frame.isVisible()) { + frame.setTitle("LWJGL Fmod streaming player"); + } + } + + /** + * Initializes the test + * + * @return true if initialization was successfull + */ + protected boolean initialize() { + if (!initialized) { + // create FMOD first! + try { + FMOD.create(); + } catch (FMODException fmode) { + error(fmode.getMessage()); + fmode.printStackTrace(); + return false; + } + + // 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) + FSound.FSOUND_DSP_SetActive(FSound.FSOUND_DSP_GetFFTUnit(), true); + + // 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 + */ + private void error(String string) { + 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 + * + * @param i how long time to pause + */ + private static void pause(long i) { + try { + Thread.sleep(i); + } catch (InterruptedException inte) { + } + } - public static void main(String[] args) { - - final Frame frame = new Frame("LWJGL Fmod streaming player"); - final NetTest netTest = new NetTest(frame); - final boolean playing = false; - - final TextField txtField; - final Button btnPlay; + /** + * 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 - Panel panel = new Panel(); - panel.setLayout(new FlowLayout()); - panel.add(new Label("URL:")); - panel.add(txtField = new TextField("http://207.200.96.227:8038/", 60)); - panel.add(btnPlay = new Button("Play")); - panel.add(btnStop = new Button("Stop")); - panel.setBackground(new Color(0x99, 0x99, 0x99)); + 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)); - frame.add(panel); + // add list of known urls + for(int i=0; i 0) { - FSound.FSOUND_SetPaused(channel, paused = !paused); - - if(paused) { - frame.setTitle("LWJGL Fmod streaming player: Paused"); - } - } - } - - /** - * - */ - protected void play(final String url) { - if(!initialized) { - frame.setTitle("Initializing..."); - if(!initialize()) { - frame.setTitle("LWJGL Fmod streaming player"); - return; - } - } - - if(fmodThread != null) { - stop(); - } - - - fmodThread = new Thread() { - public void run() { - frame.setTitle("Opening [" + url + "]"); - running = true; - FSoundStream stream = FSound.FSOUND_Stream_Open(url, FSound.FSOUND_NORMAL | FSound.FSOUND_NONBLOCKING, 0, 0); - - if (stream != null) { - IntBuffer status = BufferUtils.createIntBuffer(4); - while(running) { - if(channel < 0) { - channel = FSound.FSOUND_Stream_PlayEx(FSound.FSOUND_FREE, stream, null, true); - FSound.FSOUND_SetPaused(channel, false); - paused = false; - } - - int openstate = FSound.FSOUND_Stream_GetOpenState(stream); - if ((openstate == -1) || (openstate == -3)) { - error("failed to open stream!: " + FSound.FSOUND_Stream_Net_GetLastServerStatus()); - break; - } - - FSound.FSOUND_Stream_Net_GetStatus(stream, status); - - if(!paused) { - frame.setTitle("Playing [state: " + getNameFor(status.get(0)) + ", buffer: " + status.get(1) + ", bitrate: " + status.get(2) + "]"); - } - pause(10); - } - - while(!FSound.FSOUND_Stream_Stop(stream)) { - pause(10); - } - while(!FSound.FSOUND_Stream_Close(stream)) { - pause(10); - } - channel = -1; - } else { - error("Unable to play: " + url + ". Error: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError())); - } - } - }; - fmodThread.start(); - } - - /** - * @param i - * @return - */ - protected String getNameFor(int i) { - switch(i) { - case FSound.FSOUND_STREAM_NET_NOTCONNECTED: - return "FSOUND_STREAM_NET_NOTCONNECTED"; - case FSound.FSOUND_STREAM_NET_CONNECTING: - return "FSOUND_STREAM_NET_CONNECTING"; - case FSound.FSOUND_STREAM_NET_BUFFERING: - return "FSOUND_STREAM_NET_BUFFERING"; - case FSound.FSOUND_STREAM_NET_READY: - return "FSOUND_STREAM_NET_READY"; - case FSound.FSOUND_STREAM_NET_ERROR: - return "FSOUND_STREAM_NET_ERROR"; - } - return ""; - } - - /** - * - */ - protected void stop() { - if(frame.isVisible()) { - frame.setTitle("LWJGL Fmod streaming player: stopping..."); - } - - running = false; - if(fmodThread != null) { - try { - fmodThread.join(); - } catch (InterruptedException inte) { - } - fmodThread = null; - } - - if(frame.isVisible()) { - frame.setTitle("LWJGL Fmod streaming player"); - } - } - - protected boolean initialize() { - if(!initialized) { - try { - FMOD.create(); - } catch (FMODException fmode) { - error(fmode.getMessage()); - fmode.printStackTrace(); - return false; - } - - if (!FSound.FSOUND_Init(44100, 32, 0)) { - error("Failed to initialize FMOD: " + FMOD.FMOD_ErrorString(FSound.FSOUND_GetError())); - return false; - } - - FSound.FSOUND_Stream_SetBufferSize(500); - FSound.FSOUND_Stream_Net_SetBufferProperties(64000, 60, 80); - initialized = true; - } - return initialized; } + - /** - * @param string - */ - private void error(String string) { - 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); - } - - /** - * @param i + /** + * Simplish spectrum + * @author Brian Matzon + * @version $Revision$ */ - private static void pause(long i) { - try { - Thread.sleep(i); - } catch (InterruptedException inte) { - } - } + 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); + 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 (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); + for (int i = 0; i < 256; i++) { + int height = (int) (getHeight() * 4.0 * spectrum.get(i)); + bufferGraphics.fillRect(x, getHeight() - height, 2, height); + x+=2; + } + } + + // map offscreen onscreen + g.drawImage(bufferImage, 0, 0, this); + } + + /** + * Just draw the sucker instead of default update + */ + public void update(Graphics g) { + paint(g); + } + } } \ No newline at end of file