Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(gpui): share desktop recording and notification sounds

Richie McIlroy60d3615db2e1
diff --git a/apps/desktop-gpui/Cargo.lock b/apps/desktop-gpui/Cargo.lock
index b045500db..cf9255478 100644
--- a/apps/desktop-gpui/Cargo.lock
+++ b/apps/desktop-gpui/Cargo.lock
@@ -5883,6 +5883,17 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
+[[package]]
+name = "lewton"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "777b48df9aaab155475a83a7df3070395ea1ac6902f5cd062b8f2b028075c030"
+dependencies = [
+ "byteorder",
+ "ogg",
+ "tinyvec",
+]
+
[[package]]
name = "libappindicator"
version = "0.9.0"
@@ -7390,6 +7401,15 @@ dependencies = [
"cc",
]
+[[package]]
+name = "ogg"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6951b4e8bf21c8193da321bcce9c9dd2e13c858fe078bf9054a288b419ae5d6e"
+dependencies = [
+ "byteorder",
+]
+
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -9080,6 +9100,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6006a627c1a38d37f3d3a85c6575418cfe34a5392d60a686d0071e1c8d427acb"
dependencies = [
"cpal 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lewton",
"symphonia",
"thiserror 1.0.69",
]
diff --git a/apps/desktop-gpui/Cargo.toml b/apps/desktop-gpui/Cargo.toml
index b2411ea5a..c90bcce4a 100644
--- a/apps/desktop-gpui/Cargo.toml
+++ b/apps/desktop-gpui/Cargo.toml
@@ -68,7 +68,7 @@ cap-project = { path = "../../crates/project" }
# a `RenderImage` (`src/editor_sidebar/cursor.rs`).
cap-cursor-info = { path = "../../crates/cursor-info" }
resvg = "0.45"
-rodio = { version = "0.19.0", default-features = false, features = ["mp3"] }
+rodio = { version = "0.19.0", default-features = false, features = ["mp3", "vorbis"] }
# The editor stack. `cap-rendering` is not a new subtree -- cap-recording
# already depends on it, so wgpu 25 and its vendored wgpu-hal have been built
# by this workspace since the recording unit; naming it here only makes the
diff --git a/apps/desktop-gpui/src/app_sounds.rs b/apps/desktop-gpui/src/app_sounds.rs
new file mode 100644
index 000000000..1604ab843
--- /dev/null
+++ b/apps/desktop-gpui/src/app_sounds.rs
@@ -0,0 +1,98 @@
+use std::io::Cursor;
+
+use anyhow::Context as _;
+use rodio::{Decoder, OutputStream, Sink};
+
+#[path = "../../desktop/src-tauri/src/recording_start_sound.rs"]
+mod recording_start_sound;
+
+pub use recording_start_sound::should_play_countdown_sound;
+
+pub async fn play_recording_start_sound() {
+ recording_start_sound::play(AppSound::StartRecording.bytes()).await;
+}
+
+#[derive(Clone, Copy, Debug)]
+pub enum AppSound {
+ StartRecording,
+ StopRecording,
+ Notification,
+}
+
+impl AppSound {
+ fn bytes(self) -> &'static [u8] {
+ match self {
+ Self::StartRecording => {
+ include_bytes!("../../desktop/src-tauri/sounds/start-recording.ogg")
+ }
+ Self::StopRecording => {
+ include_bytes!("../../desktop/src-tauri/sounds/stop-recording.ogg")
+ }
+ Self::Notification => include_bytes!("../../desktop/src-tauri/sounds/action.ogg"),
+ }
+ }
+
+ pub fn play(self) {
+ if let Err(error) = std::thread::Builder::new()
+ .name("app-sound".into())
+ .spawn(move || {
+ if let Err(error) = self.play_to_end() {
+ tracing::warn!(?self, %error, "App sound unavailable");
+ }
+ })
+ {
+ tracing::warn!(%error, "Could not start app sound playback");
+ }
+ }
+
+ fn play_to_end(self) -> anyhow::Result<()> {
+ let (_stream, handle) = OutputStream::try_default().context("opening audio output")?;
+ let source = Decoder::new(Cursor::new(self.bytes())).context("decoding app sound")?;
+ let sink = Sink::try_new(&handle).context("creating app sound sink")?;
+ sink.append(source);
+ sink.sleep_until_end();
+ tracing::debug!(?self, "App sound played");
+ Ok(())
+ }
+}
+
+pub fn play_notification() {
+ if crate::store::notification_sounds_enabled() {
+ AppSound::Notification.play();
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use rodio::Source as _;
+
+ const SOUNDS: [AppSound; 3] = [
+ AppSound::StartRecording,
+ AppSound::StopRecording,
+ AppSound::Notification,
+ ];
+
+ #[test]
+ fn tauri_sound_assets_decode_to_audible_pcm() {
+ for sound in SOUNDS {
+ let source = Decoder::new(Cursor::new(sound.bytes())).unwrap();
+ let channels = source.channels();
+ let rate = source.sample_rate();
+ assert!(channels > 0);
+ assert!(rate > 0);
+ let samples: Vec<_> = source.collect();
+ assert!(samples.iter().any(|sample| *sample != 0), "{sound:?}");
+ let duration = samples.len() as f64 / f64::from(channels) / f64::from(rate);
+ assert!((0.05..10.0).contains(&duration), "{sound:?}: {duration}");
+ }
+ }
+
+ #[test]
+ #[ignore = "plays the recording and notification cues through the default audio output"]
+ fn native_app_sound_playback() {
+ for sound in SOUNDS {
+ sound.play_to_end().unwrap();
+ }
+ }
+}
diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs
index 6771d8b12..9eb04f0e9 100644
--- a/apps/desktop-gpui/src/main.rs
+++ b/apps/desktop-gpui/src/main.rs
@@ -5,6 +5,7 @@
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
+mod app_sounds;
mod app_windows;
mod assets;
mod auth;
diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs
index bf3c9f13c..0d82334db 100644
--- a/apps/desktop-gpui/src/store.rs
+++ b/apps/desktop-gpui/src/store.rs
@@ -453,6 +453,17 @@ pub fn store_section(section: &str) -> Map<String, Value> {
.unwrap_or_default()
}
+pub fn notification_sounds_enabled() -> bool {
+ read_store(&tauri_store_path())
+ .and_then(|mut store| store.remove(GENERAL_SETTINGS))
+ .and_then(|settings| {
+ settings
+ .as_object()
+ .map(|settings| bool_at(settings, "enableNotifications", true))
+ })
+ .unwrap_or(false)
+}
+
/// Write one key of one section, preserving every other byte of meaning in
/// the file.
///
@@ -1854,6 +1865,31 @@ mod tests {
}
}
+ #[test]
+ fn notification_sounds_follow_tauri_settings_without_writing() {
+ for (contents, enabled) in [
+ (None, false),
+ (Some("{}"), false),
+ (Some("invalid json"), false),
+ (Some(r#"{"general_settings":{}}"#), true),
+ (
+ Some(r#"{"general_settings":{"enableNotifications":false}}"#),
+ false,
+ ),
+ (
+ Some(r#"{"general_settings":{"enableNotifications":true}}"#),
+ true,
+ ),
+ ] {
+ let store = TempStore::new("notification-sounds", contents);
+ assert_eq!(notification_sounds_enabled(), enabled);
+ assert_eq!(
+ std::fs::read_to_string(&store.path).ok().as_deref(),
+ contents
+ );
+ }
+ }
+
#[test]
fn device_format_preferences_use_tauri_keys_and_preserve_other_fields() {
use cap_recording::feeds::{