Permalink
Browse files

Fixed previous data replay bug. Still major latency issues.

  • Loading branch information...
1 parent 6118978 commit 0c43025c107d02857e6e55b7577295b0ac391d4d @finnkuusisto committed Feb 26, 2012
Showing with 30 additions and 23 deletions.
  1. +28 −21 src/kuusisto/tinysound/TinySound.java
  2. +2 −2 src/kuusisto/tinysound/internal/UpdateRunner.java
@@ -109,7 +109,7 @@ public static void init(int updateRate, boolean autoUpdate) {
if (TinySound.inited) {
return;
}
- //first try to open a line to the speakers
+ //try to open a line to the speakers
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
TinySound.FORMAT);
if (!javax.sound.sampled.AudioSystem.isLineSupported(info)) {
@@ -129,18 +129,19 @@ public static void init(int updateRate, boolean autoUpdate) {
//now initialize the other stuff
TinySound.mixer = new Mixer();
TinySound.updateRate = updateRate;
- //make the buffer the size needed for update rate
- double bytesPerUpdate = (((double)TinySound.FORMAT.getSampleRate() *
- TinySound.FORMAT.getFrameSize()) / updateRate);
- int bufSize = (int)bytesPerUpdate + 1;
- bufSize += (bufSize % 2 == 0) ? 0 : 1; //make it even
+ //compute the buffer size needed for update rate
+ double framesPerUpdate = TinySound.FORMAT.getSampleRate() / updateRate;
+ double bytesPerUpdate =
+ framesPerUpdate * TinySound.FORMAT.getFrameSize();
+ int bufSize = //make sure it's a multiple of frame size
+ ((int)framesPerUpdate + 1) * TinySound.FORMAT.getFrameSize();
TinySound.audioBuffer = new byte[bufSize];
TinySound.numBytesRead = 0;
//now calculate frame drift per update
double byteDriftPerUpdate = (double)bufSize - bytesPerUpdate;
TinySound.driftFramesPerUpdate = byteDriftPerUpdate /
TinySound.FORMAT.getFrameSize();
- if (TinySound.driftFramesPerUpdate <= 0.0001) {
+ if (TinySound.driftFramesPerUpdate < 0.0) { //just in case
TinySound.driftFramesPerUpdate = 0.0;
}
TinySound.driftFramesAccrued = 0.0;
@@ -161,6 +162,12 @@ public static void shutdown() {
return;
}
TinySound.inited = false;
+ //stop the auto-updater if running
+ if (TinySound.autoUpdate) {
+ TinySound.autoUpdater.stop();
+ TinySound.autoUpdater = null;
+ TinySound.autoUpdate = false;
+ }
TinySound.outLine.stop();
TinySound.outLine.flush();
TinySound.mixer.clearMusic();
@@ -171,12 +178,6 @@ public static void shutdown() {
TinySound.numBytesRead = 0;
TinySound.driftFramesPerUpdate = 0.0;
TinySound.driftFramesAccrued = 0.0;
- //stop the auto-updater if running
- if (TinySound.autoUpdate) {
- TinySound.autoUpdater.stop();
- TinySound.autoUpdater = null;
- TinySound.autoUpdate = false;
- }
}
/**
@@ -222,12 +223,12 @@ public static void update(boolean fillNextBuffer) {
if (TinySound.numBytesRead <= 0) {
TinySound.fillAudioBuffer();
}
- if (TinySound.numBytesRead <= 0) {
- return;
- }
//and write to the speakers
- TinySound.outLine.write(TinySound.audioBuffer, 0,
- TinySound.numBytesRead);
+ int numBytesWritten = 0;
+ while (numBytesWritten < TinySound.numBytesRead) {
+ numBytesWritten = TinySound.outLine.write(TinySound.audioBuffer,
+ numBytesWritten, TinySound.numBytesRead);
+ }
TinySound.numBytesRead = 0;
//now refill the buffer if desired
if (fillNextBuffer) {
@@ -239,6 +240,9 @@ public static void update(boolean fillNextBuffer) {
* Fills the audio buffer with audio data. Also handles drift compensation.
*/
private static void fillAudioBuffer() {
+ if (!TinySound.inited) {
+ return;
+ }
//default to full buffer
int length = TinySound.audioBuffer.length;
//check for full frames of drift
@@ -252,10 +256,13 @@ private static void fillAudioBuffer() {
//now read the bytes
TinySound.numBytesRead = TinySound.mixer.read(TinySound.audioBuffer,
length);
- //accrue drift if no compensation
- if (length == TinySound.audioBuffer.length) {
- TinySound.driftFramesAccrued += TinySound.driftFramesPerUpdate;
+ //fill remainder with zeroes
+ for (int i = TinySound.numBytesRead; i < length; i++) {
+ TinySound.audioBuffer[i] = 0;
}
+ TinySound.numBytesRead = length;
+ //accrue drift
+ TinySound.driftFramesAccrued += TinySound.driftFramesPerUpdate;
}
/**
@@ -63,7 +63,7 @@ public void stop() {
@Override
public void run() {
this.running.set(true);
- long nanosPerUpdate = 1000000000 / this.updateRate;
+ long nanosPerUpdate = 1000000000L / this.updateRate;
long lastUpdate = 0;
//keep running until told to stop
while (this.running.get()) {
@@ -73,7 +73,7 @@ public void run() {
}
//give the CPU back to the OS for a bit
try {
- Thread.sleep(0, 500000);
+ Thread.sleep(0, 100000);
} catch (InterruptedException e) {}
}
}

0 comments on commit 0c43025

Please sign in to comment.