initial commit of cursed encoder project

This commit is contained in:
sigil-03 2025-10-16 09:38:28 -06:00
commit c72c5aee87
5 changed files with 69 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "dpcm-encoder-decoder"
version = "0.1.0"

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "dpcm-encoder-decoder"
version = "0.1.0"
edition = "2024"
[dependencies]

4
README.md Normal file
View file

@ -0,0 +1,4 @@
# DPCM ENCODER / DECODER
goal:
- [ ] CLI application to encode PCM_u8 audio to DPCM audo with a given step size

51
src/main.rs Normal file
View file

@ -0,0 +1,51 @@
fn main() {
let input = include_bytes!("../sweep_short.raw");
let min = input.iter().min().unwrap();
let max = input.iter().max().unwrap();
let step_size = 0x3;
println!("len:\t\t{} samples", input.len());
println!("initial:\t0x{:02x}", input[0]);
println!("min:\t\t0x{:02x}", min);
println!("max:\t\t0x{:02x}", max);
println!("step size:\t0x{:02x}", step_size);
let mut output: Vec<u8> = Vec::new();
// let mut max_diff = 0;
// let prev = input[0];
// for byte in input {
// }
let mut sample_pair: u8 = 0;
let mut index = 0;
let mut prev = input[0];
for byte in &input[1..] {
let (diff, dir) = match byte >= &prev {
true => ({byte - prev}, 0x1),
false => ({prev - byte}, 0x0),
};
let steps = (diff / step_size) & 0x0E;
sample_pair |= ((steps << 1) | dir) << 4 * (index % 2);
if (index % 2) == 1 {
output.push(sample_pair);
sample_pair = 0;
}
// println!("0x{:01x} | {}", steps, dir);
prev = match dir {
0x1 => prev + (steps * step_size),
0x0 => prev - (steps * step_size),
_ => prev + (steps * step_size),
};
index += 1;
}
// for sample in &output {
// println!("0x{:02x}", sample);
// }
std::fs::write("./sweep_dpcm_u4.raw", &output).unwrap();
}