synthizer/
context.rs

1//! The `Context`.
2use crate::internal_prelude::*;
3
4/// The `Context` represents an audio device.
5#[derive(Clone, Eq, Ord, PartialEq, PartialOrd, Hash, Debug)]
6pub struct Context(pub(crate) Handle);
7
8impl Context {
9    pub fn new() -> Result<Context> {
10        wrap_constructor(|ud, cb| {
11            let mut h = 0;
12            check_error(unsafe { syz_createContext(&mut h as *mut syz_Handle, ud, Some(cb)) })?;
13            Ok(Context(Handle::new(h)))
14        })
15    }
16
17    pub fn enable_events(&self) -> Result<()> {
18        check_error(unsafe { syz_contextEnableEvents(self.to_syz_handle()) })
19    }
20
21    /// Get any pending events.  The returned iterator will not block, and
22    /// iterates over any pending events until the first error.  This is lazy:
23    /// to limit the number of events received, use `.take`.
24    #[allow(clippy::needless_lifetimes)] // Actually appears to be a false positive.
25    pub fn get_events<'a>(&'a self) -> impl Iterator<Item = Result<events::Event>> + 'a {
26        events::EventIterator {
27            context: self,
28            errored: false,
29        }
30    }
31
32    // Configure a route given a [RouteConfig], which can be constructed with a
33    // [RouteConfigBuilder].  Corresponds to the `syz_initRouteConfig` and
34    // `syz_routingConfigRoute` flow.
35    pub fn config_route(
36        &self,
37        output: &dyn RouteOutput,
38        input: &dyn RouteInput,
39        config: &RouteConfig,
40    ) -> Result<()> {
41        check_error(unsafe {
42            syz_routingConfigRoute(
43                self.to_syz_handle(),
44                output.to_syz_handle(),
45                input.to_syz_handle(),
46                &config.0 as *const syz_RouteConfig,
47            )
48        })?;
49        Ok(())
50    }
51
52    /// Configure a route with the default settings.
53    pub fn config_route_simple(
54        &self,
55        output: &dyn RouteOutput,
56        input: &dyn RouteInput,
57    ) -> Result<()> {
58        self.config_route(output, input, &Default::default())
59    }
60
61    pub fn remove_route(
62        &self,
63        output: &dyn RouteOutput,
64        input: &dyn RouteInput,
65        fade_out: f64,
66    ) -> Result<()> {
67        check_error(unsafe {
68            syz_routingRemoveRoute(
69                self.to_syz_handle(),
70                output.to_syz_handle(),
71                input.to_syz_handle(),
72                fade_out,
73            )
74        })
75    }
76
77    pub fn remove_all_routes(&self, output: &dyn RouteOutput, fade_out: f64) -> Result<()> {
78        check_error(unsafe {
79            syz_routingRemoveAllRoutes(self.to_syz_handle(), output.to_syz_handle(), fade_out)
80        })
81    }
82
83    double_p!(SYZ_P_GAIN, gain);
84    enum_p!(
85        PannerStrategy,
86        SYZ_P_DEFAULT_PANNER_STRATEGY,
87        default_panner_strategy
88    );
89    enum_p!(
90        DistanceModel,
91        SYZ_P_DEFAULT_DISTANCE_MODEL,
92        default_distance_model
93    );
94    double_p!(SYZ_P_DEFAULT_DISTANCE_REF, default_distance_ref);
95    double_p!(SYZ_P_DEFAULT_DISTANCE_MAX, default_distance_max);
96    double_p!(SYZ_P_DEFAULT_ROLLOFF, default_rolloff);
97    double_p!(SYZ_P_DEFAULT_CLOSENESS_BOOST, default_closeness_boost);
98    double_p!(
99        SYZ_P_DEFAULT_CLOSENESS_BOOST_DISTANCE,
100        default_closeness_boost_distance
101    );
102    double3_p!(SYZ_P_POSITION, position);
103    double6_p!(SYZ_P_ORIENTATION, orientation);
104
105    object_common!();
106    pausable_common!();
107}
108
109handle_traits!(Context);