sim-lib-stream-core 0.1.0

Core stream metadata, packets, envelopes, and buffer values.
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
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
//! Stream metadata values and their publication into the runtime claim store.
//!
//! This module supplies the concrete description of a stream's identity:
//! [`StreamMetadata`] bundles a stream id, its [`StreamMedia`] kind,
//! [`StreamDirection`], clock symbol, and buffer policy. [`RateContract`]
//! captures the clock-domain/latency/rate agreement two ports must share to
//! connect.
//!
//! The kernel defines the claim/fact contract and the clock-domain surface;
//! this module supplies the streaming-fabric behavior on top of it. The
//! `stream_*_predicate` helpers name the predicate symbols, and
//! [`publish_metadata_claims`] writes a stream's metadata into the runtime as
//! public facts so other libraries can query a stream's shape.

use sim_kernel::{
    Claim, ClaimPattern, Cx, Error, Expr, Ref, Result, Symbol, stream_surface,
    stream_surface::publish_stream_metadata_claims,
};

use crate::buffer::{BufferPolicy, expr_kind, field, string_field, symbol_field};
use crate::{ClockDomain, LatencyClass};

/// Media kind carried by a stream.
///
/// Selects which packet profile an envelope on the stream is expected to
/// carry and which `stream/media/*` symbol identifies the stream in the
/// claim store.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StreamMedia {
    /// Real-time PCM audio frames (see [`PcmPacket`](crate::PcmPacket)).
    Pcm,
    /// MIDI events (see [`MidiPacket`](crate::MidiPacket)).
    Midi,
    /// Diagnostic messages (see [`StreamDiagnostic`](crate::StreamDiagnostic)).
    Diagnostic,
    /// Opaque structured data payloads (see [`DataPacket`](crate::DataPacket)).
    Data,
}

impl StreamMedia {
    /// Returns the `stream/media/*` symbol identifying this media kind.
    pub fn symbol(self) -> Symbol {
        match self {
            Self::Pcm => Symbol::qualified("stream/media", "pcm"),
            Self::Midi => Symbol::qualified("stream/media", "midi"),
            Self::Diagnostic => Symbol::qualified("stream/media", "diagnostic"),
            Self::Data => Symbol::qualified("stream/media", "data"),
        }
    }

    /// Parses a [`StreamMedia`] from its `stream/media/*` symbol.
    ///
    /// Returns an error for any symbol outside the known media kinds.
    pub fn from_symbol(symbol: &Symbol) -> Result<Self> {
        match symbol.as_qualified_str().as_str() {
            "stream/media/pcm" => Ok(Self::Pcm),
            "stream/media/midi" => Ok(Self::Midi),
            "stream/media/diagnostic" => Ok(Self::Diagnostic),
            "stream/media/data" => Ok(Self::Data),
            other => Err(Error::Eval(format!("unknown stream media {other}"))),
        }
    }
}

/// Flow direction of a stream relative to its owner.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StreamDirection {
    /// Produces envelopes (output).
    Source,
    /// Consumes envelopes (input).
    Sink,
    /// Both produces and consumes envelopes.
    Duplex,
}

impl StreamDirection {
    /// Returns the `stream/direction/*` symbol identifying this direction.
    pub fn symbol(self) -> Symbol {
        match self {
            Self::Source => Symbol::qualified("stream/direction", "source"),
            Self::Sink => Symbol::qualified("stream/direction", "sink"),
            Self::Duplex => Symbol::qualified("stream/direction", "duplex"),
        }
    }

    /// Parses a [`StreamDirection`] from its `stream/direction/*` symbol.
    ///
    /// Returns an error for any symbol outside the known directions.
    pub fn from_symbol(symbol: &Symbol) -> Result<Self> {
        match symbol.as_qualified_str().as_str() {
            "stream/direction/source" => Ok(Self::Source),
            "stream/direction/sink" => Ok(Self::Sink),
            "stream/direction/duplex" => Ok(Self::Duplex),
            other => Err(Error::Eval(format!("unknown stream direction {other}"))),
        }
    }
}

/// Timing agreement a stream port advertises and must share to connect.
///
/// Pairs a [`ClockDomain`] with a [`LatencyClass`] and an optional nominal
/// sample rate in hertz. Two ports may be wired together only when their
/// contracts are compatible (see [`is_compatible_with`](RateContract::is_compatible_with)).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RateContract {
    clock_domain: ClockDomain,
    latency_class: LatencyClass,
    nominal_rate_hz: Option<u32>,
}

impl RateContract {
    /// Builds a contract from an explicit clock domain, latency class, and
    /// optional nominal rate.
    pub fn new(
        clock_domain: ClockDomain,
        latency_class: LatencyClass,
        nominal_rate_hz: Option<u32>,
    ) -> Self {
        Self {
            clock_domain,
            latency_class,
            nominal_rate_hz,
        }
    }

    /// Contract for sample-exact audio: sample clock domain, sample-exact
    /// latency, and the given nominal rate.
    pub fn sample_exact(nominal_rate_hz: Option<u32>) -> Self {
        Self::new(
            ClockDomain::Sample,
            LatencyClass::SampleExact,
            nominal_rate_hz,
        )
    }

    /// Contract for block-local processing: block clock domain and block-local
    /// latency, with no fixed nominal rate.
    pub fn block_local() -> Self {
        Self::new(ClockDomain::Block, LatencyClass::BlockLocal, None)
    }

    /// Contract for interactive control traffic: control clock domain and
    /// interactive latency.
    pub fn control() -> Self {
        Self::new(ClockDomain::Control, LatencyClass::Interactive, None)
    }

    /// Contract for MIDI-tick traffic: MIDI-tick clock domain and interactive
    /// latency.
    pub fn midi_tick() -> Self {
        Self::new(ClockDomain::MidiTick, LatencyClass::Interactive, None)
    }

    /// Contract for offline trace stepping: trace-step clock domain and
    /// offline-render latency.
    pub fn trace_step() -> Self {
        Self::new(ClockDomain::TraceStep, LatencyClass::OfflineRender, None)
    }

    /// Returns the clock domain this contract runs in.
    pub fn clock_domain(self) -> ClockDomain {
        self.clock_domain
    }

    /// Returns the latency class this contract promises.
    pub fn latency_class(self) -> LatencyClass {
        self.latency_class
    }

    /// Returns the nominal sample rate in hertz, if one is fixed.
    pub fn nominal_rate_hz(self) -> Option<u32> {
        self.nominal_rate_hz
    }

    /// Reports whether `self` and `other` may be connected.
    ///
    /// Compatible means matching clock domain and latency class; nominal rates
    /// must agree only when both are fixed (an unset rate matches any rate).
    pub fn is_compatible_with(self, other: Self) -> bool {
        self.clock_domain == other.clock_domain
            && self.latency_class == other.latency_class
            && rates_are_compatible(self.nominal_rate_hz, other.nominal_rate_hz)
    }

    /// Checks compatibility with `other`, returning a descriptive error when
    /// the two contracts cannot be connected.
    pub fn ensure_compatible(self, other: Self) -> Result<()> {
        if self.is_compatible_with(other) {
            return Ok(());
        }
        Err(Error::Eval(format!(
            "incompatible port rate contracts: source {} {} {:?}, target {} {} {:?}",
            self.clock_domain.wire_label(),
            self.latency_class.wire_label(),
            self.nominal_rate_hz,
            other.clock_domain.wire_label(),
            other.latency_class.wire_label(),
            other.nominal_rate_hz
        )))
    }
}

fn rates_are_compatible(left: Option<u32>, right: Option<u32>) -> bool {
    match (left, right) {
        (Some(left), Some(right)) => left == right,
        _ => true,
    }
}

/// Full identity of a stream: id, media kind, direction, clock, and buffer
/// policy.
///
/// This is the value other libraries inspect to learn a stream's shape, and
/// the value [`publish_metadata_claims`] turns into runtime facts. It
/// round-trips to and from an [`Expr`] map (`table_expr`/`from_table_expr`)
/// and to constructor arguments (`to_constructor_args`/`from_constructor_args`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamMetadata {
    id: Symbol,
    media: StreamMedia,
    direction: StreamDirection,
    clock: Symbol,
    buffer: BufferPolicy,
}

impl StreamMetadata {
    /// Builds metadata from its id, media kind, direction, clock symbol, and
    /// buffer policy.
    pub fn new(
        id: Symbol,
        media: StreamMedia,
        direction: StreamDirection,
        clock: Symbol,
        buffer: BufferPolicy,
    ) -> Self {
        Self {
            id,
            media,
            direction,
            clock,
            buffer,
        }
    }

    /// Returns the stream's identity symbol.
    pub fn id(&self) -> &Symbol {
        &self.id
    }

    /// Returns the stream's media kind.
    pub fn media(&self) -> StreamMedia {
        self.media
    }

    /// Returns the stream's flow direction.
    pub fn direction(&self) -> StreamDirection {
        self.direction
    }

    /// Returns the symbol naming the stream's clock.
    pub fn clock(&self) -> &Symbol {
        &self.clock
    }

    /// Returns the stream's buffer policy.
    pub fn buffer(&self) -> &BufferPolicy {
        &self.buffer
    }

    /// Returns the claim-store subject reference for this stream (its id as a
    /// symbol ref).
    pub fn subject_ref(&self) -> Ref {
        Ref::Symbol(self.id.clone())
    }

    /// Encodes the metadata as the ordered constructor argument expressions
    /// accepted by [`from_constructor_args`](StreamMetadata::from_constructor_args).
    pub fn to_constructor_args(&self) -> Vec<Expr> {
        vec![
            Expr::Symbol(self.id.clone()),
            Expr::Symbol(self.media.symbol()),
            Expr::Symbol(self.direction.symbol()),
            Expr::Symbol(self.clock.clone()),
            self.buffer.to_expr(),
        ]
    }

    /// Rebuilds metadata from the five constructor argument expressions
    /// produced by [`to_constructor_args`](StreamMetadata::to_constructor_args).
    ///
    /// Returns an error when the argument count or any argument shape is wrong.
    pub fn from_constructor_args(args: Vec<Expr>) -> Result<Self> {
        let [id, media, direction, clock, buffer] = args.as_slice() else {
            return Err(Error::Eval(
                "stream/Metadata expects five constructor arguments".to_owned(),
            ));
        };
        Ok(Self::new(
            symbol_expr(id, "stream id")?,
            StreamMedia::from_symbol(symbol_expr_ref(media, "stream media")?)?,
            StreamDirection::from_symbol(symbol_expr_ref(direction, "stream direction")?)?,
            symbol_expr(clock, "stream clock")?,
            BufferPolicy::from_expr(buffer)?,
        ))
    }

    /// Encodes the metadata as a self-describing `Expr` map keyed by field
    /// name (`kind`, `id`, `media`, `direction`, `clock`, `buffer`).
    pub fn table_expr(&self) -> Expr {
        Expr::Map(vec![
            (
                Expr::Symbol(Symbol::new("kind")),
                Expr::Symbol(stream_surface::stream_kind()),
            ),
            (
                Expr::Symbol(Symbol::new("id")),
                Expr::String(self.id.to_string()),
            ),
            (
                Expr::Symbol(Symbol::new("media")),
                Expr::Symbol(self.media.symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("direction")),
                Expr::Symbol(self.direction.symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("clock")),
                Expr::Symbol(self.clock.clone()),
            ),
            (Expr::Symbol(Symbol::new("buffer")), self.buffer.to_expr()),
        ])
    }

    /// Rebuilds metadata from the `Expr` map produced by
    /// [`table_expr`](StreamMetadata::table_expr).
    ///
    /// Returns an error when the expression is not a map or a required field
    /// is missing or malformed.
    pub fn from_table_expr(expr: &Expr) -> Result<Self> {
        let Expr::Map(entries) = expr else {
            return Err(Error::TypeMismatch {
                expected: "stream metadata map",
                found: expr_kind(expr),
            });
        };
        Ok(Self::new(
            Symbol::new(string_field(entries, "id")?.to_owned()),
            StreamMedia::from_symbol(symbol_field(entries, "media")?)?,
            StreamDirection::from_symbol(symbol_field(entries, "direction")?)?,
            symbol_field(entries, "clock")?.clone(),
            BufferPolicy::from_expr(field(entries, "buffer")?)?,
        ))
    }
}

/// Returns the `stream/id` predicate symbol used for stream-identity facts.
pub fn stream_id_predicate() -> Symbol {
    Symbol::qualified("stream", "id")
}

/// Returns the `stream/media` predicate symbol used for media-kind facts.
pub fn stream_media_predicate() -> Symbol {
    Symbol::qualified("stream", "media")
}

/// Returns the `stream/direction` predicate symbol used for direction facts.
pub fn stream_direction_predicate() -> Symbol {
    Symbol::qualified("stream", "direction")
}

/// Returns the `stream/buffer` predicate symbol used for buffer-policy facts.
pub fn stream_buffer_predicate() -> Symbol {
    Symbol::qualified("stream", "buffer")
}

/// Publishes a stream's metadata into the runtime as public facts about
/// `subject`.
///
/// Writes one claim per field (id, media, direction, clock, buffer) through
/// the kernel's stream-metadata claim surface, then records the default
/// in-memory transport fact once. The kernel owns the claim/fact contract;
/// this function supplies the streaming-fabric mapping from [`StreamMetadata`]
/// onto it. The transport fact is inserted only if not already present.
pub fn publish_metadata_claims(cx: &mut Cx, subject: Ref, metadata: &StreamMetadata) -> Result<()> {
    publish_stream_metadata_claims(
        cx,
        subject.clone(),
        [
            (stream_id_predicate(), Ref::Symbol(metadata.id.clone())),
            (
                stream_media_predicate(),
                Ref::Symbol(metadata.media.symbol()),
            ),
            (
                stream_direction_predicate(),
                Ref::Symbol(metadata.direction.symbol()),
            ),
            (
                stream_surface::stream_clock_predicate(),
                Ref::Symbol(metadata.clock.clone()),
            ),
            (
                stream_buffer_predicate(),
                Ref::Symbol(metadata.buffer.symbol()),
            ),
        ],
    )?;
    insert_once(
        cx,
        subject,
        stream_surface::stream_transport_predicate(),
        Ref::Symbol(Symbol::qualified("stream", "memory")),
    )
}

fn insert_once(cx: &mut Cx, subject: Ref, predicate: Symbol, object: Ref) -> Result<()> {
    let exists = !cx
        .query_facts(ClaimPattern::exact(
            subject.clone(),
            predicate.clone(),
            object.clone(),
        ))?
        .is_empty();
    if !exists {
        cx.insert_fact(Claim::public(subject, predicate, object))?;
    }
    Ok(())
}

fn symbol_expr(expr: &Expr, expected: &'static str) -> Result<Symbol> {
    Ok(symbol_expr_ref(expr, expected)?.clone())
}

fn symbol_expr_ref<'a>(expr: &'a Expr, expected: &'static str) -> Result<&'a Symbol> {
    match expr {
        Expr::Symbol(symbol) => Ok(symbol),
        other => Err(Error::TypeMismatch {
            expected,
            found: expr_kind(other),
        }),
    }
}