Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the sleep logic for streaming as per sample rate #279

Merged
merged 11 commits into from
Aug 4, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public void onCompleted() {
.build();
requestObserver.onNext(request);
// To simulate real-time audio, sleep after sending each audio buffer.
// For 16000 Hz sample rate, sleep 100 milliseconds.
Thread.sleep(samplingRate / 160);
// Sleep 100ms
Thread.sleep(100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approximating realtime is dependent on the samplingRate, so you can't hard-code 100ms unless you're also hard-coding the samplingRate.

The calculation here should instead be dependent on samplingRate, buffer size, and encoding. Since encoding is hard-coded to 2 bytes per sample, you should sleep for:

int BYTES_PER_SAMPLE = 2;  // because LINEAR16 - ie 16 bits per sample, at 8 bits per byte
int BYTES_PER_BUFFER = 3200;
int SAMPLES_PER_BUFFER = BYTES_PER_BUFFER / BYTES_PER_SAMPLE; // 1600

int samplesPerMillisecond = samplingRate / 1000;
int millisecondsPerBuffer = SAMPLES_PER_BUFFER / samplesPerMillisecond;
Thread.sleep(millisecondsPerBuffer);

which for 16000 Hz is 1600 / 16 = 100 (as expected), and for 32000 Hz is 1600 / 32 = 50 (which is faster than the original 32000 / 160 = 200). I suspect there was just an error in the original calculation. (feel free to check my math as well - but at least it gives a shorter sleep duration, which is what you'd expect if you increase the sample rate while keeping the buffer size the same)

}
logger.info("Sent " + totalBytes + " bytes from audio file: " + file);
} catch (RuntimeException e) {
Expand Down