feat: add the Kotlin Multiplatform AudioManager client
A shared Compose Multiplatform client for Android, desktop and web on the native mood-first API. - Bearer login with persisted, restorable sessions. - Mood grid and full editor (create, settings, seeds, delete). - Server-driven radio sessions with automatic started/completed/skipped events and prefetching. - Add to current mood or choose another mood from the player. - Library search, original MP3 downloads, offline playback of downloads. - Offline cache of moods, songs and the last sync revision. - Platform audio: Media3 on Android, MP3SPI on desktop, HTML audio on web. - Platform storage: filesystem on Android/desktop, localStorage on web. - Desktop tests for API parsing, errors and cache persistence. - Gitea CI image with Android SDK 35, build pipeline and tagged releases.
This commit is contained in:
commit
3281e46104
40 files changed
+7021
No files matched your search
+126
@@ -0,0 +1,126 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.InputStream
|
||||
import java.net.URI
|
||||
import javax.sound.sampled.AudioSystem
|
||||
import javax.sound.sampled.SourceDataLine
|
||||
import kotlin.concurrent.thread
|
||||
import win.taptap.musicmanager.client.api.Song
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
/**
|
||||
* Desktop playback through the MP3SPI decoder and a PCM line. Pause/resume is a
|
||||
* decoder loop gate, so no audio device is reopened. Seeking is not supported.
|
||||
*/
|
||||
class DesktopAudioPlayer : AudioPlayer {
|
||||
|
||||
private var input: InputStream? = null
|
||||
private var line: SourceDataLine? = null
|
||||
private var worker: Thread? = null
|
||||
private val lock = Object()
|
||||
|
||||
@Volatile private var paused = false
|
||||
@Volatile private var stopped = false
|
||||
@Volatile private var finished = false
|
||||
@Volatile private var framesRead = 0L
|
||||
private var frameSize = 4
|
||||
private var frameRate = 44_100f
|
||||
private var duration = 0.0
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean) {
|
||||
stop()
|
||||
finished = false
|
||||
paused = false
|
||||
stopped = false
|
||||
framesRead = 0
|
||||
duration = song.duration.toDouble()
|
||||
|
||||
val raw = if (local) FileInputStream(File(source)) else URI(source).toURL().openStream()
|
||||
val buffered = BufferedInputStream(raw, 128 * 1024)
|
||||
input = buffered
|
||||
val audio = AudioSystem.getAudioInputStream(buffered)
|
||||
frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4
|
||||
frameRate = audio.format.frameRate.takeIf { it > 0 } ?: 44_100f
|
||||
|
||||
val info = javax.sound.sampled.DataLine.Info(SourceDataLine::class.java, audio.format)
|
||||
val dataLine = AudioSystem.getLine(info) as SourceDataLine
|
||||
dataLine.open(audio.format)
|
||||
dataLine.start()
|
||||
line = dataLine
|
||||
|
||||
worker = thread(isDaemon = true, name = "audiomanager-player") {
|
||||
val buffer = ByteArray(16 * 1024)
|
||||
try {
|
||||
while (!stopped) {
|
||||
synchronized(lock) {
|
||||
while (paused && !stopped) lock.wait()
|
||||
}
|
||||
if (stopped) break
|
||||
|
||||
val read = audio.read(buffer)
|
||||
if (read < 0) {
|
||||
finished = true
|
||||
break
|
||||
}
|
||||
dataLine.write(buffer, 0, read)
|
||||
framesRead += read.toLong()
|
||||
}
|
||||
dataLine.drain()
|
||||
} catch (_: Throwable) {
|
||||
// Stopping closes the stream; that is expected.
|
||||
} finally {
|
||||
runCatching { dataLine.close() }
|
||||
runCatching { audio.close() }
|
||||
runCatching { buffered.close() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
paused = true
|
||||
}
|
||||
|
||||
override fun resume() {
|
||||
synchronized(lock) {
|
||||
paused = false
|
||||
lock.notifyAll()
|
||||
}
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
stopped = true
|
||||
paused = false
|
||||
synchronized(lock) { lock.notifyAll() }
|
||||
runCatching { line?.stop() }
|
||||
runCatching { line?.flush() }
|
||||
worker?.interrupt()
|
||||
runCatching { worker?.join(400) }
|
||||
runCatching { input?.close() }
|
||||
line = null
|
||||
input = null
|
||||
worker = null
|
||||
}
|
||||
|
||||
override fun isPlaying(): Boolean =
|
||||
!paused && !finished && worker?.isAlive == true
|
||||
|
||||
override fun isFinished(): Boolean = finished
|
||||
|
||||
override fun positionSeconds(): Double {
|
||||
val seconds = framesRead.toDouble() / frameSize / frameRate
|
||||
return seconds.coerceIn(0.0, duration.takeIf { it > 0 } ?: seconds)
|
||||
}
|
||||
|
||||
override fun durationSeconds(): Double = duration
|
||||
|
||||
override val canSeek: Boolean = false
|
||||
|
||||
override fun seekTo(seconds: Double) = Unit
|
||||
|
||||
override fun release() {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.unit.DpSize
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Window
|
||||
import androidx.compose.ui.window.application
|
||||
import androidx.compose.ui.window.rememberWindowState
|
||||
import win.taptap.musicmanager.client.ui.App
|
||||
|
||||
fun main() = application {
|
||||
Window(
|
||||
onCloseRequest = ::exitApplication,
|
||||
title = "AudioManager",
|
||||
state = rememberWindowState(size = DpSize(1100.dp, 820.dp)),
|
||||
) {
|
||||
App()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun PlatformBackHandler(enabled: Boolean, onBack: () -> Unit) {
|
||||
// Desktop navigation is explicit; no system back gesture.
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package win.taptap.musicmanager.client
|
||||
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.toComposeImageBitmap
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import java.io.File
|
||||
import win.taptap.musicmanager.client.data.AppStorage
|
||||
import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
actual fun platformEngine(): HttpClientEngine = CIO.create()
|
||||
|
||||
actual val platformName: String = "desktop"
|
||||
|
||||
actual fun defaultServerUrl(): String = "http://localhost:8080"
|
||||
|
||||
actual fun platformStorage(): AppStorage = DesktopStorage()
|
||||
|
||||
actual fun createAudioPlayer(): AudioPlayer = DesktopAudioPlayer()
|
||||
|
||||
class DesktopStorage : AppStorage {
|
||||
|
||||
private val root: File = run {
|
||||
val base = System.getenv("XDG_DATA_HOME")?.takeIf { it.isNotBlank() }
|
||||
?: (System.getProperty("user.home") + "/.local/share")
|
||||
File(base, "AudioManager").apply { mkdirs() }
|
||||
}
|
||||
private val downloads = File(root, "downloads").apply { mkdirs() }
|
||||
|
||||
override fun read(name: String): String? =
|
||||
File(root, name).takeIf { it.isFile }?.readText()
|
||||
|
||||
override fun write(name: String, text: String) {
|
||||
root.mkdirs()
|
||||
File(root, name).writeText(text)
|
||||
}
|
||||
|
||||
override fun delete(name: String) {
|
||||
File(root, name).delete()
|
||||
}
|
||||
|
||||
override fun saveDownload(songId: String, bytes: ByteArray): String? {
|
||||
downloads.mkdirs()
|
||||
val file = File(downloads, "${safeName(songId)}.mp3")
|
||||
file.writeBytes(bytes)
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
override fun downloadPath(songId: String): String? =
|
||||
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath
|
||||
|
||||
override fun listDownloads(): Map<String, Long> =
|
||||
downloads.listFiles()
|
||||
?.filter { it.isFile && it.extension == "mp3" }
|
||||
?.associate { it.nameWithoutExtension to it.length() }
|
||||
?: emptyMap()
|
||||
|
||||
override fun deleteDownload(songId: String) {
|
||||
File(downloads, "${safeName(songId)}.mp3").delete()
|
||||
}
|
||||
|
||||
override fun storageUsedBytes(): Long =
|
||||
downloads.listFiles()?.sumOf { it.length() } ?: 0L
|
||||
|
||||
private fun safeName(songId: String): String =
|
||||
songId.map { if (it.isLetterOrDigit() || it == '-' || it == '_') it else '_' }.joinToString("")
|
||||
}
|
||||
|
||||
actual fun decodeImage(bytes: ByteArray): ImageBitmap? =
|
||||
runCatching { org.jetbrains.skia.Image.makeFromEncoded(bytes).toComposeImageBitmap() }.getOrNull()
|
||||
Reference in new issue
Block a user