audio.rs

  1use anyhow::{Context as _, Result};
  2use collections::HashMap;
  3use gpui::{App, BackgroundExecutor, BorrowAppContext, Global};
  4
  5#[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
  6mod non_windows_and_freebsd_deps {
  7    pub(super) use gpui::AsyncApp;
  8    pub(super) use libwebrtc::native::apm;
  9    pub(super) use log::info;
 10    pub(super) use parking_lot::Mutex;
 11    pub(super) use rodio::cpal::Sample;
 12    pub(super) use rodio::source::{LimitSettings, UniformSourceIterator};
 13    pub(super) use std::sync::Arc;
 14}
 15
 16#[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
 17use non_windows_and_freebsd_deps::*;
 18
 19use rodio::{
 20    Decoder, OutputStream, OutputStreamBuilder, Source, mixer::Mixer, nz, source::Buffered,
 21};
 22use settings::Settings;
 23use std::{io::Cursor, num::NonZero, path::PathBuf, sync::atomic::Ordering, time::Duration};
 24use util::ResultExt;
 25
 26mod audio_settings;
 27mod replays;
 28mod rodio_ext;
 29pub use audio_settings::AudioSettings;
 30pub use rodio_ext::RodioExt;
 31
 32use crate::audio_settings::LIVE_SETTINGS;
 33
 34// NOTE: We used to use WebRTC's mixer which only supported
 35// 16kHz, 32kHz and 48kHz. As 48 is the most common "next step up"
 36// for audio output devices like speakers/bluetooth, we just hard-code
 37// this; and downsample when we need to.
 38//
 39// Since most noise cancelling requires 16kHz we will move to
 40// that in the future.
 41pub const SAMPLE_RATE: NonZero<u32> = nz!(48000);
 42pub const CHANNEL_COUNT: NonZero<u16> = nz!(2);
 43pub const BUFFER_SIZE: usize = // echo canceller and livekit want 10ms of audio
 44    (SAMPLE_RATE.get() as usize / 100) * CHANNEL_COUNT.get() as usize;
 45
 46pub const REPLAY_DURATION: Duration = Duration::from_secs(30);
 47
 48pub fn init(cx: &mut App) {
 49    AudioSettings::register(cx);
 50    LIVE_SETTINGS.initialize(cx);
 51}
 52
 53#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
 54pub enum Sound {
 55    Joined,
 56    Leave,
 57    Mute,
 58    Unmute,
 59    StartScreenshare,
 60    StopScreenshare,
 61    AgentDone,
 62}
 63
 64impl Sound {
 65    fn file(&self) -> &'static str {
 66        match self {
 67            Self::Joined => "joined_call",
 68            Self::Leave => "leave_call",
 69            Self::Mute => "mute",
 70            Self::Unmute => "unmute",
 71            Self::StartScreenshare => "start_screenshare",
 72            Self::StopScreenshare => "stop_screenshare",
 73            Self::AgentDone => "agent_done",
 74        }
 75    }
 76}
 77
 78pub struct Audio {
 79    output_handle: Option<OutputStream>,
 80    output_mixer: Option<Mixer>,
 81    #[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
 82    pub echo_canceller: Arc<Mutex<apm::AudioProcessingModule>>,
 83    source_cache: HashMap<Sound, Buffered<Decoder<Cursor<Vec<u8>>>>>,
 84    replays: replays::Replays,
 85}
 86
 87impl Default for Audio {
 88    fn default() -> Self {
 89        Self {
 90            output_handle: Default::default(),
 91            output_mixer: Default::default(),
 92            #[cfg(not(any(
 93                all(target_os = "windows", target_env = "gnu"),
 94                target_os = "freebsd"
 95            )))]
 96            echo_canceller: Arc::new(Mutex::new(apm::AudioProcessingModule::new(
 97                true, false, false, false,
 98            ))),
 99            source_cache: Default::default(),
100            replays: Default::default(),
101        }
102    }
103}
104
105impl Global for Audio {}
106
107impl Audio {
108    fn ensure_output_exists(&mut self) -> Result<&Mixer> {
109        if self.output_handle.is_none() {
110            self.output_handle = Some(
111                OutputStreamBuilder::open_default_stream()
112                    .context("Could not open default output stream")?,
113            );
114            if let Some(output_handle) = &self.output_handle {
115                let (mixer, source) = rodio::mixer::mixer(CHANNEL_COUNT, SAMPLE_RATE);
116                // or the mixer will end immediately as its empty.
117                mixer.add(rodio::source::Zero::new(CHANNEL_COUNT, SAMPLE_RATE));
118                self.output_mixer = Some(mixer);
119
120                // The webrtc apm is not yet compiling for windows & freebsd
121                #[cfg(not(any(
122                    any(all(target_os = "windows", target_env = "gnu")),
123                    target_os = "freebsd"
124                )))]
125                let echo_canceller = Arc::clone(&self.echo_canceller);
126                #[cfg(not(any(
127                    any(all(target_os = "windows", target_env = "gnu")),
128                    target_os = "freebsd"
129                )))]
130                let source = source.inspect_buffer::<BUFFER_SIZE, _>(move |buffer| {
131                    let mut buf: [i16; _] = buffer.map(|s| s.to_sample());
132                    echo_canceller
133                        .lock()
134                        .process_reverse_stream(
135                            &mut buf,
136                            SAMPLE_RATE.get() as i32,
137                            CHANNEL_COUNT.get().into(),
138                        )
139                        .expect("Audio input and output threads should not panic");
140                });
141                output_handle.mixer().add(source);
142            }
143        }
144
145        Ok(self
146            .output_mixer
147            .as_ref()
148            .expect("we only get here if opening the outputstream succeeded"))
149    }
150
151    pub fn save_replays(
152        &self,
153        executor: BackgroundExecutor,
154    ) -> gpui::Task<anyhow::Result<(PathBuf, Duration)>> {
155        self.replays.replays_to_tar(executor)
156    }
157
158    #[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
159    pub fn open_microphone(voip_parts: VoipParts) -> anyhow::Result<impl Source> {
160        let stream = rodio::microphone::MicrophoneBuilder::new()
161            .default_device()?
162            .default_config()?
163            .prefer_sample_rates([SAMPLE_RATE, SAMPLE_RATE.saturating_mul(nz!(2))])
164            // .prefer_channel_counts([nz!(1), nz!(2)])
165            .prefer_buffer_sizes(512..)
166            .open_stream()?;
167        info!("Opened microphone: {:?}", stream.config());
168
169        let (replay, stream) = UniformSourceIterator::new(stream, CHANNEL_COUNT, SAMPLE_RATE)
170            .limit(LimitSettings::live_performance())
171            .process_buffer::<BUFFER_SIZE, _>(move |buffer| {
172                let mut int_buffer: [i16; _] = buffer.map(|s| s.to_sample());
173                if voip_parts
174                    .echo_canceller
175                    .lock()
176                    .process_stream(
177                        &mut int_buffer,
178                        SAMPLE_RATE.get() as i32,
179                        CHANNEL_COUNT.get() as i32,
180                    )
181                    .context("livekit audio processor error")
182                    .log_err()
183                    .is_some()
184                {
185                    for (sample, processed) in buffer.iter_mut().zip(&int_buffer) {
186                        *sample = (*processed).to_sample();
187                    }
188                }
189            })
190            .automatic_gain_control(1.0, 4.0, 0.0, 5.0)
191            .periodic_access(Duration::from_millis(100), move |agc_source| {
192                agc_source.set_enabled(LIVE_SETTINGS.control_input_volume.load(Ordering::Relaxed));
193            })
194            .replayable(REPLAY_DURATION)?;
195
196        voip_parts
197            .replays
198            .add_voip_stream("local microphone".to_string(), replay);
199        Ok(stream)
200    }
201
202    pub fn play_voip_stream(
203        source: impl rodio::Source + Send + 'static,
204        speaker_name: String,
205        is_staff: bool,
206        cx: &mut App,
207    ) -> anyhow::Result<()> {
208        let (replay_source, source) = source
209            .automatic_gain_control(1.0, 4.0, 0.0, 5.0)
210            .periodic_access(Duration::from_millis(100), move |agc_source| {
211                agc_source.set_enabled(LIVE_SETTINGS.control_input_volume.load(Ordering::Relaxed));
212            })
213            .replayable(REPLAY_DURATION)
214            .expect("REPLAY_DURATION is longer than 100ms");
215
216        cx.update_default_global(|this: &mut Self, _cx| {
217            let output_mixer = this
218                .ensure_output_exists()
219                .context("Could not get output mixer")?;
220            output_mixer.add(source);
221            if is_staff {
222                this.replays.add_voip_stream(speaker_name, replay_source);
223            }
224            Ok(())
225        })
226    }
227
228    pub fn play_sound(sound: Sound, cx: &mut App) {
229        cx.update_default_global(|this: &mut Self, cx| {
230            let source = this.sound_source(sound, cx).log_err()?;
231            let output_mixer = this
232                .ensure_output_exists()
233                .context("Could not get output mixer")
234                .log_err()?;
235
236            output_mixer.add(source);
237            Some(())
238        });
239    }
240
241    pub fn end_call(cx: &mut App) {
242        cx.update_default_global(|this: &mut Self, _cx| {
243            this.output_handle.take();
244        });
245    }
246
247    fn sound_source(&mut self, sound: Sound, cx: &App) -> Result<impl Source + use<>> {
248        if let Some(wav) = self.source_cache.get(&sound) {
249            return Ok(wav.clone());
250        }
251
252        let path = format!("sounds/{}.wav", sound.file());
253        let bytes = cx
254            .asset_source()
255            .load(&path)?
256            .map(anyhow::Ok)
257            .with_context(|| format!("No asset available for path {path}"))??
258            .into_owned();
259        let cursor = Cursor::new(bytes);
260        let source = Decoder::new(cursor)?.buffered();
261
262        self.source_cache.insert(sound, source.clone());
263
264        Ok(source)
265    }
266}
267
268#[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
269pub struct VoipParts {
270    echo_canceller: Arc<Mutex<apm::AudioProcessingModule>>,
271    replays: replays::Replays,
272}
273
274#[cfg(not(any(all(target_os = "windows", target_env = "gnu"), target_os = "freebsd")))]
275impl VoipParts {
276    pub fn new(cx: &AsyncApp) -> anyhow::Result<Self> {
277        let (apm, replays) = cx.try_read_default_global::<Audio, _>(|audio, _| {
278            (Arc::clone(&audio.echo_canceller), audio.replays.clone())
279        })?;
280
281        Ok(Self {
282            echo_canceller: apm,
283            replays,
284        })
285    }
286}