I think I finally have speaker playback that doesn't sound too skippy. Yay.

This commit is contained in:
Brendan Robert
2014-09-18 19:24:45 -05:00
parent dfef919769
commit f3245a4245

View File

@@ -99,8 +99,6 @@ public class Speaker extends Device {
// Number of samples available in output stream before playback happens (avoid extra blocking) // Number of samples available in output stream before playback happens (avoid extra blocking)
// static int MIN_PLAYBACK_BUFFER = BUFFER_SIZE / 2; // static int MIN_PLAYBACK_BUFFER = BUFFER_SIZE / 2;
static int MIN_PLAYBACK_BUFFER = 64; static int MIN_PLAYBACK_BUFFER = 64;
// Number of samples in buffer to wait until playback (avoid underrun)
private int MIN_SAMPLE_PLAYBACK = 64;
/** /**
* Playback volume (should be < 1423) * Playback volume (should be < 1423)
*/ */
@@ -110,7 +108,7 @@ public class Speaker extends Device {
* Number of idle cycles until speaker playback is deactivated * Number of idle cycles until speaker playback is deactivated
*/ */
@ConfigurableField(name = "Idle cycles before sleep", shortName = "idle") @ConfigurableField(name = "Idle cycles before sleep", shortName = "idle")
public static int MAX_IDLE_CYCLES = 100000; public static int MAX_IDLE_CYCLES = 2000000;
/** /**
* Java sound output * Java sound output
*/ */
@@ -189,11 +187,16 @@ public class Speaker extends Device {
*/ */
@Override @Override
public void resume() { public void resume() {
if (sdl != null && isRunning()) return;
System.out.println("Resuming speaker sound"); System.out.println("Resuming speaker sound");
sdl = null; sdl = null;
try { try {
sdl = computer.getMotherboard().mixer.getLine(this); sdl = computer.getMotherboard().mixer.getLine(this);
sdl.start(); sdl.start();
counter = 0;
idleCycles = 0;
level = 0;
bufferPos = 0;
setRun(true); setRun(true);
playbackTimer = new Timer(); playbackTimer = new Timer();
playbackTimer.scheduleAtFixedRate(new TimerTask() { playbackTimer.scheduleAtFixedRate(new TimerTask() {
@@ -201,16 +204,10 @@ public class Speaker extends Device {
public void run() { public void run() {
playCurrentBuffer(); playCurrentBuffer();
} }
}, 25, 50); }, 10, 30);
} catch (LineUnavailableException ex) { } catch (LineUnavailableException ex) {
System.out.println("ERROR: Could not output sound: " + ex.getMessage()); System.out.println("ERROR: Could not output sound: " + ex.getMessage());
} }
if (sdl != null) {
counter = 0;
idleCycles = 0;
level = 0;
bufferPos = 0;
}
} }
public void playCurrentBuffer() { public void playCurrentBuffer() {
@@ -224,7 +221,6 @@ public class Speaker extends Device {
} }
secondaryBuffer = buffer; secondaryBuffer = buffer;
sdl.write(buffer, 0, len); sdl.write(buffer, 0, len);
sdl.start();
} }
/** /**
@@ -310,15 +306,13 @@ public class Speaker extends Device {
if (primaryBuffer != null && secondaryBuffer != null) { if (primaryBuffer != null && secondaryBuffer != null) {
return; return;
} }
BUFFER_SIZE = 10000 * (SoundMixer.BITS >> 3); BUFFER_SIZE = 20000 * (SoundMixer.BITS >> 3);
MIN_SAMPLE_PLAYBACK = SoundMixer.BITS * 8;
primaryBuffer = new byte[BUFFER_SIZE]; primaryBuffer = new byte[BUFFER_SIZE];
secondaryBuffer = new byte[BUFFER_SIZE]; secondaryBuffer = new byte[BUFFER_SIZE];
} }
@Override @Override
public void attach() { public void attach() {
reconfigure();
configureListener(); configureListener();
resume(); resume();
} }