creek_core/read/
data.rs

1/// A prefetch data block.
2pub struct DataBlock<T: Copy + Clone + Default + Send> {
3    pub block: Vec<Vec<T>>,
4}
5
6impl<T: Copy + Clone + Default + Send> DataBlock<T> {
7    pub fn new(num_channels: usize, block_size: usize) -> Self {
8        DataBlock {
9            block: (0..num_channels)
10                .map(|_| Vec::with_capacity(block_size))
11                .collect(),
12        }
13    }
14
15    pub fn clear(&mut self) {
16        for ch in self.block.iter_mut() {
17            ch.clear();
18        }
19    }
20}
21
22pub(crate) struct DataBlockCache<T: Copy + Clone + Default + Send> {
23    pub blocks: Vec<DataBlock<T>>,
24}
25
26impl<T: Copy + Clone + Default + Send> DataBlockCache<T> {
27    pub(crate) fn new(num_channels: usize, num_prefetch_blocks: usize, block_size: usize) -> Self {
28        Self {
29            blocks: (0..num_prefetch_blocks)
30                .map(|_| DataBlock::new(num_channels, block_size))
31                .collect(),
32        }
33    }
34}
35
36pub(crate) struct DataBlockEntry<T: Copy + Clone + Default + Send> {
37    pub use_cache_index: Option<usize>,
38    pub block: Option<DataBlock<T>>,
39    pub wanted_start_frame: usize,
40}
41
42pub(crate) struct DataBlockCacheEntry<T: Copy + Clone + Default + Send> {
43    pub cache: Option<DataBlockCache<T>>,
44    pub wanted_start_frame: usize,
45}
46
47pub(crate) struct HeapData<T: Copy + Clone + Default + Send> {
48    pub read_buffer: DataBlock<T>,
49    pub prefetch_buffer: Vec<DataBlockEntry<T>>,
50    pub caches: Vec<DataBlockCacheEntry<T>>,
51}
52
53/// The sample data returned by a `ReadClient`.
54pub struct ReadData<'a, T: Copy + Clone + Default + Send> {
55    data: &'a DataBlock<T>,
56    len: usize,
57    reached_end_of_file: bool,
58}
59
60impl<'a, T: Copy + Clone + Default + Send> ReadData<'a, T> {
61    pub(crate) fn new(data: &'a DataBlock<T>, len: usize, reached_end_of_file: bool) -> Self {
62        Self {
63            data,
64            len,
65            reached_end_of_file,
66        }
67    }
68
69    /// Read a single channel of samples.
70    ///
71    /// Use `ReadData::num_channels()` to get the number of available channels.
72    ///
73    /// The length of this data will be equal to `ReadData::num_frames()`.
74    pub fn read_channel(&self, channel: usize) -> &[T] {
75        &self.data.block[channel][0..self.len]
76    }
77
78    /// Return the number of channels in this data.
79    pub fn num_channels(&self) -> usize {
80        self.data.block.len()
81    }
82
83    /// Return the number of samples in a single channel of data.
84    pub fn num_frames(&self) -> usize {
85        self.len
86    }
87
88    /// This returns (true) if the last frame in this data is the end of the file,
89    /// (false) otherwise.
90    pub fn reached_end_of_file(&self) -> bool {
91        self.reached_end_of_file
92    }
93}