Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/terminalai/apc into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePyProgrammer committed Oct 1, 2022
2 parents f70d711 + cbd63f1 commit 15257a8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
2 changes: 2 additions & 0 deletions development/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.nush.hearme">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand Down
67 changes: 53 additions & 14 deletions development/app/src/main/java/app/nush/hearme/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package app.nush.hearme

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.speech.RecognitionListener
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -34,34 +34,73 @@ import app.nush.hearme.ui.TTSField
import app.nush.hearme.ui.theme.HearMeTheme
import com.jamal.composeprefs.ui.PrefsScreen
import com.jamal.composeprefs.ui.prefs.SwitchPref
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import java.util.*


val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

setContent {
HearMeTheme {
Content()
onCreate2()
}
}
}

@Composable
fun onCreate2() {
val text = remember { mutableStateOf("") }

val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this)
val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)

speechRecognizerIntent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true)
speechRecognizer.setRecognitionListener(object : RecognitionListener {
override fun onReadyForSpeech(bundle: Bundle) {}
override fun onBeginningOfSpeech() {}
override fun onRmsChanged(v: Float) {}
override fun onBufferReceived(bytes: ByteArray) {}
override fun onEndOfSpeech() {}
override fun onError(i: Int) {}
override fun onResults(bundle: Bundle) {
speechRecognizer.startListening(speechRecognizerIntent)
}

override fun onPartialResults(bundle: Bundle) {
val data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
text.value = data!![0]
}

override fun onEvent(i: Int, bundle: Bundle) {}
})

Content(
text = text,
startListening = { speechRecognizer.startListening(speechRecognizerIntent) },
stopListening = { speechRecognizer.stopListening() }
)
}
}

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun Content() {
fun Content(text: MutableState<String>, startListening: () -> Unit, stopListening: () -> Unit) {
val navController = rememberNavController()
val useTTS = remember { mutableStateOf(true) }
val useLipSync = remember { mutableStateOf(true) }

Column {
CallBar()
CallBar({ startListening() }, { stopListening() })
NavHost(navController = navController, startDestination = "main") {
composable("main") {
Column(
Expand All @@ -70,7 +109,7 @@ fun Content() {
Divider()

if (useTTS.value) {
TTSField()
TTSField(text=text)

Divider()
}
Expand Down
28 changes: 22 additions & 6 deletions development/app/src/main/java/app/nush/hearme/ui/CallBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import app.nush.hearme.ui.theme.HearMeTheme

@Preview
@Composable
fun CallBar() {
val callTime = remember { mutableStateOf(23230) }
fun CallBar(startListening: () -> Unit, stopListening: () -> Unit) {
val threadStarted = remember { mutableStateOf(false) }
val callTime = remember { mutableStateOf(0) }
val callRunning = remember { mutableStateOf(false) }

Card(
modifier = Modifier.padding(10.dp).fillMaxWidth(),
Expand All @@ -36,17 +37,32 @@ fun CallBar() {
)

Text(
text = callTime.value.toString(),
text="${(callTime.value/60).toString().padStart(2, '0')}:" +
(callTime.value%60).toString().padStart(2, '0'),
color=Color.White,
modifier = Modifier.align(Alignment.CenterVertically)
)

Spacer(Modifier.weight(3f))

OutlinedButton(
onClick = {}
onClick = {
callRunning.value = !callRunning.value
if (callRunning.value) startListening()
else stopListening()

if (!threadStarted.value) {
Thread {
while (true) {
if (callRunning.value) callTime.value++
else callTime.value = 0
Thread.sleep(1000)
}
}.start()
}
}
) {
Text("End Call")
Text(if (callRunning.value) "End call" else "Start call")
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions development/app/src/main/java/app/nush/hearme/ui/TTSField.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
package app.nush.hearme.ui

import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview
@Composable
fun TTSField() {
fun TTSField(text: MutableState<String>) {
val scroll = rememberScrollState(0)

/*
val transcript = remember { mutableStateOf("""
Good morning, I am Jed Lim and I will be reporting on Problem 13, Candle Powered Turbine.
The problem statement states that a paper spiral suspended above the candle will start to rotate. We are tasked to optimize this setup for maximum torque. The relevant parameters to investigate are the properties of the paper, the shape of the spiral, the candle’s heat output, the radial position of the candle and the height of the spiral above the candle.
Good Morning Sir, would you be willing to participate in a survey on the government's efforts to combat COVID-19?
Here is my content for today.
Yes sure.
First, let’s take a look at some basics.
On a scale of 1 - 5, with 5 being the strongly agree and 1 being strongly disagree, do you agree the government has provided sufficient support for people who have lost their jobs due to COVID-19?
Our experimental setup consists of a paper spiral held at the top by a pin. The pin is suspended by the retort stand. The candle is placed below the spiral.
""".trimIndent()) }
*/

Text(
text = transcript.value,
text = text.value,
modifier = Modifier.verticalScroll(scroll).padding(10.dp)
)
}

0 comments on commit 15257a8

Please sign in to comment.