biodream 0.2.7

Zero-copy, streaming-capable toolkit for reading and writing BIOPAC AcqKnowledge (.acq) files
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
//! Write support for `.acq` files (requires `write` feature).
//!
//! # Supported formats
//!
//! - **Pre-4 uncompressed** (default, revision 43) — widest compatibility;
//!   all physiological analysis tools can read it.
//! - **Post-4 compressed** (revision 68) — per-channel zlib compression;
//!   typically halves file size for large recordings.
//!
//! # Example
//!
//! ```rust,ignore
//! use biodream::{WriteOptions, write_file};
//!
//! // Write with defaults (uncompressed, Pre-4, little-endian)
//! write_file(&datafile, "output.acq")?;
//!
//! // Write compressed
//! WriteOptions::new().compressed(true).write_file(&datafile, "output.acq")?;
//! ```

use alloc::{format, string::ToString, vec, vec::Vec};
use std::io::Write;
use std::path::Path;

use crate::{
    domain::{ByteOrder, Channel, ChannelData, Datafile, Journal, Marker, MarkerStyle, Timestamp},
    error::BiopacError,
    parser::interleaved::compute_sample_pattern,
};

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Pre-4 format revision used by default (3.7.3.x era).
const DEFAULT_REVISION: i32 = 43;

/// Post-4 revision used for compressed output (`AcqKnowledge` 4.0 era).
const COMPRESSED_REVISION: i32 = 68;

/// Minimum revision with per-marker creation timestamps.
const REVISION_TIMESTAMP: i32 = 77;

/// Byte length of the fixed Pre-4 graph header.
const PRE4_GRAPH_HDR_LEN: usize = 256;

/// Byte length of the channel header written by this library.
///
/// 252 bytes: matches the real BIOPAC Pre-4/Post-4 channel header layout
/// (`V_20a` base, 112 bytes of required fields + 140 bytes of padding/optional fields).
const CHAN_HDR_LEN: usize = 252;

/// First Pre-4 revision that stores `nVarSampleDivider` at offset 250 of the channel header.
const REVISION_V30R: i32 = 44;

/// First revision using the Post-4 format (`AcqKnowledge` 4.0+).
const REVISION_POST4: i32 = 68;

/// Byte length of the Post-4 graph header written for compressed output.
///
/// 1944 bytes: covers the `bCompressed` flag (offset 1936) and the optional
/// max-rate field (offset 1940).
const POST4_GRAPH_HDR_LEN: usize = 1944;

/// Byte offset of the `bCompressed` flag within a Post-4 graph header.
const POST4_COMPRESSED_FLAG_OFFSET: usize = 1936;

/// Byte length of the marker section header (lLength + lNumMarkers).
const MARKER_HDR_BYTES: i32 = 8;

/// Fixed per-marker field bytes (lSample + nChannel + szStyle + lMarkerTextLen).
const MARKER_FIXED_BYTES: i32 = 14;

/// Extra bytes per marker when revision >= `REVISION_TIMESTAMP` (i64 timestamp).
const MARKER_TIMESTAMP_BYTES: i32 = 8;

// ---------------------------------------------------------------------------
// Public API: WriteOptions
// ---------------------------------------------------------------------------

/// Options controlling how a [`Datafile`] is serialised to `.acq` format.
///
/// Use the builder methods to override defaults, then call
/// [`WriteOptions::write_file`] or [`WriteOptions::write_stream`].
///
/// # Defaults
///
/// | Field | Default | Notes |
/// |-------|---------|-------|
/// | `revision` | 43 | Pre-4 (3.7.3.x), uncompressed |
/// | `compressed` | `false` | Uncompressed |
/// | `byte_order` | `LittleEndian` | LE is universal |
#[derive(Debug, Clone)]
pub struct WriteOptions {
    /// Format revision written into the graph header.
    ///
    /// Revision 43 = Pre-4 (uncompressed). When `compressed = true` the
    /// revision is overridden to 68 (Post-4) automatically.
    pub revision: i32,
    /// Compress channel data with zlib.
    ///
    /// Enabling compression selects the Post-4 file layout (revision ≥ 68)
    /// and applies per-channel zlib compression — typically halving file size.
    pub compressed: bool,
    /// Byte order used for all multi-byte integers and floats.
    pub byte_order: ByteOrder,
}

impl WriteOptions {
    /// Create default options (uncompressed Pre-4, revision 43, LE).
    pub const fn new() -> Self {
        Self {
            revision: DEFAULT_REVISION,
            compressed: false,
            byte_order: ByteOrder::LittleEndian,
        }
    }

    /// Set `compressed = true`; selects Post-4 format and zlib compression.
    #[must_use]
    pub const fn compressed(mut self, compressed: bool) -> Self {
        self.compressed = compressed;
        self
    }

    /// Override the format revision (default 43 for uncompressed, 68 for compressed).
    #[must_use]
    pub const fn revision(mut self, revision: i32) -> Self {
        self.revision = revision;
        self
    }

    /// Override the byte order (default little-endian).
    #[must_use]
    pub const fn byte_order(mut self, byte_order: ByteOrder) -> Self {
        self.byte_order = byte_order;
        self
    }

    /// Write `df` to a file at `path`, creating or truncating it.
    pub fn write_file(&self, df: &Datafile, path: impl AsRef<Path>) -> Result<(), BiopacError> {
        let file = std::fs::File::create(path).map_err(BiopacError::Io)?;
        let mut w = std::io::BufWriter::new(file);
        self.write_stream(df, &mut w)
    }

    /// Write `df` to any [`Write`] sink.
    pub fn write_stream<W: Write>(&self, df: &Datafile, w: &mut W) -> Result<(), BiopacError> {
        if self.compressed {
            write_compressed(df, w, self)
        } else {
            write_uncompressed(df, w, self)
        }
    }
}

impl Default for WriteOptions {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// Public convenience functions
// ---------------------------------------------------------------------------

/// Write `df` to a file at `path` using default options (uncompressed, Pre-4).
///
/// Both compressed and uncompressed files can be read back with
/// [`crate::read_file`].
pub fn write_file(df: &Datafile, path: impl AsRef<Path>) -> Result<(), BiopacError> {
    WriteOptions::default().write_file(df, path)
}

/// Write `df` to any [`Write`] sink using default options.
pub fn write_stream<W: Write>(df: &Datafile, w: &mut W) -> Result<(), BiopacError> {
    WriteOptions::default().write_stream(df, w)
}

// ---------------------------------------------------------------------------
// Uncompressed writer
// ---------------------------------------------------------------------------

fn write_uncompressed<W: Write>(
    df: &Datafile,
    w: &mut W,
    opts: &WriteOptions,
) -> Result<(), BiopacError> {
    let le = opts.byte_order == ByteOrder::LittleEndian;
    let n_ch = df.channels.len();

    // 1. Graph header (256 bytes, Pre-4).
    write_pre4_graph_header(w, opts.revision, n_ch, df.metadata.samples_per_second, le)?;

    // 2. Per-channel headers.
    for ch in &df.channels {
        write_channel_header(w, ch, le, opts.revision)?;
    }

    // 3. Foreign data section (empty).
    write_foreign_data_section(w, le)?;

    // 4. Dtype headers.
    for ch in &df.channels {
        write_dtype_header(w, &ch.data, le)?;
    }

    // 5. Interleaved channel data.
    write_interleaved_data(w, df, le)?;

    // 6. Marker section.
    write_marker_section(w, &df.markers, opts.revision, le)?;

    // 7. Journal.
    write_journal_section(w, df.journal.as_ref(), le)?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Compressed writer (Post-4, revision 68)
// ---------------------------------------------------------------------------

fn write_compressed<W: Write>(
    df: &Datafile,
    w: &mut W,
    _opts: &WriteOptions,
) -> Result<(), BiopacError> {
    // Compressed output is always little-endian.
    let le = true;
    let n_ch = df.channels.len();

    // 1. Post-4 graph header (1944 bytes) with bCompressed = 1.
    write_post4_graph_header(w, COMPRESSED_REVISION, n_ch, df.metadata.samples_per_second)?;

    // 2. Per-channel headers.
    for ch in &df.channels {
        write_channel_header(w, ch, le, COMPRESSED_REVISION)?;
    }

    // 3. Foreign data section (empty).
    write_foreign_data_section(w, le)?;

    // 4. Dtype headers.
    for ch in &df.channels {
        write_dtype_header(w, &ch.data, le)?;
    }

    // 5. Compressed layout: markers and journal come BEFORE channel data.
    write_marker_section(w, &df.markers, COMPRESSED_REVISION, le)?;
    write_journal_section(w, df.journal.as_ref(), le)?;

    // 6. Per-channel compressed blobs.
    for ch in &df.channels {
        write_compressed_channel(w, ch)?;
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Graph headers
// ---------------------------------------------------------------------------

/// Write a 256-byte Pre-4 graph header.
fn write_pre4_graph_header<W: Write>(
    w: &mut W,
    revision: i32,
    n_channels: usize,
    samples_per_second: f64,
    le: bool,
) -> Result<(), BiopacError> {
    let mut buf = [0u8; PRE4_GRAPH_HDR_LEN];

    let n_ch = i16::try_from(n_channels).map_err(|_| {
        BiopacError::Validation(format!("too many channels to write as Pre-4: {n_channels}"))
    })?;
    let sample_time_ms = 1000.0_f64 / samples_per_second;
    let chan_hdr_len = i16::try_from(CHAN_HDR_LEN).unwrap_or(i16::MAX);

    put_i32(&mut buf, 2, revision, le); // lVersion (offset 2)
    put_i32(
        &mut buf,
        6,
        i32::try_from(PRE4_GRAPH_HDR_LEN).unwrap_or(i32::MAX),
        le,
    ); // lExtItemHeaderLen (offset 6, = 256)
    put_i16(&mut buf, 10, n_ch, le); // nChannels (offset 10)
    // offsets 12-15: nHorizAxisType, nCurrChannel = 0 (already zero)
    put_f64(&mut buf, 16, sample_time_ms, le); // dSampleTime (offset 16)
    // offsets 24-251: zeros (already zero)
    put_i16(&mut buf, 252, chan_hdr_len, le);
    // offset 254–255: zeros

    w.write_all(&buf).map_err(BiopacError::Io)
}

/// Write a 1944-byte Post-4 graph header with `bCompressed = 1`.
fn write_post4_graph_header<W: Write>(
    w: &mut W,
    revision: i32,
    n_channels: usize,
    samples_per_second: f64,
) -> Result<(), BiopacError> {
    let mut buf = [0u8; POST4_GRAPH_HDR_LEN];

    let n_ch = i16::try_from(n_channels).map_err(|_| {
        BiopacError::Validation(format!(
            "too many channels to write as Post-4: {n_channels}"
        ))
    })?;
    let graph_hdr_len = i32::try_from(POST4_GRAPH_HDR_LEN).unwrap_or(i32::MAX);
    let sample_time_ms = 1000.0_f64 / samples_per_second;

    // Always little-endian for compressed files.
    put_i32(&mut buf, 2, revision, true); // lVersion (offset 2)
    put_i32(&mut buf, 6, graph_hdr_len, true); // lExtItemHeaderLen (offset 6)
    put_i16(&mut buf, 10, n_ch, true); // nChannels (offset 10)
    // offsets 12-15: nHorizAxisType, nCurrChannel = 0 (already zero)
    put_f64(&mut buf, 16, sample_time_ms, true); // dSampleTime (offset 16)
    // offsets 20–1935: zeros
    if let Some(flag) = buf.get_mut(POST4_COMPRESSED_FLAG_OFFSET) {
        *flag = 1; // bCompressed = 1
    }
    // offsets 1937–1943: zeros (max_rate = 0)

    w.write_all(&buf).map_err(BiopacError::Io)
}

// ---------------------------------------------------------------------------
// Channel header (252 bytes, V_20a base layout)
// ---------------------------------------------------------------------------

/// Write a 252-byte channel header using the BIOPAC `V_20a` base layout.
///
/// Field offsets (byte positions within the 252-byte header):
/// - 0:   `lChanHeaderLen` (i32) = 252
/// - 4:   `nNum` (i16) = 0
/// - 6:   `szCommentText` (40 bytes, null-padded ASCII channel name)
/// - 46:  `notColor` (4 bytes) = 0
/// - 50:  `nDispChan` (i16) = 0
/// - 52:  `dVoltOffset` (f64) = 0.0
/// - 60:  `dVoltScale` (f64) = 0.0
/// - 68:  `szUnitsText` (20 bytes, null-padded ASCII units)
/// - 88:  `lBufLength` (i32) = sample count
/// - 92:  `dAmplScale` (f64) = amplitude scale factor
/// - 100: `dAmplOffset` (f64) = amplitude offset
/// - 108: `nChanOrder` (i16) = 0
/// - 110: `nDispSize` (i16) = 0
/// - 152: `nVarSampleDivider` (i16, Post-4 only)
/// - 250: `nVarSampleDivider` (i16, Pre-4 `V_30r+` only, revision ≥ 44)
fn write_channel_header<W: Write>(
    w: &mut W,
    ch: &Channel,
    le: bool,
    revision: i32,
) -> Result<(), BiopacError> {
    let mut buf = [0u8; CHAN_HDR_LEN];

    let chan_hdr_len = i32::try_from(CHAN_HDR_LEN).unwrap_or(i32::MAX);
    let sample_count = i32::try_from(ch.data.len()).map_err(|_| {
        BiopacError::Validation(format!("channel '{}' has too many samples", ch.name))
    })?;
    let (scale, offset_val) = channel_calibration(ch);
    let var_sample_divider = i16::try_from(ch.frequency_divider).unwrap_or(i16::MAX);

    // offset 0: lChanHeaderLen = 252
    put_i32(&mut buf, 0, chan_hdr_len, le);
    // offset 4: nNum = 0 (already zero)
    // offset 6: szCommentText (channel name, 40 bytes null-padded)
    let name_bytes = ch.name.as_bytes();
    let name_len = name_bytes.len().min(39);
    if let (Some(dst), Some(src)) = (buf.get_mut(6..6 + name_len), name_bytes.get(..name_len)) {
        dst.copy_from_slice(src);
    }
    // offset 46-51: notColor (4 bytes) + nDispChan (2 bytes) = 0 (already zero)
    // offset 52: dVoltOffset = 0.0 (already zero)
    // offset 60: dVoltScale = 0.0 (already zero)
    // offset 68: szUnitsText (units label, 20 bytes null-padded)
    let units_bytes = ch.units.as_bytes();
    let units_len = units_bytes.len().min(19);
    if let (Some(dst), Some(src)) = (
        buf.get_mut(68..68 + units_len),
        units_bytes.get(..units_len),
    ) {
        dst.copy_from_slice(src);
    }
    // offset 88: lBufLength = sample count
    put_i32(&mut buf, 88, sample_count, le);
    // offset 92: dAmplScale
    put_f64(&mut buf, 92, scale, le);
    // offset 100: dAmplOffset
    put_f64(&mut buf, 100, offset_val, le);
    // offset 108-111: nChanOrder + nDispSize = 0 (already zero)
    // nVarSampleDivider at version-dependent offset:
    if revision >= REVISION_POST4 {
        // Post-4 (V_400B+): offset 152
        put_i16(&mut buf, 152, var_sample_divider, le);
    } else if revision >= REVISION_V30R {
        // Pre-4 V_30r+ (revision ≥ 44): offset 250
        put_i16(&mut buf, 250, var_sample_divider, le);
    }
    // Pre-4 revision < 44: no nVarSampleDivider; reader defaults to 1.

    w.write_all(&buf).map_err(BiopacError::Io)
}

const fn channel_calibration(ch: &Channel) -> (f64, f64) {
    match &ch.data {
        ChannelData::Scaled { scale, offset, .. } => (*scale, *offset),
        _ => (1.0, 0.0),
    }
}

// ---------------------------------------------------------------------------
// Foreign data section
// ---------------------------------------------------------------------------

/// Write an empty foreign data section (nLength = 0; 4 bytes total).
fn write_foreign_data_section<W: Write>(w: &mut W, le: bool) -> Result<(), BiopacError> {
    let bytes = if le {
        0i32.to_le_bytes()
    } else {
        0i32.to_be_bytes()
    };
    w.write_all(&bytes).map_err(BiopacError::Io)
}

// ---------------------------------------------------------------------------
// Dtype header (4 bytes per channel)
// ---------------------------------------------------------------------------

fn write_dtype_header<W: Write>(
    w: &mut W,
    data: &ChannelData,
    le: bool,
) -> Result<(), BiopacError> {
    // n_size field in the file (always 4, the size of the dtype record itself).
    let n_size: u16 = 4;
    // n_type: 1 = f64 samples, 2 = i16 samples.
    let n_type: u16 = match data {
        ChannelData::Float(_) => 1,
        ChannelData::Raw(_) | ChannelData::Scaled { .. } => 2,
    };

    let mut buf = [0u8; 4];
    put_u16(&mut buf, 0, n_size, le);
    put_u16(&mut buf, 2, n_type, le);
    w.write_all(&buf).map_err(BiopacError::Io)
}

// ---------------------------------------------------------------------------
// Interleaved channel data
// ---------------------------------------------------------------------------

fn write_interleaved_data<W: Write>(w: &mut W, df: &Datafile, le: bool) -> Result<(), BiopacError> {
    let dividers: Vec<u16> = df.channels.iter().map(|ch| ch.frequency_divider).collect();
    let pattern = compute_sample_pattern(&dividers);

    if pattern.is_empty() {
        return Ok(());
    }

    let n_ch = df.channels.len();
    let mut indices = vec![0usize; n_ch];
    let totals: Vec<usize> = df.channels.iter().map(|ch| ch.data.len()).collect();

    loop {
        let mut any_written = false;

        for &ch_idx in &pattern {
            let Some(&cur) = indices.get(ch_idx) else {
                continue;
            };
            let Some(&total) = totals.get(ch_idx) else {
                continue;
            };

            if cur < total {
                let Some(ch) = df.channels.get(ch_idx) else {
                    continue;
                };
                write_one_sample(w, &ch.data, cur, le)?;
                if let Some(idx) = indices.get_mut(ch_idx) {
                    *idx += 1;
                }
                any_written = true;
            }
        }

        if !any_written || indices.iter().zip(totals.iter()).all(|(i, t)| i >= t) {
            break;
        }
    }

    Ok(())
}

fn write_one_sample<W: Write>(
    w: &mut W,
    data: &ChannelData,
    idx: usize,
    le: bool,
) -> Result<(), BiopacError> {
    match data {
        ChannelData::Raw(v) | ChannelData::Scaled { raw: v, .. } => {
            let sample = v.get(idx).copied().unwrap_or(0);
            let bytes = if le {
                sample.to_le_bytes()
            } else {
                sample.to_be_bytes()
            };
            w.write_all(&bytes).map_err(BiopacError::Io)
        }
        ChannelData::Float(v) => {
            let sample = v.get(idx).copied().unwrap_or(0.0);
            let bytes = if le {
                sample.to_le_bytes()
            } else {
                sample.to_be_bytes()
            };
            w.write_all(&bytes).map_err(BiopacError::Io)
        }
    }
}

// ---------------------------------------------------------------------------
// Marker section
// ---------------------------------------------------------------------------

fn write_marker_section<W: Write>(
    w: &mut W,
    markers: &[Marker],
    revision: i32,
    le: bool,
) -> Result<(), BiopacError> {
    let has_timestamp = revision >= REVISION_TIMESTAMP;

    // Compute the total section byte count (including the 8-byte header).
    let total_bytes = compute_marker_section_bytes(markers, has_timestamp)?;

    let num_markers = i32::try_from(markers.len())
        .map_err(|_| BiopacError::Validation("too many markers to write".to_string()))?;

    write_i32(w, total_bytes, le)?; // lLength (total section size)
    write_i32(w, num_markers, le)?; // lNumMarkers

    for m in markers {
        write_marker_record(w, m, has_timestamp, le)?;
    }

    Ok(())
}

/// Compute total section bytes for the marker section header `lLength` field.
///
/// Includes the 8-byte section header itself.
fn compute_marker_section_bytes(
    markers: &[Marker],
    has_timestamp: bool,
) -> Result<i32, BiopacError> {
    let mut total: i32 = MARKER_HDR_BYTES;

    for m in markers {
        let text_len = i32::try_from(m.label.len()).map_err(|_| {
            BiopacError::Validation(format!(
                "marker label '{}' is too long to serialise",
                m.label
            ))
        })?;

        let fixed = MARKER_FIXED_BYTES;
        let ts = if has_timestamp {
            MARKER_TIMESTAMP_BYTES
        } else {
            0
        };

        total = total
            .checked_add(fixed)
            .and_then(|v| v.checked_add(text_len))
            .and_then(|v| v.checked_add(ts))
            .ok_or_else(|| BiopacError::Validation("marker section too large".to_string()))?;
    }

    Ok(total)
}

fn write_marker_record<W: Write>(
    w: &mut W,
    m: &Marker,
    has_timestamp: bool,
    le: bool,
) -> Result<(), BiopacError> {
    let sample = i32::try_from(m.global_sample_index).map_err(|_| {
        BiopacError::Validation(format!(
            "marker sample index {} overflows i32",
            m.global_sample_index
        ))
    })?;

    let n_channel: i16 = match m.channel {
        None => -1i16,
        Some(ch) => i16::try_from(ch).map_err(|_| {
            BiopacError::Validation(format!("marker channel index {ch} overflows i16"))
        })?,
    };

    let text_bytes = m.label.as_bytes();
    let text_len = i32::try_from(text_bytes.len())
        .map_err(|_| BiopacError::Validation("marker label too long".to_string()))?;

    write_i32(w, sample, le)?;
    write_i16(w, n_channel, le)?;
    w.write_all(&marker_style_code(&m.style))
        .map_err(BiopacError::Io)?;
    write_i32(w, text_len, le)?;
    w.write_all(text_bytes).map_err(BiopacError::Io)?;

    if has_timestamp {
        let ts = m.created_at.map_or(0i64, Timestamp::as_secs);
        write_i64(w, ts, le)?;
    }

    Ok(())
}

fn marker_style_code(style: &MarkerStyle) -> [u8; 4] {
    match style {
        MarkerStyle::Append => *b"apnd",
        MarkerStyle::UserEvent => *b"usr1",
        MarkerStyle::Waveform => *b"wave",
        MarkerStyle::GlobalEvent => *b"glbl",
        MarkerStyle::Unknown(s) => {
            let mut code = [0u8; 4];
            for (i, b) in s.bytes().take(4).enumerate() {
                if let Some(c) = code.get_mut(i) {
                    *c = b;
                }
            }
            code
        }
    }
}

// ---------------------------------------------------------------------------
// Journal section
// ---------------------------------------------------------------------------

fn write_journal_section<W: Write>(
    w: &mut W,
    journal: Option<&Journal>,
    le: bool,
) -> Result<(), BiopacError> {
    match journal {
        None => write_i32(w, 0, le),
        Some(j) => {
            let text = j.as_text();
            let len = i32::try_from(text.len()).map_err(|_| {
                BiopacError::Validation("journal text too large to write".to_string())
            })?;
            write_i32(w, len, le)?;
            w.write_all(text.as_bytes()).map_err(BiopacError::Io)
        }
    }
}

// ---------------------------------------------------------------------------
// Compressed channel blobs (Post-4 layout)
// ---------------------------------------------------------------------------

fn write_compressed_channel<W: Write>(w: &mut W, ch: &Channel) -> Result<(), BiopacError> {
    use flate2::{Compression, write::ZlibEncoder};

    // Serialise samples to little-endian bytes (compressed channels are always LE).
    let raw_bytes = channel_samples_to_le_bytes(ch);

    let uncompressed_len = i32::try_from(raw_bytes.len()).map_err(|_| {
        BiopacError::Validation(format!("channel '{}' data too large to compress", ch.name))
    })?;

    // Compress with zlib (default level).
    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(&raw_bytes).map_err(BiopacError::Io)?;
    let compressed = encoder.finish().map_err(BiopacError::Io)?;

    let compressed_len = i32::try_from(compressed.len()).map_err(|_| {
        BiopacError::Validation(format!("channel '{}' compressed data too large", ch.name))
    })?;

    // Post-4 compression header: uncompressed_len, compressed_len, lOffset (= 0).
    write_i32(w, uncompressed_len, true)?;
    write_i32(w, compressed_len, true)?;
    write_i32(w, 0, true)?; // lOffset (not needed; reader seeks by index)

    w.write_all(&compressed).map_err(BiopacError::Io)
}

/// Collect all channel samples as little-endian bytes.
fn channel_samples_to_le_bytes(ch: &Channel) -> Vec<u8> {
    match &ch.data {
        ChannelData::Raw(v) | ChannelData::Scaled { raw: v, .. } => {
            let mut bytes = Vec::with_capacity(v.len() * 2);
            for &s in v {
                bytes.extend_from_slice(&s.to_le_bytes());
            }
            bytes
        }
        ChannelData::Float(v) => {
            let mut bytes = Vec::with_capacity(v.len() * 8);
            for &s in v {
                bytes.extend_from_slice(&s.to_le_bytes());
            }
            bytes
        }
    }
}

// ---------------------------------------------------------------------------
// Byte-writing helpers
// ---------------------------------------------------------------------------

fn write_i16<W: Write>(w: &mut W, v: i16, le: bool) -> Result<(), BiopacError> {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    w.write_all(&bytes).map_err(BiopacError::Io)
}

fn write_i32<W: Write>(w: &mut W, v: i32, le: bool) -> Result<(), BiopacError> {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    w.write_all(&bytes).map_err(BiopacError::Io)
}

fn write_i64<W: Write>(w: &mut W, v: i64, le: bool) -> Result<(), BiopacError> {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    w.write_all(&bytes).map_err(BiopacError::Io)
}

fn put_i16(buf: &mut [u8], offset: usize, v: i16, le: bool) {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    if let Some(dst) = buf.get_mut(offset..offset + 2) {
        dst.copy_from_slice(&bytes);
    }
}

fn put_i32(buf: &mut [u8], offset: usize, v: i32, le: bool) {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    if let Some(dst) = buf.get_mut(offset..offset + 4) {
        dst.copy_from_slice(&bytes);
    }
}

fn put_f64(buf: &mut [u8], offset: usize, v: f64, le: bool) {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    if let Some(dst) = buf.get_mut(offset..offset + 8) {
        dst.copy_from_slice(&bytes);
    }
}

fn put_u16(buf: &mut [u8], offset: usize, v: u16, le: bool) {
    let bytes = if le { v.to_le_bytes() } else { v.to_be_bytes() };
    if let Some(dst) = buf.get_mut(offset..offset + 2) {
        dst.copy_from_slice(&bytes);
    }
}