chunk to sample process

This commit is contained in:
2026-06-15 12:50:30 +02:00
parent d8012551cd
commit d2bc12d5fd
6 changed files with 83 additions and 91 deletions

View File

@ -26,6 +26,8 @@ pub struct FileSource {
pub raw_buffer: Vec<u8>,
// Size of a sample chunk
pub chunk_samples_size: usize,
// raw_buffer position (in samples)
pub cursor: usize,
}
impl FileSource {
@ -42,40 +44,38 @@ impl FileSource {
reader,
raw_buffer: raw_reader,
chunk_samples_size,
cursor: chunk_samples_size,
})
}
}
impl Iterator for FileSource {
type Item = Result<IqChunk, Box<dyn Error>>;
type Item = Result<IqSample, Box<dyn Error>>;
fn next(&mut self) -> Option<Self::Item> {
// Buffer read
match self.reader.read_exact(&mut self.raw_buffer) {
Ok(_) => {}
if self.cursor >= self.chunk_samples_size {
// Buffer read chunk
match self.reader.read_exact(&mut self.raw_buffer) {
Ok(_) => {}
// EOF
Err(e) if e.kind() == ErrorKind::UnexpectedEof => {
return None;
}
// EOF
Err(e) if e.kind() == ErrorKind::UnexpectedEof => {
return None;
}
Err(e) => {
return Some(Err(e.into()));
Err(e) => {
return Some(Err(e.into()));
}
}
self.cursor = 0;
}
// Output samples
let mut samples = Vec::with_capacity(self.chunk_samples_size);
// Buffer read sample
let i = (self.raw_buffer[self.cursor * 2] as i8) as f32 / 128.0;
let q = (self.raw_buffer[self.cursor * 2 + 1] as i8) as f32 / 128.0;
// Buffer read
for iq in self.raw_buffer.chunks(2) {
let i = iq[0] as i8;
let q = iq[1] as i8;
let i_f32 = (i as f32) / 128.0;
let q_f32 = (q as f32) / 128.0;
samples.push(Complex::new(i_f32, q_f32));
}
self.cursor += 1;
Some(Ok(IqChunk { samples }))
Some(Ok(IqSample::new(i, q)))
}
}