got boredm spiced it up
This commit is contained in:
parent
ec0cb01da0
commit
1e83aad48d
|
@ -32,20 +32,28 @@
|
||||||
package org.lwjgl.test.fmod3;
|
package org.lwjgl.test.fmod3;
|
||||||
|
|
||||||
import java.awt.Button;
|
import java.awt.Button;
|
||||||
|
import java.awt.Canvas;
|
||||||
|
import java.awt.Choice;
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dialog;
|
import java.awt.Dialog;
|
||||||
import java.awt.FlowLayout;
|
import java.awt.FlowLayout;
|
||||||
import java.awt.Frame;
|
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.Label;
|
||||||
import java.awt.Panel;
|
import java.awt.Panel;
|
||||||
import java.awt.TextField;
|
|
||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.nio.FloatBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
|
import javax.swing.BoxLayout;
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
import org.lwjgl.fmod3.FMOD;
|
import org.lwjgl.fmod3.FMOD;
|
||||||
import org.lwjgl.fmod3.FMODException;
|
import org.lwjgl.fmod3.FMODException;
|
||||||
|
@ -60,68 +68,48 @@ import org.lwjgl.fmod3.FSoundStream;
|
||||||
*/
|
*/
|
||||||
public class NetTest {
|
public class NetTest {
|
||||||
|
|
||||||
|
/** Main frame */
|
||||||
private Frame frame;
|
private Frame frame;
|
||||||
private boolean initialized;
|
|
||||||
private Thread fmodThread;
|
|
||||||
private volatile boolean running;
|
|
||||||
private int channel = -1;
|
|
||||||
private boolean paused = true;
|
|
||||||
|
|
||||||
|
/** 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) {
|
public NetTest(Frame frame) {
|
||||||
this.frame = frame;
|
this.frame = frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
final Button btnStop;
|
|
||||||
|
|
||||||
// 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.add(panel);
|
|
||||||
frame.pack();
|
|
||||||
|
|
||||||
frame.addWindowListener(new WindowAdapter() {
|
|
||||||
public void windowClosing(WindowEvent e) {
|
|
||||||
frame.dispose();
|
|
||||||
netTest.dispose();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
btnPlay.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
netTest.play(txtField.getText());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
btnStop.addActionListener(new ActionListener() {
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Disposes NetTest
|
||||||
*/
|
*/
|
||||||
private void dispose() {
|
private void dispose() {
|
||||||
if (initialized) {
|
if (initialized) {
|
||||||
|
@ -132,22 +120,12 @@ public class NetTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Plays the supplied URL
|
||||||
*
|
*
|
||||||
*/
|
* @param url URK to play
|
||||||
protected void pause() {
|
|
||||||
if(running && channel > 0) {
|
|
||||||
FSound.FSOUND_SetPaused(channel, paused = !paused);
|
|
||||||
|
|
||||||
if(paused) {
|
|
||||||
frame.setTitle("LWJGL Fmod streaming player: Paused");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
protected void play(final String url) {
|
protected void play(final String url) {
|
||||||
|
// do initialize if needed
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
frame.setTitle("Initializing...");
|
frame.setTitle("Initializing...");
|
||||||
if (!initialize()) {
|
if (!initialize()) {
|
||||||
|
@ -156,40 +134,53 @@ public class NetTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if already running, stop
|
||||||
if (fmodThread != null) {
|
if (fmodThread != null) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// ============================================
|
||||||
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
|
||||||
|
// ============================================
|
||||||
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
|
||||||
if (channel < 0) {
|
if (channel < 0) {
|
||||||
channel = FSound.FSOUND_Stream_PlayEx(FSound.FSOUND_FREE, stream, null, true);
|
channel = FSound.FSOUND_Stream_PlayEx(FSound.FSOUND_FREE, stream, null, false);
|
||||||
FSound.FSOUND_SetPaused(channel, false);
|
|
||||||
paused = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
FSound.FSOUND_Stream_Net_GetStatus(stream, status);
|
FSound.FSOUND_Stream_Net_GetStatus(stream, status);
|
||||||
|
|
||||||
if(!paused) {
|
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) + "]");
|
||||||
}
|
|
||||||
pause(10);
|
// 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)) {
|
while (!FSound.FSOUND_Stream_Stop(stream)) {
|
||||||
pause(10);
|
pause(10);
|
||||||
}
|
}
|
||||||
|
@ -200,14 +191,17 @@ 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
|
||||||
|
spectrumCanvas.repaint();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fmodThread.start();
|
fmodThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param i
|
* @return enum name for supplied stream open state
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
protected String getNameFor(int i) {
|
protected String getNameFor(int i) {
|
||||||
switch (i) {
|
switch (i) {
|
||||||
|
@ -226,13 +220,15 @@ public class NetTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Stops the playing
|
||||||
*/
|
*/
|
||||||
protected void stop() {
|
protected void stop() {
|
||||||
|
// 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!
|
||||||
running = false;
|
running = false;
|
||||||
if (fmodThread != null) {
|
if (fmodThread != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -242,13 +238,20 @@ public class NetTest {
|
||||||
fmodThread = null;
|
fmodThread = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update title
|
||||||
if (frame.isVisible()) {
|
if (frame.isVisible()) {
|
||||||
frame.setTitle("LWJGL Fmod streaming player");
|
frame.setTitle("LWJGL Fmod streaming player");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the test
|
||||||
|
*
|
||||||
|
* @return true if initialization was successfull
|
||||||
|
*/
|
||||||
protected boolean initialize() {
|
protected boolean initialize() {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
|
// create FMOD first!
|
||||||
try {
|
try {
|
||||||
FMOD.create();
|
FMOD.create();
|
||||||
} catch (FMODException fmode) {
|
} catch (FMODException fmode) {
|
||||||
|
@ -257,13 +260,22 @@ public class NetTest {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
FSound.FSOUND_Stream_SetBufferSize(500);
|
// get fft unit and activate it (else, we can't get the spectrum)
|
||||||
FSound.FSOUND_Stream_Net_SetBufferProperties(64000, 60, 80);
|
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;
|
initialized = true;
|
||||||
}
|
}
|
||||||
return initialized;
|
return initialized;
|
||||||
|
@ -291,7 +303,9 @@ public class NetTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param i
|
* Pause calling thread for i milliseconds
|
||||||
|
*
|
||||||
|
* @param i how long time to pause
|
||||||
*/
|
*/
|
||||||
private static void pause(long i) {
|
private static void pause(long i) {
|
||||||
try {
|
try {
|
||||||
|
@ -299,4 +313,137 @@ public class NetTest {
|
||||||
} catch (InterruptedException inte) {
|
} 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<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 <brian@matzon.dk>
|
||||||
|
* @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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue