Skip to content

Instantly share code, notes, and snippets.

@niusounds
Created October 13, 2020 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niusounds/4bc7abf964afcc2ac3df7bda65282592 to your computer and use it in GitHub Desktop.
Save niusounds/4bc7abf964afcc2ac3df7bda65282592 to your computer and use it in GitHub Desktop.
細切れのデータを一旦バッファに貯めてからオーディオデバイスへ送りたかった
class AudioTrack {
private val bufferSize: Int // some value depends on environment
private val writeBuffer = FloatArray(bufferSize / 4) // actual buffer to send to audio device
private var writeOffset = 0 // current offset to write next buffer
fun write(data: FloatArray, offset: Int, size: Int) {
val remainingWriteBufferSize = writeBuffer.size - writeOffset
if (remainingWriteBufferSize >= size) {
System.arraycopy(data, offset, writeBuffer, writeOffset, size)
writeOffset += size
} else {
val firstWriteSize = remainingWriteBufferSize
System.arraycopy(data, offset, writeBuffer, writeOffset, firstWriteSize)
audioDevice.write(writeBuffer, 0, writeBuffer.size) // send to audio device
val secondWriteOffset = offset + firstWriteSize
val secondWriteSize = size - firstWriteSize
System.arraycopy(data, secondWriteOffset, writeBuffer, 0, secondWriteSize)
writeOffset = secondWriteSize
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment