react-native-audio-browser 0.2.0-next.94 → 0.2.0-next.96
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/audiobrowser/AudioBrowser.kt +2 -0
- package/android/src/main/java/com/audiobrowser/player/Player.kt +24 -0
- package/android/src/main/java/com/audiobrowser/player/PlayerListener.kt +1 -0
- package/android/src/main/java/com/audiobrowser/player/SleepTimer.kt +7 -4
- package/android/src/main/java/com/audiobrowser/player/SleepTimerManager.kt +2 -2
- package/android/src/main/java/com/audiobrowser/player/VolumeFader.kt +18 -7
- package/android/src/test/java/com/audiobrowser/player/PlayerQueueVersionTest.kt +79 -0
- package/android/src/test/java/com/audiobrowser/player/SleepTimerTest.kt +48 -0
- package/android/src/test/java/com/audiobrowser/player/VolumeFaderTest.kt +43 -0
- package/ios/HybridAudioBrowser+Player.swift +4 -0
- package/ios/Player/Player.swift +2 -0
- package/ios/Player/QueueManager.swift +12 -1
- package/ios/Player/SleepTimerManager.swift +33 -7
- package/ios/Player/VolumeFader.swift +38 -29
- package/ios/Tests/PlayerTests.swift +5 -5
- package/ios/Tests/QueueManagerTests.swift +40 -0
- package/ios/Tests/SleepTimerManagerTests.swift +31 -3
- package/lib/commonjs/features/queue/queue.js +11 -0
- package/lib/commonjs/features/queue/queue.js.map +1 -1
- package/lib/commonjs/web/NativeAudioBrowser.js +5 -0
- package/lib/commonjs/web/NativeAudioBrowser.js.map +1 -1
- package/lib/module/features/queue/queue.js +10 -0
- package/lib/module/features/queue/queue.js.map +1 -1
- package/lib/module/web/NativeAudioBrowser.js +5 -0
- package/lib/module/web/NativeAudioBrowser.js.map +1 -1
- package/lib/typescript/src/features/queue/queue.d.ts +7 -0
- package/lib/typescript/src/features/queue/queue.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.cpp +5 -0
- package/nitrogen/generated/android/c++/JHybridAudioBrowserSpec.hpp +1 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/audiobrowser/HybridAudioBrowserSpec.kt +4 -0
- package/nitrogen/generated/ios/c++/HybridAudioBrowserSpecSwift.hpp +8 -0
- package/nitrogen/generated/ios/swift/HybridAudioBrowserSpec.swift +1 -0
- package/nitrogen/generated/ios/swift/HybridAudioBrowserSpec_cxx.swift +12 -0
- package/nitrogen/generated/shared/c++/HybridAudioBrowserSpec.cpp +1 -0
- package/nitrogen/generated/shared/c++/HybridAudioBrowserSpec.hpp +1 -0
- package/package.json +1 -1
- package/src/features/queue/queue.ts +10 -0
- package/src/specs/audio-browser.nitro.ts +7 -0
- package/src/web/NativeAudioBrowser.playWhenReady.test.ts +33 -0
- package/src/web/NativeAudioBrowser.ts +7 -0
|
@@ -977,6 +977,8 @@ class AudioBrowser : HybridAudioBrowserSpec(), ServiceConnection {
|
|
|
977
977
|
|
|
978
978
|
override fun getQueue(): Array<Track> = readPlayer(emptyArray()) { it.tracks }
|
|
979
979
|
|
|
980
|
+
override fun getQueueVersion(): Double = readPlayer(-1.0) { it.queueVersion.toDouble() }
|
|
981
|
+
|
|
980
982
|
override fun getTrack(index: Double): Track? =
|
|
981
983
|
readPlayer(null) { player ->
|
|
982
984
|
try {
|
|
@@ -496,6 +496,26 @@ internal constructor(
|
|
|
496
496
|
val isLastInPlaybackOrder: Boolean
|
|
497
497
|
get() = sessionPlayer.nextMediaItemIndex == C.INDEX_UNSET
|
|
498
498
|
|
|
499
|
+
/**
|
|
500
|
+
* Changes whenever the queue's tracks or their order change, whoever changed them, and never
|
|
501
|
+
* repeats in the process, so equal readings mean the same queue even across players.
|
|
502
|
+
*/
|
|
503
|
+
var queueVersion = 0
|
|
504
|
+
private set
|
|
505
|
+
|
|
506
|
+
private var versionedMediaIds = emptyList<String>()
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* Compares media ids: an engine rebuild sets the same items again and reports a playlist change.
|
|
510
|
+
*/
|
|
511
|
+
internal fun onPlaylistChanged() {
|
|
512
|
+
val mediaIds =
|
|
513
|
+
(0 until sessionPlayer.mediaItemCount).map { sessionPlayer.getMediaItemAt(it).mediaId }
|
|
514
|
+
if (mediaIds == versionedMediaIds) return
|
|
515
|
+
versionedMediaIds = mediaIds
|
|
516
|
+
queueVersion = ++lastQueueVersion
|
|
517
|
+
}
|
|
518
|
+
|
|
499
519
|
/** The path the queue was expanded from, so selecting from the same source skips re-expanding. */
|
|
500
520
|
var queueSourcePath: String? = null
|
|
501
521
|
internal set
|
|
@@ -1542,4 +1562,8 @@ internal constructor(
|
|
|
1542
1562
|
fun resetBufferConfig() {
|
|
1543
1563
|
loadControl.resetToDefaults()
|
|
1544
1564
|
}
|
|
1565
|
+
|
|
1566
|
+
private companion object {
|
|
1567
|
+
var lastQueueVersion = 0
|
|
1568
|
+
}
|
|
1545
1569
|
}
|
|
@@ -333,6 +333,7 @@ class PlayerListener(private val player: Player) : MediaPlayer.Listener {
|
|
|
333
333
|
|
|
334
334
|
override fun onTimelineChanged(timeline: Timeline, reason: Int) {
|
|
335
335
|
if (reason == MediaPlayer.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED) {
|
|
336
|
+
player.onPlaylistChanged()
|
|
336
337
|
player.callbacks?.onPlaybackQueueChanged(player.tracks)
|
|
337
338
|
// Replacing the current item with one of the same URL updates it in place, without a
|
|
338
339
|
// transition, so a changed `live` arrives only here.
|
|
@@ -2,6 +2,7 @@ package com.audiobrowser.player
|
|
|
2
2
|
|
|
3
3
|
import android.os.Handler
|
|
4
4
|
import android.os.Looper
|
|
5
|
+
import android.os.SystemClock
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* A sleep timer whose hooks run on the main looper: [onComplete] at the deadline, and with a fade,
|
|
@@ -31,19 +32,20 @@ open class SleepTimer {
|
|
|
31
32
|
onFadeCancel()
|
|
32
33
|
sleepWhenPlayedToEnd = false
|
|
33
34
|
|
|
35
|
+
val deadlineUptimeMs = SystemClock.uptimeMillis() + (seconds * 1000).toLong()
|
|
34
36
|
val runnable = Runnable { complete() }
|
|
35
37
|
this.runnable = runnable
|
|
36
|
-
handler.
|
|
38
|
+
handler.postAtTime(runnable, deadlineUptimeMs)
|
|
37
39
|
time = System.currentTimeMillis() + (seconds * 1000)
|
|
38
40
|
|
|
39
41
|
if (fadeDuration != null && fadeDuration > 0) {
|
|
40
42
|
val fade = minOf(fadeDuration, seconds)
|
|
41
43
|
val fadeRunnable = Runnable {
|
|
42
44
|
this.fadeRunnable = null
|
|
43
|
-
onFadeStart(fade)
|
|
45
|
+
onFadeStart(deadlineUptimeMs, fade)
|
|
44
46
|
}
|
|
45
47
|
this.fadeRunnable = fadeRunnable
|
|
46
|
-
handler.
|
|
48
|
+
handler.postAtTime(fadeRunnable, deadlineUptimeMs - (fade * 1000).toLong())
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
|
|
@@ -70,7 +72,8 @@ open class SleepTimer {
|
|
|
70
72
|
|
|
71
73
|
open fun onComplete() {}
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
/** [deadlineUptimeMs] is the deadline on [SystemClock.uptimeMillis]. */
|
|
76
|
+
open fun onFadeStart(deadlineUptimeMs: Long, durationSeconds: Double) {}
|
|
74
77
|
|
|
75
78
|
/** Called on every re-arm or clear, whether or not a fade is running; not on completion. */
|
|
76
79
|
open fun onFadeCancel() {}
|
|
@@ -19,8 +19,8 @@ internal class SleepTimerManager(
|
|
|
19
19
|
|
|
20
20
|
private val timer =
|
|
21
21
|
object : SleepTimer() {
|
|
22
|
-
override fun onFadeStart(durationSeconds: Double) {
|
|
23
|
-
volumeFader.start(durationSeconds)
|
|
22
|
+
override fun onFadeStart(deadlineUptimeMs: Long, durationSeconds: Double) {
|
|
23
|
+
volumeFader.start(deadlineUptimeMs, durationSeconds)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
override fun onFadeCancel() {
|
|
@@ -3,10 +3,12 @@ package com.audiobrowser.player
|
|
|
3
3
|
import android.os.Handler
|
|
4
4
|
import android.os.Looper
|
|
5
5
|
import android.os.SystemClock
|
|
6
|
+
import kotlin.math.pow
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
* Ramps the player volume to silence
|
|
9
|
-
* The gain
|
|
9
|
+
* Ramps the player volume to silence by a deadline (the sleep timer's fade), on the main looper.
|
|
10
|
+
* The gain falls linearly in decibels to [FLOOR_DECIBELS], so every second of the fade is heard as
|
|
11
|
+
* one; a gain that falls linearly or squared spends its last seconds inaudible.
|
|
10
12
|
*/
|
|
11
13
|
class VolumeFader(private val getVolume: () -> Float, private val setVolume: (Float) -> Unit) {
|
|
12
14
|
private val handler = Handler(Looper.getMainLooper())
|
|
@@ -19,21 +21,24 @@ class VolumeFader(private val getVolume: () -> Float, private val setVolume: (Fl
|
|
|
19
21
|
val isActive: Boolean
|
|
20
22
|
get() = originalVolume != null
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Starts a fade that reaches silence at [endUptimeMs] ([SystemClock.uptimeMillis]),
|
|
26
|
+
* [durationSeconds] after it would have begun. Started late, it joins the ramp where it would be
|
|
27
|
+
* by now.
|
|
28
|
+
*/
|
|
29
|
+
fun start(endUptimeMs: Long, durationSeconds: Double) {
|
|
23
30
|
// Restarting mid-fade keeps the first fade's baseline, not the partly faded current volume.
|
|
24
31
|
val baseVolume = originalVolume ?: getVolume()
|
|
25
32
|
cancel(restoreVolume = false)
|
|
26
33
|
originalVolume = baseVolume
|
|
27
34
|
if (durationSeconds <= 0) return
|
|
28
35
|
|
|
29
|
-
val startTime = SystemClock.uptimeMillis()
|
|
30
36
|
val durationMs = durationSeconds * 1000
|
|
31
37
|
val runnable =
|
|
32
38
|
object : Runnable {
|
|
33
39
|
override fun run() {
|
|
34
|
-
val progress = (
|
|
35
|
-
|
|
36
|
-
setVolume(baseVolume * remaining * remaining)
|
|
40
|
+
val progress = 1 - (endUptimeMs - SystemClock.uptimeMillis()) / durationMs
|
|
41
|
+
setVolume(baseVolume * gain(progress))
|
|
37
42
|
if (progress < 1.0) {
|
|
38
43
|
handler.postDelayed(this, STEP_INTERVAL_MS)
|
|
39
44
|
} else {
|
|
@@ -70,5 +75,11 @@ class VolumeFader(private val getVolume: () -> Float, private val setVolume: (Fl
|
|
|
70
75
|
|
|
71
76
|
companion object {
|
|
72
77
|
private const val STEP_INTERVAL_MS = 50L
|
|
78
|
+
const val FLOOR_DECIBELS = -50.0
|
|
79
|
+
|
|
80
|
+
/** The gain, relative to the pre-fade volume, [progress] (0..1) of the way through a fade. */
|
|
81
|
+
fun gain(progress: Double): Float =
|
|
82
|
+
if (progress >= 1) 0f
|
|
83
|
+
else 10.0.pow(FLOOR_DECIBELS * progress.coerceAtLeast(0.0) / 20).toFloat()
|
|
73
84
|
}
|
|
74
85
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import com.audiobrowser.TestFixtures
|
|
4
|
+
import com.audiobrowser.model.PlayerSetupOptions
|
|
5
|
+
import com.audiobrowser.util.TrackFactory
|
|
6
|
+
import org.junit.After
|
|
7
|
+
import org.junit.Assert.assertFalse
|
|
8
|
+
import org.junit.Assert.assertNotEquals
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
10
|
+
import org.junit.Before
|
|
11
|
+
import org.junit.Test
|
|
12
|
+
import org.junit.runner.RunWith
|
|
13
|
+
import org.robolectric.RobolectricTestRunner
|
|
14
|
+
import org.robolectric.RuntimeEnvironment
|
|
15
|
+
|
|
16
|
+
@RunWith(RobolectricTestRunner::class)
|
|
17
|
+
class PlayerQueueVersionTest {
|
|
18
|
+
|
|
19
|
+
private val player = Player(RuntimeEnvironment.getApplication())
|
|
20
|
+
|
|
21
|
+
private fun tracks(vararg titles: String) =
|
|
22
|
+
titles.map { TestFixtures.track(title = it, src = "https://s/$it.mp3") }.toTypedArray()
|
|
23
|
+
|
|
24
|
+
@Before fun setUp() = player.setup(PlayerSetupOptions())
|
|
25
|
+
|
|
26
|
+
@After fun release() = player.exoPlayer.release()
|
|
27
|
+
|
|
28
|
+
private fun changes(mutate: () -> Unit): Boolean {
|
|
29
|
+
player.setQueue(tracks("a", "b", "c"))
|
|
30
|
+
val before = player.queueVersion
|
|
31
|
+
mutate()
|
|
32
|
+
return player.queueVersion != before
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@Test
|
|
36
|
+
fun `every change to the queue changes it before the call returns`() {
|
|
37
|
+
assertTrue(changes { player.setQueue(tracks("c", "b", "a")) })
|
|
38
|
+
assertTrue(changes { player.add(tracks("d")) })
|
|
39
|
+
assertTrue(changes { player.remove(listOf(1)) })
|
|
40
|
+
assertTrue(changes { player.move(0, 2) })
|
|
41
|
+
assertTrue(
|
|
42
|
+
changes { player.replaceTrack(0, TestFixtures.track(title = "x", src = "https://s/x.mp3")) }
|
|
43
|
+
)
|
|
44
|
+
assertTrue(changes { player.removeUpcomingTracks() })
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Test
|
|
48
|
+
fun `a queue the session sets, as a car pick does, changes it`() {
|
|
49
|
+
assertTrue(
|
|
50
|
+
changes {
|
|
51
|
+
player.sessionPlayer.setMediaItems(TrackFactory.toMedia3(tracks("c", "a")).toList())
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Test
|
|
57
|
+
fun `the same tracks set again leave it`() {
|
|
58
|
+
assertFalse(changes { player.setQueue(tracks("a", "b", "c")) })
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Test
|
|
62
|
+
fun `navigation leaves it`() {
|
|
63
|
+
assertFalse(changes { player.skipTo(2) })
|
|
64
|
+
assertFalse(changes { player.previous() })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Test
|
|
68
|
+
fun `it never repeats across players`() {
|
|
69
|
+
val second = Player(RuntimeEnvironment.getApplication())
|
|
70
|
+
try {
|
|
71
|
+
player.setQueue(tracks("a", "b", "c"))
|
|
72
|
+
second.setup(PlayerSetupOptions())
|
|
73
|
+
second.setQueue(tracks("a", "b", "c"))
|
|
74
|
+
assertNotEquals(player.queueVersion, second.queueVersion)
|
|
75
|
+
} finally {
|
|
76
|
+
second.exoPlayer.release()
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Looper
|
|
4
|
+
import android.os.SystemClock
|
|
5
|
+
import java.time.Duration
|
|
6
|
+
import org.junit.Assert.assertEquals
|
|
7
|
+
import org.junit.Assert.assertNull
|
|
8
|
+
import org.junit.Test
|
|
9
|
+
import org.junit.runner.RunWith
|
|
10
|
+
import org.robolectric.RobolectricTestRunner
|
|
11
|
+
import org.robolectric.Shadows.shadowOf
|
|
12
|
+
import org.robolectric.shadows.ShadowSystemClock
|
|
13
|
+
|
|
14
|
+
@RunWith(RobolectricTestRunner::class)
|
|
15
|
+
class SleepTimerTest {
|
|
16
|
+
|
|
17
|
+
private val looper = shadowOf(Looper.getMainLooper())
|
|
18
|
+
private var fadeArgs: Pair<Long, Double>? = null
|
|
19
|
+
private val timer =
|
|
20
|
+
object : SleepTimer() {
|
|
21
|
+
override fun onFadeStart(deadlineUptimeMs: Long, durationSeconds: Double) {
|
|
22
|
+
fadeArgs = deadlineUptimeMs to durationSeconds
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Test
|
|
27
|
+
fun `the fade starts its duration before the deadline`() {
|
|
28
|
+
val armedAt = SystemClock.uptimeMillis()
|
|
29
|
+
timer.sleepAfter(20.0, fadeDuration = 10.0)
|
|
30
|
+
|
|
31
|
+
looper.idleFor(Duration.ofMillis(9_999))
|
|
32
|
+
assertNull(fadeArgs)
|
|
33
|
+
|
|
34
|
+
looper.idleFor(Duration.ofMillis(1))
|
|
35
|
+
assertEquals(armedAt + 20_000 to 10.0, fadeArgs)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Test
|
|
39
|
+
fun `a fade that runs late still ends at the armed deadline`() {
|
|
40
|
+
val armedAt = SystemClock.uptimeMillis()
|
|
41
|
+
timer.sleepAfter(20.0, fadeDuration = 10.0)
|
|
42
|
+
|
|
43
|
+
ShadowSystemClock.advanceBy(Duration.ofSeconds(15))
|
|
44
|
+
looper.idle()
|
|
45
|
+
|
|
46
|
+
assertEquals(armedAt + 20_000 to 10.0, fadeArgs)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
package com.audiobrowser.player
|
|
2
|
+
|
|
3
|
+
import android.os.Looper
|
|
4
|
+
import android.os.SystemClock
|
|
5
|
+
import java.time.Duration
|
|
6
|
+
import kotlin.math.log10
|
|
7
|
+
import org.junit.Assert.assertEquals
|
|
8
|
+
import org.junit.Test
|
|
9
|
+
import org.junit.runner.RunWith
|
|
10
|
+
import org.robolectric.RobolectricTestRunner
|
|
11
|
+
import org.robolectric.Shadows.shadowOf
|
|
12
|
+
|
|
13
|
+
@RunWith(RobolectricTestRunner::class)
|
|
14
|
+
class VolumeFaderTest {
|
|
15
|
+
|
|
16
|
+
private var volume = 0.8f
|
|
17
|
+
private val fader = VolumeFader(getVolume = { volume }, setVolume = { volume = it })
|
|
18
|
+
private val looper = shadowOf(Looper.getMainLooper())
|
|
19
|
+
|
|
20
|
+
@Test
|
|
21
|
+
fun `a fade started late joins the ramp where it would be`() {
|
|
22
|
+
fader.start(SystemClock.uptimeMillis() + 5_000, durationSeconds = 10.0)
|
|
23
|
+
looper.idle()
|
|
24
|
+
|
|
25
|
+
assertEquals(0.8f * VolumeFader.gain(0.5), volume, 1e-6f)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@Test
|
|
29
|
+
fun `the fade falls by equal decibels each second`() {
|
|
30
|
+
fader.start(SystemClock.uptimeMillis() + 10_000, durationSeconds = 10.0)
|
|
31
|
+
looper.idle()
|
|
32
|
+
|
|
33
|
+
val decibels =
|
|
34
|
+
(0 until 10).map {
|
|
35
|
+
val level = 20 * log10(volume / 0.8)
|
|
36
|
+
looper.idleFor(Duration.ofSeconds(1))
|
|
37
|
+
level
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
decibels.zipWithNext { a, b -> assertEquals(VolumeFader.FLOOR_DECIBELS / 10, b - a, 0.01) }
|
|
41
|
+
assertEquals(0f, volume, 0f)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -268,6 +268,10 @@ public extension HybridAudioBrowser {
|
|
|
268
268
|
onMainActor { player?.tracks ?? [] }
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
+
func getQueueVersion() throws -> Double {
|
|
272
|
+
onMainActor { Double(player?.queueVersion ?? -1) }
|
|
273
|
+
}
|
|
274
|
+
|
|
271
275
|
func getTrack(index: Double) throws -> Track? {
|
|
272
276
|
onMainActor {
|
|
273
277
|
guard let tracks = player?.tracks else { return nil }
|
package/ios/Player/Player.swift
CHANGED
|
@@ -137,6 +137,7 @@ final class Player {
|
|
|
137
137
|
// MARK: - TrackSelectionPlayer and NowPlayingSource
|
|
138
138
|
|
|
139
139
|
var tracks: [Track] { queue.tracks }
|
|
140
|
+
var queueVersion: Int { queue.version }
|
|
140
141
|
var queueSourcePath: String? { queue.queueSourcePath }
|
|
141
142
|
var currentTrack: Track? { queue.currentTrack }
|
|
142
143
|
var currentTime: Double { engine.currentTime }
|
|
@@ -249,6 +250,7 @@ final class Player {
|
|
|
249
250
|
fader: VolumeFader(
|
|
250
251
|
getVolume: { [weak engine] in engine?.volume ?? 1 },
|
|
251
252
|
setVolume: { [weak engine] in engine?.volume = $0 },
|
|
253
|
+
scheduler: sleepTimerScheduler,
|
|
252
254
|
),
|
|
253
255
|
scheduler: sleepTimerScheduler,
|
|
254
256
|
)
|
|
@@ -32,7 +32,18 @@ class QueueManager {
|
|
|
32
32
|
/// The path the queue was expanded from, if any.
|
|
33
33
|
private(set) var queueSourcePath: String?
|
|
34
34
|
|
|
35
|
-
private(set) var tracks: [Track] = []
|
|
35
|
+
private(set) var tracks: [Track] = [] {
|
|
36
|
+
didSet {
|
|
37
|
+
Self.lastVersion += 1
|
|
38
|
+
version = Self.lastVersion
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private static var lastVersion = 0
|
|
43
|
+
|
|
44
|
+
/// Changes with every change to `tracks` and never repeats in the process, so equal readings
|
|
45
|
+
/// mean the same queue even across players.
|
|
46
|
+
private(set) var version = 0
|
|
36
47
|
|
|
37
48
|
private(set) var shuffleOrder = ShuffleOrder()
|
|
38
49
|
|
|
@@ -12,25 +12,49 @@ protocol SleepTimerJob: AnyObject {
|
|
|
12
12
|
func cancel()
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
extension DispatchWorkItem: SleepTimerJob {}
|
|
16
|
-
|
|
17
15
|
/// Runs work after a delay; injected so tests advance the clock by hand instead of racing it.
|
|
18
16
|
@MainActor
|
|
19
17
|
protocol SleepTimerScheduling {
|
|
18
|
+
/// Seconds on the clock `schedule` counts delays on.
|
|
19
|
+
var now: TimeInterval { get }
|
|
20
|
+
|
|
20
21
|
func schedule(
|
|
21
22
|
after delay: TimeInterval,
|
|
22
23
|
_ work: @escaping @Sendable @MainActor () -> Void,
|
|
23
24
|
) -> any SleepTimerJob
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
/// Fires on the main queue at the deadline. Strict and without leeway: `asyncAfter` lets the
|
|
28
|
+
/// system fire up to 5–10% of the delay late, seconds on a timer of minutes.
|
|
29
|
+
private final class StrictTimerJob: SleepTimerJob {
|
|
30
|
+
private let source: DispatchSourceTimer
|
|
31
|
+
|
|
32
|
+
init(after delay: TimeInterval, _ work: @escaping @Sendable @MainActor () -> Void) {
|
|
33
|
+
source = DispatchSource.makeTimerSource(flags: .strict, queue: .main)
|
|
34
|
+
source.schedule(deadline: .now() + delay, leeway: .nanoseconds(0))
|
|
35
|
+
// The handler holds the source until it fires or is cancelled.
|
|
36
|
+
source.setEventHandler { [source] in
|
|
37
|
+
source.cancel()
|
|
38
|
+
MainActor.assumeIsolated { work() }
|
|
39
|
+
}
|
|
40
|
+
source.resume()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func cancel() {
|
|
44
|
+
source.cancel()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
26
48
|
struct MainQueueSleepTimerScheduler: SleepTimerScheduling {
|
|
49
|
+
var now: TimeInterval {
|
|
50
|
+
TimeInterval(DispatchTime.now().uptimeNanoseconds) / 1_000_000_000
|
|
51
|
+
}
|
|
52
|
+
|
|
27
53
|
func schedule(
|
|
28
54
|
after delay: TimeInterval,
|
|
29
55
|
_ work: @escaping @Sendable @MainActor () -> Void,
|
|
30
56
|
) -> any SleepTimerJob {
|
|
31
|
-
|
|
32
|
-
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: job)
|
|
33
|
-
return job
|
|
57
|
+
StrictTimerJob(after: delay, work)
|
|
34
58
|
}
|
|
35
59
|
}
|
|
36
60
|
|
|
@@ -112,18 +136,20 @@ final class SleepTimerManager {
|
|
|
112
136
|
}
|
|
113
137
|
|
|
114
138
|
/// Stops playback `seconds` from now. With `fadeDuration`, the volume fades over that many
|
|
115
|
-
/// seconds (clamped to `seconds`) so silence lands at the deadline
|
|
139
|
+
/// seconds (clamped to `seconds`) so silence lands at the deadline, also when the fade starts
|
|
140
|
+
/// late.
|
|
116
141
|
func set(seconds: TimeInterval, fadeDuration: TimeInterval? = nil) {
|
|
117
142
|
cancelSleepTimerJob()
|
|
118
143
|
fader.cancel(restoringVolume: true)
|
|
119
144
|
sleepTimerTime = Date().timeIntervalSince1970 + seconds
|
|
145
|
+
let deadline = scheduler.now + seconds
|
|
120
146
|
sleepTimerJob = scheduler.schedule(after: seconds) { [weak self] in
|
|
121
147
|
self?.complete()
|
|
122
148
|
}
|
|
123
149
|
if let fadeDuration, fadeDuration > 0 {
|
|
124
150
|
let fade = min(fadeDuration, seconds)
|
|
125
151
|
fadeJob = scheduler.schedule(after: seconds - fade) { [weak self] in
|
|
126
|
-
self?.fader.start(duration: fade)
|
|
152
|
+
self?.fader.start(endingAt: deadline, duration: fade)
|
|
127
153
|
}
|
|
128
154
|
}
|
|
129
155
|
if let state = get() { onChanged?(state) }
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import Foundation
|
|
2
2
|
|
|
3
|
-
/// Ramps the volume down to silence
|
|
4
|
-
/// The gain
|
|
5
|
-
///
|
|
3
|
+
/// Ramps the volume down to silence by a deadline (the sleep-timer fade), stepping on the
|
|
4
|
+
/// scheduler's clock. The gain falls linearly in decibels to `floorDecibels`, so every second of
|
|
5
|
+
/// the fade is heard as one; a gain that falls linearly or squared spends its last seconds
|
|
6
|
+
/// inaudible.
|
|
6
7
|
@MainActor
|
|
7
8
|
final class VolumeFader {
|
|
8
|
-
|
|
9
|
+
static let stepInterval: TimeInterval = 0.05
|
|
10
|
+
static let floorDecibels: Float = -50
|
|
9
11
|
|
|
10
12
|
private let getVolume: () -> Float
|
|
11
13
|
private let setVolume: (Float) -> Void
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
private let scheduler: any SleepTimerScheduling
|
|
15
|
+
// nonisolated(unsafe) so `deinit`, which is nonisolated in Swift 6, can cancel it.
|
|
16
|
+
private nonisolated(unsafe) var step: (any SleepTimerJob)?
|
|
14
17
|
|
|
15
18
|
/// The volume before the fade, to restore once it resolves. Stays set from
|
|
16
19
|
/// `start` until `cancel` or `resolve`, also after the ramp reaches silence.
|
|
@@ -20,15 +23,27 @@ final class VolumeFader {
|
|
|
20
23
|
|
|
21
24
|
private(set) var duration: TimeInterval?
|
|
22
25
|
|
|
23
|
-
init(
|
|
26
|
+
init(
|
|
27
|
+
getVolume: @escaping () -> Float,
|
|
28
|
+
setVolume: @escaping (Float) -> Void,
|
|
29
|
+
scheduler: any SleepTimerScheduling,
|
|
30
|
+
) {
|
|
24
31
|
self.getVolume = getVolume
|
|
25
32
|
self.setVolume = setVolume
|
|
33
|
+
self.scheduler = scheduler
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
///
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
/// The gain, relative to the pre-fade volume, `progress` (0...1) of the way through a fade.
|
|
37
|
+
static func gain(at progress: Double) -> Float {
|
|
38
|
+
if progress >= 1 { return 0 }
|
|
39
|
+
return pow(10, floorDecibels * Float(max(0, progress)) / 20)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// Starts a fade that reaches silence at `end` on the scheduler's clock, `duration` seconds
|
|
43
|
+
/// after it would have begun. Started late, it joins the ramp where it would be by now.
|
|
44
|
+
/// Restarts from the pre-fade volume if one is running. A `duration` of zero or less marks the
|
|
45
|
+
/// fade active without touching the volume.
|
|
46
|
+
func start(endingAt end: TimeInterval, duration: TimeInterval) {
|
|
32
47
|
// A running fade's current volume is part-faded; its captured one is the
|
|
33
48
|
// value to restore.
|
|
34
49
|
let baseVolume = originalVolume ?? getVolume()
|
|
@@ -36,31 +51,25 @@ final class VolumeFader {
|
|
|
36
51
|
originalVolume = baseVolume
|
|
37
52
|
self.duration = duration
|
|
38
53
|
guard duration > 0 else { return }
|
|
54
|
+
ramp(base: baseVolume, end: end, duration: duration)
|
|
55
|
+
}
|
|
39
56
|
|
|
40
|
-
|
|
41
|
-
let
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
self.setVolume(baseVolume * remaining * remaining)
|
|
47
|
-
if progress >= 1 {
|
|
48
|
-
self.timer?.invalidate()
|
|
49
|
-
self.timer = nil
|
|
50
|
-
}
|
|
57
|
+
private func ramp(base: Float, end: TimeInterval, duration: TimeInterval) {
|
|
58
|
+
let progress = 1 - (end - scheduler.now) / duration
|
|
59
|
+
setVolume(base * Self.gain(at: progress))
|
|
60
|
+
step = progress < 1
|
|
61
|
+
? scheduler.schedule(after: Self.stepInterval) { [weak self] in
|
|
62
|
+
self?.ramp(base: base, end: end, duration: duration)
|
|
51
63
|
}
|
|
52
|
-
|
|
53
|
-
self.timer = timer
|
|
54
|
-
// `.common` so the ramp keeps running while UIKit tracks a scroll.
|
|
55
|
-
RunLoop.main.add(timer, forMode: .common)
|
|
64
|
+
: nil
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
/// Stops the ramp and ends the fade. With `restoringVolume` the pre-fade
|
|
59
68
|
/// volume comes back at once; without it the volume stays where the ramp
|
|
60
69
|
/// left it, for a caller that halts playback before restoring.
|
|
61
70
|
func cancel(restoringVolume: Bool) {
|
|
62
|
-
|
|
63
|
-
|
|
71
|
+
step?.cancel()
|
|
72
|
+
step = nil
|
|
64
73
|
guard let originalVolume else { return }
|
|
65
74
|
self.originalVolume = nil
|
|
66
75
|
duration = nil
|
|
@@ -77,6 +86,6 @@ final class VolumeFader {
|
|
|
77
86
|
}
|
|
78
87
|
|
|
79
88
|
deinit {
|
|
80
|
-
|
|
89
|
+
step?.cancel()
|
|
81
90
|
}
|
|
82
91
|
}
|
|
@@ -969,11 +969,11 @@ struct NaturalEndPlayIntentTests {
|
|
|
969
969
|
|
|
970
970
|
@Suite("Player - sleep timer fade")
|
|
971
971
|
struct SleepTimerFadeTests {
|
|
972
|
-
/// A 20 s timer whose 10 s fade
|
|
972
|
+
/// A 20 s timer whose 10 s fade is half done.
|
|
973
973
|
@MainActor
|
|
974
974
|
private func startFade(_ c: Player, _ clock: ManualSleepTimerScheduler) {
|
|
975
975
|
c.sleepTimerManager.set(seconds: 20, fadeDuration: 10)
|
|
976
|
-
clock.advance(to:
|
|
976
|
+
clock.advance(to: 15)
|
|
977
977
|
}
|
|
978
978
|
|
|
979
979
|
@Test @MainActor
|
|
@@ -982,7 +982,7 @@ struct SleepTimerFadeTests {
|
|
|
982
982
|
eh.volume = 0.8
|
|
983
983
|
|
|
984
984
|
startFade(c, clock)
|
|
985
|
-
eh.volume
|
|
985
|
+
#expect(eh.volume < 0.8)
|
|
986
986
|
|
|
987
987
|
#expect(c.sleepTimerManager.isFading)
|
|
988
988
|
c.sleepTimerManager.clear()
|
|
@@ -998,7 +998,7 @@ struct SleepTimerFadeTests {
|
|
|
998
998
|
eh.pausePlaybackCallCount = 0
|
|
999
999
|
|
|
1000
1000
|
startFade(c, clock)
|
|
1001
|
-
eh.volume
|
|
1001
|
+
#expect(eh.volume < 1.0)
|
|
1002
1002
|
|
|
1003
1003
|
clock.advance(to: 20)
|
|
1004
1004
|
|
|
@@ -1028,7 +1028,7 @@ struct SleepTimerFadeTests {
|
|
|
1028
1028
|
eh.volume = 0.8
|
|
1029
1029
|
|
|
1030
1030
|
startFade(c, clock)
|
|
1031
|
-
eh.volume
|
|
1031
|
+
#expect(eh.volume < 0.8)
|
|
1032
1032
|
|
|
1033
1033
|
c.sleepTimerManager.set(seconds: 60)
|
|
1034
1034
|
|
|
@@ -953,6 +953,46 @@ struct QueueChangeTests {
|
|
|
953
953
|
}
|
|
954
954
|
}
|
|
955
955
|
|
|
956
|
+
// MARK: - version
|
|
957
|
+
|
|
958
|
+
@Suite("version")
|
|
959
|
+
@MainActor
|
|
960
|
+
struct QueueVersionTests {
|
|
961
|
+
private func changes(_ mutate: (QueueManager) throws -> Void) rethrows -> Bool {
|
|
962
|
+
let q = QueueManager()
|
|
963
|
+
q.setQueue(tracks("a", "b", "c"), initialIndex: 0)
|
|
964
|
+
let before = q.version
|
|
965
|
+
try mutate(q)
|
|
966
|
+
return q.version != before
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
@Test func everyChangeToTheTracksChangesIt() throws {
|
|
970
|
+
#expect(changes { $0.setQueue(tracks("a", "b", "c")) })
|
|
971
|
+
#expect(changes { $0.add(tracks("d")) })
|
|
972
|
+
#expect(try changes { try $0.addAt(tracks("d"), at: 1) })
|
|
973
|
+
#expect(try changes { try $0.remove(1) })
|
|
974
|
+
#expect(try changes { try $0.move(fromIndex: 0, toIndex: 2) })
|
|
975
|
+
#expect(try changes { try $0.replace(1, Track(id: "x")) })
|
|
976
|
+
#expect(changes { $0.removeUpcomingTracks() })
|
|
977
|
+
#expect(changes { $0.clear() })
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
@Test func navigationLeavesIt() throws {
|
|
981
|
+
#expect(try !changes { try $0.skipTo(2) })
|
|
982
|
+
#expect(!changes { _ = $0.next() })
|
|
983
|
+
#expect(!changes { _ = $0.previous() })
|
|
984
|
+
#expect(!changes { $0.shuffleEnabled = true })
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
@Test func neverRepeatsAcrossQueueManagers() {
|
|
988
|
+
let first = QueueManager()
|
|
989
|
+
first.setQueue(tracks("a"))
|
|
990
|
+
let second = QueueManager()
|
|
991
|
+
second.setQueue(tracks("a"))
|
|
992
|
+
#expect(first.version != second.version)
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
|
|
956
996
|
// MARK: - canNext / canPrevious
|
|
957
997
|
|
|
958
998
|
@Suite("canNext / canPrevious")
|