openassistant: audio input
- Commit
bf5b083494ef5418bd6089e925f48e331722fc78- Author
- Vayun Mathur <[email protected]>
- When
- 2026-03-17 01:36 UTC (8 months ago)
- Parents
b537e0c
Diff
openassistant/src/main/AndroidManifest.xml
diff --git a/openassistant/src/main/AndroidManifest.xml b/openassistant/src/main/AndroidManifest.xmlindex 14fec21eb5..1e7c5507fd 100644--- a/openassistant/src/main/AndroidManifest.xml+++ b/openassistant/src/main/AndroidManifest.xml@@ -9,6 +9,8 @@ <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> + <uses-permission android:name="android.permission.RECORD_AUDIO" />+ <queries> <intent> <action android:name="android.intent.action.MAIN" />
openassistant/src/main/java/com/vayunmathur/openassistant/LiteRTChatUi.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/LiteRTChatUi.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/LiteRTChatUi.ktindex 3af2ab576e..c49a7d386b 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/LiteRTChatUi.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/LiteRTChatUi.kt@@ -1,10 +1,16 @@ package com.vayunmathur.openassistant +import android.Manifest import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.pm.PackageManager+import android.media.AudioFormat+import android.media.AudioRecord+import android.media.MediaRecorder import android.net.Uri+import android.os.Build+import android.widget.Toast import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.background@@ -27,12 +33,14 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp+import androidx.core.content.ContextCompat import androidx.core.net.toUri import coil.compose.AsyncImage import com.google.ai.edge.litertlm.* import com.vayunmathur.library.ui.IconAdd import com.vayunmathur.library.ui.IconClose import com.vayunmathur.library.ui.IconStop+import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.catch import kotlinx.coroutines.launch@@ -44,12 +52,13 @@ import java.util.* import java.util.regex.Pattern /**- * Data class for Chat Messages supporting multiple images+ * Data class for Chat Messages supporting multiple images and audio */ data class ChatMessage( val text: String, val isUser: Boolean,- val imageUris: List<Uri> = emptyList()+ val imageUris: List<Uri> = emptyList(),+ val hasAudio: Boolean = false ) /**@@ -110,7 +119,7 @@ class AssistantToolSet(private val context: Context) : ToolSet { fun getWeather(latitude: Double, longitude: Double): String = "Weather: 22°C, Sunny." } -@OptIn(ExperimentalMaterial3Api::class, ExperimentalApi::class)+@OptIn(ExperimentalMaterial3Api::class) @Composable fun LiteRTChatUi(modelFile: File) { val context = LocalContext.current@@ -122,11 +131,16 @@ fun LiteRTChatUi(modelFile: File) { val messages = remember { mutableStateListOf<ChatMessage>() } var inputText by remember { mutableStateOf("") } var isInitializing by remember { mutableStateOf(true) }+ var globalError by remember { mutableStateOf<String?>(null) } // State for multiple images val selectedImageUris = remember { mutableStateListOf<Uri>() } val selectedImageFiles = remember { mutableStateListOf<File>() } + var isRecording by remember { mutableStateOf(false) }+ var audioRecorder by remember { mutableStateOf<WavRecorder?>(null) }+ var recordedAudioFile by remember { mutableStateOf<File?>(null) }+ var activeJob by remember { mutableStateOf<kotlinx.coroutines.Job?>(null) } val isGenerating by remember { derivedStateOf { activeJob?.isActive == true } } @@ -136,6 +150,24 @@ fun LiteRTChatUi(modelFile: File) { if (messages.isNotEmpty()) listState.animateScrollToItem(messages.size - 1) } + val recordAudioPermission = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->+ if (isGranted) {+ val file = File(context.cacheDir, "recording_${System.currentTimeMillis()}.wav")+ recordedAudioFile = file+ try {+ val recorder = WavRecorder(context, file, scope)+ recorder.start()+ audioRecorder = recorder+ isRecording = true+ globalError = null+ } catch (e: Exception) {+ globalError = "Mic error: ${e.localizedMessage}"+ }+ } else {+ Toast.makeText(context, "Microphone permission denied", Toast.LENGTH_SHORT).show()+ }+ }+ // Support multiple picks val imagePickerLauncher = rememberLauncherForActivityResult(ActivityResultContracts.GetMultipleContents()) { uris: List<Uri> -> uris.forEach { uri ->@@ -154,11 +186,12 @@ fun LiteRTChatUi(modelFile: File) { modelPath = modelFile.absolutePath, backend = Backend.GPU(), visionBackend = Backend.GPU(),- //audioBackend = Backend.CPU(),+ audioBackend = Backend.CPU(), cacheDir = context.cacheDir.absolutePath )- ExperimentalFlags.enableConversationConstrainedDecoding = false- engine = Engine(config).apply { initialize() }+ val newEngine = Engine(config)+ newEngine.initialize()+ engine = newEngine val systemPrompt = """ You are a helpful Android assistant. Use tool calls:@@ -166,13 +199,16 @@ fun LiteRTChatUi(modelFile: File) { Interpret tool results conversationally. """.trimIndent() - conversation = engine?.createConversation(ConversationConfig(+ conversation = newEngine.createConversation(ConversationConfig( systemInstruction = Contents.of(systemPrompt), tools = listOf(tool(assistantTools)), automaticToolCalling = false- ))+ ) ) isInitializing = false- } catch (e: Exception) { isInitializing = false }+ } catch (e: Exception) {+ isInitializing = false+ globalError = "Init error: ${e.localizedMessage}"+ } } } @@ -188,34 +224,56 @@ fun LiteRTChatUi(modelFile: File) { .fillMaxWidth() .padding(horizontal = 16.dp, vertical = 8.dp) ) {- // MULTI-IMAGE PRE-SEND PREVIEW- if (selectedImageUris.isNotEmpty()) {+ if (selectedImageUris.isNotEmpty() || isRecording) { Row( modifier = Modifier.padding(bottom = 8.dp), verticalAlignment = Alignment.Bottom ) {- LazyRow(- horizontalArrangement = Arrangement.spacedBy(12.dp),- modifier = Modifier.weight(1f, fill = false)- ) {- items(selectedImageUris) { uri ->- Box(modifier = Modifier.size(80.dp)) {- AsyncImage(- model = uri,- contentDescription = "Selected image",- modifier = Modifier- .fillMaxSize()- .clip(RoundedCornerShape(12.dp))- .background(MaterialTheme.colorScheme.surfaceVariant),- contentScale = ContentScale.Crop- )+ if (selectedImageUris.isNotEmpty()) {+ LazyRow(+ horizontalArrangement = Arrangement.spacedBy(12.dp),+ modifier = Modifier.weight(1f, fill = false)+ ) {+ items(selectedImageUris) { uri ->+ Box(modifier = Modifier.size(80.dp)) {+ AsyncImage(+ model = uri,+ contentDescription = "Selected image",+ modifier = Modifier+ .fillMaxSize()+ .clip(RoundedCornerShape(12.dp))+ .background(MaterialTheme.colorScheme.surfaceVariant),+ contentScale = ContentScale.Crop+ )+ IconButton(+ onClick = {+ val index = selectedImageUris.indexOf(uri)+ if (index != -1) {+ selectedImageUris.removeAt(index)+ if (index < selectedImageFiles.size) selectedImageFiles.removeAt(index)+ }+ }+ ) {+ IconClose()+ }+ }+ }+ }+ }+ if (isRecording) {+ Spacer(Modifier.width(8.dp))+ Card(+ colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer),+ shape = RoundedCornerShape(12.dp)+ ) {+ Row(Modifier.padding(horizontal = 12.dp, vertical = 8.dp), verticalAlignment = Alignment.CenterVertically) {+ Text("Recording...", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error) IconButton( onClick = {- val index = selectedImageUris.indexOf(uri)- if (index != -1) {- selectedImageUris.removeAt(index)- if (index < selectedImageFiles.size) selectedImageFiles.removeAt(index)- }+ audioRecorder?.stop()+ audioRecorder = null+ isRecording = false+ recordedAudioFile = null } ) { IconClose()@@ -233,12 +291,32 @@ fun LiteRTChatUi(modelFile: File) { ) { Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) { IconButton(onClick = { imagePickerLauncher.launch("image/*") }, enabled = !isGenerating) { IconAdd() }+ IconButton(+ onClick = {+ if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {+ val file = File(context.cacheDir, "recording_${System.currentTimeMillis()}.wav")+ recordedAudioFile = file+ try {+ val recorder = WavRecorder(context, file, scope)+ recorder.start()+ audioRecorder = recorder+ isRecording = true+ globalError = null+ } catch (e: Exception) {+ globalError = "Failed to start mic: ${e.localizedMessage}"+ }+ } else recordAudioPermission.launch(Manifest.permission.RECORD_AUDIO)+ },+ enabled = !isGenerating+ ) {+ Icon(painterResource(android.R.drawable.ic_btn_speak_now), contentDescription = "Voice")+ } TextField( value = inputText, onValueChange = { inputText = it }, modifier = Modifier.weight(1f),- enabled = !isGenerating,+ enabled = !isGenerating && !isRecording, placeholder = { Text("Message...") }, colors = TextFieldDefaults.colors( focusedContainerColor = Color.Transparent,@@ -252,15 +330,20 @@ fun LiteRTChatUi(modelFile: File) { if (isGenerating) { IconButton(onClick = { activeJob?.cancel(); activeJob = null }) { IconStop(MaterialTheme.colorScheme.error) } } else {- val canSend = (inputText.isNotBlank() || selectedImageFiles.isNotEmpty())+ val canSend = (inputText.isNotBlank() || selectedImageFiles.isNotEmpty() || recordedAudioFile != null) IconButton( enabled = canSend, onClick = {+ if (isRecording) {+ audioRecorder?.stop()+ audioRecorder = null+ isRecording = false+ } sendMessageManual( scope, conversation, assistantTools, messages,- inputText, selectedImageFiles.toList(), selectedImageUris.toList(),+ inputText, selectedImageFiles.toList(), selectedImageUris.toList(), recordedAudioFile, onComplete = {- inputText = ""; selectedImageFiles.clear(); selectedImageUris.clear()+ inputText = ""; selectedImageFiles.clear(); selectedImageUris.clear(); recordedAudioFile = null }, onJobFinished = { activeJob = null }, onJobStarted = { activeJob = it }@@ -277,54 +360,188 @@ fun LiteRTChatUi(modelFile: File) { ) { padding -> Box(modifier = Modifier.padding(padding).fillMaxSize()) { if (isInitializing) LinearProgressIndicator(modifier = Modifier.fillMaxWidth())+ LazyColumn(state = listState, modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(16.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) {+ if (globalError != null) {+ item {+ Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer)) {+ Text(globalError!!, modifier = Modifier.padding(8.dp), color = MaterialTheme.colorScheme.error, fontSize = 12.sp)+ }+ }+ } items(messages) { msg -> ChatBubble(msg) } } } } } +/**+ * Custom WavRecorder using AudioRecord to produce 16kHz PCM WAV files+ * needed for LiteRT-LM miniaudio decoder stability.+ */+class WavRecorder(val context: Context, val outputFile: File, val scope: CoroutineScope) {+ private var audioRecord: AudioRecord? = null+ private var isRecording = false+ private val sampleRate = 16000+ private val channelConfig = AudioFormat.CHANNEL_IN_MONO+ private val audioFormat = AudioFormat.ENCODING_PCM_16BIT+ private val bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat)++ @SuppressLint("MissingPermission")+ fun start() {+ audioRecord = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, bufferSize)+ if (audioRecord?.state != AudioRecord.STATE_INITIALIZED) throw Exception("AudioRecord init failed")++ isRecording = true+ audioRecord?.startRecording()++ scope.launch(Dispatchers.IO) {+ val tempRaw = File(context.cacheDir, "temp_${System.currentTimeMillis()}.raw")+ FileOutputStream(tempRaw).use { fos ->+ val buffer = ByteArray(bufferSize)+ while (isRecording) {+ val read = audioRecord?.read(buffer, 0, bufferSize) ?: 0+ if (read > 0) fos.write(buffer, 0, read)+ }+ }+ writeWavFile(tempRaw, outputFile)+ tempRaw.delete()+ }+ }++ fun stop() {+ isRecording = false+ audioRecord?.apply {+ if (state == AudioRecord.STATE_INITIALIZED) {+ try { stop() } catch(e: Exception) {}+ }+ release()+ }+ audioRecord = null+ }++ private fun writeWavFile(rawFile: File, wavFile: File) {+ val rawData = rawFile.readBytes()+ val totalAudioLen = rawData.size.toLong()+ val totalDataLen = totalAudioLen + 36+ val byteRate = (16 * sampleRate * 1 / 8).toLong()++ FileOutputStream(wavFile).use { out ->+ val header = ByteArray(44)+ header[0] = 'R'.toByte(); header[1] = 'I'.toByte(); header[2] = 'F'.toByte(); header[3] = 'F'.toByte()+ header[4] = (totalDataLen and 0xff).toByte()+ header[5] = ((totalDataLen shr 8) and 0xff).toByte()+ header[6] = ((totalDataLen shr 16) and 0xff).toByte()+ header[7] = ((totalDataLen shr 24) and 0xff).toByte()+ header[8] = 'W'.toByte(); header[9] = 'A'.toByte(); header[10] = 'V'.toByte(); header[11] = 'E'.toByte()+ header[12] = 'f'.toByte(); header[13] = 'm'.toByte(); header[14] = 't'.toByte(); header[15] = ' '.toByte()+ header[16] = 16; header[17] = 0; header[18] = 0; header[19] = 0 // fmt chunk size+ header[20] = 1; header[21] = 0 // PCM = 1+ header[22] = 1; header[23] = 0 // Mono+ header[24] = (sampleRate and 0xff).toByte()+ header[25] = ((sampleRate shr 8) and 0xff).toByte()+ header[26] = ((sampleRate shr 16) and 0xff).toByte()+ header[27] = ((sampleRate shr 24) and 0xff).toByte()+ header[28] = (byteRate and 0xff).toByte()+ header[29] = ((byteRate shr 8) and 0xff).toByte()+ header[30] = ((byteRate shr 16) and 0xff).toByte()+ header[31] = ((byteRate shr 24) and 0xff).toByte()+ header[32] = 2; header[33] = 0 // block align+ header[34] = 16; header[35] = 0 // bits per sample+ header[36] = 'd'.toByte(); header[37] = 'a'.toByte(); header[38] = 't'.toByte(); header[39] = 'a'.toByte()+ header[40] = (totalAudioLen and 0xff).toByte()+ header[41] = ((totalAudioLen shr 8) and 0xff).toByte()+ header[42] = ((totalAudioLen shr 16) and 0xff).toByte()+ header[43] = ((totalAudioLen shr 24) and 0xff).toByte()+ out.write(header)+ out.write(rawData)+ }+ }+}+ @Composable fun ChatBubble(message: ChatMessage) { Column( modifier = Modifier.fillMaxWidth(), horizontalAlignment = if (message.isUser) Alignment.End else Alignment.Start ) {- val bubbleColor = if (message.isUser) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.secondaryContainer- val textColor = if (message.isUser) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSecondaryContainer- val shape = if (message.isUser) RoundedCornerShape(20.dp, 20.dp, 4.dp, 20.dp) else RoundedCornerShape(20.dp, 20.dp, 20.dp, 4.dp)-- Surface(- color = bubbleColor,- shape = shape,- modifier = Modifier.widthIn(max = 320.dp)- ) {- Column(modifier = Modifier.padding(if (message.imageUris.isNotEmpty()) 4.dp else 12.dp)) {+ if (message.isUser) {+ // User message remains in a primary colored bubble+ Surface(+ color = MaterialTheme.colorScheme.primary,+ shape = RoundedCornerShape(20.dp, 20.dp, 4.dp, 20.dp),+ modifier = Modifier.widthIn(max = 300.dp)+ ) {+ Column(modifier = Modifier.padding(if (message.imageUris.isNotEmpty() || message.hasAudio) 4.dp else 12.dp)) {+ if (message.imageUris.isNotEmpty()) {+ Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {+ message.imageUris.forEach { uri ->+ AsyncImage(+ model = uri,+ contentDescription = "User attached image",+ modifier = Modifier+ .fillMaxWidth()+ .heightIn(max = 240.dp)+ .clip(RoundedCornerShape(16.dp)),+ contentScale = ContentScale.Crop+ )+ }+ }+ }++ if (message.hasAudio) {+ Row(Modifier.padding(8.dp), verticalAlignment = Alignment.CenterVertically) {+ Icon(painterResource(android.R.drawable.ic_btn_speak_now), null, tint = MaterialTheme.colorScheme.onPrimary, modifier = Modifier.size(16.dp))+ Spacer(Modifier.width(8.dp))+ Text("Voice Message", color = MaterialTheme.colorScheme.onPrimary, fontSize = 14.sp)+ }+ }++ if (message.text.isNotBlank()) {+ Text(+ text = message.text,+ color = MaterialTheme.colorScheme.onPrimary,+ fontSize = 15.sp,+ modifier = Modifier.padding(+ horizontal = if (message.imageUris.isNotEmpty() || message.hasAudio) 8.dp else 0.dp,+ vertical = if (message.imageUris.isNotEmpty() || message.hasAudio) 8.dp else 0.dp+ )+ )+ }+ }+ }+ } else {+ // AI output is plain text without a bubble surface+ Column(+ modifier = Modifier+ .widthIn(max = 320.dp)+ .padding(vertical = 4.dp),+ horizontalAlignment = Alignment.Start+ ) {+ // In case AI ever sends images/audio (currently handled for consistency) if (message.imageUris.isNotEmpty()) { Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { message.imageUris.forEach { uri -> AsyncImage( model = uri,- contentDescription = "User attached image",+ contentDescription = "AI attached image", modifier = Modifier .fillMaxWidth() .heightIn(max = 240.dp)- .clip(RoundedCornerShape(16.dp)),+ .clip(RoundedCornerShape(12.dp)), contentScale = ContentScale.Crop ) } }+ Spacer(Modifier.height(4.dp)) } if (message.text.isNotBlank()) { Text( text = message.text,- color = textColor,- fontSize = 15.sp,- modifier = Modifier.padding(- horizontal = if (message.imageUris.isNotEmpty()) 8.dp else 0.dp,- vertical = if (message.imageUris.isNotEmpty()) 8.dp else 0.dp- )+ color = MaterialTheme.colorScheme.onBackground,+ fontSize = 16.sp,+ lineHeight = 22.sp ) } }@@ -340,6 +557,7 @@ private fun sendMessageManual( text: String, imageFiles: List<File>, imageUris: List<Uri>,+ audioFile: File?, onComplete: () -> Unit, onJobFinished: () -> Unit, onJobStarted: (kotlinx.coroutines.Job) -> Unit@@ -347,7 +565,8 @@ private fun sendMessageManual( messages.add(ChatMessage( text = text, isUser = true,- imageUris = imageUris+ imageUris = imageUris,+ hasAudio = audioFile != null )) val aiMessageIndex = messages.size@@ -356,6 +575,7 @@ private fun sendMessageManual( val job = scope.launch { val initialParts = mutableListOf<Content>() imageFiles.forEach { initialParts.add(Content.ImageFile(it.absolutePath)) }+ audioFile?.let { initialParts.add(Content.AudioFile(it.absolutePath)) } if (text.isNotBlank()) initialParts.add(Content.Text(text)) var nextInput: Any = Contents.of(initialParts)