firewheel_graph/
context.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::time::{Duration, Instant};

use rtrb::PushError;

use crate::{
    graph::{AudioGraph, AudioGraphConfig, CompileGraphError},
    processor::{ContextToProcessorMsg, FwProcessor, ProcessorToContextMsg},
};

const CHANNEL_CAPACITY: usize = 16;
const CLOSE_STREAM_TIMEOUT: Duration = Duration::from_secs(3);
const CLOSE_STREAM_SLEEP_INTERVAL: Duration = Duration::from_millis(2);

pub struct InactiveFwCtx<C> {
    graph: AudioGraph<C>,
}

impl<C: 'static> InactiveFwCtx<C> {
    pub fn new(graph_config: AudioGraphConfig) -> Self {
        Self {
            graph: AudioGraph::new(&graph_config),
        }
    }

    pub fn graph(&self) -> &AudioGraph<C> {
        &self.graph
    }

    pub fn graph_mut(&mut self) -> &mut AudioGraph<C> {
        &mut self.graph
    }

    pub fn activate(
        self,
        sample_rate: u32,
        num_stream_in_channels: usize,
        num_stream_out_channels: usize,
        user_cx: C,
    ) -> (ActiveFwCtx<C>, FwProcessor<C>) {
        let (to_executor_tx, from_graph_rx) =
            rtrb::RingBuffer::<ContextToProcessorMsg<C>>::new(CHANNEL_CAPACITY);
        let (to_graph_tx, from_executor_rx) =
            rtrb::RingBuffer::<ProcessorToContextMsg<C>>::new(CHANNEL_CAPACITY);

        let processor = FwProcessor::new(
            from_graph_rx,
            to_graph_tx,
            self.graph.current_node_capacity(),
            num_stream_in_channels,
            num_stream_out_channels,
            self.graph.max_block_frames(),
            user_cx,
        );

        (
            ActiveFwCtx {
                inner: Some(ActiveFwCtxInner {
                    graph: self.graph,
                    to_executor_tx,
                    from_executor_rx,
                    sample_rate,
                }),
            },
            processor,
        )
    }
}

struct ActiveFwCtxInner<C> {
    pub graph: AudioGraph<C>,

    // TODO: Do research on whether `rtrb` is compatible with
    // webassembly. If not, use conditional compilation to
    // use a different channel type when targeting webassembly.
    to_executor_tx: rtrb::Producer<ContextToProcessorMsg<C>>,
    from_executor_rx: rtrb::Consumer<ProcessorToContextMsg<C>>,

    sample_rate: u32,
}

impl<C: 'static> ActiveFwCtxInner<C> {
    /// Update the firewheel context.
    ///
    /// This must be called reguarly (i.e. once every frame).
    fn update(&mut self) -> UpdateStatusInternal<C> {
        let mut dropped = false;
        let mut dropped_user_cx = None;

        self.update_internal(&mut dropped, &mut dropped_user_cx);

        if dropped {
            self.graph.deactivate();
            return UpdateStatusInternal::Deactivated(dropped_user_cx);
        }

        if self.graph.needs_compile() {
            match self.graph.compile(self.sample_rate) {
                Ok(schedule_data) => {
                    if let Err(e) = self
                        .to_executor_tx
                        .push(ContextToProcessorMsg::NewSchedule(Box::new(schedule_data)))
                    {
                        let PushError::Full(msg) = e;

                        log::error!(
                            "Failed to send new schedule: Firewheel message channel is full"
                        );

                        if let ContextToProcessorMsg::NewSchedule(schedule_data) = msg {
                            self.graph.on_schedule_returned(schedule_data);
                        }
                    }
                }
                Err(e) => {
                    return UpdateStatusInternal::GraphError(e);
                }
            }
        }

        UpdateStatusInternal::Ok
    }

    /// Deactivate the firewheel context.
    ///
    /// This will block the thread until either the processor has
    /// been successfully dropped or a timeout has been reached.
    ///
    /// If the stream is still currently running, then the context
    /// will attempt to cleanly deactivate the processor. If not,
    /// then the context will wait for either the processor to be
    /// dropped or a timeout being reached.
    fn deactivate(mut self, stream_is_running: bool) -> (InactiveFwCtx<C>, Option<C>) {
        let start = Instant::now();

        let mut dropped = false;
        let mut dropped_user_cx = None;

        if stream_is_running {
            loop {
                if let Err(_) = self.to_executor_tx.push(ContextToProcessorMsg::Stop) {
                    log::error!("Failed to send stop signal: Firewheel message channel is full");

                    // TODO: I don't think sleep is supported in WASM, so we will
                    // need to figure out something if that's the case.
                    std::thread::sleep(CLOSE_STREAM_SLEEP_INTERVAL);

                    if start.elapsed() > CLOSE_STREAM_TIMEOUT {
                        log::error!("Timed out trying to send stop signal to firewheel processor");
                        dropped = true;
                        break;
                    }
                } else {
                    break;
                }
            }
        }

        while !dropped {
            self.update_internal(&mut dropped, &mut dropped_user_cx);

            if !dropped {
                // TODO: I don't think sleep is supported in WASM, so we will
                // need to figure out something if that's the case.
                std::thread::sleep(CLOSE_STREAM_SLEEP_INTERVAL);

                if start.elapsed() > CLOSE_STREAM_TIMEOUT {
                    log::error!("Timed out waiting for firewheel processor to drop");
                    dropped = true;
                }
            }
        }

        self.graph.deactivate();

        (InactiveFwCtx { graph: self.graph }, dropped_user_cx)
    }

    fn update_internal(&mut self, dropped: &mut bool, dropped_user_cx: &mut Option<C>) {
        while let Ok(msg) = self.from_executor_rx.pop() {
            match msg {
                ProcessorToContextMsg::ReturnSchedule(schedule_data) => {
                    self.graph.on_schedule_returned(schedule_data);
                }
                ProcessorToContextMsg::Dropped { nodes, user_cx, .. } => {
                    self.graph.on_processor_dropped(nodes);
                    *dropped = true;
                    *dropped_user_cx = user_cx;
                }
            }
        }
    }
}

pub struct ActiveFwCtx<C: 'static> {
    inner: Option<ActiveFwCtxInner<C>>,
}

impl<C: 'static> ActiveFwCtx<C> {
    pub fn graph(&self) -> &AudioGraph<C> {
        &self.inner.as_ref().unwrap().graph
    }

    pub fn graph_mut(&mut self) -> &mut AudioGraph<C> {
        &mut self.inner.as_mut().unwrap().graph
    }

    /// Update the firewheel context.
    ///
    /// This must be called reguarly (i.e. once every frame).
    pub fn update(mut self) -> UpdateStatus<C> {
        match self.inner.as_mut().unwrap().update() {
            UpdateStatusInternal::Ok => UpdateStatus::Ok {
                cx: self,
                graph_error: None,
            },
            UpdateStatusInternal::GraphError(e) => UpdateStatus::Ok {
                cx: self,
                graph_error: Some(e),
            },
            UpdateStatusInternal::Deactivated(user_cx) => UpdateStatus::Deactivated {
                cx: InactiveFwCtx {
                    graph: self.inner.take().unwrap().graph,
                },
                user_cx,
            },
        }
    }

    /// Deactivate the firewheel context.
    ///
    /// This will block the thread until either the processor has
    /// been successfully dropped or a timeout has been reached.
    ///
    /// If the stream is still currently running, then the context
    /// will attempt to cleanly deactivate the processor. If not,
    /// then the context will wait for either the processor to be
    /// dropped or a timeout being reached.
    pub fn deactivate(mut self, stream_is_running: bool) -> (InactiveFwCtx<C>, Option<C>) {
        let inner = self.inner.take().unwrap();
        inner.deactivate(stream_is_running)
    }
}

impl<C: 'static> Drop for ActiveFwCtx<C> {
    fn drop(&mut self) {
        if let Some(inner) = self.inner.take() {
            inner.deactivate(true);
        }
    }
}

pub enum UpdateStatus<C: 'static> {
    Ok {
        cx: ActiveFwCtx<C>,
        graph_error: Option<CompileGraphError>,
    },
    Deactivated {
        cx: InactiveFwCtx<C>,
        user_cx: Option<C>,
    },
}

enum UpdateStatusInternal<C> {
    Ok,
    GraphError(CompileGraphError),
    Deactivated(Option<C>),
}