reindent/format

This commit is contained in:
Brian Matzon 2004-10-03 09:00:36 +00:00
parent 1e83aad48d
commit 53dfc0da74
1 changed files with 207 additions and 211 deletions

View File

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