forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBddf43e8Tahinli2 months ago3186 commits

source.rs79 lines, 1.8 KiBHistoryRawDownload
1//! `flacenc::source`: where samples come from.
2
3/// A signal the encoder can read.
4///
5/// The incumbent's trait is a pull interface with a reusable context; the
6/// replica only ever hands over a [`MemSource`] it built from a `Vec`, so this
7/// is the whole-buffer shape of the same thing.
8pub trait Source {
9 /// Channel count.
10 fn channels(&self) -> usize;
11 /// Bits per sample.
12 fn bits_per_sample(&self) -> usize;
13 /// Sample rate in Hz.
14 fn sample_rate(&self) -> usize;
15 /// The interleaved samples.
16 fn as_raw_slice(&self) -> &[i32];
17}
18
19/// Samples already in memory — `flacenc::source::MemSource`.
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct MemSource {
22 channels: usize,
23 bits_per_sample: usize,
24 sample_rate: usize,
25 samples: Vec<i32>,
26}
27
28impl MemSource {
29 /// Build from interleaved samples.
30 pub fn from_samples(
31 samples: &[i32],
32 channels: usize,
33 bits_per_sample: usize,
34 sample_rate: usize,
35 ) -> Self {
36 MemSource {
37 channels,
38 bits_per_sample,
39 sample_rate,
40 samples: samples.to_owned(),
41 }
42 }
43
44 /// The samples, interleaved.
45 pub fn as_raw_slice(&self) -> &[i32] {
46 &self.samples
47 }
48
49 /// Inter-channel samples held.
50 pub fn len(&self) -> usize {
51 match self.channels {
52 0 => 0,
53 n => self.samples.len() / n,
54 }
55 }
56
57 /// True when the source holds no samples.
58 pub fn is_empty(&self) -> bool {
59 self.samples.is_empty()
60 }
61}
62
63impl Source for MemSource {
64 fn channels(&self) -> usize {
65 self.channels
66 }
67
68 fn bits_per_sample(&self) -> usize {
69 self.bits_per_sample
70 }
71
72 fn sample_rate(&self) -> usize {
73 self.sample_rate
74 }
75
76 fn as_raw_slice(&self) -> &[i32] {
77 &self.samples
78 }
79}