diff --git a/Cargo.lock b/Cargo.lock index 545499b..54b9224 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,30 @@ version = 4 [[package]] name = "analog-sdr-video-demodulator" version = "0.1.0" +dependencies = [ + "num-complex", +] + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] diff --git a/Cargo.toml b/Cargo.toml index 416ccf1..e17f3b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +num-complex = "0.4.6" diff --git a/src/iq_reader.rs b/src/iq_reader.rs index e69de29..3c39800 100644 --- a/src/iq_reader.rs +++ b/src/iq_reader.rs @@ -0,0 +1,56 @@ +use num_complex::Complex; +use std::error::Error; +use std::fs::File; +use std::io::{BufRead, BufReader, Read}; +use std::os::unix::process::CommandExt; + +pub type IqSample = Complex; + +// Data chunk +pub struct IqChunk { + pub samples: Vec, +} + +pub struct FileSource { + // Buffer + pub reader: BufReader, + // Hackrf I & Q 8 bits + pub raw_buffer: Vec, + // Size of a sample chunk + pub chunk_samples_size: usize, +} + +impl FileSource { + pub fn new(file_path: &str, chunk_samples_size: usize) -> Self { + let file = File::open(file_path).unwrap(); + + // Init buffer with size 16 Mo + let reader = BufReader::with_capacity(16 * 1024 * 1024, file); + + // 1 sample = 2 bytes (1 byte : I, 1 byte : Q) + let raw_reader = vec![0; chunk_samples_size * 2]; + + Self { + reader, + raw_buffer: raw_reader, + chunk_samples_size, + } + } + + pub fn read_chunk(&mut self) -> Option { + // TODO : match for EOF + self.reader.read_exact(&mut self.raw_buffer); + + // Output samples + let mut samples = Vec::with_capacity(self.chunk_samples_size); + + // Buffer read + for iq in self.raw_buffer.chunks(2) { + let i = (iq[0] as f32) / 128.0; + let q = (iq[1] as f32) / 128.0; + samples.push(Complex::new(i, q)); + } + + Some(IqChunk { samples }) + } +}