optimize audio
This commit is contained in:
parent
66e39b62a2
commit
f69f6bc8d4
@ -11,7 +11,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
type Xdo = *const c_void;
|
type Xdo = *const c_void;
|
||||||
|
|
||||||
pub const PA_SAMPLE_RATE: u32 = 24000;
|
pub const PA_SAMPLE_RATE: u32 = 48000;
|
||||||
static mut UNMODIFIED: bool = true;
|
static mut UNMODIFIED: bool = true;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
|
@ -115,9 +115,6 @@ mod cpal_impl {
|
|||||||
encoder: &mut Encoder,
|
encoder: &mut Encoder,
|
||||||
sp: &GenericService,
|
sp: &GenericService,
|
||||||
) {
|
) {
|
||||||
if data.iter().filter(|x| **x != 0.).next().is_none() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let buffer;
|
let buffer;
|
||||||
let data = if sample_rate0 != sample_rate {
|
let data = if sample_rate0 != sample_rate {
|
||||||
buffer = crate::common::resample_channels(data, sample_rate0, sample_rate, channels);
|
buffer = crate::common::resample_channels(data, sample_rate0, sample_rate, channels);
|
||||||
@ -194,7 +191,6 @@ mod cpal_impl {
|
|||||||
log::error!("an error occurred on stream: {}", err);
|
log::error!("an error occurred on stream: {}", err);
|
||||||
};
|
};
|
||||||
// Sample rate must be one of 8000, 12000, 16000, 24000, or 48000.
|
// Sample rate must be one of 8000, 12000, 16000, 24000, or 48000.
|
||||||
// Note: somehow 48000 not work
|
|
||||||
let sample_rate_0 = config.sample_rate().0;
|
let sample_rate_0 = config.sample_rate().0;
|
||||||
let sample_rate = if sample_rate_0 < 12000 {
|
let sample_rate = if sample_rate_0 < 12000 {
|
||||||
8000
|
8000
|
||||||
@ -202,9 +198,12 @@ mod cpal_impl {
|
|||||||
12000
|
12000
|
||||||
} else if sample_rate_0 < 24000 {
|
} else if sample_rate_0 < 24000 {
|
||||||
16000
|
16000
|
||||||
} else {
|
} else if sample_rate_0 < 48000 {
|
||||||
24000
|
24000
|
||||||
|
} else {
|
||||||
|
48000
|
||||||
};
|
};
|
||||||
|
log::debug!("Audio sample rate : {}",sample_rate);
|
||||||
let mut encoder = Encoder::new(
|
let mut encoder = Encoder::new(
|
||||||
sample_rate,
|
sample_rate,
|
||||||
if config.channels() > 1 { Stereo } else { Mono },
|
if config.channels() > 1 { Stereo } else { Mono },
|
||||||
@ -278,8 +277,29 @@ fn create_format_msg(sample_rate: u32, channels: u16) -> Message {
|
|||||||
msg
|
msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use AUDIO_ZERO_COUNT for the Noise(Zero) Gate Attack Time
|
||||||
|
// every audio data length is set to 480
|
||||||
|
// MAX_AUDIO_ZERO_COUNT=800 is similar as Gate Attack Time 3~5s(Linux) || 6~8s(Windows)
|
||||||
|
const MAX_AUDIO_ZERO_COUNT: u16 = 800;
|
||||||
|
static mut AUDIO_ZERO_COUNT: u16 = 0;
|
||||||
|
|
||||||
fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) {
|
fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) {
|
||||||
if data.iter().filter(|x| **x != 0.).next().is_some() {
|
if data.iter().filter(|x| **x != 0.).next().is_some() {
|
||||||
|
unsafe {
|
||||||
|
AUDIO_ZERO_COUNT = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
unsafe {
|
||||||
|
if AUDIO_ZERO_COUNT > MAX_AUDIO_ZERO_COUNT {
|
||||||
|
if AUDIO_ZERO_COUNT == MAX_AUDIO_ZERO_COUNT + 1 {
|
||||||
|
log::debug!("Audio Zero Gate Attack");
|
||||||
|
AUDIO_ZERO_COUNT += 1;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AUDIO_ZERO_COUNT += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
match encoder.encode_vec_float(data, data.len() * 6) {
|
match encoder.encode_vec_float(data, data.len() * 6) {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
let mut msg_out = Message::new();
|
let mut msg_out = Message::new();
|
||||||
@ -291,7 +311,6 @@ fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) {
|
|||||||
}
|
}
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
13
src/ui/cm.rs
13
src/ui/cm.rs
@ -416,6 +416,7 @@ async fn start_pa() {
|
|||||||
};
|
};
|
||||||
log::info!("pa monitor: {:?}", device);
|
log::info!("pa monitor: {:?}", device);
|
||||||
// systemctl --user status pulseaudio.service
|
// systemctl --user status pulseaudio.service
|
||||||
|
let mut buf: Vec<u8> = vec![0; 480 * 4];
|
||||||
match psimple::Simple::new(
|
match psimple::Simple::new(
|
||||||
None, // Use the default server
|
None, // Use the default server
|
||||||
APP_NAME, // Our application’s name
|
APP_NAME, // Our application’s name
|
||||||
@ -430,14 +431,10 @@ async fn start_pa() {
|
|||||||
if let Some(Err(_)) = stream.next_timeout2(1).await {
|
if let Some(Err(_)) = stream.next_timeout2(1).await {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let mut out: Vec<u8> = Vec::with_capacity(480 * 4);
|
if let Ok(_) = s.read(&mut buf) {
|
||||||
unsafe {
|
allow_err!(
|
||||||
out.set_len(out.capacity());
|
stream.send(&Data::RawMessage(buf.clone())).await
|
||||||
}
|
);
|
||||||
if let Ok(_) = s.read(&mut out) {
|
|
||||||
if out.iter().filter(|x| **x != 0).next().is_some() {
|
|
||||||
allow_err!(stream.send(&Data::RawMessage(out)).await);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user