fix: 🐛 microphone && audio sync
feat: ✨ noise gate for microphone
This commit is contained in:
parent
d44cfb37bf
commit
ada65ea7c1
2 changed files with 29 additions and 9 deletions
|
@ -5,7 +5,7 @@ use tokio::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn record(
|
pub async fn record(
|
||||||
sound_stream_sender: Sender<f32>,
|
microphone_stream_sender: Sender<f32>,
|
||||||
recording_to_base: Sender<bool>,
|
recording_to_base: Sender<bool>,
|
||||||
mut base_to_recording: Receiver<bool>,
|
mut base_to_recording: Receiver<bool>,
|
||||||
) {
|
) {
|
||||||
|
@ -13,11 +13,10 @@ pub async fn record(
|
||||||
let input_device = host.default_input_device().unwrap();
|
let input_device = host.default_input_device().unwrap();
|
||||||
|
|
||||||
let config: cpal::StreamConfig = input_device.default_input_config().unwrap().into();
|
let config: cpal::StreamConfig = input_device.default_input_config().unwrap().into();
|
||||||
|
|
||||||
let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| {
|
let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| {
|
||||||
for &sample in data {
|
for &sample in data {
|
||||||
if sound_stream_sender.receiver_count() > 0 {
|
if microphone_stream_sender.receiver_count() > 0 {
|
||||||
match sound_stream_sender.send(sample) {
|
match microphone_stream_sender.send(sample) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{io::Write, sync::Arc, time::Duration};
|
use std::{cmp::min, io::Write, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use brotli::CompressorWriter;
|
use brotli::CompressorWriter;
|
||||||
use futures_util::SinkExt;
|
use futures_util::SinkExt;
|
||||||
|
@ -94,10 +94,21 @@ async fn mixer(
|
||||||
flow_sender: Sender<f32>,
|
flow_sender: Sender<f32>,
|
||||||
latency: u16,
|
latency: u16,
|
||||||
) {
|
) {
|
||||||
|
microphone_stream_receiver = microphone_stream_receiver.resubscribe();
|
||||||
|
audio_stream_receiver = audio_stream_receiver.resubscribe();
|
||||||
loop {
|
loop {
|
||||||
let mut microphone_stream = vec![];
|
let mut microphone_stream = vec![];
|
||||||
let mut audio_stream = vec![];
|
let mut audio_stream = vec![];
|
||||||
|
|
||||||
let mut microphone_stream_iteration = microphone_stream_receiver.len();
|
let mut microphone_stream_iteration = microphone_stream_receiver.len();
|
||||||
|
let mut audio_stream_iteration = audio_stream_receiver.len();
|
||||||
|
|
||||||
|
if microphone_stream_iteration > 0 && audio_stream_iteration > 0 {
|
||||||
|
let sync_iteration = min(microphone_stream_iteration, audio_stream_iteration);
|
||||||
|
microphone_stream_iteration = sync_iteration;
|
||||||
|
audio_stream_iteration = sync_iteration;
|
||||||
|
}
|
||||||
|
|
||||||
while microphone_stream_iteration > 0 {
|
while microphone_stream_iteration > 0 {
|
||||||
microphone_stream_iteration -= 1;
|
microphone_stream_iteration -= 1;
|
||||||
match microphone_stream_receiver.recv().await {
|
match microphone_stream_receiver.recv().await {
|
||||||
|
@ -113,7 +124,6 @@ async fn mixer(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut audio_stream_iteration = audio_stream_receiver.len();
|
|
||||||
while audio_stream_iteration > 0 {
|
while audio_stream_iteration > 0 {
|
||||||
audio_stream_iteration -= 1;
|
audio_stream_iteration -= 1;
|
||||||
match audio_stream_receiver.recv().await {
|
match audio_stream_receiver.recv().await {
|
||||||
|
@ -127,18 +137,29 @@ async fn mixer(
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut flow = vec![];
|
let mut flow = vec![];
|
||||||
|
let microphone_volume = *microphone_stream_volume.lock().await;
|
||||||
|
let audio_volume = *audio_stream_volume.lock().await;
|
||||||
|
|
||||||
for element in microphone_stream {
|
for element in microphone_stream {
|
||||||
flow.push(element * (*microphone_stream_volume.lock().await));
|
if element < 0.01 || element > -0.01 {
|
||||||
|
flow.push(element * microphone_volume);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i, element) in audio_stream.iter().enumerate() {
|
for (i, element) in audio_stream.iter().enumerate() {
|
||||||
let audio_volumized = element * (*audio_stream_volume.lock().await);
|
let audio_volumized = element * audio_volume;
|
||||||
if flow.len() > i && flow.len() != 0 {
|
if flow.len() > i {
|
||||||
flow[i] = flow[i] + audio_volumized;
|
flow[i] = flow[i] + audio_volumized;
|
||||||
} else {
|
} else {
|
||||||
flow.push(audio_volumized);
|
flow.push(audio_volumized);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i in 0..flow.len() {
|
||||||
|
if flow[i] > 1.0 {
|
||||||
|
flow[i] = 0.5 * (flow[i] / flow[i].trunc() * 10.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
for element in flow {
|
for element in flow {
|
||||||
match flow_sender.send(element) {
|
match flow_sender.send(element) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue