alpha2-host-core 0.10.0

FFmpeg-free native host logic for alpha2: the policies, audio timelines, cursor rules, providers, plugin runtime, protocol frames and request parsing native hosts share, and the decode, export and still pipeline the mobile hosts run.
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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
use std::collections::HashMap;

use alpha2_audio::{AudioGraph, ChannelLayout, ProcessCtx, TransportState};
#[cfg(feature = "engine")]
use alpha2_engine::{AudioRenderer, AudioTap, EngineProject};
use alpha2_media::{
    AudioBlock, AudioBlockProvider, AudioBlockSink, AudioSpan, CancelToken, EncodeError, SourceId,
};
use alpha2_time::{MediaTime, TICKS_PER_SECOND};

/// Native offline-render failure.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum OfflineAudioError {
    /// Input channel count or length is invalid.
    InvalidInput,
    /// Sink rejected a block.
    Sink(EncodeError),
    /// The project could not be mixed at the requested configuration.
    #[cfg(feature = "engine")]
    Engine(alpha2_engine::EngineError),
    /// An audible clip needed media the caller had not decoded.
    MissingSources(Vec<SourceId>),
    /// The sources could not make ready what a block reads.
    #[cfg(feature = "engine")]
    Sources(crate::audio_windows::AudioWindowError),
}

impl std::fmt::Display for OfflineAudioError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidInput => formatter.write_str("offline audio input is invalid"),
            Self::Sink(error) => write!(formatter, "offline audio sink failed: {error}"),
            #[cfg(feature = "engine")]
            Self::Engine(error) => write!(formatter, "offline audio rendering failed: {error}"),
            #[cfg(feature = "engine")]
            Self::Sources(error) => write!(formatter, "offline audio sources failed: {error}"),
            Self::MissingSources(sources) => {
                write!(
                    formatter,
                    "offline audio is missing {} sources",
                    sources.len()
                )
            }
        }
    }
}

impl std::error::Error for OfflineAudioError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Sink(error) => Some(error),
            #[cfg(feature = "engine")]
            Self::Engine(error) => Some(error),
            #[cfg(feature = "engine")]
            Self::Sources(error) => Some(error),
            _ => None,
        }
    }
}

/// Decoded, project-rate planar audio held per source audio stream for offline mixing; one
/// whole-stream block each, indexed by absolute source position.
#[derive(Default)]
pub struct DecodedAudioSources {
    sources: HashMap<(SourceId, Option<u32>), AudioBlock>,
}

impl DecodedAudioSources {
    /// Creates an empty cache.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Retains one source's decoded default audio stream, replacing any earlier block.
    pub fn insert(&mut self, source: SourceId, block: AudioBlock) {
        self.sources.insert((source, None), block);
    }

    /// Retains audio stream `stream` of a source (`AudioProperties::stream`), replacing any
    /// earlier block.
    pub fn insert_stream(&mut self, source: SourceId, stream: Option<u32>, block: AudioBlock) {
        self.sources.insert((source, stream), block);
    }

    /// Drops every stream of one source's decoded audio.
    pub fn remove(&mut self, source: &SourceId) -> bool {
        let before = self.sources.len();
        self.sources.retain(|(held, _), _| held != source);
        self.sources.len() != before
    }

    /// Returns how many source streams are retained.
    #[must_use]
    pub fn len(&self) -> usize {
        self.sources.len()
    }

    /// Returns whether no source is retained.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.sources.is_empty()
    }
}

/// Each block is its source's entire audio; outside it the source is silent.
impl AudioBlockProvider for DecodedAudioSources {
    fn block(
        &self,
        source: &SourceId,
        stream: Option<u32>,
        _start_sample: i64,
        _frames: usize,
    ) -> Option<AudioSpan<'_>> {
        self.sources
            .get(&(source.clone(), stream))
            .map(AudioSpan::whole)
    }
}

/// The audio a project mix reads: decoded whole ([`DecodedAudioSources`]), or held in windows a
/// host fills as the mix reaches them ([`crate::audio_windows::WindowedAudioSources`]).
#[cfg(feature = "engine")]
pub trait ProjectAudioSources: AudioBlockProvider {
    /// Makes ready what `needs` reads, before the block that reads it renders. Audio decoded
    /// whole needs nothing.
    ///
    /// # Errors
    /// What decoding or holding the audio met.
    fn prepare(
        &mut self,
        _needs: &[alpha2_engine::AudioNeed],
        _cancel: &CancelToken,
    ) -> Result<(), crate::audio_windows::AudioWindowError> {
        Ok(())
    }
}

#[cfg(feature = "engine")]
impl ProjectAudioSources for DecodedAudioSources {}

/// One offline project-audio render request.
#[cfg(feature = "engine")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ProjectAudioRequest<'a> {
    /// Output channel count. One or two.
    pub channels: usize,
    /// Absolute project-rate sample index the range starts at, written as sample zero.
    pub start_sample: i64,
    /// Samples per channel to produce.
    pub total_frames: usize,
    /// Repeating block schedule, which may be irregular.
    pub block_sizes: &'a [usize],
    /// Tracks and buses to read beside the mix, for [`render_project_audio_with_taps`].
    pub taps: &'a [AudioTap],
}

/// Mixes a whole project audio range and writes exact project-rate blocks.
///
/// The offline counterpart of the browser pump: `sources` makes ready what each block reads and
/// the engine does gain, fades, panning, effects, bus summing and limiting. Irregular `block_sizes` repeat, so
/// callers can prove the mix is independent of block size.
///
/// # Errors
/// Returns [`OfflineAudioError::InvalidInput`] for an empty schedule or range,
/// [`OfflineAudioError::Engine`] when the renderer rejects the configuration,
/// [`OfflineAudioError::Sources`] when the sources cannot make a block's audio ready,
/// [`OfflineAudioError::MissingSources`] when an audible clip had no decoded media, and
/// [`OfflineAudioError::Sink`] when the destination rejects output.
#[cfg(feature = "engine")]
pub fn render_project_audio(
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    sources: &mut dyn ProjectAudioSources,
    sink: &mut dyn AudioBlockSink,
    cancel: &CancelToken,
) -> Result<(), OfflineAudioError> {
    render_project_audio_with_taps(snapshot, request, sources, sink, &mut [], cancel)
}

/// [`render_project_audio`], also writing each of `request.taps` into the sink at its position
/// in `tap_sinks`: the same blocks as the mix's, sample for sample in line with them. A tap is
/// read before the master, so it carries no master effects, gain or limiter.
///
/// # Errors
/// As [`render_project_audio`]; [`OfflineAudioError::InvalidInput`] also when `tap_sinks` are
/// not one per tap. Every sink is aborted when the render fails.
#[cfg(feature = "engine")]
pub fn render_project_audio_with_taps(
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    sources: &mut dyn ProjectAudioSources,
    sink: &mut dyn AudioBlockSink,
    tap_sinks: &mut [&mut dyn AudioBlockSink],
    cancel: &CancelToken,
) -> Result<(), OfflineAudioError> {
    let mut renderer = project_audio_renderer(snapshot, request, tap_sinks.len())?;
    write_project_audio(
        &mut renderer,
        snapshot,
        request,
        sources,
        (sink, tap_sinks),
        cancel,
    )
}

/// The true-peak ceiling a loudness target takes when it names none, in dBTP (EBU R128).
pub const DEFAULT_TRUE_PEAK_CEILING_DBTP: f64 = -1.0;

/// A loudness target (EBU R128, ITU-R BS.1770-4).
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct LoudnessTarget {
    /// Integrated loudness, in `-60..=0` LUFS.
    pub target_lufs: f64,
    /// The true-peak ceiling, in `-20..=0` dBTP. When the target would pass it, the gain is
    /// lowered and the render left quieter than the target.
    pub true_peak_ceiling_dbtp: f64,
}

impl LoudnessTarget {
    /// # Errors
    /// A message naming the field for a value out of range.
    pub fn new(target_lufs: f64, true_peak_ceiling_dbtp: f64) -> Result<Self, String> {
        if !(target_lufs.is_finite() && (-60.0..=0.0).contains(&target_lufs)) {
            return Err(format!(
                "loudness.targetLufs must be within -60..=0, received {target_lufs}"
            ));
        }
        if !(true_peak_ceiling_dbtp.is_finite() && (-20.0..=0.0).contains(&true_peak_ceiling_dbtp))
        {
            return Err(format!(
                "loudness.truePeakCeilingDbtp must be within -20..=0, received {true_peak_ceiling_dbtp}"
            ));
        }
        Ok(Self {
            target_lufs,
            true_peak_ceiling_dbtp,
        })
    }
}

/// What a normalized render measured, planned and wrote.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct LoudnessOutcome {
    /// The target it was asked for.
    pub target: LoudnessTarget,
    /// The master limiter's linear threshold, when the project has one.
    pub limiter_threshold: Option<f64>,
    /// The mix as it reached the limiter before any gain.
    pub measured: alpha2_audio::LoudnessReport,
    /// The gain planned from `measured`; `None` when the mix is silent below the absolute gate.
    pub applied: Option<alpha2_audio::LoudnessNormalization>,
    /// The mix as written, after the master limiter.
    pub output: Option<alpha2_audio::LoudnessReport>,
}

impl LoudnessOutcome {
    /// The export result's `loudness`, as every host reports it.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        let report = |report: &alpha2_audio::LoudnessReport| {
            serde_json::json!({
                "integratedLufs": report.integrated_lufs,
                "loudnessRangeLu": report.loudness_range_lu,
                "truePeakDbtp": report.true_peak_dbtp,
                "samplePeakDbfs": report.sample_peak_dbfs,
            })
        };
        serde_json::json!({
            "targetLufs": self.target.target_lufs,
            "truePeakCeilingDbtp": self.target.true_peak_ceiling_dbtp,
            "limiterThreshold": self.limiter_threshold,
            "measured": report(&self.measured),
            "applied": self.applied.map(|plan| serde_json::json!({
                "gainDb": plan.gain_db,
                "integratedLufs": plan.integrated_lufs,
                "truePeakDbtp": plan.true_peak_dbtp,
                "peakLimited": plan.peak_limited,
                "limiterLimited": plan.limiter_limited,
            })),
            "output": self.output.as_ref().map(report),
        })
    }
}

/// [`render_project_audio_with_taps`] normalized to `target` by a first pass that writes nothing.
///
/// Taps are read before the master, so the gain does not reach them.
///
/// # Errors
/// As [`render_project_audio_with_taps`]; [`OfflineAudioError::MissingSources`] after the first
/// pass, before anything is written. Every sink is aborted when the render fails.
#[cfg(feature = "engine")]
pub fn render_project_audio_normalized(
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    target: LoudnessTarget,
    sources: &mut dyn ProjectAudioSources,
    (sink, tap_sinks): (&mut dyn AudioBlockSink, &mut [&mut dyn AudioBlockSink]),
    cancel: &CancelToken,
) -> Result<LoudnessOutcome, OfflineAudioError> {
    let mut renderer = project_audio_renderer(snapshot, request, tap_sinks.len())?;
    let measured = match meter_project_audio(&mut renderer, snapshot, request, sources, cancel) {
        Ok(measured) => measured,
        Err(error) => {
            abort_all(sink, tap_sinks)?;
            return Err(error);
        }
    };
    renderer.reset();
    let limiter_threshold = renderer.limiter_threshold();
    let applied = alpha2_audio::LoudnessNormalization::plan(
        &measured,
        target.target_lufs,
        target.true_peak_ceiling_dbtp,
        limiter_threshold,
    );
    if let Some(plan) = applied {
        renderer
            .set_normalization_gain_db(plan.gain_db)
            .map_err(OfflineAudioError::Engine)?;
    }
    renderer
        .set_output_loudness_metering(true)
        .map_err(OfflineAudioError::Engine)?;
    write_project_audio(
        &mut renderer,
        snapshot,
        request,
        sources,
        (sink, tap_sinks),
        cancel,
    )?;
    Ok(LoudnessOutcome {
        target,
        limiter_threshold,
        measured,
        applied,
        output: renderer.output_loudness_report(),
    })
}

#[cfg(feature = "engine")]
fn meter_project_audio(
    renderer: &mut AudioRenderer,
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    sources: &mut dyn ProjectAudioSources,
    cancel: &CancelToken,
) -> Result<alpha2_audio::LoudnessReport, OfflineAudioError> {
    let ProjectAudioRequest {
        start_sample,
        total_frames,
        block_sizes,
        ..
    } = *request;
    renderer
        .set_loudness_metering(true)
        .map_err(OfflineAudioError::Engine)?;
    let mut produced = 0;
    let mut sequence = 0;
    let mut missing: Vec<SourceId> = Vec::new();
    while produced < total_frames {
        if cancel.is_cancelled() {
            return Err(OfflineAudioError::Sink(EncodeError::Cancelled));
        }
        let count = block_sizes[sequence % block_sizes.len()].min(total_frames - produced);
        let at = start_sample.saturating_add(i64::try_from(produced).unwrap_or(i64::MAX));
        let needs = renderer
            .audio_needs(snapshot, at, count)
            .map_err(OfflineAudioError::Engine)?;
        sources
            .prepare(&needs, cancel)
            .map_err(OfflineAudioError::Sources)?;
        let status = renderer
            .render(snapshot, at, count, &*sources, TransportState::Playing)
            .map_err(OfflineAudioError::Engine)?;
        for source in status.missing_sources {
            if !missing.contains(&source) {
                missing.push(source);
            }
        }
        produced += count;
        sequence += 1;
    }
    if !missing.is_empty() {
        return Err(OfflineAudioError::MissingSources(missing));
    }
    let report = renderer
        .loudness_report()
        .ok_or(OfflineAudioError::InvalidInput)?;
    renderer
        .set_loudness_metering(false)
        .map_err(OfflineAudioError::Engine)?;
    Ok(report)
}

#[cfg(feature = "engine")]
fn project_audio_renderer(
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    tap_sinks: usize,
) -> Result<AudioRenderer, OfflineAudioError> {
    let ProjectAudioRequest {
        channels,
        start_sample,
        total_frames,
        block_sizes,
        taps,
    } = *request;
    if total_frames == 0
        || block_sizes.is_empty()
        || block_sizes.contains(&0)
        || start_sample < 0
        || tap_sinks != taps.len()
    {
        return Err(OfflineAudioError::InvalidInput);
    }
    let max_block = block_sizes.iter().copied().max().unwrap_or(1);
    AudioRenderer::with_taps(snapshot, channels, max_block, taps).map_err(OfflineAudioError::Engine)
}

#[cfg(feature = "engine")]
fn write_project_audio(
    renderer: &mut AudioRenderer,
    snapshot: &EngineProject,
    request: &ProjectAudioRequest<'_>,
    sources: &mut dyn ProjectAudioSources,
    (sink, tap_sinks): (&mut dyn AudioBlockSink, &mut [&mut dyn AudioBlockSink]),
    cancel: &CancelToken,
) -> Result<(), OfflineAudioError> {
    let ProjectAudioRequest {
        start_sample,
        total_frames,
        block_sizes,
        ..
    } = *request;
    let mut produced = 0;
    let mut sequence = 0;
    let mut missing: Vec<SourceId> = Vec::new();
    while produced < total_frames {
        if cancel.is_cancelled() {
            abort_all(sink, tap_sinks)?;
            return Err(OfflineAudioError::Sink(EncodeError::Cancelled));
        }
        let count = block_sizes[sequence % block_sizes.len()].min(total_frames - produced);
        let offset = i64::try_from(produced).unwrap_or(i64::MAX);
        let at = start_sample.saturating_add(offset);
        let needs = renderer
            .audio_needs(snapshot, at, count)
            .map_err(OfflineAudioError::Engine)?;
        sources
            .prepare(&needs, cancel)
            .map_err(OfflineAudioError::Sources)?;
        let status = renderer
            .render(snapshot, at, count, &*sources, TransportState::Playing)
            .map_err(OfflineAudioError::Engine)?;
        for source in &status.missing_sources {
            if !missing.contains(source) {
                missing.push(source.clone());
            }
        }
        let mut block = renderer.block(&status);
        // The range is its own file: its first sample is the sink's sample zero.
        block.start_sample = offset;
        sink.write_audio(&block, cancel)
            .map_err(OfflineAudioError::Sink)?;
        for (index, tap_sink) in tap_sinks.iter_mut().enumerate() {
            if let Some(mut block) = renderer.tap_block(index, &status) {
                block.start_sample = offset;
                tap_sink
                    .write_audio(&block, cancel)
                    .map_err(OfflineAudioError::Sink)?;
            }
        }
        produced += count;
        sequence += 1;
    }
    if !missing.is_empty() {
        // Report an incomplete mix rather than committing partial audio.
        abort_all(sink, tap_sinks)?;
        return Err(OfflineAudioError::MissingSources(missing));
    }
    sink.finish_audio(cancel).map_err(OfflineAudioError::Sink)?;
    for tap_sink in tap_sinks {
        tap_sink
            .finish_audio(cancel)
            .map_err(OfflineAudioError::Sink)?;
    }
    Ok(())
}

#[cfg(feature = "engine")]
fn abort_all(
    sink: &mut dyn AudioBlockSink,
    tap_sinks: &mut [&mut dyn AudioBlockSink],
) -> Result<(), OfflineAudioError> {
    let mut aborted = sink.abort_audio();
    for tap_sink in tap_sinks {
        aborted = aborted.and(tap_sink.abort_audio());
    }
    aborted.map_err(OfflineAudioError::Sink)
}

/// Host sample representation considered during deterministic negotiation.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum HostSampleFormat {
    /// Native 32-bit floating point.
    F32,
    /// Signed 16-bit integer.
    I16,
    /// Any format not supported by the realtime host.
    Unsupported,
}

/// Platform-neutral device range used by negotiation tests and cpal mapping.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct HostAudioConfig {
    /// Channel count.
    pub channels: u16,
    /// Lowest supported sample rate.
    pub min_sample_rate: u32,
    /// Highest supported sample rate.
    pub max_sample_rate: u32,
    /// Device sample representation.
    pub format: HostSampleFormat,
}

/// Selects an output that plays stereo, requiring exact project-rate `f32` support.
///
/// Prefers exactly two channels, then the fewest above two, then mono. Backends list counts
/// differently (ALSA every count, WASAPI only the mix format's, `CoreAudio` the sum of stream
/// channels), so a multichannel device may offer no stereo entry; stereo then goes to its first two
/// channels (front left and right on all three) and the rest are silent.
#[must_use]
pub fn negotiate_output_config(
    configs: &[HostAudioConfig],
    project_rate: u32,
) -> Option<HostAudioConfig> {
    configs
        .iter()
        .copied()
        .filter(|config| {
            config.format == HostSampleFormat::F32
                && config.channels > 0
                && (config.min_sample_rate..=config.max_sample_rate).contains(&project_rate)
        })
        .min_by_key(|config| match config.channels {
            2 => (0, 0),
            1 => (2, 0),
            more => (1, more),
        })
}

/// Runs the same graph used by realtime hosts and writes exact project-rate blocks.
///
/// `inputs` are the graph's buses flattened, each bus's planes in the graph's layout; all channels
/// must have the same length. Irregular `block_sizes` repeat until the source is exhausted.
///
/// # Errors
/// Returns [`OfflineAudioError::InvalidInput`] when `inputs` are not exactly the graph's buses or
/// the schedule is malformed, and [`OfflineAudioError::Sink`] when the destination rejects output.
pub fn render_offline(
    graph: &mut AudioGraph,
    sample_rate: u32,
    inputs: &[Vec<f32>],
    block_sizes: &[usize],
    sink: &mut dyn AudioBlockSink,
    cancel: &CancelToken,
) -> Result<(), OfflineAudioError> {
    let layout = graph.layout();
    let frames = inputs.first().map_or(0, Vec::len);
    if sample_rate == 0
        || inputs.is_empty()
        || graph.buses().checked_mul(layout.channels()) != Some(inputs.len())
        || inputs.iter().any(|channel| channel.len() != frames)
        || block_sizes.is_empty()
        || block_sizes.contains(&0)
    {
        return Err(OfflineAudioError::InvalidInput);
    }
    let max_block = block_sizes.iter().copied().max().unwrap_or(1);
    graph.prepare(sample_rate, max_block);
    let mut output: Vec<Vec<f32>> = (0..layout.channels())
        .map(|_| vec![0.0; max_block])
        .collect();
    let mut offset = 0;
    let mut sequence = 0;
    while offset < frames {
        if cancel.is_cancelled() {
            sink.abort_audio().map_err(OfflineAudioError::Sink)?;
            return Err(OfflineAudioError::Sink(EncodeError::Cancelled));
        }
        let count = block_sizes[sequence % block_sizes.len()].min(frames - offset);
        let refs: Vec<&[f32]> = inputs
            .iter()
            .map(|channel| &channel[offset..offset + count])
            .collect();
        let mut outputs: Vec<&mut [f32]> = output
            .iter_mut()
            .map(|channel| &mut channel[..count])
            .collect();
        let sample_position = i64::try_from(offset).unwrap_or(i64::MAX);
        graph.process(
            &ProcessCtx {
                position: MediaTime::from_ticks(
                    sample_position
                        .saturating_mul(TICKS_PER_SECOND)
                        .div_euclid(i64::from(sample_rate)),
                ),
                sample_position,
                sample_rate,
                transport: TransportState::Playing,
            },
            &refs,
            &mut outputs,
        );
        drop(outputs);
        sink.write_audio(
            &AudioBlock {
                start_sample: sample_position,
                sample_rate,
                speakers: match layout {
                    ChannelLayout::Mono => alpha2_media::SpeakerLayout::MONO,
                    ChannelLayout::Stereo => alpha2_media::SpeakerLayout::STEREO,
                },
                channels: output
                    .iter()
                    .map(|channel| channel[..count].to_vec())
                    .collect(),
            },
            cancel,
        )
        .map_err(OfflineAudioError::Sink)?;
        offset += count;
        sequence += 1;
    }
    sink.finish_audio(cancel).map_err(OfflineAudioError::Sink)
}

#[cfg(feature = "cpal-host")]
mod realtime {
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::{Arc, Mutex, PoisonError};

    use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
    use cpal::{SampleFormat, Stream, StreamConfig};

    use super::{
        HostAudioConfig, HostSampleFormat, MediaTime, ProcessCtx, TICKS_PER_SECOND, TransportState,
        negotiate_output_config,
    };

    /// Pull source invoked exclusively by the audio callback.
    ///
    /// `alpha2_engine::AudioRenderer` does not implement this, since timeline
    /// evaluation may allocate. Realtime hosts supply a pre-indexed, allocation-free
    /// source or a bounded producer ring.
    pub trait RealtimeAudioSource: Send + 'static {
        /// Fills project-rate planar output without allocation or blocking.
        fn render(&mut self, ctx: &ProcessCtx, output: &mut [&mut [f32]]);
        /// Algorithmic latency already present in the source graph.
        fn latency_samples(&self) -> u32 {
            0
        }
    }

    /// Native realtime host failure.
    #[derive(Debug)]
    #[non_exhaustive]
    pub enum CpalHostError {
        /// No output device exists.
        NoOutputDevice,
        /// No compatible floating-point configuration exists.
        NoCompatibleFormat,
        /// The callback limit is zero frames.
        InvalidCallbackSize,
        /// Device query or stream construction failed.
        Device(String),
    }

    impl std::fmt::Display for CpalHostError {
        fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                Self::NoOutputDevice => formatter.write_str("no audio output device is available"),
                Self::NoCompatibleFormat => {
                    formatter.write_str("no compatible audio output format is available")
                }
                Self::InvalidCallbackSize => {
                    formatter.write_str("the audio callback limit must be at least one frame")
                }
                Self::Device(message) => write!(formatter, "audio device failed: {message}"),
            }
        }
    }

    impl std::error::Error for CpalHostError {}

    /// Running cpal output stream, its observable underrun count and its last stream error.
    pub struct CpalOutputHost {
        stream: Stream,
        underruns: Arc<AtomicU64>,
        stream_error: Arc<Mutex<Option<String>>>,
        sample_rate: u32,
        channels: u16,
        latency_samples: u32,
    }

    impl CpalOutputHost {
        /// Opens the default output at the exact project rate in `f32`, with the
        /// channels [`negotiate_output_config`] picks.
        ///
        /// `source` renders at most `max_callback_frames` at a time into preallocated scratch;
        /// larger device buffers take several calls.
        ///
        /// # Errors
        /// Returns [`CpalHostError::InvalidCallbackSize`] when `max_callback_frames` is zero, and a
        /// device or format error when no exact realtime configuration can be opened.
        pub fn open(
            mut source: impl RealtimeAudioSource,
            project_rate: u32,
            max_callback_frames: usize,
        ) -> Result<Self, CpalHostError> {
            let device = cpal::default_host()
                .default_output_device()
                .ok_or(CpalHostError::NoOutputDevice)?;
            if max_callback_frames == 0 {
                return Err(CpalHostError::InvalidCallbackSize);
            }
            let supported = device
                .supported_output_configs()
                .map_err(|error| CpalHostError::Device(error.to_string()))?
                .collect::<Vec<_>>();
            let offered = supported
                .iter()
                .map(|config| HostAudioConfig {
                    channels: config.channels(),
                    min_sample_rate: config.min_sample_rate(),
                    max_sample_rate: config.max_sample_rate(),
                    format: match config.sample_format() {
                        SampleFormat::F32 => HostSampleFormat::F32,
                        SampleFormat::I16 => HostSampleFormat::I16,
                        _ => HostSampleFormat::Unsupported,
                    },
                })
                .collect::<Vec<_>>();
            let chosen = negotiate_output_config(&offered, project_rate)
                .ok_or(CpalHostError::NoCompatibleFormat)?;
            let selected = offered
                .iter()
                .position(|config| *config == chosen)
                .map(|index| supported[index])
                .ok_or(CpalHostError::NoCompatibleFormat)?;
            let device_rate = project_rate;
            let config: StreamConfig = selected.with_sample_rate(device_rate).config();
            let channels = usize::from(config.channels);
            let mut planar = [
                vec![0.0_f32; max_callback_frames],
                vec![0.0_f32; max_callback_frames],
            ];
            let underruns = Arc::new(AtomicU64::new(0));
            let callback_underruns = Arc::clone(&underruns);
            let error_underruns = Arc::clone(&underruns);
            let stream_error = Arc::new(Mutex::new(None));
            let error_text = Arc::clone(&stream_error);
            let mut sample_position = 0_i64;
            let latency_samples = source.latency_samples();
            let stream = device
                .build_output_stream(
                    &config,
                    move |data: &mut [f32], _| {
                        if !fill_device_buffer(
                            &mut source,
                            &mut planar,
                            data,
                            channels,
                            device_rate,
                            &mut sample_position,
                        ) {
                            callback_underruns.fetch_add(1, Ordering::Relaxed);
                        }
                    },
                    move |error| {
                        error_underruns.fetch_add(1, Ordering::Relaxed);
                        *error_text.lock().unwrap_or_else(PoisonError::into_inner) =
                            Some(error.to_string());
                    },
                    None,
                )
                .map_err(|error| CpalHostError::Device(error.to_string()))?;
            Ok(Self {
                stream,
                underruns,
                stream_error,
                sample_rate: device_rate,
                channels: config.channels,
                latency_samples,
            })
        }
        /// Starts callback delivery.
        ///
        /// # Errors
        /// Returns the device error reported by cpal.
        pub fn play(&self) -> Result<(), CpalHostError> {
            self.stream
                .play()
                .map_err(|error| CpalHostError::Device(error.to_string()))
        }
        /// Stops callback delivery until [`Self::play`].
        ///
        /// # Errors
        /// Returns the device error reported by cpal.
        pub fn pause(&self) -> Result<(), CpalHostError> {
            self.stream
                .pause()
                .map_err(|error| CpalHostError::Device(error.to_string()))
        }
        /// Errors the stream reported, plus device buffers that were not whole
        /// frames and played silent.
        #[must_use]
        pub fn underruns(&self) -> u64 {
            self.underruns.load(Ordering::Relaxed)
        }
        /// The last error the stream reported (a device that went away, for one), `None` while
        /// it reported none.
        #[must_use]
        pub fn stream_error(&self) -> Option<String> {
            self.stream_error
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .clone()
        }
        /// Negotiated device sample rate.
        #[must_use]
        pub const fn sample_rate(&self) -> u32 {
            self.sample_rate
        }
        /// Negotiated output channels.
        #[must_use]
        pub const fn channels(&self) -> u16 {
            self.channels
        }
        /// Source graph latency for transport compensation.
        #[must_use]
        pub const fn latency_samples(&self) -> u32 {
            self.latency_samples
        }
    }

    /// Renders `source` into one interleaved device buffer of
    /// `channels`, advancing `sample_position`.
    ///
    /// Rendered in scratch-sized pieces. Mono devices get mono, others stereo in the first
    /// two channels and silence elsewhere. Returns false, after writing silence, for a buffer
    /// that is not whole frames.
    pub(super) fn fill_device_buffer(
        source: &mut impl RealtimeAudioSource,
        planar: &mut [Vec<f32>; 2],
        data: &mut [f32],
        channels: usize,
        device_rate: u32,
        sample_position: &mut i64,
    ) -> bool {
        let piece = planar[0].len();
        if channels == 0 || piece == 0 || !data.len().is_multiple_of(channels) {
            data.fill(0.0);
            return false;
        }
        let rendered = channels.min(2);
        for chunk in data.chunks_mut(piece * channels) {
            let frames = chunk.len() / channels;
            let ctx = ProcessCtx {
                position: MediaTime::from_ticks(
                    sample_position
                        .saturating_mul(TICKS_PER_SECOND)
                        .div_euclid(i64::from(device_rate)),
                ),
                sample_position: *sample_position,
                sample_rate: device_rate,
                transport: TransportState::Playing,
            };
            let (left, right) = planar.split_at_mut(1);
            if rendered == 1 {
                source.render(&ctx, &mut [&mut left[0][..frames]]);
            } else {
                source.render(&ctx, &mut [&mut left[0][..frames], &mut right[0][..frames]]);
            }
            for (frame, destination) in chunk.chunks_exact_mut(channels).enumerate() {
                for (channel, sample) in destination.iter_mut().enumerate() {
                    *sample = if channel < rendered {
                        planar[channel][frame]
                    } else {
                        0.0
                    };
                }
            }
            *sample_position =
                sample_position.saturating_add(i64::try_from(frames).unwrap_or(i64::MAX));
        }
        true
    }
}

#[cfg(feature = "cpal-host")]
pub use realtime::*;

#[cfg(test)]
#[path = "audio_host_tests.rs"]
mod tests;