Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Phone as microphone: a volume slider
Bee519444e27b1a
diff --git a/desktop/app/build.rs b/desktop/app/build.rsindex abd5b50..31a5a51 100644--- a/desktop/app/build.rs+++ b/desktop/app/build.rs@@ -51,6 +51,7 @@ const QML: &[&str] = &["qml/components/Toast.qml","qml/components/Toggle.qml","qml/components/TransferItem.qml",+ "qml/components/ValueSlider.qml","qml/components/Txt.qml","qml/pages/AppsPage.qml","qml/pages/CallsPage.qml",diff --git a/desktop/app/qml/components/ValueSlider.qml b/desktop/app/qml/components/ValueSlider.qmlnew file mode 100644index 0000000..8cf32ec--- /dev/null+++ b/desktop/app/qml/components/ValueSlider.qml@@ -0,0 +1,107 @@+// SPDX-License-Identifier: GPL-3.0-or-later+import QtQuick+import app.nectarlink++// A slider for a whole number between `from` and `to`, in steps of `step`.+// It shows `value` and reports where the user moves it with `moved`; the+// owner sets `value` (so a drag that's refused springs back). Arrow keys+// step it; screen readers get its name and value.+FocusScope {+ id: slider+ property int from: 0+ property int to: 100+ property int step: 5+ property int value: 0+ property string label+ // How the value reads ("150%"), for the screen reader.+ property string valueText: String(value)+ signal moved(int value)++ readonly property real fraction: to > from ? (Math.max(from, Math.min(to, value)) - from) / (to - from) : 0++ implicitWidth: 200+ implicitHeight: 24+ activeFocusOnTab: enabled+ opacity: enabled ? 1 : 0.38++ Accessible.role: Accessible.Slider+ Accessible.name: label+ Accessible.description: valueText++ function stepped(next) {+ const clamped = Math.max(from, Math.min(to, Math.round(next / step) * step))+ if (clamped !== value)+ moved(clamped)+ }++ Keys.onPressed: (event) => {+ if (event.key === Qt.Key_Left || event.key === Qt.Key_Down) {+ stepped(value - step)+ event.accepted = true+ } else if (event.key === Qt.Key_Right || event.key === Qt.Key_Up) {+ stepped(value + step)+ event.accepted = true+ } else if (event.key === Qt.Key_Home) {+ stepped(from)+ event.accepted = true+ } else if (event.key === Qt.Key_End) {+ stepped(to)+ event.accepted = true+ }+ }++ Rectangle {+ id: track+ anchors.verticalCenter: parent.verticalCenter+ width: parent.width+ height: 6+ radius: 3+ color: Theme.surfaceContainerHighest+ border.width: Theme.graphite ? 1 : 0+ border.color: Theme.outlineVariant++ Rectangle {+ id: fill+ width: parent.width * slider.fraction+ height: parent.height+ radius: parent.radius+ color: Theme.graphite ? Theme.surfaceContent : Theme.primary+ Behavior on width {+ enabled: !Theme.reduceMotion && !drag.pressed+ NumberAnimation { duration: 120; easing.type: Easing.OutCubic }+ }+ }+ }++ Rectangle {+ id: handle+ x: Math.max(0, Math.min(slider.width - width, fill.width - width / 2))+ anchors.verticalCenter: parent.verticalCenter+ width: 16; height: 16; radius: 8+ color: Theme.graphite ? Theme.surfaceContent : Theme.primary+ border.width: Theme.focusVisible(slider) ? 2 : 0+ border.color: Theme.surface+ scale: drag.pressed ? 1.15 : 1.0+ Behavior on scale {+ enabled: !Theme.reduceMotion+ NumberAnimation { duration: 150; easing.type: Easing.OutCubic }+ }+ }++ MouseArea {+ id: drag+ anchors.fill: parent+ anchors.margins: -4+ enabled: slider.enabled+ cursorShape: Qt.PointingHandCursor+ function valueAt(x) {+ const f = Math.max(0, Math.min(1, (x - 4) / slider.width))+ return slider.from + f * (slider.to - slider.from)+ }+ onPressed: (mouse) => {+ slider.forceActiveFocus(Qt.MouseFocusReason)+ slider.stepped(valueAt(mouse.x))+ }+ onPositionChanged: (mouse) => { if (pressed) slider.stepped(valueAt(mouse.x)) }+ }+}diff --git a/desktop/app/qml/pages/SettingsPage.qml b/desktop/app/qml/pages/SettingsPage.qmlindex a6f299a..ac2de5d 100644--- a/desktop/app/qml/pages/SettingsPage.qml+++ b/desktop/app/qml/pages/SettingsPage.qml@@ -1625,6 +1625,23 @@ Item {}}+ ListRow {+ width: parent.width+ iconPath: Icons.speaker+ title: qsTr("Microphone volume")+ description: qsTr("%1%").arg(PhoneMic.gain)+ ValueSlider {+ width: 200+ from: PhoneMic.minGain+ to: PhoneMic.maxGain+ step: 10+ value: PhoneMic.gain+ label: qsTr("Microphone volume")+ valueText: qsTr("%1%").arg(PhoneMic.gain)+ onMoved: (v) => PhoneMic.setLoudness(v)+ }+ }+Divider { width: parent.width }// Row 2: Virtual camera add-ondiff --git a/desktop/app/src/bridge/phone_mic.rs b/desktop/app/src/bridge/phone_mic.rsindex 6498e27..a2d7448 100644--- a/desktop/app/src/bridge/phone_mic.rs+++ b/desktop/app/src/bridge/phone_mic.rs@@ -36,6 +36,10 @@ pub mod qobject {#[qproperty(QString, output_name, cxx_name = "outputName")]/// Whether `output` is a virtual audio cable (apps see it as a microphone).#[qproperty(bool, cable)]+ /// How loud it plays, in percent (`minGain` to `maxGain`).+ #[qproperty(i32, gain)]+ #[qproperty(i32, min_gain, cxx_name = "minGain")]+ #[qproperty(i32, max_gain, cxx_name = "maxGain")]type PhoneMic = super::PhoneMicRust;}@@ -50,6 +54,9 @@ pub mod qobject {#[qinvokable]#[cxx_name = "chooseOutput"]fn choose_output(self: &PhoneMic, id: &QString);+ #[qinvokable]+ #[cxx_name = "setLoudness"]+ fn set_loudness(self: &PhoneMic, percent: i32);/// Lists the outputs again (one may have been plugged in).#[qinvokable]fn refresh(self: Pin<&mut PhoneMic>);@@ -65,6 +72,9 @@ pub struct PhoneMicRust {output: QString,output_name: QString,cable: bool,+ gain: i32,+ min_gain: i32,+ max_gain: i32,}impl cxx_qt::Initialize for qobject::PhoneMic {@@ -99,6 +109,9 @@ impl qobject::PhoneMic {self.as_mut().set_output(QString::from(output.unwrap_or_default().as_str()));self.as_mut().set_output_name(QString::from(&name));self.as_mut().set_cable(cable);+ self.as_mut().set_gain(phone_mic::gain() as i32);+ self.as_mut().set_min_gain(phone_mic::MIN_GAIN as i32);+ self.as_mut().set_max_gain(phone_mic::MAX_GAIN as i32);}pub fn start(&self, device: &QString) {@@ -114,4 +127,8 @@ impl qobject::PhoneMic {pub fn choose_output(&self, id: &QString) {phone_mic::set_output(String::from(id));}++ pub fn set_loudness(&self, percent: i32) {+ phone_mic::set_gain(percent.max(0) as u32);+ }}diff --git a/desktop/app/src/bridge/prefs.rs b/desktop/app/src/bridge/prefs.rsindex 2afb498..82186b2 100644--- a/desktop/app/src/bridge/prefs.rs+++ b/desktop/app/src/bridge/prefs.rs@@ -901,6 +901,7 @@ impl qobject::Preferences {webcam_mirror: crate::webcam::mirror(),webcam_camera: crate::webcam::camera().to_owned(),phone_mic_output: crate::phone_mic::chosen_output(),+ phone_mic_gain: crate::phone_mic::gain(),timeline_retention_days: p.timeline_retention_days.max(0) as u32,continuity_photo_hotkey: String::from(&p.continuity_photo_hotkey),continuity_scan_hotkey: String::from(&p.continuity_scan_hotkey),diff --git a/desktop/app/src/phone_mic.rs b/desktop/app/src/phone_mic.rsindex f46c551..206386e 100644--- a/desktop/app/src/phone_mic.rs+++ b/desktop/app/src/phone_mic.rs@@ -6,7 +6,10 @@//! listening in. A virtual microphone of NectarLink's own needs an audio//! driver and is a later, separate step (GitHub #7).-use std::sync::{Arc, Mutex};+use std::sync::{+ Arc, Mutex,+ atomic::{AtomicU32, Ordering},+};use nectarlink_core::{AudioOutputDevice, DeviceId, Error, LinkState, MicSink, MirrorAudioConfig, NodeEvent};@@ -29,6 +32,13 @@ pub enum Phase {}static PHASE: Mutex<Phase> = Mutex::new(Phase::Off);+/// How loud the microphone plays, in percent.+static GAIN: AtomicU32 = AtomicU32::new(DEFAULT_GAIN);+/// Phones' microphones come in quieter than a headset's: a little louder+/// by default.+pub const DEFAULT_GAIN: u32 = 150;+pub const MIN_GAIN: u32 = 50;+pub const MAX_GAIN: u32 = 400;/// The output device chosen in Settings (an endpoint ID), if any.static OUTPUT: Mutex<Option<String>> = Mutex::new(None);@@ -49,6 +59,53 @@ pub fn phase() -> Phase {/// Reads the chosen output from the settings.pub fn init(settings: &crate::settings::Settings) {*OUTPUT.lock().unwrap_or_else(|e| e.into_inner()) = settings.phone_mic_output.clone();+ GAIN.store(settings.phone_mic_gain.clamp(MIN_GAIN, MAX_GAIN), Ordering::Relaxed);+}++/// How loud the microphone plays, in percent.+pub fn gain() -> u32 {+ GAIN.load(Ordering::Relaxed)+}++/// Plays the microphone this loud from now on (and the next time).+pub fn set_gain(percent: u32) {+ let percent = percent.clamp(MIN_GAIN, MAX_GAIN);+ if GAIN.swap(percent, Ordering::Relaxed) == percent {+ return;+ }+ save(|s| s.phone_mic_gain = percent);+ core_host::host().hub.changed(Changes::MIC);+}++fn save(update: impl FnOnce(&mut crate::settings::Settings)) {+ let dir = &core_host::host().data_dir;+ let mut settings = crate::settings::Settings::load(dir);+ update(&mut settings);+ if let Err(e) = settings.save(dir) {+ tracing::warn!(error = %e, "can't save the microphone's settings");+ }+}++/// 16-bit samples made `percent` louder, leveled off softly near full scale+/// so loud speech doesn't crackle.+pub(crate) fn amplify(pcm: &mut [u8], percent: u32) {+ if percent == 100 {+ return;+ }+ let gain = percent as f32 / 100.0;+ // Below this (a fraction of full scale) samples scale as they are.+ const KNEE: f32 = 0.7;+ for sample in pcm.as_chunks_mut::<2>().0 {+ let x = f32::from(i16::from_le_bytes(*sample)) / 32768.0 * gain;+ let magnitude = x.abs();+ let y = if magnitude <= KNEE {+ magnitude+ } else {+ KNEE + (1.0 - KNEE) * ((magnitude - KNEE) / (1.0 - KNEE)).tanh()+ };+ let out = (y.copysign(x) * 32767.0).round().clamp(-32768.0, 32767.0) as i16;+ *sample = out.to_le_bytes();+ }}/// The output chosen in Settings, if any (kept when other settings save).@@ -88,12 +145,7 @@ pub fn outputs() -> (Vec<AudioOutputDevice>, Option<String>) {/// Plays the microphone into this output from now on (and the next time).pub fn set_output(id: String) {*OUTPUT.lock().unwrap_or_else(|e| e.into_inner()) = Some(id.clone());- let dir = &core_host::host().data_dir;- let mut settings = crate::settings::Settings::load(dir);- settings.phone_mic_output = Some(id);- if let Err(e) = settings.save(dir) {- tracing::warn!(error = %e, "can't save the microphone's output");- }+ save(|s| s.phone_mic_output = Some(id));core_host::host().hub.changed(Changes::MIC);}@@ -180,8 +232,9 @@ impl MicSink for Sink {}}- fn audio(&self, _time_us: u64, data: Vec<u8>) {+ fn audio(&self, _time_us: u64, mut data: Vec<u8>) {if let Some(player) = self.player.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {+ amplify(&mut data, gain());player.push(data);}}@@ -216,6 +269,29 @@ mod tests {);}+ fn samples(values: &[i16]) -> Vec<u8> {+ values.iter().flat_map(|v| v.to_le_bytes()).collect()+ }++ fn read(pcm: &[u8]) -> Vec<i16> {+ pcm.as_chunks::<2>().0.iter().map(|b| i16::from_le_bytes(*b)).collect()+ }++ #[test]+ fn louder_doubles_quiet_sound_and_levels_off_loud_sound() {+ let mut pcm = samples(&[1000, -1000, 0, 30000, -32768]);+ amplify(&mut pcm, 200);+ let out = read(&pcm);+ assert_eq!(&out[..3], &[2000, -2000, 0], "quiet sound doubles exactly");+ // Loud sound stays inside full scale, still loud, sign kept.+ assert!(out[3] > 28000 && out[3] < 32767, "{}", out[3]);+ assert!(out[4] < -28000 && out[4] > -32768, "{}", out[4]);+ // 100% leaves it as it is.+ let mut same = samples(&[123, -4567]);+ amplify(&mut same, 100);+ assert_eq!(read(&same), [123, -4567]);+ }+#[test]fn without_a_cable_it_is_the_default_output() {assert_eq!(preferred(&[device("spk", "Speakers", true)], None), None);diff --git a/desktop/app/src/settings.rs b/desktop/app/src/settings.rsindex 3b9f858..1999b23 100644--- a/desktop/app/src/settings.rs+++ b/desktop/app/src/settings.rs@@ -335,6 +335,8 @@ pub struct Settings {/// virtual audio cable if there is one, else the default output).#[serde(skip_serializing_if = "Option::is_none")]pub phone_mic_output: Option<String>,+ /// How loud a phone's microphone plays, in percent (50 to 400).+ pub phone_mic_gain: u32,/// Which phone camera lens to stream (`"back"` or `"front"`).#[serde(default = "default_webcam_camera", alias = "webcam_camera")]pub webcam_camera: String,@@ -451,6 +453,7 @@ impl Default for Settings {webcam_mirror: false,webcam_camera: default_webcam_camera(),phone_mic_output: None,+ phone_mic_gain: crate::phone_mic::DEFAULT_GAIN,timeline_retention_days: 90,continuity_photo_hotkey: "Ctrl+Alt+C".into(),continuity_scan_hotkey: "Ctrl+Alt+D".into(),@@ -603,6 +606,7 @@ mod tests {webcam_mirror: true,webcam_camera: "front".into(),phone_mic_output: Some("{0.0.0.00000000}.{cable}".into()),+ phone_mic_gain: 250,timeline_retention_days: 30,continuity_photo_hotkey: "Ctrl+Alt+P".into(),continuity_scan_hotkey: "Ctrl+Alt+S".into(),diff --git a/desktop/app/tests/qml/tst_valueslider.qml b/desktop/app/tests/qml/tst_valueslider.qmlnew file mode 100644index 0000000..2e6f006--- /dev/null+++ b/desktop/app/tests/qml/tst_valueslider.qml@@ -0,0 +1,59 @@+// SPDX-License-Identifier: GPL-3.0-or-later+import QtQuick+import QtTest+import app.nectarlink++// A ValueSlider reports steps within its range, from the keyboard and a+// click, and leaves its value to its owner.+Item {+ id: root+ width: 300+ height: 60++ ValueSlider {+ id: slider+ x: 10+ y: 20+ width: 200+ from: 50+ to: 400+ step: 10+ value: 100+ label: "Microphone volume"+ }++ SignalSpy {+ id: moved+ target: slider+ signalName: "moved"+ }++ TestCase {+ name: "ValueSlider"+ when: windowShown++ function init() {+ slider.value = 100+ moved.clear()+ slider.forceActiveFocus()+ }++ function test_arrow_keys_step_and_stop_at_the_ends() {+ keyClick(Qt.Key_Right)+ compare(moved.signalArguments[0][0], 110)+ compare(slider.value, 100, "the owner sets the value")+ slider.value = 400+ moved.clear()+ keyClick(Qt.Key_Right)+ compare(moved.count, 0, "nothing past the end")+ keyClick(Qt.Key_Home)+ compare(moved.signalArguments[0][0], 50)+ }++ function test_a_click_picks_the_value_there() {+ // Three quarters along 50..400 is 312.5, a step of 10 makes 310.+ mouseClick(slider, slider.width * 0.75, slider.height / 2)+ compare(moved.signalArguments[0][0], 310)+ }+ }+}