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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use std::collections::BTreeSet;
use sim_kernel::{Expr, Symbol};
use sim_lib_stream_core::RateContract;
use crate::{LaneDescriptor, LaneKind};
const DESCRIPTOR_NS: &str = "music/component-descriptor";
/// A capability a music component advertises to the runtime.
///
/// Capabilities describe what a component can do (be played, drive other
/// components, render, and so on) and are carried in a
/// [`MusicComponentDescriptor`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MusicCapability {
/// The component can be played as part of a performance.
Playable,
/// The component plays incoming material (a player-family component).
Player,
/// The component produces a control/modulation signal.
Modulator,
/// The component generates an oscillating signal.
Oscillator,
/// The component is a source of live performance events.
PerformanceSource,
/// The component can be rendered to output.
Renderable,
}
impl MusicCapability {
/// Returns the stable wire label for this capability.
///
/// # Examples
///
/// ```
/// use sim_lib_music_core::MusicCapability;
///
/// assert_eq!(MusicCapability::Playable.wire_label(), "playable");
/// assert_eq!(MusicCapability::PerformanceSource.wire_label(), "performance-source");
/// ```
pub fn wire_label(self) -> &'static str {
match self {
Self::Playable => "playable",
Self::Player => "player",
Self::Modulator => "modulator",
Self::Oscillator => "oscillator",
Self::PerformanceSource => "performance-source",
Self::Renderable => "renderable",
}
}
/// Returns the qualified `music/capability` symbol for this capability.
pub fn symbol(self) -> Symbol {
Symbol::qualified("music/capability", self.wire_label())
}
}
/// The broad category a music component belongs to.
///
/// Categories group descriptors for browsing and routing; they are carried in
/// a [`MusicComponentDescriptor`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MusicComponentCategory {
/// A player-family component that interprets incoming material.
PlayerFamily,
/// An instrument that renders events to audio.
Instrument,
/// A control component such as a modulator or performance source.
Control,
}
impl MusicComponentCategory {
/// Returns the stable wire label for this category.
pub fn wire_label(self) -> &'static str {
match self {
Self::PlayerFamily => "player-family",
Self::Instrument => "instrument",
Self::Control => "control",
}
}
/// Returns the qualified `music/component-category` symbol for this category.
pub fn symbol(self) -> Symbol {
Symbol::qualified("music/component-category", self.wire_label())
}
}
/// The direction of a component port relative to the component.
///
/// Carried in a [`MusicPortDescriptor`] to mark whether a port consumes or
/// produces material.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MusicPortDirection {
/// The port receives material into the component.
Input,
/// The port emits material from the component.
Output,
/// The port receives auxiliary side-chain material.
Sidechain,
}
impl MusicPortDirection {
/// Returns the stable wire label for this direction.
pub fn wire_label(self) -> &'static str {
match self {
Self::Input => "input",
Self::Output => "output",
Self::Sidechain => "sidechain",
}
}
/// Returns the qualified `music/port-direction` symbol for this direction.
pub fn symbol(self) -> Symbol {
Symbol::qualified("music/port-direction", self.wire_label())
}
}
/// The unit a parameter value is measured in.
///
/// Carried in a [`MusicParamDescriptor`] to describe how a parameter's value
/// should be interpreted.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MusicUnit {
/// No unit; the value is dimensionless or categorical.
None,
/// Measured in beats.
Beats,
/// Measured in MIDI ticks.
Ticks,
/// Measured as a percentage.
Percent,
/// Measured in semitones.
Semitone,
/// Measured in hertz.
Hertz,
}
impl MusicUnit {
/// Returns the stable wire label for this unit.
///
/// # Examples
///
/// ```
/// use sim_lib_music_core::MusicUnit;
///
/// assert_eq!(MusicUnit::None.wire_label(), "none");
/// assert_eq!(MusicUnit::Hertz.wire_label(), "hertz");
/// ```
pub fn wire_label(self) -> &'static str {
match self {
Self::None => "none",
Self::Beats => "beats",
Self::Ticks => "ticks",
Self::Percent => "percent",
Self::Semitone => "semitone",
Self::Hertz => "hertz",
}
}
/// Returns the qualified `music/unit` symbol for this unit.
pub fn symbol(self) -> Symbol {
Symbol::qualified("music/unit", self.wire_label())
}
}
/// How reproducible a component's output is.
///
/// Carried in a [`MusicComponentDescriptor`] to declare whether output is
/// fixed, seed-driven, or dependent on live input.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DeterminismPolicy {
/// Output is fully determined by the inputs.
Deterministic,
/// Output is reproducible given a seed.
Seeded,
/// Output depends on live input and is not reproducible.
LiveInput,
}
impl DeterminismPolicy {
/// Returns the stable wire label for this policy.
pub fn wire_label(self) -> &'static str {
match self {
Self::Deterministic => "deterministic",
Self::Seeded => "seeded",
Self::LiveInput => "live-input",
}
}
/// Returns the qualified `music/determinism` symbol for this policy.
pub fn symbol(self) -> Symbol {
Symbol::qualified("music/determinism", self.wire_label())
}
}
/// Describes a single input or output port of a music component.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MusicPortDescriptor {
/// Stable identifier for the port.
pub id: Symbol,
/// Human-readable label.
pub label: String,
/// Whether the port is an input, output, or side-chain.
pub direction: MusicPortDirection,
/// The rate contract the port operates at.
pub rate: RateContract,
/// Lane kinds the port accepts as input.
pub accepted_event_families: Vec<LaneKind>,
/// Lane kinds the port emits as output.
pub output_families: Vec<LaneKind>,
}
impl MusicPortDescriptor {
/// Creates a port descriptor with no accepted or output families.
pub fn new(
id: Symbol,
label: impl Into<String>,
direction: MusicPortDirection,
rate: RateContract,
) -> Self {
Self {
id,
label: label.into(),
direction,
rate,
accepted_event_families: Vec::new(),
output_families: Vec::new(),
}
}
/// Sets the accepted and output lane kinds, sorting and deduplicating each.
pub fn with_events(mut self, accepted: Vec<LaneKind>, output: Vec<LaneKind>) -> Self {
self.accepted_event_families = stable_lane_kinds(accepted);
self.output_families = stable_lane_kinds(output);
self
}
/// Renders the port descriptor as an `Expr` map for wire transport.
pub fn to_expr(&self) -> Expr {
Expr::Map(vec![
field("id", Expr::Symbol(self.id.clone())),
field("label", Expr::String(self.label.clone())),
field("direction", Expr::Symbol(self.direction.symbol())),
field("rate", rate_expr(self.rate)),
field("accepted", lane_kind_list(&self.accepted_event_families)),
field("output", lane_kind_list(&self.output_families)),
])
}
}
/// Describes a single configurable parameter of a music component.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MusicParamDescriptor {
/// Stable identifier for the parameter.
pub id: Symbol,
/// Human-readable label.
pub label: String,
/// The unit the parameter value is measured in.
pub unit: MusicUnit,
/// The rate contract the parameter is updated at.
pub rate: RateContract,
/// The default value for the parameter.
pub default: Expr,
}
impl MusicParamDescriptor {
/// Creates a parameter descriptor from its fields.
pub fn new(
id: Symbol,
label: impl Into<String>,
unit: MusicUnit,
rate: RateContract,
default: Expr,
) -> Self {
Self {
id,
label: label.into(),
unit,
rate,
default,
}
}
/// Renders the parameter descriptor as an `Expr` map for wire transport.
pub fn to_expr(&self) -> Expr {
Expr::Map(vec![
field("id", Expr::Symbol(self.id.clone())),
field("label", Expr::String(self.label.clone())),
field("unit", Expr::Symbol(self.unit.symbol())),
field("rate", rate_expr(self.rate)),
field("default", self.default.clone()),
])
}
}
/// Full description of a music component: its identity, category,
/// capabilities, ports, lanes, parameters, and behavioral contracts.
///
/// Built fluently via [`MusicComponentDescriptor::new`] and the `with_*`
/// methods, then rendered for transport with
/// [`MusicComponentDescriptor::to_expr`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MusicComponentDescriptor {
/// Stable identifier for the component.
pub id: Symbol,
/// Human-readable label.
pub label: String,
/// The category the component belongs to.
pub category: MusicComponentCategory,
/// Capabilities the component advertises.
pub capabilities: BTreeSet<MusicCapability>,
/// Input and output ports, ordered by id.
pub ports: Vec<MusicPortDescriptor>,
/// Lane descriptors produced by the component, in stable order.
pub lanes: Vec<LaneDescriptor>,
/// Configurable parameters, ordered by id.
pub params: Vec<MusicParamDescriptor>,
/// The rate contract the component runs at.
pub rate: RateContract,
/// How reproducible the component's output is.
pub determinism: DeterminismPolicy,
/// Lane kinds the component accepts as input.
pub accepted_event_families: Vec<LaneKind>,
/// Lane kinds the component emits as output.
pub output_families: Vec<LaneKind>,
/// The latency class symbol derived from the rate contract.
pub latency: Symbol,
/// Whether the component has a working implementation.
pub implemented: bool,
}
impl MusicComponentDescriptor {
/// Creates a descriptor with default capabilities, ports, lanes, and params.
///
/// The latency symbol is derived from `rate` and `implemented` defaults to
/// `true`.
pub fn new(
id: Symbol,
label: impl Into<String>,
category: MusicComponentCategory,
rate: RateContract,
) -> Self {
let latency = rate.latency_class().symbol();
Self {
id,
label: label.into(),
category,
capabilities: BTreeSet::new(),
ports: Vec::new(),
lanes: Vec::new(),
params: Vec::new(),
rate,
determinism: DeterminismPolicy::Deterministic,
accepted_event_families: Vec::new(),
output_families: Vec::new(),
latency,
implemented: true,
}
}
/// Adds a capability and returns the updated descriptor.
pub fn with_capability(mut self, capability: MusicCapability) -> Self {
self.capabilities.insert(capability);
self
}
/// Adds a port and re-sorts the port list by id.
pub fn with_port(mut self, port: MusicPortDescriptor) -> Self {
self.ports.push(port);
self.ports.sort_by(|left, right| left.id.cmp(&right.id));
self
}
/// Adds a lane and restores the stable lane order.
pub fn with_lane(mut self, lane: LaneDescriptor) -> Self {
self.lanes.push(lane);
self.lanes = crate::stable_lane_order(self.lanes);
self
}
/// Adds a parameter and re-sorts the parameter list by id.
pub fn with_param(mut self, param: MusicParamDescriptor) -> Self {
self.params.push(param);
self.params.sort_by(|left, right| left.id.cmp(&right.id));
self
}
/// Sets the accepted and output lane kinds, sorting and deduplicating each.
pub fn with_events(mut self, accepted: Vec<LaneKind>, output: Vec<LaneKind>) -> Self {
self.accepted_event_families = stable_lane_kinds(accepted);
self.output_families = stable_lane_kinds(output);
self
}
/// Sets the determinism policy and returns the updated descriptor.
pub fn with_determinism(mut self, determinism: DeterminismPolicy) -> Self {
self.determinism = determinism;
self
}
/// Sets the implemented flag and returns the updated descriptor.
pub fn with_implemented(mut self, implemented: bool) -> Self {
self.implemented = implemented;
self
}
/// Returns `true` if the descriptor advertises `capability`.
pub fn has_capability(&self, capability: MusicCapability) -> bool {
self.capabilities.contains(&capability)
}
/// Renders the full descriptor as an `Expr` map for wire transport.
pub fn to_expr(&self) -> Expr {
Expr::Map(vec![
field("id", Expr::Symbol(self.id.clone())),
field("label", Expr::String(self.label.clone())),
field("category", Expr::Symbol(self.category.symbol())),
field(
"capabilities",
Expr::Vector(
self.capabilities
.iter()
.map(|capability| Expr::Symbol(capability.symbol()))
.collect(),
),
),
field(
"ports",
Expr::Vector(
self.ports
.iter()
.map(MusicPortDescriptor::to_expr)
.collect(),
),
),
field(
"lanes",
Expr::Vector(self.lanes.iter().map(lane_expr).collect()),
),
field(
"params",
Expr::Vector(
self.params
.iter()
.map(MusicParamDescriptor::to_expr)
.collect(),
),
),
field("rate", rate_expr(self.rate)),
field("determinism", Expr::Symbol(self.determinism.symbol())),
field("accepted", lane_kind_list(&self.accepted_event_families)),
field("output", lane_kind_list(&self.output_families)),
field("latency", Expr::Symbol(self.latency.clone())),
field("implemented", Expr::Bool(self.implemented)),
])
}
}
fn stable_lane_kinds(mut kinds: Vec<LaneKind>) -> Vec<LaneKind> {
kinds.sort();
kinds.dedup();
kinds
}
fn lane_kind_list(kinds: &[LaneKind]) -> Expr {
Expr::Vector(
kinds
.iter()
.map(|kind| Expr::Symbol(kind.symbol()))
.collect(),
)
}
fn lane_expr(lane: &LaneDescriptor) -> Expr {
Expr::Map(vec![
field("id", Expr::String(lane.id.0.clone())),
field("kind", Expr::Symbol(lane.kind.symbol())),
field("target", Expr::Symbol(lane.target.symbol())),
field("order", Expr::String(lane.order.to_string())),
])
}
pub(crate) fn rate_expr(rate: RateContract) -> Expr {
Expr::Map(vec![
field("clock-domain", Expr::Symbol(rate.clock_domain().symbol())),
field("latency-class", Expr::Symbol(rate.latency_class().symbol())),
field(
"nominal-rate-hz",
Expr::String(
rate.nominal_rate_hz()
.map(|rate| rate.to_string())
.unwrap_or_else(|| "none".to_owned()),
),
),
])
}
fn field(name: &'static str, value: Expr) -> (Expr, Expr) {
(Expr::Symbol(Symbol::qualified(DESCRIPTOR_NS, name)), value)
}