diff --git a/src/main/kotlin/com/epam/brn/dto/AudioFileMetaData.kt b/src/main/kotlin/com/epam/brn/dto/AudioFileMetaData.kt index a139d8e80..34b51687d 100644 --- a/src/main/kotlin/com/epam/brn/dto/AudioFileMetaData.kt +++ b/src/main/kotlin/com/epam/brn/dto/AudioFileMetaData.kt @@ -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" + } } diff --git a/src/main/kotlin/com/epam/brn/service/YandexSpeechKitService.kt b/src/main/kotlin/com/epam/brn/service/YandexSpeechKitService.kt index 101298076..ef074c941 100644 --- a/src/main/kotlin/com/epam/brn/service/YandexSpeechKitService.kt +++ b/src/main/kotlin/com/epam/brn/service/YandexSpeechKitService.kt @@ -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 var iamToken: String = "" var iamTokenExpiresTime: LocalDateTime = LocalDateTime.now(ZoneOffset.UTC) @@ -78,6 +78,7 @@ class YandexSpeechKitService( */ fun generateAudioStream(audioFileMetaData: AudioFileMetaData): InputStream { val token = getYandexIamTokenForAudioGeneration() + val emotion = emotions.random() val parameters = ArrayList().apply { add(BasicNameValuePair("folderId", folderId)) add(BasicNameValuePair("lang", audioFileMetaData.locale)) diff --git a/src/main/kotlin/com/epam/brn/service/impl/UserAnalyticsServiceImpl.kt b/src/main/kotlin/com/epam/brn/service/impl/UserAnalyticsServiceImpl.kt index 1f7b3ae2b..919e691e3 100644 --- a/src/main/kotlin/com/epam/brn/service/impl/UserAnalyticsServiceImpl.kt +++ b/src/main/kotlin/com/epam/brn/service/impl/UserAnalyticsServiceImpl.kt @@ -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 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1d311d189..804cc5707 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 diff --git a/src/test/kotlin/com/epam/brn/service/UserAnalyticsServiceTest.kt b/src/test/kotlin/com/epam/brn/service/UserAnalyticsServiceTest.kt index 92d593fbe..ccaec2986 100644 --- a/src/test/kotlin/com/epam/brn/service/UserAnalyticsServiceTest.kt +++ b/src/test/kotlin/com/epam/brn/service/UserAnalyticsServiceTest.kt @@ -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 "мама, папа" } @@ -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 "мама папа" } @@ -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 "мама, папа" } @@ -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 "мама папа" } @@ -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 "мама" } @@ -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 diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 9c35a137b..6bd08dfd7 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -48,8 +48,7 @@ yandex.getTokenLink=https://iam.api.cloud.yandex.net/iam/v1/tokens yandex.generationAudioLink=https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize yandex.folderId=b1gqn2760f5ongt82lm3 yandex.format=oggopus -yandex.emotion=good -yandex.speeds=1,1.2 +yandex.emotions=good yandex.folderForFiles=audioTest brn.resources.default-pictures.path=pictures/