-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge pull request #13 from snuhcs-course/feat/tts-implementation-f…
…rontend Text-to-speech in the house!
- Loading branch information
Showing
8 changed files
with
214 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
frontend/app/src/main/java/com/example/speechbuddy/ui/models/TextToSpeechUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.speechbuddy.ui.models | ||
|
||
data class TextToSpeechUiState( | ||
val buttonStatus: ButtonStatusType = ButtonStatusType.PLAY | ||
) | ||
|
||
enum class ButtonStatusType { | ||
PLAY, | ||
STOP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
frontend/app/src/main/java/com/example/speechbuddy/viewmodel/TextToSpeechViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.example.speechbuddy.viewmodel | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.speech.tts.TextToSpeech | ||
import android.speech.tts.UtteranceProgressListener | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModel | ||
import com.example.speechbuddy.ui.models.ButtonStatusType | ||
import com.example.speechbuddy.ui.models.TextToSpeechUiState | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class TextToSpeechViewModel @Inject internal constructor( | ||
) : ViewModel() { | ||
|
||
private val _uiState = MutableStateFlow(TextToSpeechUiState()) | ||
val uiState: StateFlow<TextToSpeechUiState> = _uiState.asStateFlow() | ||
|
||
private var textToSpeech: TextToSpeech? = null | ||
|
||
var textInput by mutableStateOf("") | ||
private set | ||
|
||
fun setText(input: String) { | ||
textInput = input | ||
} | ||
|
||
private fun clearText() { | ||
textInput = "" | ||
} | ||
|
||
fun ttsStop() { | ||
textToSpeech?.stop() | ||
} | ||
|
||
fun ttsStart(context: Context) { | ||
// disable button | ||
_uiState.update { currentState -> | ||
currentState.copy( | ||
buttonStatus = ButtonStatusType.STOP | ||
) | ||
} | ||
|
||
textToSpeech = TextToSpeech(context) { | ||
if (it == TextToSpeech.SUCCESS) { | ||
textToSpeech?.let { txtToSpeech -> | ||
txtToSpeech.language = Locale.KOREAN | ||
txtToSpeech.setSpeechRate(1.0f) | ||
|
||
val params = Bundle() | ||
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "") | ||
txtToSpeech.setOnUtteranceProgressListener(object : | ||
UtteranceProgressListener() { | ||
override fun onStart(utteranceId: String?) { | ||
// Speech has started, button is already disabled | ||
} | ||
|
||
override fun onStop(utteranceId: String?, interrupted: Boolean) { | ||
_uiState.update { currentState -> | ||
currentState.copy( | ||
buttonStatus = ButtonStatusType.PLAY | ||
) | ||
} | ||
} | ||
|
||
override fun onDone(utteranceId: String?) { | ||
// Speech has finished, re-enable the button | ||
_uiState.update { currentState -> | ||
currentState.copy( | ||
buttonStatus = ButtonStatusType.PLAY | ||
) | ||
} | ||
clearText() | ||
} | ||
|
||
@Deprecated("Deprecated in Java") | ||
override fun onError(utteranceId: String?) { | ||
// There was an error, re-enable the button | ||
_uiState.update { currentState -> | ||
currentState.copy( | ||
buttonStatus = ButtonStatusType.PLAY | ||
) | ||
} | ||
} | ||
}) | ||
|
||
txtToSpeech.speak( | ||
textInput, | ||
TextToSpeech.QUEUE_ADD, | ||
params, | ||
"UniqueID" | ||
) | ||
} | ||
} else { | ||
// Initialization failed, re-enable the button | ||
_uiState.update { currentState -> | ||
currentState.copy( | ||
buttonStatus = ButtonStatusType.PLAY | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.