react-native-audio-browser 0.2.0-next.95 → 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/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/SleepTimerTest.kt +48 -0
- package/android/src/test/java/com/audiobrowser/player/VolumeFaderTest.kt +43 -0
- package/ios/Player/Player.swift +1 -0
- package/ios/Player/SleepTimerManager.swift +33 -7
- package/ios/Player/VolumeFader.swift +38 -29
- package/ios/Tests/PlayerTests.swift +5 -5
- package/ios/Tests/SleepTimerManagerTests.swift +31 -3
- package/package.json +1 -1
|
@@ -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,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
|
+
}
|
package/ios/Player/Player.swift
CHANGED
|
@@ -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
|
|
|
@@ -11,7 +11,7 @@ private final class Volume {
|
|
|
11
11
|
private func makeManager() -> (SleepTimerManager, ManualSleepTimerScheduler, Volume, VolumeFader) {
|
|
12
12
|
let clock = ManualSleepTimerScheduler()
|
|
13
13
|
let volume = Volume()
|
|
14
|
-
let fader = VolumeFader(getVolume: { volume.level }, setVolume: { volume.level = $0 })
|
|
14
|
+
let fader = VolumeFader(getVolume: { volume.level }, setVolume: { volume.level = $0 }, scheduler: clock)
|
|
15
15
|
return (SleepTimerManager(fader: fader, scheduler: clock), clock, volume, fader)
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -36,17 +36,45 @@ struct SleepTimerManagerTests {
|
|
|
36
36
|
#expect(manager.isFading)
|
|
37
37
|
#expect(fader.duration == 0.15)
|
|
38
38
|
#expect(fader.originalVolume == 0.8)
|
|
39
|
+
#expect(volume.level < 0.8)
|
|
39
40
|
#expect(completes == 0)
|
|
40
|
-
volume.level = 0.1 // stands in for the fade lowering it
|
|
41
41
|
|
|
42
42
|
clock.advance(to: 0.4)
|
|
43
43
|
#expect(completes == 1)
|
|
44
|
-
#expect(volumeAtHalt
|
|
44
|
+
#expect(volumeAtHalt.map { $0 < 0.8 } == true) // halted before the restore
|
|
45
45
|
#expect(volume.level == 0.8)
|
|
46
46
|
#expect(!manager.isFading)
|
|
47
47
|
#expect(manager.get() == nil)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
@Test @MainActor
|
|
51
|
+
func lateFadeStart_joinsTheRampWhereItWouldBe() {
|
|
52
|
+
let (manager, clock, volume, _) = makeManager()
|
|
53
|
+
manager.set(seconds: 20, fadeDuration: 10)
|
|
54
|
+
|
|
55
|
+
clock.advance(to: 15) // the fade, due at 10, runs 5 s late
|
|
56
|
+
|
|
57
|
+
#expect(volume.level == 0.8 * VolumeFader.gain(at: 0.5))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Test @MainActor
|
|
61
|
+
func fadeFallsByEqualDecibelsEachStep() {
|
|
62
|
+
let (manager, clock, volume, _) = makeManager()
|
|
63
|
+
manager.set(seconds: 20, fadeDuration: 10)
|
|
64
|
+
|
|
65
|
+
var decibels: [Float] = []
|
|
66
|
+
for second in 10 ... 19 {
|
|
67
|
+
clock.advance(to: TimeInterval(second))
|
|
68
|
+
decibels.append(20 * log10(volume.level / 0.8))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let drops = zip(decibels, decibels.dropFirst()).map { $0 - $1 }
|
|
72
|
+
for drop in drops {
|
|
73
|
+
#expect(abs(drop + VolumeFader.floorDecibels / 10) < 0.01)
|
|
74
|
+
}
|
|
75
|
+
#expect(abs(decibels.last! - 0.9 * VolumeFader.floorDecibels) < 0.01)
|
|
76
|
+
}
|
|
77
|
+
|
|
50
78
|
@Test @MainActor
|
|
51
79
|
func fadeLongerThanTimer_clampsAndStartsImmediately() {
|
|
52
80
|
let (manager, clock, _, fader) = makeManager()
|