|
1 |
| -//! Scheduler and Controller for precise timings |
2 |
| -
|
| 1 | +use crate::AtomicF64; |
3 | 2 | use std::sync::atomic::{AtomicBool, Ordering};
|
4 | 3 | use std::sync::Arc;
|
5 | 4 |
|
6 |
| -use crate::AtomicF64; |
7 |
| - |
8 |
| -/// Helper struct to start and stop audio streams |
9 |
| -#[derive(Clone, Debug)] |
10 |
| -pub(crate) struct Scheduler { |
11 |
| - inner: Arc<SchedulerInner>, |
12 |
| -} |
13 |
| - |
14 |
| -#[derive(Debug)] |
15 |
| -struct SchedulerInner { |
16 |
| - start: AtomicF64, |
17 |
| - stop: AtomicF64, |
| 5 | +/// Helper struct to control playback settings |
| 6 | +pub(crate) struct Controller { |
| 7 | + /// Raw values |
| 8 | + values: ControllerValues, |
| 9 | + /// Values to be shared with another thread |
| 10 | + shared: Arc<ControllerShared>, |
18 | 11 | }
|
19 | 12 |
|
20 |
| -// Uses the canonical ordering for handover of values, i.e. `Acquire` on load and `Release` on |
21 |
| -// store. |
22 |
| -impl Scheduler { |
23 |
| - /// Create a new Scheduler. Initial playback state will be: inactive. |
24 |
| - pub fn new() -> Self { |
25 |
| - let inner = SchedulerInner { |
26 |
| - start: AtomicF64::new(f64::MAX), |
27 |
| - stop: AtomicF64::new(f64::MAX), |
28 |
| - }; |
| 13 | +impl Default for Controller { |
| 14 | + /// Create a new Controller. It will not be active |
| 15 | + fn default() -> Self { |
| 16 | + let values = ControllerValues::default(); |
29 | 17 | Self {
|
30 |
| - inner: Arc::new(inner), |
| 18 | + values: values.clone(), |
| 19 | + shared: Arc::new(values.into()), |
31 | 20 | }
|
32 | 21 | }
|
| 22 | +} |
33 | 23 |
|
34 |
| - /// Retrieve playback start value |
35 |
| - pub fn get_start_at(&self) -> f64 { |
36 |
| - self.inner.start.load(Ordering::Acquire) |
| 24 | +impl Controller { |
| 25 | + pub fn shared(&self) -> &Arc<ControllerShared> { |
| 26 | + &self.shared |
37 | 27 | }
|
38 | 28 |
|
39 |
| - /// Schedule playback start at this timestamp |
40 |
| - pub fn start_at(&self, start: f64) { |
41 |
| - // todo panic on invalid values, or when already called |
42 |
| - self.inner.start.store(start, Ordering::Release); |
| 29 | + pub fn loop_(&self) -> bool { |
| 30 | + self.values.loop_ |
43 | 31 | }
|
44 | 32 |
|
45 |
| - /// Retrieve playback stop value |
46 |
| - pub fn get_stop_at(&self) -> f64 { |
47 |
| - self.inner.stop.load(Ordering::Acquire) |
| 33 | + pub fn set_loop(&mut self, loop_: bool) { |
| 34 | + self.values.loop_ = loop_; |
| 35 | + self.shared.loop_.store(loop_, Ordering::Release); |
48 | 36 | }
|
49 | 37 |
|
50 |
| - /// Stop playback at this timestamp |
51 |
| - pub fn stop_at(&self, stop: f64) { |
52 |
| - // todo panic on invalid values, or when already called |
53 |
| - self.inner.stop.store(stop, Ordering::Release); |
| 38 | + pub fn loop_start(&self) -> f64 { |
| 39 | + self.values.loop_start |
54 | 40 | }
|
55 |
| -} |
56 | 41 |
|
57 |
| -impl Default for Scheduler { |
58 |
| - fn default() -> Self { |
59 |
| - Self::new() |
| 42 | + pub fn set_loop_start(&mut self, loop_start: f64) { |
| 43 | + self.values.loop_start = loop_start; |
| 44 | + self.shared.loop_start.store(loop_start, Ordering::Release); |
| 45 | + } |
| 46 | + |
| 47 | + pub fn loop_end(&self) -> f64 { |
| 48 | + self.values.loop_end |
| 49 | + } |
| 50 | + |
| 51 | + pub fn set_loop_end(&mut self, loop_end: f64) { |
| 52 | + self.values.loop_end = loop_end; |
| 53 | + self.shared.loop_end.store(loop_end, Ordering::Release); |
60 | 54 | }
|
61 | 55 | }
|
62 | 56 |
|
63 |
| -/// Helper struct to control audio streams |
64 |
| -#[derive(Clone, Debug)] |
65 |
| -pub(crate) struct Controller { |
66 |
| - inner: Arc<ControllerInner>, |
| 57 | +#[derive(Debug, Clone)] |
| 58 | +pub(crate) struct ControllerValues { |
| 59 | + pub loop_: bool, |
| 60 | + pub loop_start: f64, |
| 61 | + pub loop_end: f64, |
| 62 | +} |
| 63 | + |
| 64 | +impl Default for ControllerValues { |
| 65 | + fn default() -> Self { |
| 66 | + Self { |
| 67 | + loop_: false, |
| 68 | + loop_start: 0., |
| 69 | + loop_end: f64::MAX, |
| 70 | + } |
| 71 | + } |
67 | 72 | }
|
68 | 73 |
|
69 | 74 | #[derive(Debug)]
|
70 |
| -struct ControllerInner { |
71 |
| - scheduler: Scheduler, |
| 75 | +pub(crate) struct ControllerShared { |
72 | 76 | loop_: AtomicBool,
|
73 | 77 | loop_start: AtomicF64,
|
74 | 78 | loop_end: AtomicF64,
|
75 |
| - offset: AtomicF64, |
76 |
| - duration: AtomicF64, |
77 | 79 | }
|
78 | 80 |
|
79 |
| -// Uses the canonical ordering for handover of values, i.e. `Acquire` on load and `Release` on |
80 |
| -// store. |
81 |
| -impl Controller { |
82 |
| - /// Create a new Controller. It will not be active |
83 |
| - pub fn new() -> Self { |
84 |
| - let inner = ControllerInner { |
85 |
| - scheduler: Scheduler::new(), |
86 |
| - loop_: AtomicBool::new(false), |
87 |
| - loop_start: AtomicF64::new(0.), |
88 |
| - loop_end: AtomicF64::new(f64::MAX), |
89 |
| - offset: AtomicF64::new(f64::MAX), |
90 |
| - duration: AtomicF64::new(f64::MAX), |
91 |
| - }; |
92 |
| - |
| 81 | +impl From<ControllerValues> for ControllerShared { |
| 82 | + fn from(values: ControllerValues) -> Self { |
93 | 83 | Self {
|
94 |
| - inner: Arc::new(inner), |
| 84 | + loop_: AtomicBool::new(values.loop_), |
| 85 | + loop_start: AtomicF64::new(values.loop_start), |
| 86 | + loop_end: AtomicF64::new(values.loop_end), |
95 | 87 | }
|
96 | 88 | }
|
| 89 | +} |
97 | 90 |
|
98 |
| - pub fn scheduler(&self) -> &Scheduler { |
99 |
| - &self.inner.scheduler |
100 |
| - } |
101 |
| - |
| 91 | +// Uses the canonical ordering for handover of values, i.e. `Acquire` on load and `Release` on |
| 92 | +// store. |
| 93 | +impl ControllerShared { |
102 | 94 | pub fn loop_(&self) -> bool {
|
103 |
| - self.inner.loop_.load(Ordering::Acquire) |
104 |
| - } |
105 |
| - |
106 |
| - pub fn set_loop(&self, loop_: bool) { |
107 |
| - self.inner.loop_.store(loop_, Ordering::Release); |
| 95 | + self.loop_.load(Ordering::Acquire) |
108 | 96 | }
|
109 | 97 |
|
110 | 98 | pub fn loop_start(&self) -> f64 {
|
111 |
| - self.inner.loop_start.load(Ordering::Acquire) |
112 |
| - } |
113 |
| - |
114 |
| - pub fn set_loop_start(&self, loop_start: f64) { |
115 |
| - self.inner.loop_start.store(loop_start, Ordering::Release); |
| 99 | + self.loop_start.load(Ordering::Acquire) |
116 | 100 | }
|
117 | 101 |
|
118 | 102 | pub fn loop_end(&self) -> f64 {
|
119 |
| - self.inner.loop_end.load(Ordering::Acquire) |
120 |
| - } |
121 |
| - |
122 |
| - pub fn set_loop_end(&self, loop_end: f64) { |
123 |
| - self.inner.loop_end.store(loop_end, Ordering::Release); |
124 |
| - } |
125 |
| - |
126 |
| - pub fn offset(&self) -> f64 { |
127 |
| - self.inner.offset.load(Ordering::Acquire) |
128 |
| - } |
129 |
| - |
130 |
| - pub fn set_offset(&self, offset: f64) { |
131 |
| - self.inner.offset.store(offset, Ordering::Release); |
132 |
| - } |
133 |
| - |
134 |
| - pub fn duration(&self) -> f64 { |
135 |
| - self.inner.duration.load(Ordering::Acquire) |
136 |
| - } |
137 |
| - |
138 |
| - pub fn set_duration(&self, duration: f64) { |
139 |
| - self.inner.duration.store(duration, Ordering::Release) |
140 |
| - } |
141 |
| -} |
142 |
| - |
143 |
| -impl Default for Controller { |
144 |
| - fn default() -> Self { |
145 |
| - Self::new() |
146 |
| - } |
147 |
| -} |
148 |
| - |
149 |
| -#[cfg(test)] |
150 |
| -mod tests { |
151 |
| - use super::*; |
152 |
| - |
153 |
| - #[test] |
154 |
| - fn test_controller() { |
155 |
| - let controller = Controller::new(); |
156 |
| - |
157 |
| - assert!(!controller.loop_()); |
158 |
| - assert!(controller.loop_start() == 0.); |
159 |
| - assert!(controller.loop_end() == f64::MAX); |
| 103 | + self.loop_end.load(Ordering::Acquire) |
160 | 104 | }
|
161 | 105 | }
|
0 commit comments