diff --git a/androidApp/app/build.gradle.kts b/androidApp/app/build.gradle.kts index 2aef623..f283633 100644 --- a/androidApp/app/build.gradle.kts +++ b/androidApp/app/build.gradle.kts @@ -68,8 +68,8 @@ val stockfish = tasks.register("stockfish") { outputs.dir(stockfishOut) } -/// The sample games, the piece sets and the board images of the desktop app, as app -/// assets. +/// The sample games, the piece sets, the board images and sounds of the desktop app, as +/// app assets. val bundleAssets = tasks.register("bundleAssets") { from(File(repo, "examples")) { include("*.pgn") @@ -83,6 +83,15 @@ val bundleAssets = tasks.register("bundleAssets") { include("*.jpg", "*.png") into("boards") } + // One set of sounds (the desktop's «SFX») and the trainer's wrong move (§33). + from(File(repo, "apps/desktop/assets/sounds/sfx")) { + include("*.ogg") + into("sounds") + } + from(File(repo, "apps/desktop/assets/sounds/common")) { + include("Wrong.ogg") + into("sounds") + } from(stockfishOut.map { it.dir("source") }) { into("licenses/stockfish") } diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ChessPenguinApp.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ChessPenguinApp.kt index 7eae00a..36764a0 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ChessPenguinApp.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ChessPenguinApp.kt @@ -11,6 +11,7 @@ import io.github.vrubelroman.chesspenguin.data.DbEntry import io.github.vrubelroman.chesspenguin.data.EngineFiles import io.github.vrubelroman.chesspenguin.data.MaiaFiles import io.github.vrubelroman.chesspenguin.data.MaiaGames +import io.github.vrubelroman.chesspenguin.data.Sounds import io.github.vrubelroman.chesspenguin.data.Library import io.github.vrubelroman.chesspenguin.data.OnlineDownloads import io.github.vrubelroman.chesspenguin.data.Settings @@ -36,6 +37,8 @@ class ChessPenguinApp : Application() { val maiaFiles by lazy { MaiaFiles(this, scope) } /// The game against Maia in hand (android-play.md §31.7). val maiaGames by lazy { MaiaGames(this) } + /// The sounds of moves and of the end of a game (android-play.md §33). + val sounds by lazy { Sounds(this, settings) } /// Games from Lichess and Chess.com being downloaded (android-ui.md §23). val downloads by lazy { OnlineDownloads(library, scope) } /// The app's own updates (android-ui.md §25). diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/data/Sounds.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/data/Sounds.kt new file mode 100644 index 0000000..5c0ed74 --- /dev/null +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/data/Sounds.kt @@ -0,0 +1,41 @@ +package io.github.vrubelroman.chesspenguin.data + +import android.content.Context +import android.media.AudioAttributes +import android.media.SoundPool + +/// What a sound says (android-play.md §33). +enum class Sound(val file: String) { + Move("Move"), + Capture("Capture"), + Check("Check"), + Defeat("Defeat"), + Success("Success"), + Wrong("Wrong"), +} + +/// The desktop's «SFX» set, loaded once; played when the setting «Sound» is on. +class Sounds(context: Context, private val settings: Settings) { + private val pool = SoundPool.Builder() + .setMaxStreams(2) + .setAudioAttributes(AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build()) + .build() + private val ids: Map = Sound.entries.associateWith { s -> + runCatching { context.assets.openFd("sounds/${s.file}.ogg").use { pool.load(it, 1) } }.getOrDefault(0) + } + + fun play(sound: Sound) { + if (!settings.prefs.value.sound) return + val id = ids[sound] ?: return + if (id != 0) pool.play(id, 1f, 1f, 1, 0, 1f) + } + + /// The sound of a move by its SAN: a check, a capture, or a move. + fun move(san: String) = play( + when { + san.endsWith("+") || san.endsWith("#") -> Sound.Check + 'x' in san -> Sound.Capture + else -> Sound.Move + }, + ) +} diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/AboutScreen.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/AboutScreen.kt index b916d30..298ef14 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/AboutScreen.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/AboutScreen.kt @@ -49,6 +49,9 @@ private val NOTICES = listOf( "Pieces kosal" to "CC BY 4.0 · Kosal Sen, philatype.com (via PyChess)", "Pieces metaltops" to "GPL · PyChess", "Board images" to "AGPLv3+ · the lila authors and pirouetti (Lichess)", + "Sounds (SFX)" to "AGPLv3+ · Enigmahack (via Lichess)", + "Sound of a wrong move" to "CC0-1.0 · Kenney, kenney.nl", + "Maia-3 (downloaded from its authors on request)" to "CSSLab, University of Toronto · huggingface.co/UofTCSSLab", "Figurines, Noto Sans Math" to "SIL Open Font License 1.1", "Jetpack Compose, AndroidX, Kotlin" to "Apache-2.0 · Google, JetBrains", "UniFFI" to "MPL-2.0 · Mozilla", diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GameScreen.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GameScreen.kt index e773dbf..38cd2e4 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GameScreen.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/GameScreen.kt @@ -446,6 +446,13 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o game = e.data().copy(notes = held.notes) r.over.value = over app.maiaGames.finish(e.pgn()) + app.sounds.play( + when (over.heading) { + R.string.play_won -> io.github.vrubelroman.chesspenguin.data.Sound.Success + R.string.play_draw -> io.github.vrubelroman.chesspenguin.data.Sound.Move + else -> io.github.vrubelroman.chesspenguin.data.Sound.Defeat + }, + ) } // After a move of either side: kept, and the end of the game if it came. fun afterMove(e: Edited) { @@ -485,10 +492,12 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o r.judge?.hopeless()?.let { if (it) r.hopeless++ else r.hopeless = 0u } if (!r.playOn && r.hopeless >= uniffi.mobile_api.hopelessTurns()) { r.offer.value = true + app.sounds.play(io.github.vrubelroman.chesspenguin.data.Sound.Defeat) return@LaunchedEffect } runCatching { e.play(end.toUInt(), uci) }.getOrNull()?.let { ed -> apply(ed) + app.sounds.move(ed.data.nodes[ed.cursor.toInt()].san) afterMove(ed) } } finally { @@ -764,6 +773,7 @@ fun GameScreen(db: String, startPlace: Int, query: String, onBack: () -> Unit, o BoardInput(legal, onMove = { uci -> runCatching { e.play(current.toUInt(), uci) }.getOrNull()?.let { apply(it) + app.sounds.move(it.data.nodes[it.cursor.toInt()].san) if (gameOn) afterMove(it) } }) diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/SettingsScreen.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/SettingsScreen.kt index 12d7b38..849c896 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/SettingsScreen.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/SettingsScreen.kt @@ -116,6 +116,7 @@ fun SettingsScreen(onBack: () -> Unit, onAbout: () -> Unit, onBoardStyle: () -> Toggle(stringResource(R.string.animation), prefs.animation) { v -> set { it.copy(animation = v) } } Toggle(stringResource(R.string.best_arrow), prefs.bestArrow) { v -> set { it.copy(bestArrow = v) } } + Toggle(stringResource(R.string.sound), prefs.sound) { v -> set { it.copy(sound = v) } } Toggle(stringResource(R.string.vibration), prefs.vibration) { v -> set { it.copy(vibration = v) } } Group(stringResource(R.string.group_look)) diff --git a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/TrainScreen.kt b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/TrainScreen.kt index e8edad2..8adbb90 100644 --- a/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/TrainScreen.kt +++ b/androidApp/app/src/main/java/io/github/vrubelroman/chesspenguin/ui/TrainScreen.kt @@ -87,17 +87,26 @@ fun TrainScreen(trainer: Trainer, ply0: Int, startFlipped: Boolean, onPly: (Int) } fun move(uci: String) { when (trainer.judge(ply.toUInt(), uci)) { - TrainVerdict.RIGHT, TrainVerdict.FINISHED -> { + TrainVerdict.RIGHT -> { missed = false ply += 1 onPly(ply) buzz(false) + trainer.state(ply.toUInt()).played.lastOrNull()?.let { app.sounds.move(it) } + } + TrainVerdict.FINISHED -> { + missed = false + ply += 1 + onPly(ply) + buzz(false) + app.sounds.play(io.github.vrubelroman.chesspenguin.data.Sound.Success) } TrainVerdict.WRONG -> { trainer.fenAfter(ply.toUInt(), uci)?.let { wrong = Wrong(it, uci.substring(0, 2), uci.substring(2, 4)) } missed = true changes++ buzz(true) + app.sounds.play(io.github.vrubelroman.chesspenguin.data.Sound.Wrong) } TrainVerdict.DONE -> {} } diff --git a/androidApp/app/src/main/res/values-de/strings.xml b/androidApp/app/src/main/res/values-de/strings.xml index 28ce771..b8e914a 100644 --- a/androidApp/app/src/main/res/values-de/strings.xml +++ b/androidApp/app/src/main/res/values-de/strings.xml @@ -513,4 +513,5 @@ Sie spielen %1$s · %2$s Von hier gegen Maia weiterspielen… Nach %1$s + Ton diff --git a/androidApp/app/src/main/res/values-es/strings.xml b/androidApp/app/src/main/res/values-es/strings.xml index ba1bf18..f4e86cc 100644 --- a/androidApp/app/src/main/res/values-es/strings.xml +++ b/androidApp/app/src/main/res/values-es/strings.xml @@ -521,4 +521,5 @@ juegas con %1$s · %2$s Seguir desde aquí contra Maia… Tras %1$s + Sonido diff --git a/androidApp/app/src/main/res/values-fr/strings.xml b/androidApp/app/src/main/res/values-fr/strings.xml index bfaa795..4091bfc 100644 --- a/androidApp/app/src/main/res/values-fr/strings.xml +++ b/androidApp/app/src/main/res/values-fr/strings.xml @@ -521,4 +521,5 @@ vous jouez %1$s · %2$s Continuer d’ici contre Maia… Après %1$s + Son diff --git a/androidApp/app/src/main/res/values-it/strings.xml b/androidApp/app/src/main/res/values-it/strings.xml index 0f3cfb0..6a945f2 100644 --- a/androidApp/app/src/main/res/values-it/strings.xml +++ b/androidApp/app/src/main/res/values-it/strings.xml @@ -521,4 +521,5 @@ giochi con %1$s · %2$s Continua da qui contro Maia… Dopo %1$s + Suono diff --git a/androidApp/app/src/main/res/values-kk/strings.xml b/androidApp/app/src/main/res/values-kk/strings.xml index db7e88d..fd2d554 100644 --- a/androidApp/app/src/main/res/values-kk/strings.xml +++ b/androidApp/app/src/main/res/values-kk/strings.xml @@ -514,4 +514,5 @@ сіз %1$s · %2$s Осы жерден Maia-мен жалғастыру… %1$s кейін + Дыбыс diff --git a/androidApp/app/src/main/res/values-pt/strings.xml b/androidApp/app/src/main/res/values-pt/strings.xml index c1c49aa..6146c17 100644 --- a/androidApp/app/src/main/res/values-pt/strings.xml +++ b/androidApp/app/src/main/res/values-pt/strings.xml @@ -522,4 +522,5 @@ você joga de %1$s · %2$s Continuar daqui contra a Maia… Depois de %1$s + Som diff --git a/androidApp/app/src/main/res/values-ru/strings.xml b/androidApp/app/src/main/res/values-ru/strings.xml index 85517f1..54f092a 100644 --- a/androidApp/app/src/main/res/values-ru/strings.xml +++ b/androidApp/app/src/main/res/values-ru/strings.xml @@ -529,4 +529,5 @@ вы %1$s · %2$s Играть отсюда с Maia… После %1$s + Звук diff --git a/androidApp/app/src/main/res/values-uz/strings.xml b/androidApp/app/src/main/res/values-uz/strings.xml index e1825da..65c4fbb 100644 --- a/androidApp/app/src/main/res/values-uz/strings.xml +++ b/androidApp/app/src/main/res/values-uz/strings.xml @@ -512,4 +512,5 @@ siz %1$s · %2$s Shu yerdan Maia bilan davom ettirish… %1$s dan keyin + Ovoz diff --git a/androidApp/app/src/main/res/values/strings.xml b/androidApp/app/src/main/res/values/strings.xml index 8e004c6..f9e57af 100644 --- a/androidApp/app/src/main/res/values/strings.xml +++ b/androidApp/app/src/main/res/values/strings.xml @@ -517,4 +517,5 @@ you play %1$s · %2$s Play on from here with Maia… After %1$s + Sound