File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed 
firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import io.ktor.client.plugins.websocket.ClientWebSocketSession
2525import  io.ktor.websocket.Frame 
2626import  io.ktor.websocket.close 
2727import  io.ktor.websocket.readBytes 
28+ import  java.io.ByteArrayOutputStream 
2829import  java.util.concurrent.ConcurrentLinkedQueue 
2930import  kotlin.coroutines.CoroutineContext 
3031import  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  }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments