duet-cli 0.3.0

duet: compose with an LLM in text both can read, and convert it to and from MIDI.
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
//! `duet record`: a take from the machine's microphone, kept as a FLAC file that `duet capture`
//! reads like any other.
//!
//! Writing the file is `duet-capture`'s [`TakeWriter`], and tested there. Opening the input
//! device runs only on a machine with one: no test in CI covers it. What happens to each buffer
//! after the device hands it over ([`Handoff`], [`Take`]) is tested here, and what is refused
//! before a device is opened is tested end to end.
//!
//! The device's callback never waits: it hands each buffer to the writing thread through a
//! bounded channel. Input is lost two ways, and both are written as silence where it was, so
//! what follows keeps its time, and said with when and how much: a buffer the channel cannot
//! take, and a gap the device leaves, measured from the capture time it stamps each buffer with.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender, TrySendError};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use anyhow::{Context, Result, anyhow, bail};
use clap::Args;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{SampleFormat, SizedSample};
use duet_capture::{Depth, TakeWriter, Written};

/// Buffers waiting for the writer before the device's next one is counted as lost: at a
/// typical 512 frames a buffer, about seven seconds at 48 kHz.
const QUEUE: usize = 640;

#[derive(Args)]
pub struct RecordArgs {
    /// The FLAC file to write the take to. It must not exist: a take never replaces a file.
    output: PathBuf,
    /// The input device to record from: its name, or a part of it only one input's name
    /// holds. Without it, the system's default input.
    #[arg(long)]
    device: Option<String>,
    /// Stop after this many seconds, instead of at Ctrl-C.
    #[arg(long, value_parser = positive_seconds)]
    seconds: Option<f64>,
}

fn positive_seconds(text: &str) -> std::result::Result<f64, String> {
    match text.parse::<f64>() {
        Ok(secs) if secs.is_finite() && secs > 0.0 => Ok(secs),
        _ => Err(format!("{text:?} is not a number of seconds above zero")),
    }
}

/// Input lost before the take's frame `at`, and written there as `frames` of silence.
#[derive(Debug, PartialEq, Eq)]
struct Loss {
    at: u64,
    frames: u64,
}

/// One buffer from the device, with how many frames were lost just before it.
struct Block {
    lost_before: u64,
    samples: Vec<f32>,
}

/// What the device's callbacks share with the writing thread.
#[derive(Default)]
struct Shared {
    /// Frames of the device's timeline so far, lost ones included: where an overrun happened.
    delivered: AtomicU64,
    /// Frames lost since the last buffer the channel took.
    lost: AtomicU64,
    /// Where the device reported overruns, in frames of its timeline.
    overruns: Mutex<Vec<u64>>,
    /// Why the stream ended, when the device ended it.
    failed: Mutex<Option<String>>,
}

pub fn run(args: RecordArgs) -> Result<()> {
    refuse_output(&args.output)?;
    let stop = Arc::new(AtomicBool::new(false));
    {
        let stop = Arc::clone(&stop);
        ctrlc::set_handler(move || stop.store(true, Ordering::Release))
            .context("catching Ctrl-C")?;
    }
    let host = cpal::default_host();
    let device = match &args.device {
        Some(wanted) => {
            let devices: Vec<cpal::Device> = host
                .input_devices()
                .context("listing the audio inputs")?
                .collect();
            let names: Vec<String> = devices.iter().map(name_of).collect();
            devices
                .into_iter()
                .nth(pick_device(&names, wanted)?)
                .expect("picked among them")
        }
        None => host.default_input_device().ok_or_else(|| {
            anyhow!("no audio input device; connect a microphone, or name one with --device")
        })?,
    };
    let name = name_of(&device);
    let supported = device
        .default_input_config()
        .with_context(|| format!("reading {name}'s input format"))?;
    let config = supported.config();
    let depth = depth_of(supported.sample_format())?;
    let (rate, channels) = (config.sample_rate, config.channels);

    let shared = Arc::new(Shared::default());
    let (to_writer, from_device) = mpsc::sync_channel::<Block>(QUEUE);
    // Buffers handed back for reuse, so the callback rarely allocates. One the device has no
    // room for is dropped.
    let (to_device, spare) = mpsc::sync_channel::<Vec<f32>>(QUEUE);
    let handoff = Handoff::new(to_writer, spare, rate, channels, Arc::clone(&shared));
    // The stream is built before the file exists, so a device that refuses leaves nothing.
    let stream = match supported.sample_format() {
        SampleFormat::F32 => input::<f32>(&device, config, handoff, &shared),
        SampleFormat::I16 => input::<i16>(&device, config, handoff, &shared),
        SampleFormat::U16 => input::<u16>(&device, config, handoff, &shared),
        SampleFormat::I32 => input::<i32>(&device, config, handoff, &shared),
        other => bail!("{name}'s sample format, {other}, is not one duet records"),
    }
    .with_context(|| format!("opening {name} for input"))?;
    let mut writer = TakeWriter::create(&args.output, rate, channels, depth)?;
    if let Err(e) = stream.play() {
        // Nothing was recorded: the empty file is not a take.
        drop(writer);
        let _ = std::fs::remove_file(&args.output);
        return Err(e).with_context(|| format!("starting input from {name}"));
    }
    let until = match args.seconds {
        Some(secs) => format!("for {secs} s"),
        None => "until Ctrl-C".to_owned(),
    };
    eprintln!(
        "recording {} from {name} ({rate} Hz, {channels} channel{}, {}-bit) {until}",
        args.output.display(),
        if channels == 1 { "" } else { "s" },
        depth.bits(),
    );

    let limit = args.seconds.map(|s| (s * f64::from(rate)).round() as u64);
    let mut take = Take::new(limit);
    let kept = |path: &Path| {
        format!(
            "the take up to there is kept in {}, readable up to its last whole frame",
            path.display()
        )
    };
    let ended = match take.record(&mut writer, &from_device, &to_device, &stop, &shared) {
        Ok(ended) => ended,
        Err(e) => {
            drop(stream);
            take.report_losses(rate, &shared);
            return Err(e).context(kept(&args.output));
        }
    };
    drop(stream);
    // What the device delivered before the stream stopped is part of the take.
    let drained = (|| -> Result<()> {
        while let Ok(block) = from_device.try_recv() {
            take.add(&mut writer, block, &to_device)?;
        }
        Ok(())
    })();
    if let Err(e) = drained {
        take.report_losses(rate, &shared);
        return Err(e).context(kept(&args.output));
    }
    let written = match writer.finish() {
        Ok(written) => written,
        Err(e) => {
            take.report_losses(rate, &shared);
            return Err(e).context(kept(&args.output));
        }
    };
    eprintln!(
        "wrote {} ({})",
        written.path.display(),
        at(written.frames, rate)
    );
    take.report_losses(rate, &shared);
    report_level(&written, &take);
    match ended {
        None => Ok(()),
        Some(why) => bail!("recording stopped: {why}; {}", kept(&written.path)),
    }
}

/// The writing thread's view of the take: what it has written, and what was lost.
struct Take {
    /// The frames `--seconds` allows, if it was given.
    limit: Option<u64>,
    /// Whether the limit has been reached: nothing more is written.
    full: bool,
    losses: Vec<Loss>,
    /// The loudest sample, to tell a silent take from a quiet one.
    peak: f32,
}

impl Take {
    fn new(limit: Option<u64>) -> Take {
        Take {
            limit,
            full: false,
            losses: Vec::new(),
            peak: 0.0,
        }
    }

    /// Writes the device's buffers until Ctrl-C, the limit, or the device ends the stream,
    /// which is said as the reason.
    fn record(
        &mut self,
        writer: &mut TakeWriter,
        from_device: &Receiver<Block>,
        spare: &SyncSender<Vec<f32>>,
        stop: &AtomicBool,
        shared: &Shared,
    ) -> Result<Option<String>> {
        loop {
            if self.full || stop.load(Ordering::Acquire) {
                return Ok(None);
            }
            if let Some(e) = shared.failed.lock().ok().and_then(|f| f.clone()) {
                return Ok(Some(e));
            }
            match from_device.recv_timeout(Duration::from_millis(20)) {
                Ok(block) => self.add(writer, block, spare)?,
                Err(RecvTimeoutError::Timeout) => {}
                Err(RecvTimeoutError::Disconnected) => {
                    return Ok(Some("the input stream closed".into()));
                }
            }
        }
    }

    /// Writes one buffer, after silence for any input lost before it, up to the limit. The
    /// buffer is handed back for reuse.
    fn add(
        &mut self,
        writer: &mut TakeWriter,
        block: Block,
        spare: &SyncSender<Vec<f32>>,
    ) -> Result<()> {
        if !self.full {
            let limit = self.limit;
            let room =
                |writer: &TakeWriter| limit.map_or(u64::MAX, |l| l.saturating_sub(writer.frames()));
            let silence = block.lost_before.min(room(writer));
            if silence > 0 {
                self.losses.push(Loss {
                    at: writer.frames(),
                    frames: silence,
                });
                writer.silence(silence)?;
            }
            let channels = usize::from(writer.channels());
            let frames = (block.samples.len() / channels) as u64;
            let keep = frames.min(room(writer)) as usize * channels;
            let samples = &block.samples[..keep];
            self.peak = samples.iter().fold(self.peak, |p, s| p.max(s.abs()));
            writer.write(samples)?;
            self.full = room(writer) == 0;
        }
        let _ = spare.try_send(block.samples);
        Ok(())
    }

    /// Says what was lost and what the device reported, within the take: input lost after the
    /// limit was never part of it.
    fn report_losses(&self, rate: u32, shared: &Shared) {
        for loss in &self.losses {
            eprintln!(
                "lost {} of input at {}, written as silence so what follows keeps its time",
                span(loss.frames, rate),
                at(loss.at, rate)
            );
        }
        let lost_at_end = shared.lost.load(Ordering::Acquire);
        if lost_at_end > 0 && !self.full {
            eprintln!(
                "lost the last {} of input before recording stopped",
                span(lost_at_end, rate)
            );
        }
        let overruns = shared
            .overruns
            .lock()
            .map(|o| o.clone())
            .unwrap_or_default();
        for frame in overruns
            .into_iter()
            .filter(|&f| self.limit.is_none_or(|l| f < l))
        {
            eprintln!(
                "the device reported an overrun at {}; a gap it left in the input is measured \
                 from its timestamps and listed as lost",
                at(frame, rate)
            );
        }
    }
}

/// Says what the take's level tells the owner: samples at full scale, or silence throughout.
fn report_level(written: &Written, take: &Take) {
    if written.full_scale > 0 {
        eprintln!(
            "{} samples reached full scale, so the take may be clipped; turn the input down or \
             sing further from the microphone",
            written.full_scale
        );
    }
    if written.frames > 0 && take.peak == 0.0 {
        eprintln!(
            "the take is silent throughout; on macOS, the terminal app needs microphone access \
             (System Settings, Privacy & Security, Microphone)"
        );
    }
}

/// Where the device's next buffer should start, from the capture time it stamps each one with.
struct Clock {
    rate: u32,
    /// The capture instant, in nanoseconds of the stream's clock, the next buffer should have.
    next: Option<u128>,
}

impl Clock {
    /// Frames the device left out before a buffer captured at `capture` that holds `frames`:
    /// how far it starts after where the last one ended. Under half a buffer is the timestamps'
    /// jitter, not a gap.
    fn gap(&mut self, capture: u128, frames: u64) -> u64 {
        let per_frame = 1e9 / f64::from(self.rate);
        let gap = match self.next {
            Some(expected) if capture > expected => {
                ((capture - expected) as f64 / per_frame).round() as u64
            }
            _ => 0,
        };
        self.next = Some(capture + (frames as f64 * per_frame).round() as u128);
        if gap * 2 > frames { gap } else { 0 }
    }
}

/// What the device's data callback does with each buffer, apart from the device: converts it
/// into a reused buffer, counts any gap before it, and hands it to the writer without waiting.
struct Handoff {
    to_writer: SyncSender<Block>,
    spare: Receiver<Vec<f32>>,
    /// A buffer the channel refused, kept to reuse: never freed on the audio thread.
    refused: Option<Vec<f32>>,
    clock: Clock,
    channels: u64,
    shared: Arc<Shared>,
}

impl Handoff {
    fn new(
        to_writer: SyncSender<Block>,
        spare: Receiver<Vec<f32>>,
        rate: u32,
        channels: u16,
        shared: Arc<Shared>,
    ) -> Handoff {
        Handoff {
            to_writer,
            spare,
            refused: None,
            clock: Clock { rate, next: None },
            channels: u64::from(channels.max(1)),
            shared,
        }
    }

    /// Hands over one buffer of interleaved `input`, captured at `capture` nanoseconds.
    fn hand_off(&mut self, input: impl ExactSizeIterator<Item = f32>, capture: u128) {
        let frames = input.len() as u64 / self.channels;
        let gap = self.clock.gap(capture, frames);
        self.shared
            .delivered
            .fetch_add(gap + frames, Ordering::AcqRel);
        let mut samples = self
            .refused
            .take()
            .or_else(|| self.spare.try_recv().ok())
            .unwrap_or_default();
        samples.clear();
        samples.extend(input);
        let lost_before = self.shared.lost.swap(0, Ordering::AcqRel) + gap;
        match self.to_writer.try_send(Block {
            lost_before,
            samples,
        }) {
            Ok(()) => {}
            Err(TrySendError::Full(block) | TrySendError::Disconnected(block)) => {
                self.shared
                    .lost
                    .fetch_add(lost_before + frames, Ordering::AcqRel);
                self.refused = Some(block.samples);
            }
        }
    }
}

/// The input stream, each buffer passed through `handoff`. An overrun is noted where it
/// happened; a change of device or any other error ends the take.
fn input<T: SizedSample>(
    device: &cpal::Device,
    config: cpal::StreamConfig,
    mut handoff: Handoff,
    shared: &Arc<Shared>,
) -> Result<cpal::Stream>
where
    f32: cpal::FromSample<T>,
{
    let errors = Arc::clone(shared);
    let mut warned = false;
    device
        .build_input_stream(
            config,
            move |input: &[T], info: &cpal::InputCallbackInfo| {
                let capture = info.timestamp().capture.as_nanos();
                handoff.hand_off(
                    input.iter().map(|&s| cpal::Sample::to_sample::<f32>(s)),
                    capture,
                );
            },
            move |e| match e.kind() {
                cpal::ErrorKind::Xrun => {
                    let at = errors.delivered.load(Ordering::Acquire);
                    if let Ok(mut overruns) = errors.overruns.lock() {
                        overruns.push(at);
                    }
                }
                cpal::ErrorKind::RealtimeDenied => {
                    if !warned {
                        eprintln!("duet: real-time priority refused, still recording: {e}");
                        warned = true;
                    }
                }
                // A rerouted input may change rate or channels mid-file: the take ends there.
                cpal::ErrorKind::DeviceChanged => {
                    if let Ok(mut f) = errors.failed.lock() {
                        *f = Some("the input device changed".to_owned());
                    }
                }
                _ => {
                    if let Ok(mut f) = errors.failed.lock() {
                        *f = Some(e.to_string());
                    }
                }
            },
            None,
        )
        .map_err(Into::into)
}

/// Refuses an output that exists, is not named `.flac`, or is in a directory that does not
/// exist, before any device is opened.
fn refuse_output(output: &Path) -> Result<()> {
    let flac = output
        .extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| e.eq_ignore_ascii_case("flac"));
    if !flac {
        bail!(
            "duet record writes FLAC; name the take with .flac, such as {}",
            output.with_extension("flac").display()
        );
    }
    if output.symlink_metadata().is_ok() {
        bail!(
            "{} exists; a take never replaces a file, so move it or name another",
            output.display()
        );
    }
    if let Some(dir) = output.parent().filter(|d| !d.as_os_str().is_empty())
        && !dir.is_dir()
    {
        bail!(
            "{} is not a directory, so the take cannot be written there",
            dir.display()
        );
    }
    Ok(())
}

/// The depth a device's samples are kept at: all of a 16-bit sample, or 24 bits of a wider
/// or float one.
fn depth_of(format: SampleFormat) -> Result<Depth> {
    Ok(match format {
        SampleFormat::I16 | SampleFormat::U16 => Depth::Sixteen,
        SampleFormat::F32 | SampleFormat::I32 => Depth::TwentyFour,
        other => bail!("the input's sample format, {other}, is not one duet records"),
    })
}

fn name_of(device: &cpal::Device) -> String {
    device
        .description()
        .map(|d| d.name().to_owned())
        .unwrap_or_else(|_| "an unnamed input".to_owned())
}

/// The input `wanted` names among `names`: the one with exactly that name, or else the one
/// whose name holds it, ignoring case. Refused when none or several do, or when it is empty,
/// so an input is never picked by accident.
fn pick_device(names: &[String], wanted: &str) -> Result<usize> {
    let list = |which: &mut dyn Iterator<Item = &String>| -> String {
        let lines: Vec<String> = which.map(|n| format!("  {n}")).collect();
        if lines.is_empty() {
            "  (none)".to_owned()
        } else {
            lines.join("\n")
        }
    };
    if wanted.trim().is_empty() {
        bail!(
            "--device needs an input's name; the inputs are:\n{}",
            list(&mut names.iter())
        );
    }
    let exact: Vec<usize> = (0..names.len()).filter(|&i| names[i] == wanted).collect();
    let lower = wanted.to_lowercase();
    let found = if exact.is_empty() {
        (0..names.len())
            .filter(|&i| names[i].to_lowercase().contains(&lower))
            .collect()
    } else {
        exact
    };
    match found.as_slice() {
        [one] => Ok(*one),
        [] => bail!(
            "no audio input is named {wanted:?}; the inputs are:\n{}",
            list(&mut names.iter())
        ),
        _ if found.iter().all(|&i| names[i] == wanted) => bail!(
            "{} audio inputs are named {wanted:?}, and duet cannot tell them apart",
            found.len()
        ),
        _ => bail!(
            "{wanted:?} is part of more than one input's name; name one:\n{}",
            list(&mut found.iter().map(|&i| &names[i]))
        ),
    }
}

/// A position in the take, as minutes and seconds to a tenth.
fn at(frames: u64, rate: u32) -> String {
    let tenths = (frames as f64 / f64::from(rate) * 10.0).round() as u64;
    format!("{}:{:02}.{}", tenths / 600, tenths / 10 % 60, tenths % 10)
}

/// A length of input, in milliseconds: under one is said as such, never as nothing.
fn span(frames: u64, rate: u32) -> String {
    let ms = frames as f64 * 1000.0 / f64::from(rate);
    if frames > 0 && ms < 1.0 {
        "under 1 ms".to_owned()
    } else {
        format!("{ms:.0} ms")
    }
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};
    use std::sync::Arc;
    use std::sync::atomic::Ordering;
    use std::sync::mpsc;

    use duet_capture::{Depth, FileSource, Source, TakeWriter};

    use super::{Block, Clock, Handoff, Loss, Shared, Take, at, pick_device, refuse_output, span};

    fn names(list: &[&str]) -> Vec<String> {
        list.iter().map(|&n| n.to_owned()).collect()
    }

    /// A new scratch path, for a take written in a test.
    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("duet-record-{}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("temp dir");
        let path = dir.join(name);
        let _ = std::fs::remove_file(&path);
        path
    }

    fn read_back(path: &Path) -> Vec<f32> {
        let mut source = FileSource::open(path).expect("open");
        let mut samples = Vec::new();
        while let Some(chunk) = source.next_chunk().expect("read") {
            samples.extend(chunk.samples);
        }
        samples
    }

    /// 10 ms at 48 kHz, in nanoseconds.
    const TEN_MS: u128 = 10_000_000;

    #[test]
    fn the_clock_measures_a_gap_the_device_left_and_ignores_jitter() {
        let mut clock = Clock {
            rate: 48_000,
            next: None,
        };
        // 480 frames is 10 ms: buffers on time, then one 0.2 ms late (jitter), then one that
        // starts 20 ms after the last ended (two buffers dropped).
        assert_eq!(clock.gap(0, 480), 0);
        assert_eq!(clock.gap(TEN_MS, 480), 0);
        assert_eq!(clock.gap(2 * TEN_MS + 200_000, 480), 0);
        assert_eq!(clock.gap(5 * TEN_MS + 200_000, 480), 960);
        // Early is never a gap.
        assert_eq!(clock.gap(5 * TEN_MS, 480), 0);
    }

    #[test]
    fn a_refused_buffer_is_counted_and_carried_to_the_next_one_taken() {
        let shared = Arc::new(Shared::default());
        let (to_writer, from_device) = mpsc::sync_channel::<Block>(1);
        let (_to_device, spare) = mpsc::sync_channel::<Vec<f32>>(1);
        let mut handoff = Handoff::new(to_writer, spare, 48_000, 2, Arc::clone(&shared));
        let buffer = [0.5f32; 960]; // 480 stereo frames, 10 ms
        handoff.hand_off(buffer.iter().copied(), 0);
        // The channel is full: this one is lost, and its buffer kept for reuse.
        handoff.hand_off(buffer.iter().copied(), TEN_MS);
        assert_eq!(shared.lost.load(Ordering::Acquire), 480);
        assert!(handoff.refused.is_some());
        let first = from_device.try_recv().expect("the first buffer");
        assert_eq!((first.lost_before, first.samples.len()), (0, 960));
        // The next one taken says what was lost before it; the device also left 10 ms out.
        handoff.hand_off(buffer.iter().copied(), 3 * TEN_MS);
        let third = from_device.try_recv().expect("the third buffer");
        assert_eq!(third.lost_before, 480 + 480);
        assert_eq!(shared.lost.load(Ordering::Acquire), 0);
        assert!(handoff.refused.is_none(), "the kept buffer was reused");
        assert_eq!(shared.delivered.load(Ordering::Acquire), 4 * 480);
    }

    #[test]
    fn lost_input_is_written_as_silence_where_it_was() {
        let path = scratch("lost.flac");
        let mut writer = TakeWriter::create(&path, 48_000, 1, Depth::Sixteen).expect("create");
        let (spare, _) = mpsc::sync_channel::<Vec<f32>>(4);
        let mut take = Take::new(None);
        let block = |lost_before, value: f32| Block {
            lost_before,
            samples: vec![value; 4],
        };
        take.add(&mut writer, block(0, 0.5), &spare).expect("add");
        take.add(&mut writer, block(3, 0.25), &spare).expect("add");
        assert_eq!(take.losses, [Loss { at: 4, frames: 3 }]);
        writer.finish().expect("finish");
        let mut expected = vec![0.5; 4];
        expected.extend([0.0; 3]);
        expected.extend([0.25; 4]);
        assert_eq!(read_back(&path), expected);
    }

    /// `--seconds` ends the take at its frame, silence and losses included: input lost past
    /// the limit was never part of the take, and is not reported as written.
    #[test]
    fn the_limit_cuts_the_take_and_its_losses() {
        let path = scratch("limit.flac");
        let mut writer = TakeWriter::create(&path, 48_000, 1, Depth::Sixteen).expect("create");
        let (spare, _) = mpsc::sync_channel::<Vec<f32>>(4);
        let mut take = Take::new(Some(10));
        let block = |lost_before| Block {
            lost_before,
            samples: vec![0.5; 4],
        };
        take.add(&mut writer, block(0), &spare).expect("add");
        // 4 written; 3 lost then 4 more fill 7 of the 6 left: cut at 10.
        take.add(&mut writer, block(3), &spare).expect("add");
        assert!(take.full);
        // Past the limit: nothing is written or reported.
        take.add(&mut writer, block(5), &spare).expect("add");
        assert_eq!(take.losses, [Loss { at: 4, frames: 3 }]);
        assert_eq!(writer.frames(), 10);
        let written = writer.finish().expect("finish");
        assert_eq!(written.frames, 10);

        // A loss larger than the room left is reported as the silence written, not the loss.
        let path = scratch("limit-loss.flac");
        let mut writer = TakeWriter::create(&path, 48_000, 1, Depth::Sixteen).expect("create");
        let mut take = Take::new(Some(10));
        take.add(&mut writer, block(0), &spare).expect("add");
        take.add(&mut writer, block(8), &spare).expect("add");
        assert!(take.full);
        assert_eq!(take.losses, [Loss { at: 4, frames: 6 }]);
        assert_eq!(writer.finish().expect("finish").frames, 10);
    }

    #[test]
    fn a_device_is_picked_by_its_name_or_a_part_only_it_holds() {
        let inputs = names(&["MacBook Pro Microphone", "Scarlett 2i2 USB", "USB Mic"]);
        assert_eq!(pick_device(&inputs, "Scarlett 2i2 USB").unwrap(), 1);
        assert_eq!(pick_device(&inputs, "scarlett").unwrap(), 1);
        assert_eq!(pick_device(&inputs, "USB Mic").unwrap(), 2);
        let several = pick_device(&inputs, "usb").unwrap_err().to_string();
        assert!(several.contains("more than one"), "{several}");
        let none = pick_device(&inputs, "Yeti").unwrap_err().to_string();
        assert!(none.contains("MacBook Pro Microphone"), "{none}");
        assert!(pick_device(&inputs, " ").is_err());
    }

    #[test]
    fn positions_and_lengths_read_as_time() {
        assert_eq!(at(0, 48_000), "0:00.0");
        assert_eq!(at(48_000 * 75 + 4_800, 48_000), "1:15.1");
        assert_eq!(span(480, 48_000), "10 ms");
        assert_eq!(span(10, 48_000), "under 1 ms");
        assert_eq!(span(0, 48_000), "0 ms");
    }

    #[test]
    fn only_a_new_flac_path_in_a_real_directory_is_recorded_to() {
        assert!(refuse_output(Path::new("take.wav")).is_err());
        assert!(refuse_output(Path::new("take")).is_err());
        let missing = refuse_output(Path::new("/nonexistent-dir/new-take.flac"))
            .unwrap_err()
            .to_string();
        assert!(missing.contains("not a directory"), "{missing}");
        assert!(refuse_output(Path::new("new-take.FLAC")).is_ok());
        assert!(refuse_output(&std::env::temp_dir().join("duet-never-made.flac")).is_ok());
    }
}