139 lines
4.0 KiB
Rust
139 lines
4.0 KiB
Rust
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::thread;
|
|
use std::time::Duration;
|
|
|
|
use itertools::Itertools;
|
|
|
|
|
|
|
|
#[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 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);
|
|
|
|
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);
|
|
|
|
mainloop.spawn();
|
|
pipeline.play();
|
|
|
|
|
|
loop {
|
|
let mut exit = false;
|
|
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);
|
|
exit = true;
|
|
break;
|
|
}
|
|
_ => {
|
|
println!("msg of type `{}` from element `{}`",
|
|
msg.type_name(),
|
|
msg.src_name());
|
|
}
|
|
}
|
|
}
|
|
if exit {
|
|
break;
|
|
}
|
|
|
|
match appsink.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();
|
|
println!("{} | {}", max0, max1);
|
|
}
|
|
}
|
|
Ok(gst::appsink::Message::Eos) => {
|
|
println!("Got no sample when polling. EOS");
|
|
break;
|
|
}
|
|
Err(_) => {
|
|
println!("Error, exiting");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
mainloop.quit();
|
|
}
|