Skip to content

Commit

Permalink
2641 change audio emotion/speed management
Browse files Browse the repository at this point in the history
  • Loading branch information
Elena Moshnikova committed Nov 15, 2024
1 parent 45902ae commit 8ba5f20
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/main/kotlin/com/epam/brn/dto/AudioFileMetaData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ data class AudioFileMetaData(
this.speedCode = AzureRates.X_SLOW
this.speedFloat = "0.65"
}

fun setSpeedNormal() {
this.speedCode = AzureRates.DEFAULT
this.speedFloat = "1"
}

fun setSpeedFaster() {
this.speedCode = AzureRates.FAST
this.speedFloat = "1.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class YandexSpeechKitService(
@Value("\${yandex.format}")
lateinit var format: String

@Value("\${yandex.emotion}")
lateinit var emotion: String
@Value("\${yandex.emotions}")
lateinit var emotions: List<String>

var iamToken: String = ""
var iamTokenExpiresTime: LocalDateTime = LocalDateTime.now(ZoneOffset.UTC)
Expand Down Expand Up @@ -78,6 +78,7 @@ class YandexSpeechKitService(
*/
fun generateAudioStream(audioFileMetaData: AudioFileMetaData): InputStream {
val token = getYandexIamTokenForAudioGeneration()
val emotion = emotions.random()
val parameters = ArrayList<NameValuePair>().apply {
add(BasicNameValuePair("folderId", folderId))
add(BasicNameValuePair("lang", audioFileMetaData.locale))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,34 @@ class UserAnalyticsServiceImpl(
}

override fun prepareAudioStreamForUser(exerciseId: Long, audioFileMetaData: AudioFileMetaData): InputStream =
textToSpeechService.generateAudioOggStreamWithValidation(prepareAudioFileMetaData(exerciseId, audioFileMetaData))
textToSpeechService
.generateAudioOggStreamWithValidation(
prepareAudioFileMetaData(exerciseId, audioFileMetaData)
)

override fun prepareAudioFileMetaData(exerciseId: Long, audioFileMetaData: AudioFileMetaData): AudioFileMetaData {
val currentUserId = userAccountService.getCurrentUserId()
val lastExerciseHistory = studyHistoryRepository
.findLastByUserAccountIdAndExerciseId(currentUserId, exerciseId)
val seriesType = ExerciseType.valueOf(exerciseRepository.findTypeByExerciseId(exerciseId))

val text = audioFileMetaData.text
if (!listTextExercises.contains(seriesType))
audioFileMetaData.text = text.replace(" ", ", ")

if (text.contains(" ")) {
if (isDoneBad(lastExerciseHistory))
audioFileMetaData.setSpeedSlowest()
else
audioFileMetaData.setSpeedSlow()
} else if (isDoneBad(lastExerciseHistory)) {
if (lastExerciseHistory == null)
audioFileMetaData.setSpeedNormal()
else if (isDoneBad(lastExerciseHistory))
audioFileMetaData.setSpeedSlow()
}
else if (isDoneWell(lastExerciseHistory))
audioFileMetaData.setSpeedFaster()
return audioFileMetaData
}

fun isDoneBad(lastHistory: StudyHistory?): Boolean =
lastHistory != null && !exerciseService.isDoneWell(lastHistory)

fun isDoneWell(lastHistory: StudyHistory?): Boolean =
lastHistory != null && exerciseService.isDoneWell(lastHistory)

fun isMultiWords(seriesType: ExerciseType): Boolean =
seriesType == ExerciseType.PHRASES || seriesType == ExerciseType.SENTENCE || seriesType == ExerciseType.WORDS_SEQUENCES

Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ yandex.generationAudioLink=https://tts.api.cloud.yandex.net/speech/v1/tts:synthe
yandex.folderId=${YANDEX_FOLDER_ID:b1g0m877l9r22ngujisu}
yandex.format=oggopus
#yandex.emotion=good
yandex.emotion=neutral
yandex.speeds=0.8,1,1.2
yandex.emotions=neutral,good
yandex.folderForFiles=frontend/public

brn.picture.file.default.path=pictures/%s.jpg
Expand Down
36 changes: 26 additions & 10 deletions src/test/kotlin/com/epam/brn/service/UserAnalyticsServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ internal class UserAnalyticsServiceTest {
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)

// THEN
metaDataResult.speedFloat shouldBe "0.8"
metaDataResult.speedCode shouldBe AzureRates.SLOW
metaDataResult.speedFloat shouldBe "1.2"
metaDataResult.speedCode shouldBe AzureRates.FAST
metaDataResult.text shouldBe "мама, папа"
}

Expand All @@ -152,8 +152,8 @@ internal class UserAnalyticsServiceTest {
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)

// THEN
metaDataResult.speedFloat shouldBe "0.8"
metaDataResult.speedCode shouldBe AzureRates.SLOW
metaDataResult.speedFloat shouldBe "1.2"
metaDataResult.speedCode shouldBe AzureRates.FAST
metaDataResult.text shouldBe "мама папа"
}

Expand All @@ -174,8 +174,8 @@ internal class UserAnalyticsServiceTest {
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)

// THEN
metaDataResult.speedFloat shouldBe "0.65"
metaDataResult.speedCode shouldBe AzureRates.X_SLOW
metaDataResult.speedFloat shouldBe "0.8"
metaDataResult.speedCode shouldBe AzureRates.SLOW
metaDataResult.text shouldBe "мама, папа"
}

Expand All @@ -196,8 +196,8 @@ internal class UserAnalyticsServiceTest {
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)

// THEN
metaDataResult.speedFloat shouldBe "0.65"
metaDataResult.speedCode shouldBe AzureRates.X_SLOW
metaDataResult.speedFloat shouldBe "0.8"
metaDataResult.speedCode shouldBe AzureRates.SLOW
metaDataResult.text shouldBe "мама папа"
}

Expand All @@ -217,8 +217,8 @@ internal class UserAnalyticsServiceTest {
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)

// THEN
metaDataResult.speedFloat shouldBe "1"
metaDataResult.speedCode shouldBe AzureRates.DEFAULT
metaDataResult.speedFloat shouldBe "1.2"
metaDataResult.speedCode shouldBe AzureRates.FAST
metaDataResult.text shouldBe "мама"
}

Expand All @@ -242,6 +242,22 @@ internal class UserAnalyticsServiceTest {
metaDataResult.speedCode shouldBe AzureRates.SLOW
}

@Test
fun `should prepareAudioFileMetaData normal speed for single word`() {
// GIVEN
every { userAccountService.getCurrentUserId() } returns currentUserId
every {
studyHistoryRepository.findLastByUserAccountIdAndExerciseId(currentUserId, exerciseId)
} returns null
every { exerciseRepository.findTypeByExerciseId(exerciseId) } returns ExerciseType.SINGLE_SIMPLE_WORDS.name
val audioFileMetaData = AudioFileMetaData("text", BrnLocale.RU.locale, Voice.FILIPP.name, "1", AzureRates.DEFAULT)
// WHEN
val metaDataResult = userAnalyticsService.prepareAudioFileMetaData(exerciseId, audioFileMetaData)
// THEN
metaDataResult.speedFloat shouldBe "1"
metaDataResult.speedCode shouldBe AzureRates.DEFAULT
}

@Test
fun `should prepareAudioStreamForUser`() {
// GIVEN
Expand Down
1 change: 0 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ yandex.generationAudioLink=https://tts.api.cloud.yandex.net/speech/v1/tts:synthe
yandex.folderId=b1gqn2760f5ongt82lm3
yandex.format=oggopus
yandex.emotion=good
yandex.speeds=1,1.2
yandex.folderForFiles=audioTest

brn.resources.default-pictures.path=pictures/
Expand Down

0 comments on commit 8ba5f20

Please sign in to comment.