sim-lib-music-core 0.2.0

Core music object model for notes, rests, scores, rolls, players, and arrangers.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use sim_kernel::{Error, Expr, Result, Symbol};
use sim_lib_stream_core::{ClockDomain, LatencyClass, RateContract};
use sim_lib_topology::{PlacementNodeProfile, SiteId};
use sim_value::access;

use crate::{LaneId, LaneTarget, PlayEvent};

/// How a chain device combines its output with the events flowing through it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum PlayerMode {
    /// Pass input through and append generated events.
    Through,
    /// Drop input and emit only generated events.
    Replace,
    /// Filter input by lane, dropping matched events.
    Filter,
    /// Route input while generating side events without merging them.
    Sidechain,
    /// Drop input and emit self-clocked generated events.
    SelfClocked,
}

impl PlayerMode {
    /// Returns the stable wire label for this mode.
    ///
    /// # Examples
    ///
    /// ```
    /// use sim_lib_music_core::PlayerMode;
    ///
    /// assert_eq!(PlayerMode::Through.wire_label(), "through");
    /// assert_eq!(PlayerMode::SelfClocked.wire_label(), "self_clocked");
    /// ```
    pub fn wire_label(self) -> &'static str {
        match self {
            Self::Through => "through",
            Self::Replace => "replace",
            Self::Filter => "filter",
            Self::Sidechain => "sidechain",
            Self::SelfClocked => "self_clocked",
        }
    }

    /// Returns the qualified symbol naming this mode.
    pub fn symbol(self) -> Symbol {
        Symbol::qualified("music/player-mode", self.wire_label())
    }
}

/// Stable identifier for a device within a player chain.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PlayerDeviceId(pub String);

impl PlayerDeviceId {
    /// Creates a device id from any string-like value.
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }
}

impl AsRef<str> for PlayerDeviceId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

/// Typed value for a player parameter snapshot entry.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ParamValue {
    /// Boolean parameter.
    Bool(bool),
    /// Signed 64-bit integer parameter.
    I64(i64),
    /// Text parameter.
    Text(String),
    /// Symbol parameter.
    Symbol(Symbol),
}

impl ParamValue {
    fn to_expr(&self) -> Expr {
        match self {
            Self::Bool(value) => Expr::Bool(*value),
            Self::I64(value) => Expr::String(value.to_string()),
            Self::Text(value) => Expr::String(value.clone()),
            Self::Symbol(value) => Expr::Symbol(value.clone()),
        }
    }
}

/// Sorted, deduplicated snapshot of named player parameters.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ParamSnapshot {
    /// Parameter entries sorted by key with duplicates removed.
    pub entries: Vec<(String, ParamValue)>,
}

impl ParamSnapshot {
    /// Builds a snapshot, sorting entries by key and dropping duplicate keys.
    ///
    /// # Examples
    ///
    /// ```
    /// use sim_lib_music_core::{ParamSnapshot, ParamValue};
    ///
    /// let snapshot = ParamSnapshot::new(vec![
    ///     ("gain".to_owned(), ParamValue::I64(1)),
    ///     ("attack".to_owned(), ParamValue::Bool(true)),
    /// ]);
    /// assert_eq!(snapshot.entries[0].0, "attack");
    /// assert_eq!(snapshot.entries[1].0, "gain");
    /// ```
    pub fn new(mut entries: Vec<(String, ParamValue)>) -> Self {
        entries.sort_by(|left, right| left.0.cmp(&right.0));
        entries.dedup_by(|left, right| left.0 == right.0);
        Self { entries }
    }

    /// Encodes the snapshot as an expression map keyed by parameter name.
    pub fn to_expr(&self) -> Expr {
        Expr::Map(
            self.entries
                .iter()
                .map(|(key, value)| (Expr::Symbol(Symbol::new(key.clone())), value.to_expr()))
                .collect(),
        )
    }
}

/// Placement of a chain device: where it runs and under what rate profile.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChainPlacement {
    /// Site the device is placed on.
    pub site: SiteId,
    /// Node profile describing the device's rate contract and pinning.
    pub profile: PlacementNodeProfile,
}

impl ChainPlacement {
    /// Creates a placement for `site` with the given node profile.
    pub fn new(site: impl Into<SiteId>, profile: PlacementNodeProfile) -> Self {
        Self {
            site: site.into(),
            profile,
        }
    }

    /// Returns the default local-coroutine placement at MIDI-tick rate.
    pub fn local_coroutine() -> Self {
        Self::new(
            "local-coroutine",
            PlacementNodeProfile::new(RateContract::midi_tick(), false),
        )
    }

    /// Encodes the placement, flattening its rate contract, as an expression map.
    pub fn to_expr(&self) -> Expr {
        let rate = self.profile.rate_contract();
        Expr::Map(vec![
            (
                Expr::Symbol(Symbol::new("site")),
                Expr::Symbol(self.site.as_symbol().clone()),
            ),
            (
                Expr::Symbol(Symbol::new("clock-domain")),
                Expr::Symbol(rate.clock_domain().symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("latency-class")),
                Expr::Symbol(rate.latency_class().symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("nominal-rate-hz")),
                Expr::String(
                    rate.nominal_rate_hz()
                        .map(|rate| rate.to_string())
                        .unwrap_or_else(|| "none".to_owned()),
                ),
            ),
            (
                Expr::Symbol(Symbol::new("realtime-pin")),
                Expr::Bool(self.profile.realtime_pin()),
            ),
        ])
    }

    /// Decodes a placement from a map produced by [`ChainPlacement::to_expr`].
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let Expr::Map(entries) = expr else {
            return Err(Error::Eval("chain placement must be a map".to_owned()));
        };
        let site = symbol_field(entries, "site")?.to_string();
        let clock_domain = ClockDomain::from_symbol(symbol_field(entries, "clock-domain")?)?;
        let latency_class = LatencyClass::from_symbol(symbol_field(entries, "latency-class")?)?;
        let nominal_rate_hz =
            match string_field(entries, "nominal-rate-hz")? {
                "none" => None,
                value => Some(value.parse::<u32>().map_err(|err| {
                    Error::Eval(format!("invalid placement nominal-rate-hz: {err}"))
                })?),
            };
        let realtime_pin = bool_field(entries, "realtime-pin")?;
        Ok(Self::new(
            site,
            PlacementNodeProfile::new(
                RateContract::new(clock_domain, latency_class, nominal_rate_hz),
                realtime_pin,
            ),
        ))
    }
}

/// Pairing of a device id with its resolved placement.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PlacedChainDevice {
    /// Identifier of the placed device.
    pub device_id: PlayerDeviceId,
    /// Placement assigned to the device.
    pub placement: ChainPlacement,
}

/// Plan listing the placement of every device in a chain.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChainPlacementPlan {
    /// Placed devices, sorted by device id.
    pub devices: Vec<PlacedChainDevice>,
}

impl ChainPlacementPlan {
    /// Builds a plan, sorting devices by id for stable output.
    pub fn new(mut devices: Vec<PlacedChainDevice>) -> Self {
        devices.sort_by(|left, right| left.device_id.cmp(&right.device_id));
        Self { devices }
    }

    /// Encodes the plan as an expression list of device/placement maps.
    pub fn to_expr(&self) -> Expr {
        Expr::List(
            self.devices
                .iter()
                .map(|device| {
                    Expr::Map(vec![
                        (
                            Expr::Symbol(Symbol::new("device")),
                            Expr::String(device.device_id.0.clone()),
                        ),
                        (
                            Expr::Symbol(Symbol::new("placement")),
                            device.placement.to_expr(),
                        ),
                    ])
                })
                .collect(),
        )
    }

    /// Decodes a plan from a list produced by [`ChainPlacementPlan::to_expr`].
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        let Expr::List(items) = expr else {
            return Err(Error::Eval(
                "chain placement plan must be a list".to_owned(),
            ));
        };
        let mut devices = Vec::new();
        for item in items {
            let Expr::Map(entries) = item else {
                return Err(Error::Eval(
                    "chain placement plan item must be a map".to_owned(),
                ));
            };
            devices.push(PlacedChainDevice {
                device_id: PlayerDeviceId::new(string_field(entries, "device")?),
                placement: ChainPlacement::from_expr(field(entries, "placement")?)?,
            });
        }
        Ok(Self::new(devices))
    }
}

/// Descriptor of a chain's output target and its rate contract.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PlayerTargetDescriptor {
    /// Symbol identifying the target.
    pub id: Symbol,
    /// Lane target the chain output is routed to.
    pub target: LaneTarget,
    /// Rate contract the target runs under.
    pub rate_contract: RateContract,
}

impl PlayerTargetDescriptor {
    /// Builds an instrument target descriptor at MIDI-tick rate.
    pub fn instrument(id: impl Into<String>) -> Self {
        let id = Symbol::qualified("music/target", id.into());
        Self {
            id: id.clone(),
            target: LaneTarget::Instrument(id),
            rate_contract: RateContract::midi_tick(),
        }
    }

    /// Returns the target's clock domain from its rate contract.
    pub fn clock_domain(&self) -> ClockDomain {
        self.rate_contract.clock_domain()
    }

    /// Returns the target's latency class from its rate contract.
    pub fn latency_class(&self) -> LatencyClass {
        self.rate_contract.latency_class()
    }
}

/// A single processing stage in a player chain.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChainDevice {
    /// Identifier of this device.
    pub id: PlayerDeviceId,
    /// Symbol of the player backing this device.
    pub player: Symbol,
    /// Mode controlling how the device combines with its input.
    pub mode: PlayerMode,
    /// Sort order of the device within the chain.
    pub order: u32,
    /// Whether the device passes input through untouched.
    pub bypass: bool,
    /// Whether the device is muted (excluded from rendering).
    pub mute: bool,
    /// Whether the device is soloed.
    pub solo: bool,
    /// Whether the device is enabled.
    pub enabled: bool,
    /// Parameter snapshot for the device.
    pub params: ParamSnapshot,
    /// Events the device contributes to the chain.
    pub generated: Vec<PlayEvent>,
    /// Lane ids the device filters out, kept sorted for binary search.
    pub filter_lanes: Vec<LaneId>,
    /// Optional lane the device reroutes events onto.
    pub route_lane: Option<LaneId>,
    /// Placement of the device on a site.
    pub placement: ChainPlacement,
}

impl ChainDevice {
    /// Creates an enabled device with default flags and local placement.
    pub fn new(id: impl Into<String>, player: Symbol, mode: PlayerMode, order: u32) -> Self {
        Self {
            id: PlayerDeviceId::new(id),
            player,
            mode,
            order,
            bypass: false,
            mute: false,
            solo: false,
            enabled: true,
            params: ParamSnapshot::default(),
            generated: Vec::new(),
            filter_lanes: Vec::new(),
            route_lane: None,
            placement: ChainPlacement::local_coroutine(),
        }
    }

    /// Sets the device's generated events.
    pub fn with_generated(mut self, generated: Vec<PlayEvent>) -> Self {
        self.generated = generated;
        self
    }

    /// Sets the filter lanes, keeping them sorted for lookup.
    pub fn with_filter_lanes(mut self, lanes: Vec<LaneId>) -> Self {
        self.filter_lanes = lanes;
        self.filter_lanes.sort();
        self
    }

    /// Sets the lane that events are rerouted onto.
    pub fn with_route_lane(mut self, lane: LaneId) -> Self {
        self.route_lane = Some(lane);
        self
    }

    /// Sets the device's placement.
    pub fn with_placement(mut self, placement: ChainPlacement) -> Self {
        self.placement = placement;
        self
    }

    /// Marks the device as bypassed.
    pub fn bypassed(mut self) -> Self {
        self.bypass = true;
        self
    }
}

pub(crate) fn field<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Result<&'a Expr> {
    entries
        .iter()
        .find_map(|(key, value)| match key {
            Expr::Symbol(symbol) if symbol.namespace.is_none() && symbol.name.as_ref() == name => {
                Some(value)
            }
            _ => None,
        })
        .ok_or_else(|| Error::Eval(format!("missing {name} field")))
}

pub(crate) fn string_field<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Result<&'a str> {
    access::entry_required_str(entries, name, "string field")
}

pub(crate) fn symbol_field<'a>(entries: &'a [(Expr, Expr)], name: &str) -> Result<&'a Symbol> {
    access::entry_required_sym(entries, name, "symbol field")
}

pub(crate) fn bool_field(entries: &[(Expr, Expr)], name: &str) -> Result<bool> {
    access::entry_required_bool(entries, name, "boolean field")
}