Compare commits
2 Commits
166a2e1ffb
...
600c1e1407
| Author | SHA1 | Date | |
|---|---|---|---|
| 600c1e1407 | |||
| c41a758b49 |
@@ -81,6 +81,13 @@ class LockstepViewModel(
|
||||
false,
|
||||
)
|
||||
|
||||
val feedAccelToPasada: StateFlow<Boolean> =
|
||||
prefs.feedAccelToPasada.stateIn(
|
||||
viewModelScope,
|
||||
SharingStarted.Eagerly,
|
||||
true,
|
||||
)
|
||||
|
||||
fun setAnnotationMode(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
prefs.setAnnotationMode(enabled)
|
||||
@@ -93,6 +100,12 @@ class LockstepViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun setFeedAccelToPasada(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
prefs.setFeedAccelToPasada(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
private val context get() = getApplication<Application>()
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,6 +39,12 @@ class UserPreferencesRepository(
|
||||
prefs[KEY_COLLECT_RUN_DATA] == true
|
||||
}
|
||||
|
||||
/** When true, accelerometer samples are forwarded to libpasada during playback (not while annotating). */
|
||||
val feedAccelToPasada: Flow<Boolean> =
|
||||
dataStore.data.map { prefs ->
|
||||
prefs[KEY_FEED_ACCEL_TO_PASADA] != false
|
||||
}
|
||||
|
||||
suspend fun setOnboardingComplete(done: Boolean) {
|
||||
dataStore.edit { prefs ->
|
||||
prefs[KEY_ONBOARDING_COMPLETE] = done
|
||||
@@ -67,10 +73,17 @@ class UserPreferencesRepository(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setFeedAccelToPasada(enabled: Boolean) {
|
||||
dataStore.edit { prefs ->
|
||||
prefs[KEY_FEED_ACCEL_TO_PASADA] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val KEY_ONBOARDING_COMPLETE = booleanPreferencesKey("onboarding_complete")
|
||||
private val KEY_SPOTIFY_ACCESS_TOKEN = stringPreferencesKey("spotify_access_token")
|
||||
private val KEY_ANNOTATION_MODE = booleanPreferencesKey("annotation_mode")
|
||||
private val KEY_COLLECT_RUN_DATA = booleanPreferencesKey("collect_run_data")
|
||||
private val KEY_FEED_ACCEL_TO_PASADA = booleanPreferencesKey("feed_accel_to_pasada")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,8 +37,14 @@ public final class LibPasada {
|
||||
* @param fd open read FD (Java retains ownership; do not close until track changes)
|
||||
* @param offset start offset within the FD (0 for whole file)
|
||||
* @param length byte length from offset ({@code -1} if unknown / to EOF)
|
||||
* @param beatTimesSec beat times in seconds from track start, or {@code null} when unknown
|
||||
*/
|
||||
public static native void play(int fd, long offset, long length);
|
||||
public static native void play(int fd, long offset, long length, double[] beatTimesSec);
|
||||
|
||||
/** Same as {@link #play(int, long, long, double[])} with no beat annotation. */
|
||||
public static void play(int fd, long offset, long length) {
|
||||
play(fd, offset, length, null);
|
||||
}
|
||||
|
||||
/** PLAYING → PAUSED (silent output, graph kept alive). */
|
||||
public static native void pause();
|
||||
|
||||
@@ -16,9 +16,11 @@ import androidx.media.app.NotificationCompat.MediaStyle
|
||||
import at.lockstep.player.LockstepApplication
|
||||
import at.lockstep.player.MainActivity
|
||||
import at.lockstep.player.R
|
||||
import at.lockstep.player.data.db.FileMetadataEntity
|
||||
import at.lockstep.player.playback.engine.ExoPlayerMusicPlayerEngine
|
||||
import at.lockstep.player.playback.engine.PasadaMusicPlayerEngine
|
||||
import at.lockstep.player.playback.engine.MusicPlayerEngine
|
||||
import at.lockstep.player.util.BeatAnnotationStorage
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -48,9 +50,18 @@ class PlaybackService : Service() {
|
||||
private val binder = LocalBinder()
|
||||
|
||||
private var engine: MusicPlayerEngine? = null
|
||||
private var accelFeeder: PasadaAccelFeeder? = null
|
||||
private var positionPollJob: Job? = null
|
||||
private var positionCachePollJob: Job? = null
|
||||
|
||||
/** User preference: forward accelerometer samples to libpasada. */
|
||||
@Volatile
|
||||
private var feedAccelEnabled = true
|
||||
|
||||
/** True while the beat annotation screen is visible. */
|
||||
@Volatile
|
||||
private var isAnnotating = false
|
||||
|
||||
private val _uiState = MutableStateFlow(PlaybackUiState.initial())
|
||||
val uiState: StateFlow<PlaybackUiState> = _uiState.asStateFlow()
|
||||
|
||||
@@ -211,10 +222,44 @@ class PlaybackService : Service() {
|
||||
it.setListener(engineListener)
|
||||
it.initSession()
|
||||
engine = it
|
||||
refreshAccelFeeding()
|
||||
startPositionPolling()
|
||||
}
|
||||
}
|
||||
|
||||
/** Called when the user toggles "Adapt playback to movement" in Settings. */
|
||||
fun setFeedAccelEnabled(enabled: Boolean) {
|
||||
feedAccelEnabled = enabled
|
||||
refreshAccelFeeding()
|
||||
}
|
||||
|
||||
/** Called when entering or leaving the beat annotation screen. */
|
||||
fun setAnnotating(annotating: Boolean) {
|
||||
isAnnotating = annotating
|
||||
refreshAccelFeeding()
|
||||
}
|
||||
|
||||
private fun refreshAccelFeeding() {
|
||||
if (engine == null) {
|
||||
return
|
||||
}
|
||||
if (feedAccelEnabled && !isAnnotating) {
|
||||
startAccelFeeding()
|
||||
} else {
|
||||
stopAccelFeeding()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startAccelFeeding() {
|
||||
val feeder =
|
||||
accelFeeder ?: PasadaAccelFeeder(this).also { accelFeeder = it }
|
||||
feeder.start()
|
||||
}
|
||||
|
||||
private fun stopAccelFeeding() {
|
||||
accelFeeder?.stop()
|
||||
}
|
||||
|
||||
private fun startPositionPolling() {
|
||||
positionPollJob?.cancel()
|
||||
positionPollJob =
|
||||
@@ -241,6 +286,7 @@ class PlaybackService : Service() {
|
||||
positionPollJob = null
|
||||
positionCachePollJob?.cancel()
|
||||
positionCachePollJob = null
|
||||
stopAccelFeeding()
|
||||
engine?.setListener(null)
|
||||
engine?.releaseSession()
|
||||
engine = null
|
||||
@@ -291,32 +337,59 @@ class PlaybackService : Service() {
|
||||
|
||||
private fun publishCurrentTrack() {
|
||||
val item = queue.getOrNull(index) ?: return
|
||||
applyCurrentMediaItem(item)
|
||||
scope.launch {
|
||||
val beatTimesSec =
|
||||
withContext(Dispatchers.IO) {
|
||||
loadBeatTimesSecForTrack(item.id)
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
val current = queue.getOrNull(index) ?: return@withContext
|
||||
if (current.id != item.id) {
|
||||
return@withContext
|
||||
}
|
||||
applyCurrentMediaItem(current, beatTimesSec)
|
||||
val durationSec =
|
||||
(engine?.getDurationMs()?.takeIf { it > 0 }?.div(1000)?.toInt())
|
||||
?: (item.durationMsHint / 1000).coerceAtLeast(1)
|
||||
?: (current.durationMsHint / 1000).coerceAtLeast(1)
|
||||
_uiState.value =
|
||||
PlaybackUiState(
|
||||
title = item.title,
|
||||
artist = item.artist,
|
||||
title = current.title,
|
||||
artist = current.artist,
|
||||
progress = 0f,
|
||||
durationSeconds = durationSec,
|
||||
isPlaying = _uiState.value.isPlaying,
|
||||
currentTrackId = item.id,
|
||||
currentTrackId = current.id,
|
||||
currentQueueIndex = index,
|
||||
currentPlaylistPosition = item.playlistPosition,
|
||||
currentPlaylistPosition = current.playlistPosition,
|
||||
queueSize = queue.size,
|
||||
)
|
||||
updateProgressFromEngine()
|
||||
updateSessionMetadata(item, durationSec)
|
||||
updateSessionMetadata(current, durationSec)
|
||||
updatePlaybackStateFromEngine()
|
||||
refreshForegroundNotification()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyCurrentMediaItem(item: TrackQueueItem) {
|
||||
private suspend fun loadBeatTimesSecForTrack(trackId: String): DoubleArray? {
|
||||
val metadata =
|
||||
app.database.fileMetadataDao().findByTrackIdAndType(
|
||||
trackId,
|
||||
FileMetadataEntity.TYPE_BEATS,
|
||||
) ?: return null
|
||||
if (metadata.fileUri.isBlank()) {
|
||||
return null
|
||||
}
|
||||
return BeatAnnotationStorage.readBeatTimesSec(app, metadata.fileUri)
|
||||
}
|
||||
|
||||
private fun applyCurrentMediaItem(
|
||||
item: TrackQueueItem,
|
||||
beatTimesSec: DoubleArray?,
|
||||
) {
|
||||
val uri = item.localUri ?: return
|
||||
val e = ensureEngine()
|
||||
e.prepareTrack(uri)
|
||||
e.prepareTrack(uri, beatTimesSec)
|
||||
if (_uiState.value.isPlaying) {
|
||||
e.play()
|
||||
} else {
|
||||
@@ -540,6 +613,8 @@ class PlaybackService : Service() {
|
||||
|
||||
override fun onDestroy() {
|
||||
stopPlaybackAndTeardown()
|
||||
accelFeeder?.release()
|
||||
accelFeeder = null
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,10 @@ class ExoPlayerMusicPlayerEngine(
|
||||
.also { it.addListener(playerListener) }
|
||||
}
|
||||
|
||||
override fun prepareTrack(uri: Uri) {
|
||||
override fun prepareTrack(
|
||||
uri: Uri,
|
||||
beatTimesSec: DoubleArray?,
|
||||
) {
|
||||
val p = requirePlayer()
|
||||
p.setMediaItem(MediaItem.fromUri(uri))
|
||||
p.prepare()
|
||||
|
||||
@@ -14,7 +14,13 @@ interface MusicPlayerEngine {
|
||||
* Load a track from a local URI. Asynchronous — playback starts after prepare when [play] is
|
||||
* called (or immediately if [playWhenReady] is set via [play]).
|
||||
*/
|
||||
fun prepareTrack(uri: Uri)
|
||||
/**
|
||||
* @param beatTimesSec optional beat times in seconds from [TYPE_BEATS] metadata; passed to libpasada on play.
|
||||
*/
|
||||
fun prepareTrack(
|
||||
uri: Uri,
|
||||
beatTimesSec: DoubleArray? = null,
|
||||
)
|
||||
|
||||
fun play()
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ class PasadaMusicPlayerEngine(
|
||||
private var trackFd: Int? = null
|
||||
private var trackOffset: Long = 0L
|
||||
private var trackLength: Long = -1L
|
||||
private var pendingBeatTimesSec: DoubleArray? = null
|
||||
|
||||
/** FD is open but [LibPasada.play] has not been called yet for this track. */
|
||||
private var pendingStart = false
|
||||
@@ -73,10 +74,14 @@ class PasadaMusicPlayerEngine(
|
||||
sessionInitialized = true
|
||||
}
|
||||
|
||||
override fun prepareTrack(uri: Uri) {
|
||||
override fun prepareTrack(
|
||||
uri: Uri,
|
||||
beatTimesSec: DoubleArray?,
|
||||
) {
|
||||
closeOpenTrack()
|
||||
try {
|
||||
openTrack(uri)
|
||||
pendingBeatTimesSec = beatTimesSec
|
||||
pendingStart = true
|
||||
} catch (e: IOException) {
|
||||
closeOpenTrack()
|
||||
@@ -146,9 +151,14 @@ class PasadaMusicPlayerEngine(
|
||||
|
||||
private fun startPendingTrack() {
|
||||
val fd = trackFd ?: return
|
||||
Log.i(TAG, "LibPasada.play(fd=$fd, offset=$trackOffset, length=$trackLength) ...")
|
||||
LibPasada.play(fd, trackOffset, trackLength)
|
||||
val beats = pendingBeatTimesSec
|
||||
Log.i(
|
||||
TAG,
|
||||
"LibPasada.play(fd=$fd, offset=$trackOffset, length=$trackLength, beats=${beats?.size ?: 0}) ...",
|
||||
)
|
||||
LibPasada.play(fd, trackOffset, trackLength, beats)
|
||||
Log.i(TAG, "LibPasada.play() done.")
|
||||
pendingBeatTimesSec = null
|
||||
pendingStart = false
|
||||
}
|
||||
|
||||
@@ -182,6 +192,7 @@ class PasadaMusicPlayerEngine(
|
||||
trackFd = null
|
||||
trackOffset = 0L
|
||||
trackLength = -1L
|
||||
pendingBeatTimesSec = null
|
||||
pendingStart = false
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
@@ -85,6 +86,13 @@ fun AnnotationRoute(
|
||||
}
|
||||
}
|
||||
|
||||
DisposableEffect(playback) {
|
||||
playback?.setAnnotating(true)
|
||||
onDispose {
|
||||
playback?.setAnnotating(false)
|
||||
}
|
||||
}
|
||||
|
||||
val beatTimesMs = remember { mutableStateListOf<Long>() }
|
||||
val annotationSessionFolder = remember { RunDataStorage.newRunSessionFolderName() }
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.os.IBinder
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -34,8 +35,13 @@ fun LockstepAppNavHost(
|
||||
viewModel: LockstepViewModel,
|
||||
) {
|
||||
val annotationMode by viewModel.annotationMode.collectAsStateWithLifecycle()
|
||||
val feedAccelToPasada by viewModel.feedAccelToPasada.collectAsStateWithLifecycle()
|
||||
var playback by remember { mutableStateOf<PlaybackService?>(null) }
|
||||
|
||||
LaunchedEffect(feedAccelToPasada, playback) {
|
||||
playback?.setFeedAccelEnabled(feedAccelToPasada)
|
||||
}
|
||||
|
||||
DisposableEffect(Unit) {
|
||||
val connection =
|
||||
object : ServiceConnection {
|
||||
|
||||
@@ -234,6 +234,11 @@ fun NowPlayingRoute(
|
||||
}
|
||||
}
|
||||
|
||||
DisposableEffect(playback) {
|
||||
playback?.setAnnotating(false)
|
||||
onDispose { }
|
||||
}
|
||||
|
||||
LaunchedEffect(collectRunData, playback) {
|
||||
if (!collectRunData) {
|
||||
collector.setCollectingEnabled(false)
|
||||
|
||||
@@ -38,6 +38,7 @@ fun SettingsScreen(
|
||||
val context = LocalContext.current
|
||||
val annotationMode by viewModel.annotationMode.collectAsStateWithLifecycle()
|
||||
val collectRunData by viewModel.collectRunData.collectAsStateWithLifecycle()
|
||||
val feedAccelToPasada by viewModel.feedAccelToPasada.collectAsStateWithLifecycle()
|
||||
Scaffold(
|
||||
modifier = modifier.fillMaxSize(),
|
||||
topBar = {
|
||||
@@ -83,6 +84,30 @@ fun SettingsScreen(
|
||||
onCheckedChange = { viewModel.setAnnotationMode(it) },
|
||||
)
|
||||
}
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f).padding(end = 16.dp)) {
|
||||
Text(
|
||||
text = context.getString(R.string.settings_feed_accel_to_pasada),
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
Text(
|
||||
text = context.getString(R.string.settings_feed_accel_to_pasada_help),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
Switch(
|
||||
checked = feedAccelToPasada,
|
||||
onCheckedChange = { viewModel.setFeedAccelToPasada(it) },
|
||||
)
|
||||
}
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
|
||||
@@ -62,6 +62,25 @@ object BeatAnnotationStorage {
|
||||
return RunDataStorage.writeOrReplacePublicJsonFile(context, relativePath, fileName, jsonString)
|
||||
}
|
||||
|
||||
/** Reads [beatTimesSec] from a beats or annotation JSON file; {@code null} if missing or unreadable. */
|
||||
fun readBeatTimesSec(
|
||||
context: Context,
|
||||
fileUri: String,
|
||||
): DoubleArray? {
|
||||
if (fileUri.isBlank()) {
|
||||
return null
|
||||
}
|
||||
return try {
|
||||
context.contentResolver.openInputStream(Uri.parse(fileUri))?.use { stream ->
|
||||
val json = JSONObject(stream.bufferedReader().readText())
|
||||
val arr = json.optJSONArray("beatTimesSec") ?: return null
|
||||
DoubleArray(arr.length()) { i -> arr.getDouble(i) }
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun buildAnnotationJson(
|
||||
contentId: String,
|
||||
title: String,
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
<string name="settings_annotation_mode_help">When enabled, choosing a playlist opens beat annotation (tap in time) instead of Now playing.</string>
|
||||
<string name="settings_collect_run_data">Collect run data</string>
|
||||
<string name="settings_collect_run_data_help">When enabled, Now playing records accelerometer, gyroscope, and GPS (1 Hz) per song into Documents/Pasada/ under a timestamped run folder.</string>
|
||||
<string name="settings_feed_accel_to_pasada">Adapt playback to movement</string>
|
||||
<string name="settings_feed_accel_to_pasada_help">When enabled, accelerometer data is sent to libpasada during Now playing so tempo can follow your pace. Disabled automatically on the beat annotation screen.</string>
|
||||
<string name="annotation_title">Beat annotation</string>
|
||||
<string name="annotation_subtitle">Tap on each beat; times use the same clock as playback (ExoPlayer position).</string>
|
||||
<string name="annotation_tap_area_label">Tap here on the beat</string>
|
||||
|
||||
Reference in New Issue
Block a user