Skip to content

Commit 97071b2

Browse files
authored
Merge pull request #93 from wiiznokes/amplify
add an amplify option
2 parents ee3eed9 + 2a962d0 commit 97071b2

File tree

8 files changed

+98
-7
lines changed

8 files changed

+98
-7
lines changed

RustApp/i18n/en/android_mic.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ denoise = Use audio noise reduction
1616
start_at_login = Start at login
1717
auto_connect = Auto connect
1818
theme = Theme
19+
amplify = Amplify
1920
2021
about = About
2122
repository = Repository

RustApp/justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ prettier:
5959

6060
metainfo-check:
6161
appstreamcli validate --pedantic --explain --strict res/linux/metainfo.xml
62+
63+
install_nsis: build-release nsis
64+
Start-Process (Get-ChildItem .\target\release\android-mic_*_x64-setup.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
65+

RustApp/src/audio/process.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{
1010
pub struct AudioProcessParams {
1111
pub target_format: AudioPacketFormat,
1212
pub denoise: bool,
13+
pub amplify: Option<f32>,
1314
}
1415

1516
// This function converts an audio stream from packet into producer
@@ -63,7 +64,7 @@ where
6364
const DENOISE_SAMPLE_RATE: u32 = 48000;
6465

6566
// next run resampler and denoise on the buffer
66-
let resampled_buffer = if config.denoise {
67+
let mut resampled_buffer = if config.denoise {
6768
let prepared_buffer = if packet.sample_rate == DENOISE_SAMPLE_RATE {
6869
buffer
6970
} else {
@@ -88,6 +89,14 @@ where
8889
resample_f32_stream(&buffer, packet.sample_rate, format.sample_rate.to_number())?
8990
};
9091

92+
if let Some(amplify) = config.amplify {
93+
for channel in &mut resampled_buffer {
94+
for v in channel {
95+
*v *= amplify;
96+
}
97+
}
98+
}
99+
91100
// finally convert to output format
92101
let num_channels = format.channel_count.to_number() as usize;
93102
let total_bytes: usize = resampled_buffer[0].len() * num_channels * std::mem::size_of::<F>();

RustApp/src/config.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66

77
use crate::fl;
88

9-
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9+
#[derive(Debug, Clone, Serialize, Deserialize)]
1010
#[serde(default)]
1111
pub struct Config {
1212
pub connection_mode: ConnectionMode,
@@ -22,6 +22,43 @@ pub struct Config {
2222
pub auto_connect: bool,
2323
pub denoise: bool,
2424
pub theme: AppTheme,
25+
pub amplify: bool,
26+
pub amplify_value: f32,
27+
}
28+
29+
impl Default for Config {
30+
fn default() -> Self {
31+
Self {
32+
connection_mode: Default::default(),
33+
ip: Default::default(),
34+
audio_format: Default::default(),
35+
channel_count: Default::default(),
36+
sample_rate: Default::default(),
37+
device_name: Default::default(),
38+
start_at_login: Default::default(),
39+
auto_connect: Default::default(),
40+
denoise: Default::default(),
41+
theme: Default::default(),
42+
amplify: Default::default(),
43+
amplify_value: 2.0,
44+
}
45+
}
46+
}
47+
48+
pub struct ConfigCache {
49+
pub amplify_value: String,
50+
}
51+
52+
impl ConfigCache {
53+
pub fn new(config: &Config) -> Self {
54+
Self {
55+
amplify_value: config.amplify_value.to_string(),
56+
}
57+
}
58+
59+
pub fn parse_amplify_value(&self) -> Option<f32> {
60+
self.amplify_value.replace(',', ".").parse().ok()
61+
}
2562
}
2663

2764
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Values)]

RustApp/src/streamer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct StreamConfig {
3939
pub buff: Producer<u8>,
4040
pub audio_config: AudioPacketFormat,
4141
pub denoise: bool,
42+
pub amplify: Option<f32>,
4243
}
4344

4445
impl Debug for StreamConfig {
@@ -55,6 +56,7 @@ impl StreamConfig {
5556
AudioProcessParams {
5657
target_format: self.audio_config.clone(),
5758
denoise: self.denoise,
59+
amplify: self.amplify,
5860
}
5961
}
6062
}

RustApp/src/ui/app.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use super::{
2626
};
2727
use crate::{
2828
audio::AudioPacketFormat,
29-
config::{AppTheme, AudioFormat, ChannelCount, Config, ConnectionMode, SampleRate},
29+
config::{
30+
AppTheme, AudioFormat, ChannelCount, Config, ConfigCache, ConnectionMode, SampleRate,
31+
},
3032
fl,
3133
streamer::{self, ConnectOption, StreamConfig, StreamerCommand, StreamerMsg},
3234
ui::view::{SCROLLABLE_ID, about_window},
@@ -115,6 +117,7 @@ pub struct AppState {
115117
core: Core,
116118
pub streamer: Option<Sender<StreamerCommand>>,
117119
pub config: ConfigManager<Config>,
120+
pub config_cache: ConfigCache,
118121
pub audio_host: Host,
119122
pub audio_devices: Vec<AudioDevice>,
120123
pub audio_device: Option<cpal::Device>,
@@ -142,13 +145,15 @@ impl AppState {
142145
return Task::none();
143146
}
144147
let (producer, consumer) = RingBuffer::<u8>::new(self.get_shared_buf_size());
148+
let config = self.config.data().clone();
145149

146150
match self.start_audio_stream(consumer) {
147151
Ok(audio_config) => {
148152
self.send_command(StreamerCommand::ReconfigureStream(StreamConfig {
149153
buff: producer,
150154
audio_config,
151-
denoise: self.config.data().denoise,
155+
denoise: config.denoise,
156+
amplify: config.amplify.then_some(config.amplify_value),
152157
}));
153158
Task::none()
154159
}
@@ -209,7 +214,8 @@ impl AppState {
209214
StreamConfig {
210215
buff: producer,
211216
audio_config,
212-
denoise: self.config.data().denoise,
217+
denoise: config.denoise,
218+
amplify: config.amplify.then_some(config.amplify_value),
213219
},
214220
));
215221

@@ -294,6 +300,7 @@ impl Application for AppState {
294300
core,
295301
audio_stream: None,
296302
streamer: None,
303+
config_cache: ConfigCache::new(flags.config.data()),
297304
config: flags.config,
298305
audio_device,
299306
audio_host,
@@ -455,7 +462,6 @@ impl Application for AppState {
455462
}
456463
ConfigMsg::DeNoise(denoise) => {
457464
self.config.update(|c| c.denoise = denoise);
458-
info!("set denoise: {denoise}");
459465
return self.update_audio_stream();
460466
}
461467
ConfigMsg::Theme(app_theme) => {
@@ -488,6 +494,18 @@ impl Application for AppState {
488494
.chain(set_window_title);
489495
}
490496
},
497+
ConfigMsg::Amplify(amplify) => {
498+
self.config.update(|c| c.amplify = amplify);
499+
return self.update_audio_stream();
500+
}
501+
ConfigMsg::AmplifyValue(amplify_value) => {
502+
self.config_cache.amplify_value = amplify_value;
503+
504+
if let Some(value) = self.config_cache.parse_amplify_value() {
505+
self.config.update(|c| c.amplify_value = value);
506+
return self.update_audio_stream();
507+
}
508+
}
491509
},
492510
AppMsg::Shutdown => {
493511
return cosmic::iced_runtime::task::effect(Action::Exit);

RustApp/src/ui/message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum ConfigMsg {
2929
AutoConnect(bool),
3030
DeNoise(bool),
3131
Theme(AppTheme),
32+
Amplify(bool),
33+
AmplifyValue(String),
3234
ToggleAboutWindow,
3335
}
3436

RustApp/src/ui/view.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cosmic::{
55
iced::{Length, alignment::Horizontal, widget::pick_list},
66
widget::{
77
self, about::About, button, canvas, column, container, context_menu, markdown, menu, radio,
8-
row, scrollable, settings, text, toggler, vertical_space,
8+
row, scrollable, settings, text, text_input, toggler, vertical_space,
99
},
1010
};
1111
use cpal::traits::DeviceTrait;
@@ -208,6 +208,24 @@ pub fn settings_window(app: &AppState) -> Element<'_, ConfigMsg> {
208208
.title(fl!("denoise"))
209209
.add(toggler(config.denoise).on_toggle(ConfigMsg::DeNoise)),
210210
)
211+
.push(
212+
settings::section()
213+
.title(fl!("amplify"))
214+
.add(toggler(config.amplify).on_toggle(ConfigMsg::Amplify))
215+
.add({
216+
let mut text = text_input("", &app.config_cache.amplify_value);
217+
218+
if config.amplify {
219+
text = text.on_input(ConfigMsg::AmplifyValue)
220+
}
221+
222+
if app.config_cache.parse_amplify_value().is_none() {
223+
text = text.error("")
224+
}
225+
226+
text
227+
}),
228+
)
211229
.push(button::text("Use Recommended Format").on_press(ConfigMsg::UseRecommendedFormat))
212230
.push_maybe(if cfg!(target_os = "windows") {
213231
Some(

0 commit comments

Comments
 (0)