dspfrivs/src/upsample.rs

62 lines
1.5 KiB
Rust

#![allow(unused_parens)]
use std::io::prelude::*;
use std::fs::File;
use std::env::args;
fn main()
{
let argv = args().collect::<Vec<String>>();
if argv.len() != 4
{
eprintln!("usage: upsample <factor> <input.dat> <output.dat>");
return;
}
// deal with command-line arguments first, before anything big happens
let factor = argv[1].parse::<usize>().unwrap();
let mut input_file = File::open(&argv[2]).unwrap();
let mut output_file = File::create(&argv[3]).unwrap();
// read the input file
let mut input_data:Vec<u8> = Vec::new();
input_file.read_to_end(&mut input_data).unwrap();
// decode the input data
let mut input:Vec<f32> = Vec::with_capacity(input_data.len()/4);
for chunk in input_data.chunks_exact(4)
{
let mut samplebuffer:[u8;4] = [0x00; 4];
samplebuffer.copy_from_slice(chunk);
input.push(f32::from_be_bytes(samplebuffer));
}
// actually do the processing
let output_size = (input.len()-1)*factor;
let mut output:Vec<f32> = Vec::with_capacity(output_size);
let ffac = factor as f32;
for t in 0..input.len()-1
{
for iu in 0..factor
{
let u = iu as f32;
output.push(
input[t] * (ffac-u)/ffac
+
input[t+1] * u/ffac
);
}
}
output.push(input[input.len()-1]);
// encode the output data
let mut output_data:Vec<u8> = Vec::with_capacity(output.len()*4);
for point in output.iter()
{
output_data.extend_from_slice(&point.to_be_bytes());
}
// write the output file
output_file.write_all(&output_data).unwrap();
}