68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
// vim: noet
|
||
|
||
use std::process::exit;
|
||
|
||
use byteorder::{NativeEndian, ReadBytesExt};
|
||
|
||
use mlua::Lua;
|
||
|
||
mod signal_processing;
|
||
mod config;
|
||
|
||
use crate::signal_processing::SignalProcessing;
|
||
|
||
fn main()
|
||
{
|
||
let mut stdin = std::io::stdin();
|
||
|
||
// test the mlua crate
|
||
let lua_state = Lua::new();
|
||
|
||
lua_state.globals().set("get_rust_value", lua_state.create_function(|_, ()| {
|
||
Ok(3)
|
||
}).unwrap()).unwrap();
|
||
|
||
let user_script = std::fs::read_to_string("test.lua").unwrap();
|
||
lua_state.load(&user_script).exec().unwrap();
|
||
|
||
let lua_func_test : mlua::Function = lua_state.globals().get("test").unwrap();
|
||
|
||
println!("{}", lua_func_test.call::<_, u32>(123).unwrap());
|
||
|
||
let mut sigproc = SignalProcessing::new(config::BLOCK_LEN, config::SAMP_RATE).unwrap();
|
||
|
||
println!("Done! Starting main loop…");
|
||
|
||
// array for samples directly read from stream
|
||
let mut samples = [0i16; config::BLOCK_LEN];
|
||
|
||
// main loop
|
||
loop {
|
||
|
||
// read a block of samples and exit gracefully on EOF
|
||
for sample in samples.iter_mut() {
|
||
let res = stdin.read_i16::<NativeEndian>();
|
||
|
||
match res {
|
||
Ok(s) => *sample = s,
|
||
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
|
||
println!("End of stream. Exiting.");
|
||
exit(0);
|
||
},
|
||
Err(e) => panic!(e)
|
||
}
|
||
}
|
||
|
||
sigproc.import_i16_mono(&samples).unwrap();
|
||
sigproc.update_fft().unwrap();
|
||
|
||
let energy_bass = sigproc.get_energy_in_band( 0.0, 400.0);
|
||
let energy_mid = sigproc.get_energy_in_band( 400.0, 4000.0);
|
||
let energy_treble = sigproc.get_energy_in_band(4000.0, config::SAMP_RATE/2.0);
|
||
|
||
// dump the output
|
||
println!("Bass: {:8.2} – Mid: {:8.2} – Treble: {:8.2}", energy_bass, energy_mid, energy_treble);
|
||
}
|
||
|
||
}
|