145 lines
4.0 KiB
Rust
145 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 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 handle_pipeline_events(bus : &mut gst::bus::Bus) -> bool {
|
|
let bus_receiver = bus.receiver();
|
|
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.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(_) => {
|
|
return Err("Error, exiting");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
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 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 {
|
|
|
|
if handle_pipeline_events(&mut bus) {
|
|
break;
|
|
}
|
|
|
|
let result = get_max_samples(& appsink);
|
|
match result {
|
|
Ok((max0, max1)) => {
|
|
println!("{} | {}", max0, max1);
|
|
}
|
|
Err(msg) => {
|
|
println!("Error occured: {}", msg);
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
mainloop.quit();
|
|
}
|