Skip to content

Commit 6516d0b

Browse files
committed
Implement onAudioChunkCaptured callback to get audio chunks during recording
1 parent fbcebc2 commit 6516d0b

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/FileWriter.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,30 @@ import java.nio.ByteBuffer
2929
import java.nio.ByteOrder
3030
import java.util.LinkedList
3131

32-
internal class FileWriter(private val outputStream: DataOutputStream) {
32+
internal class FileWriter(
33+
private val outputStream: DataOutputStream,
34+
private val onAudioChunkCaptured: ((ByteArray) -> Unit)?
35+
) {
3336
fun writeDataToStream(
34-
lastSkippedData: LinkedList<ByteArray>,
35-
data: ByteArray
37+
lastSkippedData: LinkedList<ByteArray>, data: ByteArray
3638
) {
3739
val totalSize = lastSkippedData.sumOf { it.size } + data.size
3840
val byteBuffer = ByteBuffer.allocate(totalSize).order(ByteOrder.LITTLE_ENDIAN)
3941

4042
lastSkippedData.forEach { byteArray ->
4143
byteBuffer.put(byteArray)
4244
}
43-
4445
byteBuffer.put(data)
46+
lastSkippedData.clear()
4547

4648
outputStream.write(byteBuffer.array())
47-
lastSkippedData.clear()
49+
onAudioChunkCaptured?.let {
50+
it(byteBuffer.array())
51+
}
4852
}
4953

5054
fun writeDataToStream(
51-
lastSkippedData: LinkedList<FloatArray>,
52-
data: FloatArray
55+
lastSkippedData: LinkedList<FloatArray>, data: FloatArray
5356
) {
5457
val totalFloats = lastSkippedData.sumOf { it.size } + data.size
5558
val totalSize = totalFloats * 4
@@ -63,9 +66,13 @@ internal class FileWriter(private val outputStream: DataOutputStream) {
6366
data.forEach { floatValue ->
6467
byteBuffer.putFloat(floatValue)
6568
}
69+
lastSkippedData.clear()
6670

6771
outputStream.write(byteBuffer.array())
68-
lastSkippedData.clear()
72+
73+
onAudioChunkCaptured?.let {
74+
it(byteBuffer.array())
75+
}
6976
}
7077

7178
}

android-wave-recorder/src/main/java/com/github/squti/androidwaverecorder/WaveRecorder.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class WaveRecorder {
8383
*/
8484
var onAmplitudeListener: ((Int) -> Unit)? = null
8585

86+
/**
87+
* Register a callback to be invoked for each recorded chunk of audio data.
88+
* Provides the captured chunk as a ByteArray.
89+
*/
90+
var onAudioChunkCaptured: ((ByteArray) -> Unit)? = null
91+
8692
/**
8793
* Register a callback to be invoked in recording state changes
8894
*/
@@ -191,7 +197,7 @@ class WaveRecorder {
191197
FileOutputStream(file)
192198
}
193199
val dataOutputStream = DataOutputStream(outputStream)
194-
val fileWriter = FileWriter(dataOutputStream)
200+
val fileWriter = FileWriter(dataOutputStream, onAudioChunkCaptured)
195201

196202
val bufferSizeToKeep =
197203
(waveConfig.sampleRate * channelCount(waveConfig.channels) * (bitPerSample(waveConfig.audioEncoding) / 8) * silenceDetectionConfig.bufferDurationInMillis / 1000).toInt()
@@ -247,7 +253,7 @@ class WaveRecorder {
247253
FileOutputStream(file)
248254
}
249255
val dataOutputStream = DataOutputStream(outputStream)
250-
val fileWriter = FileWriter(dataOutputStream)
256+
val fileWriter = FileWriter(dataOutputStream, onAudioChunkCaptured)
251257

252258
val bufferSizeToKeep =
253259
(waveConfig.sampleRate * channelCount(waveConfig.channels) * silenceDetectionConfig.bufferDurationInMillis / 1000).toInt()

0 commit comments

Comments
 (0)