Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benmargolin/5089a37d3eaf4d5c1192e3ceb8d7500c to your computer and use it in GitHub Desktop.
Save benmargolin/5089a37d3eaf4d5c1192e3ceb8d7500c to your computer and use it in GitHub Desktop.
Take the average value of part of a ByteBuffer (that is actually 16 bit little endian) easily
// posBefore = where in the mBuffer the new data started (before put)
ShortBuffer avgBuf = ((ByteBuffer) mBuffer.duplicate().flip().position(posBefore))
.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
long total = 0;
while (avgBuf.remaining() > 0) {
total += avgBuf.get() & 0xffff; // Because unsigned.
}
sLastDataAverage = total / numOfBytes / 2 / 65535f;
Logger.v(TAG, "Analysis of audio data: avg = %f", sLastDataAverage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment