flacx 0.11.0

Convert supported PCM containers to FLAC, decode FLAC back to PCM containers, and recompress existing FLAC streams.
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
use std::io::{Read, Seek, SeekFrom};

use crate::error::{Error, Result};
use crate::metadata::Metadata;

pub(crate) use crate::pcm::{
    PcmEnvelope, append_encoded_sample, container_bits_from_valid_bits, ordinary_channel_mask,
};
pub use crate::pcm::{PcmSpec, PcmStream};
pub(crate) type PcmReaderOptions = crate::wav_input::WavReaderOptions;

/// Single-pass PCM sample source consumed by the encode session.
pub trait EncodePcmStream {
    /// Return the stream specification that drives encode planning.
    fn spec(&self) -> PcmSpec;

    /// Read up to `max_frames` interleaved PCM frames into `output`.
    ///
    /// Returns the number of frames appended to `output`.
    fn read_chunk(&mut self, max_frames: usize, output: &mut Vec<i32>) -> Result<usize>;

    #[cfg(feature = "progress")]
    fn input_bytes_processed(&self) -> u64 {
        0
    }

    fn update_streaminfo_md5(
        &mut self,
        md5: &mut crate::md5::StreaminfoMd5,
        samples: &[i32],
    ) -> Result<()> {
        md5.update_samples(samples)
    }

    fn finish_streaminfo_md5(&mut self, md5: crate::md5::StreaminfoMd5) -> Result<[u8; 16]> {
        md5.finalize()
    }

    fn preferred_encode_chunk_max_frames(&self) -> Option<usize> {
        None
    }

    fn preferred_encode_chunk_target_pcm_frames(&self) -> Option<usize> {
        None
    }
}

#[cfg(feature = "progress")]
pub(crate) struct CountedEncodePcmStream<S> {
    stream: S,
    input_bytes_processed: u64,
}

#[cfg(feature = "progress")]
impl<S> CountedEncodePcmStream<S> {
    pub(crate) fn new(stream: S) -> Self {
        Self {
            stream,
            input_bytes_processed: 0,
        }
    }
}

#[cfg(not(feature = "progress"))]
pub(crate) type CountedEncodePcmStream<S> = S;

#[cfg(feature = "progress")]
pub(crate) fn counted_encode_pcm_stream<S>(stream: S) -> CountedEncodePcmStream<S> {
    CountedEncodePcmStream::new(stream)
}

#[cfg(not(feature = "progress"))]
pub(crate) fn counted_encode_pcm_stream<S>(stream: S) -> CountedEncodePcmStream<S> {
    stream
}

#[cfg(feature = "progress")]
impl<S: EncodePcmStream> EncodePcmStream for CountedEncodePcmStream<S> {
    fn spec(&self) -> PcmSpec {
        self.stream.spec()
    }

    fn read_chunk(&mut self, max_frames: usize, output: &mut Vec<i32>) -> Result<usize> {
        let frames = self.stream.read_chunk(max_frames, output)?;
        self.input_bytes_processed = self
            .input_bytes_processed
            .saturating_add(pcm_bytes_for_frames(self.stream.spec(), frames));
        Ok(frames)
    }

    #[cfg(feature = "progress")]
    fn input_bytes_processed(&self) -> u64 {
        self.input_bytes_processed
    }

    fn update_streaminfo_md5(
        &mut self,
        md5: &mut crate::md5::StreaminfoMd5,
        samples: &[i32],
    ) -> Result<()> {
        self.stream.update_streaminfo_md5(md5, samples)
    }

    fn finish_streaminfo_md5(&mut self, md5: crate::md5::StreaminfoMd5) -> Result<[u8; 16]> {
        self.stream.finish_streaminfo_md5(md5)
    }

    fn preferred_encode_chunk_max_frames(&self) -> Option<usize> {
        self.stream.preferred_encode_chunk_max_frames()
    }

    fn preferred_encode_chunk_target_pcm_frames(&self) -> Option<usize> {
        self.stream.preferred_encode_chunk_target_pcm_frames()
    }
}

/// Owned encode-side handoff that keeps metadata and the PCM stream together.
///
/// Most explicit encode workflows construct one of these from a reader's
/// [`into_source`](PcmReader::into_source) method and then pass it to
/// [`crate::Encoder::encode_source`].
pub struct EncodeSource<S> {
    metadata: Metadata,
    stream: S,
}

impl<S> EncodeSource<S> {
    /// Create a new encode source from staged metadata and a PCM stream.
    #[must_use]
    pub fn new(metadata: Metadata, stream: S) -> Self {
        Self { metadata, stream }
    }

    /// Return the staged encode metadata.
    #[must_use]
    pub fn metadata(&self) -> &Metadata {
        &self.metadata
    }

    /// Return mutable access to the staged encode metadata.
    pub fn metadata_mut(&mut self) -> &mut Metadata {
        &mut self.metadata
    }

    /// Replace the staged metadata and return the updated source.
    #[must_use]
    pub fn with_metadata(mut self, metadata: Metadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// Consume the source and return the metadata and stream.
    pub fn into_parts(self) -> (Metadata, S) {
        (self.metadata, self.stream)
    }
}

impl<S: EncodePcmStream> EncodeSource<S> {
    /// Return the PCM spec that will drive encode planning.
    #[must_use]
    pub fn spec(&self) -> PcmSpec {
        self.stream.spec()
    }
}

/// Family-dispatched PCM reader for the explicit encode workflow.
///
/// Use `PcmReader` when the input family is only known at runtime. If you know
/// the container ahead of time, prefer the more specific reader types such as
/// [`crate::WavReader`] or [`crate::AiffReader`].
#[derive(Debug)]
pub enum PcmReader<R: Read + Seek> {
    Wav(crate::wav_input::WavReader<R>),
    #[cfg(feature = "aiff")]
    Aiff(crate::aiff::AiffReader<R>),
    #[cfg(feature = "caf")]
    Caf(crate::caf::CafReader<R>),
}

impl<R: Read + Seek> PcmReader<R> {
    /// Parse a supported PCM container using explicit constructor-driven dispatch.
    pub fn new(reader: R) -> Result<Self> {
        Self::with_options(reader, PcmReaderOptions::default())
    }

    /// Parse a supported PCM container while applying RIFF/WAVE-family reader options.
    ///
    /// The options only affect WAV/RF64/Wave64 inputs. AIFF/AIFC and CAF
    /// inputs ignore them because their reader families do not share that
    /// policy surface.
    pub fn with_reader_options(reader: R, options: crate::WavReaderOptions) -> Result<Self> {
        Self::with_options(reader, options)
    }

    /// Return the parsed PCM specification without consuming the sample stream.
    #[must_use]
    pub fn spec(&self) -> PcmSpec {
        match self {
            Self::Wav(reader) => reader.spec(),
            #[cfg(feature = "aiff")]
            Self::Aiff(reader) => reader.spec(),
            #[cfg(feature = "caf")]
            Self::Caf(reader) => reader.spec(),
        }
    }

    /// Return the parsed encode-side metadata captured from the input.
    #[must_use]
    pub fn metadata(&self) -> &Metadata {
        match self {
            Self::Wav(reader) => reader.metadata(),
            #[cfg(feature = "aiff")]
            Self::Aiff(reader) => reader.metadata(),
            #[cfg(feature = "caf")]
            Self::Caf(reader) => reader.metadata(),
        }
    }

    /// Convert the parsed reader into an owned encode source.
    pub fn into_source(self) -> EncodeSource<impl EncodePcmStream> {
        let (metadata, stream) = self.into_session_parts();
        EncodeSource::new(metadata, stream)
    }

    #[allow(dead_code)]
    pub(crate) fn into_pcm_stream(self) -> AnyPcmStream<R> {
        self.into_session_parts().1
    }

    pub(crate) fn with_options(reader: R, options: PcmReaderOptions) -> Result<Self> {
        read_pcm_reader_with_options(reader, options)
    }

    pub(crate) fn into_session_parts(self) -> (Metadata, AnyPcmStream<R>) {
        match self {
            Self::Wav(reader) => {
                let (metadata, stream) = reader.into_session_parts();
                (
                    metadata,
                    AnyPcmStream::Wav(counted_encode_pcm_stream(stream)),
                )
            }
            #[cfg(feature = "aiff")]
            Self::Aiff(reader) => {
                let (metadata, stream) = reader.into_session_parts();
                (
                    metadata,
                    AnyPcmStream::Aiff(counted_encode_pcm_stream(stream)),
                )
            }
            #[cfg(feature = "caf")]
            Self::Caf(reader) => {
                let (metadata, stream) = reader.into_session_parts();
                (
                    metadata,
                    AnyPcmStream::Caf(counted_encode_pcm_stream(stream)),
                )
            }
        }
    }
}

/// Family-dispatched single-pass PCM stream.
pub(crate) enum AnyPcmStream<R: Read + Seek> {
    Wav(CountedEncodePcmStream<crate::wav_input::WavPcmStream<R>>),
    #[cfg(feature = "aiff")]
    Aiff(CountedEncodePcmStream<crate::aiff::AiffPcmStream<R>>),
    #[cfg(feature = "caf")]
    Caf(CountedEncodePcmStream<crate::caf::CafPcmStream<R>>),
}

impl<R: Read + Seek> EncodePcmStream for AnyPcmStream<R> {
    fn spec(&self) -> PcmSpec {
        match self {
            Self::Wav(stream) => stream.spec(),
            #[cfg(feature = "aiff")]
            Self::Aiff(stream) => stream.spec(),
            #[cfg(feature = "caf")]
            Self::Caf(stream) => stream.spec(),
        }
    }

    fn read_chunk(&mut self, max_frames: usize, output: &mut Vec<i32>) -> Result<usize> {
        match self {
            Self::Wav(stream) => stream.read_chunk(max_frames, output),
            #[cfg(feature = "aiff")]
            Self::Aiff(stream) => stream.read_chunk(max_frames, output),
            #[cfg(feature = "caf")]
            Self::Caf(stream) => stream.read_chunk(max_frames, output),
        }
    }

    #[cfg(feature = "progress")]
    fn input_bytes_processed(&self) -> u64 {
        match self {
            Self::Wav(stream) => stream.input_bytes_processed(),
            #[cfg(feature = "aiff")]
            Self::Aiff(stream) => stream.input_bytes_processed(),
            #[cfg(feature = "caf")]
            Self::Caf(stream) => stream.input_bytes_processed(),
        }
    }

    fn update_streaminfo_md5(
        &mut self,
        md5: &mut crate::md5::StreaminfoMd5,
        samples: &[i32],
    ) -> Result<()> {
        match self {
            Self::Wav(stream) => stream.update_streaminfo_md5(md5, samples),
            #[cfg(feature = "aiff")]
            Self::Aiff(stream) => stream.update_streaminfo_md5(md5, samples),
            #[cfg(feature = "caf")]
            Self::Caf(stream) => stream.update_streaminfo_md5(md5, samples),
        }
    }
}

#[cfg(feature = "progress")]
fn pcm_bytes_for_frames(spec: PcmSpec, frames: usize) -> u64 {
    (frames as u64)
        .saturating_mul(u64::from(spec.channels))
        .saturating_mul(u64::from(spec.bytes_per_sample))
}

/// Parse a supported PCM container and return a family-specific reader for the
/// explicit encode workflow.
#[allow(dead_code)]
pub(crate) fn read_pcm_reader<R: Read + Seek>(reader: R) -> Result<PcmReader<R>> {
    read_pcm_reader_with_options(reader, PcmReaderOptions::default())
}

#[allow(dead_code)]
pub fn read_wav<R: Read + Seek>(reader: R) -> Result<PcmStream> {
    #[cfg(not(feature = "wav"))]
    {
        return Err(wav_feature_disabled_error());
    }
    crate::wav_input::read_wav(reader)
}

/// Inspect a supported PCM-container stream and return its total sample count.
///
/// This helper reads only the container metadata needed for sample counts.
/// It is useful for preflight checks and progress planning.
///
/// # Example
///
/// ```no_run
/// use flacx::inspect_pcm_total_samples;
/// use std::fs::File;
///
/// let total_samples = inspect_pcm_total_samples(File::open("input.wav").unwrap()).unwrap();
/// assert!(total_samples > 0);
/// ```
pub fn inspect_wav_total_samples<R: Read + Seek>(mut reader: R) -> Result<u64> {
    inspect_pcm_total_samples(&mut reader)
}

pub(crate) fn inspect_pcm_total_samples<R: Read + Seek>(reader: &mut R) -> Result<u64> {
    match sniff_pcm_input_kind(reader)? {
        PcmInputKind::AiffLike => inspect_aiff_total_samples_with_features(reader),
        PcmInputKind::Caf => inspect_caf_total_samples_with_features(reader),
        PcmInputKind::RiffLike => {
            ensure_wav_family_enabled()?;
            crate::wav_input::inspect_total_samples(reader)
        }
    }
}

pub(crate) fn read_pcm_reader_with_options<R: Read + Seek>(
    mut reader: R,
    options: PcmReaderOptions,
) -> Result<PcmReader<R>> {
    match sniff_pcm_input_kind(&mut reader)? {
        PcmInputKind::AiffLike => read_aiff_reader_with_features(reader),
        PcmInputKind::Caf => read_caf_reader_with_features(reader),
        PcmInputKind::RiffLike => {
            ensure_wav_family_enabled()?;
            Ok(PcmReader::Wav(
                crate::wav_input::WavReader::with_reader_options(reader, options)?,
            ))
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PcmInputKind {
    RiffLike,
    AiffLike,
    Caf,
}

fn sniff_pcm_input_kind<R: Read + Seek>(reader: &mut R) -> Result<PcmInputKind> {
    let start = reader.stream_position()?;
    let mut magic = [0u8; 4];
    let kind = match reader.read_exact(&mut magic) {
        Ok(()) if magic == *b"FORM" => PcmInputKind::AiffLike,
        Ok(()) if magic == *b"caff" => PcmInputKind::Caf,
        Ok(()) => PcmInputKind::RiffLike,
        Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => PcmInputKind::RiffLike,
        Err(error) => return Err(error.into()),
    };
    reader.seek(SeekFrom::Start(start))?;
    Ok(kind)
}

#[cfg(feature = "aiff")]
fn inspect_aiff_total_samples_with_features<R: Read + Seek>(reader: &mut R) -> Result<u64> {
    crate::aiff::inspect_aiff_total_samples(reader)
}

#[cfg(not(feature = "aiff"))]
fn inspect_aiff_total_samples_with_features<R: Read + Seek>(_reader: &mut R) -> Result<u64> {
    Err(aiff_feature_disabled_error())
}

#[cfg(feature = "caf")]
fn inspect_caf_total_samples_with_features<R: Read + Seek>(reader: &mut R) -> Result<u64> {
    crate::caf::inspect_caf_total_samples(reader)
}

#[cfg(not(feature = "caf"))]
fn inspect_caf_total_samples_with_features<R: Read + Seek>(_reader: &mut R) -> Result<u64> {
    Err(caf_feature_disabled_error())
}

#[cfg(feature = "aiff")]
fn read_aiff_reader_with_features<R: Read + Seek>(reader: R) -> Result<PcmReader<R>> {
    Ok(PcmReader::Aiff(crate::aiff::AiffReader::new(reader)?))
}

#[cfg(not(feature = "aiff"))]
fn read_aiff_reader_with_features<R: Read + Seek>(_reader: R) -> Result<PcmReader<R>> {
    Err(aiff_feature_disabled_error())
}

#[cfg(feature = "caf")]
fn read_caf_reader_with_features<R: Read + Seek>(reader: R) -> Result<PcmReader<R>> {
    Ok(PcmReader::Caf(crate::caf::CafReader::new(reader)?))
}

#[cfg(not(feature = "caf"))]
fn read_caf_reader_with_features<R: Read + Seek>(_reader: R) -> Result<PcmReader<R>> {
    Err(caf_feature_disabled_error())
}

#[allow(dead_code)]
fn wav_feature_disabled_error() -> Error {
    Error::UnsupportedPcmContainer(
        "RIFF/WAVE family support requires the `wav` cargo feature".into(),
    )
}

#[allow(dead_code)]
fn aiff_feature_disabled_error() -> Error {
    Error::UnsupportedPcmContainer("AIFF/AIFC support requires the `aiff` cargo feature".into())
}

#[allow(dead_code)]
fn caf_feature_disabled_error() -> Error {
    Error::UnsupportedPcmContainer("CAF support requires the `caf` cargo feature".into())
}

fn ensure_wav_family_enabled() -> Result<()> {
    #[cfg(feature = "wav")]
    {
        Ok(())
    }
    #[cfg(not(feature = "wav"))]
    {
        Err(wav_feature_disabled_error())
    }
}