169 lines
4.7 KiB
Rust
169 lines
4.7 KiB
Rust
extern crate sdl2;
|
|
extern crate toml;
|
|
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate gst;
|
|
extern crate itertools;
|
|
|
|
use std::fs::File;
|
|
use std::io::prelude::*;
|
|
use std::sync::mpsc;
|
|
|
|
use itertools::Itertools;
|
|
|
|
use sdl2::event::Event;
|
|
use sdl2::pixels;
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct Config {
|
|
audio: AudioConfig,
|
|
output: OutputConfig,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct AudioConfig {
|
|
alsa_device: String,
|
|
rate: u32,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
struct OutputConfig {
|
|
directory: String,
|
|
prefix: String,
|
|
}
|
|
|
|
|
|
fn read_config(path: &str) -> Config {
|
|
|
|
let mut config_toml = String::new();
|
|
|
|
let mut file = match File::open(path) {
|
|
Ok(file) => file,
|
|
Err(_) => panic!("Could not find config file!"),
|
|
};
|
|
|
|
file.read_to_string(&mut config_toml)
|
|
.unwrap_or_else(|err| panic!("Error while reading config: [{}]", err));
|
|
|
|
let config: Config = toml::from_str(&config_toml).unwrap_or_else(|err| {
|
|
panic!("Error while parsing config: [{}]", err)
|
|
});
|
|
|
|
return config;
|
|
}
|
|
|
|
|
|
fn handle_pipeline_events(bus_receiver : &mpsc::Receiver<gst::Message>) -> bool {
|
|
|
|
while let Ok(msg) = bus_receiver.try_recv() {
|
|
match msg.parse() {
|
|
gst::Message::StateChangedParsed { ref old, ref new, .. } => {
|
|
println!("element `{}` changed from {:?} to {:?}",
|
|
msg.src_name(), old, new);
|
|
}
|
|
gst::Message::ErrorParsed {ref error, ref debug, .. } => {
|
|
println!("error msg from element `{}`: {}, {}. Quitting",
|
|
msg.src_name(), error.message(), debug);
|
|
return true;
|
|
}
|
|
_ => {
|
|
println!("msg of type `{}` from element `{}`",
|
|
msg.type_name(), msg.src_name());
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
fn get_max_samples(appsink : &gst::appsink::AppSink) -> Result<(f32, f32), &str>{
|
|
match appsink.try_recv() {
|
|
Ok(gst::appsink::Message::NewSample(sample))
|
|
| Ok(gst::appsink::Message::NewPreroll(sample)) => {
|
|
if let Some(buffer) = sample.buffer() {
|
|
let (max0, max1) = buffer.map_read(|mapping| {
|
|
mapping.iter::<f32>().tuples().fold((0.0f32, 0.0f32), |(acc0, acc1), (sample0, sample1)| {
|
|
(acc0.max(sample0.abs()), acc1.max(sample1.abs()))
|
|
})
|
|
}).unwrap();
|
|
return Ok((max0, max1));
|
|
}
|
|
return Err("Unable to access samples");
|
|
}
|
|
Ok(gst::appsink::Message::Eos) => {
|
|
return Err("Got no sample when polling. EOS");
|
|
}
|
|
Err(mpsc::TryRecvError::Empty) => {
|
|
return Ok((0.0f32,0.0f32));
|
|
}
|
|
Err(mpsc::TryRecvError::Disconnected) => {
|
|
return Err("Appsink got disconnected")
|
|
}
|
|
}
|
|
}
|
|
|
|
const SCREEN_WIDTH: u32 = 160;
|
|
const SCREEN_HEIGHT: u32 = 128;
|
|
|
|
fn main() {
|
|
gst::init();
|
|
|
|
let config = read_config("rascam.toml");
|
|
|
|
|
|
let pipeline_str = format!("alsasrc device={} ! audio/x-raw,rate={},channels=2 ! queue ! tee name=apptee ! audioconvert ! flacenc ! filesink location={}/{}.flac apptee. ! queue ! audioconvert ! appsink name=appsink0 caps=\"audio/x-raw,format=F32LE,channels=2\"",
|
|
config.audio.alsa_device,
|
|
config.audio.rate,
|
|
config.output.directory,
|
|
config.output.prefix);
|
|
|
|
println!("{}", pipeline_str);
|
|
|
|
let mut pipeline = gst::Pipeline::new_from_str(&pipeline_str).unwrap();
|
|
let mut mainloop = gst::MainLoop::new();
|
|
let mut bus = pipeline.bus().expect("Couldn't get bus from pipeline");
|
|
let bus_receiver = bus.receiver();
|
|
let appsink = pipeline
|
|
.get_by_name("appsink0")
|
|
.expect("Couldn't get appsink from pipeline");
|
|
let appsink = gst::AppSink::new_from_element(appsink);
|
|
|
|
/*
|
|
let sdl_context = sdl2::init().unwrap();
|
|
let video_subsys = sdl_context.video().unwrap();
|
|
let window = video_subsys.window("rascam", SCREEN_WIDTH, SCREEN_HEIGHT)
|
|
.position_centered()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let mut canvas = window.into_canvas().build().unwrap();
|
|
|
|
canvas.set_draw_color(pixels::Color::RGB(255, 255, 0));
|
|
canvas.clear();
|
|
canvas.present();
|
|
*/
|
|
|
|
mainloop.spawn();
|
|
pipeline.play();
|
|
|
|
loop {
|
|
if handle_pipeline_events(&bus_receiver) {
|
|
break;
|
|
}
|
|
let result = get_max_samples(& appsink);
|
|
match result {
|
|
Ok((max0, max1)) => {
|
|
println!("{} | {}", max0, max1);
|
|
}
|
|
Err(msg) => {
|
|
println!("Error occured: {}", msg);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
mainloop.quit();
|
|
}
|