diff --git a/src/talk/cmdline.rs b/src/talk/cmdline.rs index 5c53431..807d677 100644 --- a/src/talk/cmdline.rs +++ b/src/talk/cmdline.rs @@ -15,6 +15,13 @@ pub struct Opt { /// The name of the camera to change the lights of. Must be a name in the config pub camera: String, /// The path to the audio file. - #[structopt(short, long, parse(from_os_str))] - pub media_path: PathBuf, + #[structopt(short, long, parse(from_os_str), conflicts_with = "microphone")] + pub file_path: Option, + /// Use the microphone as the source. Which microphone depends + /// on [gstraemer](https://gstreamer.freedesktop.org/documentation/autodetect/autoaudiosrc.html?gi-language=c#autoaudiosrc-page) + #[structopt(short, long, conflicts_with = "file_path")] + pub microphone: bool, + /// Use to change the volume of the input + #[structopt(short, long, default_value = "1.0")] + pub volume: f32, } diff --git a/src/talk/gst.rs b/src/talk/gst.rs index 1e92ad8..05c4f25 100644 --- a/src/talk/gst.rs +++ b/src/talk/gst.rs @@ -10,10 +10,29 @@ use byte_slice_cast::*; pub(super) fn file_input( filename: &str, + volume: f32, block_align: u16, sample_rate: u16, ) -> Result>, Error> { - let pipeline = create_pipeline(filename, block_align, sample_rate)?; + let pipeline = create_pipeline( + &format!("filesrc location={}", filename), + volume, + block_align, + sample_rate, + )?; + input(pipeline) +} + +pub(super) fn mic_input( + volume: f32, + block_align: u16, + sample_rate: u16, +) -> Result>, Error> { + let pipeline = create_pipeline("autoaudiosrc", volume, block_align, sample_rate)?; + input(pipeline) +} + +fn input(pipeline: Pipeline) -> Result>, Error> { let appsink = get_sink(&pipeline)?; let (tx, rx) = sync_channel(30); @@ -121,19 +140,25 @@ fn set_data_channel(appsink: &AppSink, tx: SyncSender>) { ); } -fn create_pipeline(filename: &str, block_align: u16, sample_rate: u16) -> Result { +fn create_pipeline( + source: &str, + volume: f32, + block_align: u16, + sample_rate: u16, +) -> Result { gstreamer::init()?; let launch_str = format!( - "filesrc location={} \ + "{} \ ! decodebin \ ! audioconvert \ ! audioresample \ ! audio/x-raw,rate={},channels=1 \ + ! volume volume={:.2} \ ! queue \ ! adpcmenc blockalign={} layout=dvi \ ! appsink name=thesink", - filename, sample_rate, block_align + source, sample_rate, volume, block_align ); // log::info!("{}", launch_str); diff --git a/src/talk/mod.rs b/src/talk/mod.rs index 8be4dfe..3d4e43b 100644 --- a/src/talk/mod.rs +++ b/src/talk/mod.rs @@ -78,13 +78,16 @@ pub fn main(opt: Opt) -> Result<(), Error> { return Err(Error::TalkUnsupported); } - let rx = gst::file_input( - &opt.media_path - .to_str() - .expect("File path not UTF8 complient"), - block_size, - sample_rate, - )?; + let rx = match (&opt.file_path, &opt.microphone) { + (Some(path), false) => gst::file_input( + path.to_str().expect("File path not UTF8 complient"), + opt.volume, + block_size, + sample_rate, + )?, + (None, true) => gst::mic_input(opt.volume, block_size, sample_rate)?, + _ => unreachable!(), + }; camera.talk_stream(rx, talk_config)?; }