Add microphone and volume to talk command

This commit is contained in:
QuantumEntangledAndy committed 2021-08-04 16:13:06 +07:00
1 parent 892bbc1cd1
commit 3404345410
3 files changed
+48 -13

No files matched your search

+9 -2
View File
@@ -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<PathBuf>,
/// 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,
}
+29 -4
View File
@@ -10,10 +10,29 @@ use byte_slice_cast::*;
pub(super) fn file_input(
filename: &str,
volume: f32,
block_align: u16,
sample_rate: u16,
) -> Result<Receiver<Vec<u8>>, 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<Receiver<Vec<u8>>, Error> {
let pipeline = create_pipeline("autoaudiosrc", volume, block_align, sample_rate)?;
input(pipeline)
}
fn input(pipeline: Pipeline) -> Result<Receiver<Vec<u8>>, Error> {
let appsink = get_sink(&pipeline)?;
let (tx, rx) = sync_channel(30);
@@ -121,19 +140,25 @@ fn set_data_channel(appsink: &AppSink, tx: SyncSender<Vec<u8>>) {
);
}
fn create_pipeline(filename: &str, block_align: u16, sample_rate: u16) -> Result<Pipeline, Error> {
fn create_pipeline(
source: &str,
volume: f32,
block_align: u16,
sample_rate: u16,
) -> Result<Pipeline, Error> {
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);
+10 -7
View File
@@ -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)?;
}