Android: sounds (the desktop's SFX set and the trainer's wrong move), a «Sound» setting
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run

Moves (capture, check), the end of a game against Maia, her offer to resign,
the trainer's right, wrong and last moves. Notices in About.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-26 01:23:00 +03:00
parent 66f86ae3e5
commit df22d2812d
16 changed files with 88 additions and 3 deletions

View file

@ -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).

View file

@ -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, Int> = 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
},
)
}

View file

@ -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",

View file

@ -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)
}
})

View file

@ -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))

View file

@ -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 -> {}
}

View file

@ -513,4 +513,5 @@
<string name="play_adjourned_sub">Sie spielen %1$s · %2$s</string>
<string name="play_from_maia">Von hier gegen Maia weiterspielen…</string>
<string name="play_from_label">Nach %1$s</string>
<string name="sound">Ton</string>
</resources>

View file

@ -521,4 +521,5 @@
<string name="play_adjourned_sub">juegas con %1$s · %2$s</string>
<string name="play_from_maia">Seguir desde aquí contra Maia…</string>
<string name="play_from_label">Tras %1$s</string>
<string name="sound">Sonido</string>
</resources>

View file

@ -521,4 +521,5 @@
<string name="play_adjourned_sub">vous jouez %1$s · %2$s</string>
<string name="play_from_maia">Continuer d’ici contre Maia…</string>
<string name="play_from_label">Après %1$s</string>
<string name="sound">Son</string>
</resources>

View file

@ -521,4 +521,5 @@
<string name="play_adjourned_sub">giochi con %1$s · %2$s</string>
<string name="play_from_maia">Continua da qui contro Maia…</string>
<string name="play_from_label">Dopo %1$s</string>
<string name="sound">Suono</string>
</resources>

View file

@ -514,4 +514,5 @@
<string name="play_adjourned_sub">сіз %1$s · %2$s</string>
<string name="play_from_maia">Осы жерден Maia-мен жалғастыру…</string>
<string name="play_from_label">%1$s кейін</string>
<string name="sound">Дыбыс</string>
</resources>

View file

@ -522,4 +522,5 @@
<string name="play_adjourned_sub">você joga de %1$s · %2$s</string>
<string name="play_from_maia">Continuar daqui contra a Maia…</string>
<string name="play_from_label">Depois de %1$s</string>
<string name="sound">Som</string>
</resources>

View file

@ -529,4 +529,5 @@
<string name="play_adjourned_sub">вы %1$s · %2$s</string>
<string name="play_from_maia">Играть отсюда с Maia…</string>
<string name="play_from_label">После %1$s</string>
<string name="sound">Звук</string>
</resources>

View file

@ -512,4 +512,5 @@
<string name="play_adjourned_sub">siz %1$s · %2$s</string>
<string name="play_from_maia">Shu yerdan Maia bilan davom ettirish…</string>
<string name="play_from_label">%1$s dan keyin</string>
<string name="sound">Ovoz</string>
</resources>

View file

@ -517,4 +517,5 @@
<string name="play_adjourned_sub">you play %1$s · %2$s</string>
<string name="play_from_maia">Play on from here with Maia…</string>
<string name="play_from_label">After %1$s</string>
<string name="sound">Sound</string>
</resources>