Skip to content

Commit 09591fa

Browse files
authored
Merge branch 'main' into update_bidi_javadocs
2 parents 420e2e1 + 6e3be78 commit 09591fa

File tree

1 file changed

+7
-8
lines changed
  • firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type

1 file changed

+7
-8
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/LiveSession.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.ktor.client.plugins.websocket.ClientWebSocketSession
2525
import io.ktor.websocket.Frame
2626
import io.ktor.websocket.close
2727
import io.ktor.websocket.readBytes
28+
import java.io.ByteArrayOutputStream
2829
import java.util.concurrent.ConcurrentLinkedQueue
2930
import kotlin.coroutines.CoroutineContext
3031
import kotlinx.coroutines.CoroutineScope
@@ -142,16 +143,14 @@ internal constructor(
142143
}
143144

144145
private suspend fun sendAudioDataToServer() {
145-
var offset = 0
146-
val audioBuffer = ByteArray(MIN_BUFFER_SIZE * 2)
146+
147+
val audioBufferStream = ByteArrayOutputStream()
147148
while (isRecording) {
148149
val receivedAudio = audioQueue.poll() ?: continue
149-
receivedAudio.copyInto(audioBuffer, offset)
150-
offset += receivedAudio.size
151-
if (offset >= MIN_BUFFER_SIZE) {
152-
sendMediaStream(listOf(MediaData(audioBuffer, "audio/pcm")))
153-
audioBuffer.fill(0)
154-
offset = 0
150+
audioBufferStream.write(receivedAudio)
151+
if (audioBufferStream.size() >= MIN_BUFFER_SIZE) {
152+
sendMediaStream(listOf(MediaData(audioBufferStream.toByteArray(), "audio/pcm")))
153+
audioBufferStream.reset()
155154
}
156155
}
157156
}

0 commit comments

Comments
 (0)