forked from Zackriya-Solutions/meetily
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.rs
More file actions
1111 lines (974 loc) · 50.2 KB
/
Copy pathpipeline.rs
File metadata and controls
1111 lines (974 loc) · 50.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::sync::Arc;
use std::collections::VecDeque;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use anyhow::Result;
use log::{debug, error, info, warn};
use crate::batch_audio_metric;
use super::batch_processor::AudioMetricsBatcher;
use rubato::{Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction};
use super::devices::AudioDevice;
use super::recording_state::{AudioChunk, AudioError, RecordingState, DeviceType};
use super::audio_processing::{audio_to_mono, LoudnessNormalizer, NoiseSuppressionProcessor, HighPassFilter};
use super::vad::{ContinuousVadProcessor};
/// Ring buffer for synchronized audio mixing
/// Accumulates samples from mic and system streams until we have aligned windows
struct AudioMixerRingBuffer {
mic_buffer: VecDeque<f32>,
system_buffer: VecDeque<f32>,
window_size_samples: usize, // Fixed mixing window (e.g., 50ms)
max_buffer_size: usize, // Safety limit (e.g., 100ms)
}
impl AudioMixerRingBuffer {
fn new(sample_rate: u32) -> Self {
// Use 50ms windows for mixing
let window_ms = 600.0;
let window_size_samples = (sample_rate as f32 * window_ms / 1000.0) as usize;
// CRITICAL FIX: Increase max buffer to 400ms for system audio stability
// System audio (especially Core Audio on macOS) can have significant jitter
// due to sample-by-sample streaming → batching → channel transmission
// Accounts for: RNNoise buffering + Core Audio jitter + processing delays
let max_buffer_size = window_size_samples * 8; // 400ms (was 200ms)
info!("🔊 Ring buffer initialized: window={}ms ({} samples), max={}ms ({} samples)",
window_ms, window_size_samples,
window_ms * 8.0, max_buffer_size);
Self {
mic_buffer: VecDeque::with_capacity(max_buffer_size),
system_buffer: VecDeque::with_capacity(max_buffer_size),
window_size_samples,
max_buffer_size,
}
}
fn add_samples(&mut self, device_type: DeviceType, samples: Vec<f32>) {
// Log buffer health periodically for diagnostics
static mut SAMPLE_COUNTER: u64 = 0;
unsafe {
SAMPLE_COUNTER += 1;
if SAMPLE_COUNTER % 200 == 0 {
debug!("📊 Ring buffer status: mic={} samples, sys={} samples (max={})",
self.mic_buffer.len(), self.system_buffer.len(), self.max_buffer_size);
}
}
match device_type {
DeviceType::Microphone => self.mic_buffer.extend(samples),
DeviceType::System => self.system_buffer.extend(samples),
}
// CRITICAL FIX: Add warnings before dropping samples
// This helps diagnose timing issues in production
if self.mic_buffer.len() > self.max_buffer_size {
warn!("⚠️ Microphone buffer overflow: {} > {} samples, dropping oldest {} samples",
self.mic_buffer.len(), self.max_buffer_size,
self.mic_buffer.len() - self.max_buffer_size);
}
if self.system_buffer.len() > self.max_buffer_size {
error!("🔴 SYSTEM AUDIO BUFFER OVERFLOW: {} > {} samples, dropping {} samples - THIS CAUSES DISTORTION!",
self.system_buffer.len(), self.max_buffer_size,
self.system_buffer.len() - self.max_buffer_size);
}
// Safety: prevent buffer overflow (keep only last 200ms)
while self.mic_buffer.len() > self.max_buffer_size {
self.mic_buffer.pop_front();
}
while self.system_buffer.len() > self.max_buffer_size {
self.system_buffer.pop_front();
}
}
fn can_mix(&self) -> bool {
self.mic_buffer.len() >= self.window_size_samples ||
self.system_buffer.len() >= self.window_size_samples
}
fn extract_window(&mut self) -> Option<(Vec<f32>, Vec<f32>)> {
if !self.can_mix() {
return None;
}
// Extract mic window with zero-padding for incomplete buffers
// Zero-padding (silence) is preferred over last-sample-hold to prevent artifacts
// Extract mic window (or pad with zeros if insufficient data)
let mic_window = if self.mic_buffer.len() >= self.window_size_samples {
// Enough mic data - drain window
self.mic_buffer.drain(0..self.window_size_samples).collect()
} else if !self.mic_buffer.is_empty() {
// Some mic data but not enough - consume all + pad with zeros
let available: Vec<f32> = self.mic_buffer.drain(..).collect();
let mut padded = Vec::with_capacity(self.window_size_samples);
padded.extend_from_slice(&available);
// Use zero-padding (silence) to prevent repetition artifacts
// Zero-padding is inaudible at 48kHz sample rate
padded.resize(self.window_size_samples, 0.0);
padded
} else {
// No mic data - return silence
vec![0.0; self.window_size_samples]
};
// Extract system window (or pad with zeros if insufficient data)
let sys_window = if self.system_buffer.len() >= self.window_size_samples {
// Enough system data - drain window
self.system_buffer.drain(0..self.window_size_samples).collect()
} else if !self.system_buffer.is_empty() {
// Some system data but not enough - consume all + pad with zeros
let available: Vec<f32> = self.system_buffer.drain(..).collect();
let mut padded = Vec::with_capacity(self.window_size_samples);
padded.extend_from_slice(&available);
// Use zero-padding (silence) to prevent repetition artifacts
// Zero-padding is inaudible at 48kHz sample rate
padded.resize(self.window_size_samples, 0.0);
padded
} else {
// No system data - return silence
vec![0.0; self.window_size_samples]
};
Some((mic_window, sys_window))
}
}
/// Simple audio mixer without aggressive ducking
/// Combines mic + system audio with basic clipping prevention
struct ProfessionalAudioMixer;
impl ProfessionalAudioMixer {
fn new(_sample_rate: u32) -> Self {
Self
}
fn mix_window(&mut self, mic_window: &[f32], sys_window: &[f32]) -> Vec<f32> {
// Handle different lengths (already padded by extract_window, but defensive)
let max_len = mic_window.len().max(sys_window.len());
let mut mixed = Vec::with_capacity(max_len);
// Professional mixing with soft scaling to prevent distortion
// Uses proportional scaling instead of hard clamping to avoid artifacts
for i in 0..max_len {
let mic = mic_window.get(i).copied().unwrap_or(0.0);
let sys = sys_window.get(i).copied().unwrap_or(0.0);
// Pre-scale system audio to 70% to leave headroom
// This prevents constant soft scaling which can cause pumping artifacts
// Mic is normalized to -23 LUFS (already optimal), system needs reduction
let sys_scaled = sys * 1.0;
let _mic_scaled = mic * 0.8; // Reserved for future mic scaling
// Sum without ducking - mic stays at full volume, system slightly reduced
let sum = mic + sys_scaled;
// CRITICAL FIX: Soft scaling prevents distortion artifacts
// If the sum would exceed ±1.0, scale down PROPORTIONALLY
// This avoids hard clipping distortion that sounds like "radio breaks"
let sum_abs = sum.abs();
let mixed_sample = if sum_abs > 1.0 {
// Scale down to fit within ±1.0
sum / sum_abs
} else {
sum
};
mixed.push(mixed_sample);
}
mixed
}
}
/// Simplified audio capture without broadcast channels
#[derive(Clone)]
pub struct AudioCapture {
device: Arc<AudioDevice>,
state: Arc<RecordingState>,
sample_rate: u32, // Original device sample rate
channels: u16,
chunk_counter: Arc<std::sync::atomic::AtomicU64>,
device_type: DeviceType,
recording_sender: Option<mpsc::UnboundedSender<AudioChunk>>,
needs_resampling: bool, // Flag if resampling is required
// CRITICAL FIX: Persistent resampler to preserve energy across chunks
resampler: Arc<std::sync::Mutex<Option<SincFixedIn<f32>>>>,
// Buffering for variable-size chunks → fixed-size resampler input
resampler_input_buffer: Arc<std::sync::Mutex<Vec<f32>>>,
resampler_chunk_size: usize, // Fixed chunk size for resampler (512 samples)
// Audio enhancement processors (microphone only)
noise_suppressor: Arc<std::sync::Mutex<Option<NoiseSuppressionProcessor>>>,
high_pass_filter: Arc<std::sync::Mutex<Option<HighPassFilter>>>,
// EBU R128 normalizer for microphone audio (per-device, stateful)
normalizer: Arc<std::sync::Mutex<Option<LoudnessNormalizer>>>,
// Note: Using global recording timestamp for synchronization
}
impl AudioCapture {
pub fn new(
device: Arc<AudioDevice>,
state: Arc<RecordingState>,
sample_rate: u32,
channels: u16,
device_type: DeviceType,
recording_sender: Option<mpsc::UnboundedSender<AudioChunk>>,
) -> Self {
// CRITICAL FIX: Detect if resampling is needed
// Pipeline expects 48kHz, but Bluetooth devices often report 8kHz, 16kHz, or 44.1kHz
const TARGET_SAMPLE_RATE: u32 = 48000;
let needs_resampling = sample_rate != TARGET_SAMPLE_RATE;
// Detect device kind (Bluetooth vs Wired) for adaptive processing
// Use reasonable defaults for buffer size (512 samples is typical)
let device_kind = super::device_detection::InputDeviceKind::detect(&device.name, 512, sample_rate);
if needs_resampling {
warn!(
"⚠️ SAMPLE RATE MISMATCH DETECTED ⚠️"
);
warn!(
"🔄 [{:?}] Audio device '{}' ({:?}) reports {} Hz (pipeline expects {} Hz)",
device_type, device.name, device_kind, sample_rate, TARGET_SAMPLE_RATE
);
warn!(
"🔄 Automatic resampling will be applied: {} Hz → {} Hz",
sample_rate, TARGET_SAMPLE_RATE
);
// Log which resampling strategy will be used
let ratio = TARGET_SAMPLE_RATE as f64 / sample_rate as f64;
let strategy = if ratio >= 2.0 {
"High-quality upsampling (sinc_len=512, Cubic interpolation)"
} else if ratio >= 1.5 {
"Moderate upsampling (sinc_len=384, Cubic)"
} else if ratio > 1.0 {
"Small upsampling (sinc_len=256, Linear)"
} else if ratio <= 0.5 {
"Anti-aliased downsampling (sinc_len=512, Cubic)"
} else {
"Moderate downsampling (sinc_len=384, Linear)"
};
info!(" Resampling strategy: {}", strategy);
} else {
info!(
"✅ [{:?}] Audio device '{}' ({:?}) uses {} Hz (matches pipeline)",
device_type, device.name, device_kind, sample_rate
);
}
// Initialize audio enhancement processors for MICROPHONE ONLY
// System audio doesn't need enhancement (already clean)
let (noise_suppressor, high_pass_filter, normalizer) = if matches!(device_type, DeviceType::Microphone) {
// Initialize noise suppression (RNNoise) at 48kHz - CONDITIONAL based on flag
let ns = if super::ffmpeg_mixer::RNNOISE_APPLY_ENABLED {
match NoiseSuppressionProcessor::new(TARGET_SAMPLE_RATE) {
Ok(processor) => {
info!("✅ RNNoise noise suppression ENABLED for microphone '{}' (10-15 dB reduction)", device.name);
Some(processor)
}
Err(e) => {
warn!("⚠️ Failed to create noise suppressor: {}, continuing without noise suppression", e);
None
}
}
} else {
info!("ℹ️ RNNoise noise suppression DISABLED for microphone '{}' (flag: RNNOISE_APPLY_ENABLED=false)", device.name);
info!(" Whisper handles noise well internally - RNNoise is optional");
None
};
// Initialize high-pass filter (removes rumble below 80 Hz)
let hpf = {
let filter = HighPassFilter::new(TARGET_SAMPLE_RATE, 80.0);
info!("✅ High-pass filter initialized for microphone '{}' (cutoff: 80 Hz)", device.name);
Some(filter)
};
// Initialize EBU R128 normalizer (professional loudness standard)
let norm = match LoudnessNormalizer::new(1, TARGET_SAMPLE_RATE) {
Ok(normalizer) => {
info!("✅ EBU R128 normalizer initialized for microphone '{}' (target: -23 LUFS)", device.name);
Some(normalizer)
}
Err(e) => {
warn!("⚠️ Failed to create normalizer for microphone: {}, normalization disabled", e);
None
}
};
(ns, hpf, norm)
} else {
// System audio: no enhancement needed
info!("ℹ️ System audio '{}' captured raw (no enhancement)", device.name);
(None, None, None)
};
// CRITICAL FIX: Initialize persistent resampler to preserve energy across chunks
// Creating a new resampler per chunk causes energy amplification and incorrect output sizes
// Use fixed chunk size of 512 samples with buffering for variable-size input
const RESAMPLER_CHUNK_SIZE: usize = 512;
let resampler = if needs_resampling {
let ratio = TARGET_SAMPLE_RATE as f64 / sample_rate as f64;
// Adaptive parameters based on sample rate ratio (same logic as resample_audio)
let (sinc_len, interpolation_type, oversampling) = if ratio >= 2.0 {
(512, SincInterpolationType::Cubic, 512)
} else if ratio >= 1.5 {
(384, SincInterpolationType::Cubic, 384)
} else if ratio > 1.0 {
(256, SincInterpolationType::Linear, 256)
} else if ratio <= 0.5 {
(512, SincInterpolationType::Cubic, 512)
} else {
(384, SincInterpolationType::Linear, 384)
};
let params = SincInterpolationParameters {
sinc_len,
f_cutoff: 0.95,
interpolation: interpolation_type,
oversampling_factor: oversampling,
window: WindowFunction::BlackmanHarris2,
};
match SincFixedIn::<f32>::new(
ratio,
2.0, // Maximum relative deviation
params,
RESAMPLER_CHUNK_SIZE,
1, // Mono
) {
Ok(resampler) => {
info!("✅ Persistent resampler initialized for '{}' ({}Hz → {}Hz, chunk_size={})",
device.name, sample_rate, TARGET_SAMPLE_RATE, RESAMPLER_CHUNK_SIZE);
info!(" Buffering enabled for variable-size chunks (e.g., 320, 512, 1024, etc.)");
Some(resampler)
}
Err(e) => {
warn!("⚠️ Failed to create persistent resampler: {}, will use fallback", e);
None
}
}
} else {
None
};
Self {
device,
state,
sample_rate,
channels,
chunk_counter: Arc::new(std::sync::atomic::AtomicU64::new(0)),
device_type,
recording_sender,
needs_resampling,
resampler: Arc::new(std::sync::Mutex::new(resampler)),
resampler_input_buffer: Arc::new(std::sync::Mutex::new(Vec::with_capacity(RESAMPLER_CHUNK_SIZE * 2))),
resampler_chunk_size: RESAMPLER_CHUNK_SIZE,
noise_suppressor: Arc::new(std::sync::Mutex::new(noise_suppressor)),
high_pass_filter: Arc::new(std::sync::Mutex::new(high_pass_filter)),
normalizer: Arc::new(std::sync::Mutex::new(normalizer)),
// Using global recording time for sync
}
}
/// Process audio data directly from callback
pub fn process_audio_data(&self, data: &[f32]) {
// Check if still recording
if !self.state.is_recording() {
return;
}
// Convert to mono if needed
let mut mono_data = if self.channels > 1 {
audio_to_mono(data, self.channels)
} else {
data.to_vec()
};
// CRITICAL FIX: Resample to 48kHz if device uses different sample rate
// This fixes Bluetooth devices (like Sony WH-1000XM4) that report 16kHz or 44.1kHz
// Without this, audio is sped up 3x and VAD fails
//
// IMPORTANT: Uses PERSISTENT resampler with BUFFERING to preserve energy across chunks
// Creating a new resampler per chunk causes energy amplification (173.5% RMS)
// Buffering handles variable chunk sizes (320, 512, 1024, etc.) by accumulating to fixed 512-sample chunks
const TARGET_SAMPLE_RATE: u32 = 48000;
if self.needs_resampling {
let before_len = mono_data.len();
let before_rms = if !mono_data.is_empty() {
(mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt()
} else {
0.0
};
// Use persistent resampler with buffering to handle variable chunk sizes
let mut resampled_output = Vec::new();
let mut used_persistent_resampler = false;
if let Ok(mut buffer_lock) = self.resampler_input_buffer.lock() {
// Add new samples to buffer
buffer_lock.extend_from_slice(&mono_data);
// Process complete chunks through the resampler
if let Ok(mut resampler_lock) = self.resampler.lock() {
if let Some(ref mut resampler) = *resampler_lock {
used_persistent_resampler = true;
// Process as many complete chunks as we have
while buffer_lock.len() >= self.resampler_chunk_size {
// Extract exactly chunk_size samples
let chunk: Vec<f32> = buffer_lock.drain(0..self.resampler_chunk_size).collect();
// Rubato expects input as Vec<Vec<f32>> (one Vec per channel)
let waves_in = vec![chunk];
match resampler.process(&waves_in, None) {
Ok(mut waves_out) => {
if let Some(output) = waves_out.pop() {
resampled_output.extend_from_slice(&output);
}
}
Err(e) => {
warn!("⚠️ Persistent resampler processing failed: {}", e);
used_persistent_resampler = false;
break;
}
}
}
// Remaining samples in buffer will be processed in next iteration
}
}
}
// CRITICAL: Only update mono_data if we got output from persistent resampler
// If buffer is accumulating (< 512 samples), skip this chunk - data is safely buffered
// and will be processed in next iteration with proper resampling
let has_resampled_output = !resampled_output.is_empty();
if has_resampled_output {
mono_data = resampled_output;
} else if !used_persistent_resampler {
// Only fallback if persistent resampler is not available at all
mono_data = super::audio_processing::resample_audio(
&mono_data,
self.sample_rate,
TARGET_SAMPLE_RATE,
);
} else {
// Buffering: samples are accumulating in buffer, waiting for 512-sample chunk
// Don't send partial/unprocessed data - return early
// Audio is NOT lost - it's in the buffer and will be processed next iteration
return;
}
// Log resampling only occasionally to avoid spam
let chunk_id = self.chunk_counter.load(std::sync::atomic::Ordering::SeqCst);
if chunk_id % 100 == 0 && has_resampled_output {
let after_len = mono_data.len();
let after_rms = if !mono_data.is_empty() {
(mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt()
} else {
0.0
};
let ratio = TARGET_SAMPLE_RATE as f64 / self.sample_rate as f64;
let rms_preservation = if before_rms > 0.0 { (after_rms / before_rms) * 100.0 } else { 100.0 };
let buffer_size = if let Ok(buf) = self.resampler_input_buffer.lock() {
buf.len()
} else {
0
};
info!(
"🔄 [{:?}] Persistent buffered resampler: {}Hz → {}Hz (ratio: {:.2}x)",
self.device_type,
self.sample_rate,
TARGET_SAMPLE_RATE,
ratio
);
info!(
" Chunk {}: {} → {} samples, RMS preservation: {:.1}%, buffer: {}",
chunk_id,
before_len,
after_len,
rms_preservation,
buffer_size
);
}
}
// AUDIO ENHANCEMENT PIPELINE (Microphone Only)
// Processing order is critical: high-pass → noise suppression → normalization
// This ensures noise is removed before being amplified by the normalizer
if matches!(self.device_type, DeviceType::Microphone) {
// STEP 1: Apply high-pass filter to remove low-frequency rumble (< 80 Hz)
if let Ok(mut hpf_lock) = self.high_pass_filter.lock() {
if let Some(ref mut filter) = *hpf_lock {
mono_data = filter.process(&mono_data);
}
}
// STEP 2: Apply RNNoise noise suppression (10-15 dB reduction) - CONDITIONAL
if super::ffmpeg_mixer::RNNOISE_APPLY_ENABLED {
if let Ok(mut ns_lock) = self.noise_suppressor.lock() {
if let Some(ref mut suppressor) = *ns_lock {
let before_len = mono_data.len();
mono_data = suppressor.process(&mono_data);
let after_len = mono_data.len();
// CRITICAL MONITORING: Track buffer health
let chunk_id = self.chunk_counter.load(std::sync::atomic::Ordering::SeqCst);
if chunk_id % 100 == 0 {
let buffered = suppressor.buffered_samples();
let length_delta = (before_len as i32 - after_len as i32).abs();
debug!("🔇 Noise suppression health: in={}, out={}, delta={}, buffered={}, RMS={:.4}",
before_len, after_len, length_delta, buffered,
if !mono_data.is_empty() {
(mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt()
} else { 0.0 });
// WARN if accumulating samples (potential latency buildup)
if buffered > 1000 {
warn!("⚠️ RNNoise accumulating samples: {} buffered (potential latency issue!)",
buffered);
}
// WARN if significant length mismatch
if length_delta > 50 {
warn!("⚠️ RNNoise length mismatch: input={} output={} (delta={})",
before_len, after_len, length_delta);
}
}
}
}
}
// STEP 3: Apply EBU R128 normalization (professional loudness standard)
if let Ok(mut normalizer_lock) = self.normalizer.lock() {
if let Some(ref mut normalizer) = *normalizer_lock {
mono_data = normalizer.normalize_loudness(&mono_data);
// Log normalization occasionally for debugging
let chunk_id = self.chunk_counter.load(std::sync::atomic::Ordering::SeqCst);
if chunk_id % 200 == 0 && !mono_data.is_empty() {
let rms = (mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt();
let peak = mono_data.iter().map(|&x| x.abs()).fold(0.0f32, f32::max);
debug!("🎤 After normalization chunk {}: RMS={:.4}, Peak={:.4}", chunk_id, rms, peak);
}
}
}
}
// Create audio chunk with stream-specific timestamp (get ID first for logging)
let chunk_id = self.chunk_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
// RAW AUDIO: No gain applied here - will be applied AFTER mixing
// This prevents amplifying system audio bleed-through in the microphone
// DIAGNOSTIC: Log audio levels for debugging (especially mic issues)
// if chunk_id % 100 == 0 && !mono_data.is_empty() {
// let raw_rms = (mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt();
// let raw_peak = mono_data.iter().map(|&x| x.abs()).fold(0.0f32, f32::max);
// info!("🎙️ [{:?}] Chunk {} - Raw: RMS={:.6}, Peak={:.6}",
// self.device_type, chunk_id, raw_rms, raw_peak);
// // Warn if microphone is completely silent
// if matches!(self.device_type, DeviceType::Microphone) && raw_rms == 0.0 && raw_peak == 0.0 {
// warn!("⚠️ Microphone producing ZERO audio - check permissions or hardware!");
// }
// }
// else if chunk_id % 100 == 0 && matches!(self.device_type, DeviceType::System) {
// let raw_rms = (mono_data.iter().map(|&x| x * x).sum::<f32>() / mono_data.len() as f32).sqrt();
// let raw_peak = mono_data.iter().map(|&x| x.abs()).fold(0.0f32, f32::max);
// info!("🔊 [{:?}] Chunk {} - Raw: RMS={:.6}, Peak={:.6}",
// self.device_type, chunk_id, raw_rms, raw_peak);
// // Warn if system audio is completely silent
// if raw_rms == 0.0 && raw_peak == 0.0 {
// warn!("⚠️ System audio producing ZERO audio - check permissions or hardware!");
// }
// }
// Use global recording timestamp for proper synchronization
let timestamp = self.state.get_recording_duration().unwrap_or(0.0);
// RAW AUDIO CHUNK: No gain applied - will be mixed and gained downstream
// Use 48kHz if we resampled, otherwise use original rate
let audio_chunk = AudioChunk {
data: mono_data, // Raw audio (resampled if needed), no gain yet
sample_rate: if self.needs_resampling { 48000 } else { self.sample_rate },
timestamp,
chunk_id,
device_type: self.device_type.clone(),
};
// NOTE: Raw audio is NOT sent to recording saver to prevent echo
// Only the mixed audio (from AudioPipeline) is saved to file (see pipeline.rs:726-736)
// This ensures we only record once: mic + system properly mixed
// Individual raw streams go only to the transcription pipeline below
// Send to processing pipeline for transcription
if let Err(e) = self.state.send_audio_chunk(audio_chunk) {
// Check if this is the "pipeline not ready" error
if e.to_string().contains("Audio pipeline not ready") {
// This is expected during initialization, just log it as debug
debug!("Audio pipeline not ready yet, skipping chunk {}", chunk_id);
return;
}
warn!("Failed to send audio chunk: {}", e);
// More specific error handling based on failure reason
let error = if e.to_string().contains("channel closed") {
AudioError::ChannelClosed
} else if e.to_string().contains("full") {
AudioError::BufferOverflow
} else {
AudioError::ProcessingFailed
};
self.state.report_error(error);
} else {
debug!("Sent audio chunk {} ({} samples)", chunk_id, data.len());
}
}
/// Handle stream errors with enhanced disconnect detection
pub fn handle_stream_error(&self, error: cpal::StreamError) {
error!("Audio stream error for {}: {}", self.device.name, error);
let error_str = error.to_string().to_lowercase();
// Enhanced error detection for device disconnection
let audio_error = if error_str.contains("device is no longer available")
|| error_str.contains("device not found")
|| error_str.contains("device disconnected")
|| error_str.contains("no such device")
|| error_str.contains("device unavailable")
|| error_str.contains("device removed")
{
warn!("🔌 Device disconnect detected for: {}", self.device.name);
AudioError::DeviceDisconnected
} else if error_str.contains("permission") || error_str.contains("access denied") {
AudioError::PermissionDenied
} else if error_str.contains("channel closed") {
AudioError::ChannelClosed
} else if error_str.contains("stream") && error_str.contains("failed") {
AudioError::StreamFailed
} else {
warn!("Unknown audio error: {}", error);
AudioError::StreamFailed
};
self.state.report_error(audio_error);
}
}
/// VAD-driven audio processing pipeline
/// Uses Voice Activity Detection to segment speech in real-time and send only speech to Whisper
pub struct AudioPipeline {
receiver: mpsc::UnboundedReceiver<AudioChunk>,
transcription_sender: mpsc::UnboundedSender<AudioChunk>,
state: Arc<RecordingState>,
mic_vad_processor: ContinuousVadProcessor,
system_vad_processor: ContinuousVadProcessor,
sample_rate: u32,
chunk_id_counter: u64,
// Performance optimization: reduce logging frequency
last_summary_time: std::time::Instant,
processed_chunks: u64,
// Smart batching for audio metrics
metrics_batcher: Option<AudioMetricsBatcher>,
// PROFESSIONAL AUDIO MIXING: Ring buffer + RMS-based mixer
ring_buffer: AudioMixerRingBuffer,
mixer: ProfessionalAudioMixer,
// Recording sender for pre-mixed audio
recording_sender_for_mixed: Option<mpsc::UnboundedSender<AudioChunk>>,
}
impl AudioPipeline {
pub fn new(
receiver: mpsc::UnboundedReceiver<AudioChunk>,
transcription_sender: mpsc::UnboundedSender<AudioChunk>,
state: Arc<RecordingState>,
target_chunk_duration_ms: u32,
sample_rate: u32,
mic_device_name: String,
mic_device_kind: super::device_detection::InputDeviceKind,
system_device_name: String,
system_device_kind: super::device_detection::InputDeviceKind,
) -> Self {
// Log device characteristics for adaptive buffering
info!("🎛️ AudioPipeline initializing with device characteristics:");
info!(" Mic: '{}' ({:?}) - Buffer: {:?}",
mic_device_name, mic_device_kind, mic_device_kind.buffer_timeout());
info!(" System: '{}' ({:?}) - Buffer: {:?}",
system_device_name, system_device_kind, system_device_kind.buffer_timeout());
// Device kind information can be used for adaptive buffering in the future
// For now, we log it for monitoring and potential optimization
let _ = (mic_device_name, mic_device_kind, system_device_name, system_device_kind);
// Create VAD processor with balanced redemption time for speech accumulation
// The VAD processor now handles 48kHz->16kHz resampling internally
// This bridges natural pauses without excessive fragmentation
// For mac os core audio, 900ms, for windows 400ms seems good
let redemption_time = if cfg!(target_os = "macos") { 400 } else { 400 };
let mic_vad_processor = match ContinuousVadProcessor::new(sample_rate, redemption_time) {
Ok(processor) => {
info!("VAD-driven pipeline: Mic VAD processor created");
processor
}
Err(e) => {
error!("Failed to create mic VAD processor: {}", e);
panic!("Mic VAD processor creation failed: {}", e);
}
};
let system_vad_processor = match ContinuousVadProcessor::new(sample_rate, redemption_time) {
Ok(processor) => {
info!("VAD-driven pipeline: System VAD processor created");
processor
}
Err(e) => {
error!("Failed to create system VAD processor: {}", e);
panic!("System VAD processor creation failed: {}", e);
}
};
info!("VAD-driven pipeline: dual-channel VAD for speaker differentiation (mic=You, system=Others)");
// Initialize professional audio mixing components
let ring_buffer = AudioMixerRingBuffer::new(sample_rate);
let mixer = ProfessionalAudioMixer::new(sample_rate);
// Note: target_chunk_duration_ms is ignored - VAD controls segmentation now
let _ = target_chunk_duration_ms;
Self {
receiver,
transcription_sender,
state,
mic_vad_processor,
system_vad_processor,
sample_rate,
chunk_id_counter: 0,
// Performance optimization: reduce logging frequency
last_summary_time: std::time::Instant::now(),
processed_chunks: 0,
// Initialize metrics batcher for smart batching
metrics_batcher: Some(AudioMetricsBatcher::new()),
// Initialize professional audio mixing
ring_buffer,
mixer,
recording_sender_for_mixed: None, // Will be set by manager
}
}
/// Run the VAD-driven audio processing pipeline
pub async fn run(mut self) -> Result<()> {
info!("VAD-driven audio pipeline started - segments sent in real-time based on speech detection");
// CRITICAL FIX: Continue processing until channel is closed, not based on recording state
// This ensures ALL chunks are processed during shutdown, fixing premature meeting completion
// Previous bug: Loop checked `while self.state.is_recording()` which caused early exit when
// stop_recording() was called, losing flush signals and remaining chunks in the pipeline
loop {
// Receive audio chunks with timeout
match tokio::time::timeout(
std::time::Duration::from_millis(50), // Shorter timeout for responsiveness
self.receiver.recv()
).await {
Ok(Some(chunk)) => {
// PERFORMANCE: Check for flush signal (special chunk with ID >= u64::MAX - 10)
// Multiple flush signals may be sent to ensure processing
if chunk.chunk_id >= u64::MAX - 10 {
info!("📥 Received FLUSH signal #{} - flushing VAD processor", u64::MAX - chunk.chunk_id);
self.flush_remaining_audio()?;
// Continue processing to handle any remaining chunks
continue;
}
// PERFORMANCE OPTIMIZATION: Eliminate per-chunk logging overhead
// Logging in hot paths causes severe performance degradation
self.processed_chunks += 1;
// Smart batching: collect metrics instead of logging every chunk
if let Some(ref batcher) = self.metrics_batcher {
let avg_level = chunk.data.iter().map(|&x| x.abs()).sum::<f32>() / chunk.data.len() as f32;
let duration_ms = chunk.data.len() as f64 / chunk.sample_rate as f64 * 1000.0;
batch_audio_metric!(
Some(batcher),
chunk.chunk_id,
chunk.data.len(),
duration_ms,
avg_level
);
}
// CRITICAL: Log summary only every 200 chunks OR every 60 seconds (99.5% reduction)
// This eliminates I/O overhead in the audio processing hot path
// Use performance-optimized debug macro that compiles to nothing in release builds
if self.processed_chunks % 200 == 0 || self.last_summary_time.elapsed().as_secs() >= 60 {
perf_debug!("Pipeline processed {} chunks, current chunk: {} ({} samples)",
self.processed_chunks, chunk.chunk_id, chunk.data.len());
self.last_summary_time = std::time::Instant::now();
}
// STEP 1: Add raw audio to ring buffer for mixing
// Microphone audio is already normalized at capture level (AudioCapture)
// System audio remains raw
self.ring_buffer.add_samples(chunk.device_type.clone(), chunk.data);
// STEP 2: Mix audio in fixed windows when both streams have sufficient data
while self.ring_buffer.can_mix() {
if let Some((mic_window, sys_window)) = self.ring_buffer.extract_window() {
// Simple mixing without aggressive ducking
let mixed_clean = self.mixer.mix_window(&mic_window, &sys_window);
// NO POST-GAIN NEEDED: Microphone already normalized by EBU R128 to -23 LUFS
// This is broadcast-standard loudness (Netflix/YouTube/Spotify level)
// System audio at natural levels
// Previous 2x gain was causing excessive limiting/distortion
let mixed_with_gain = mixed_clean;
// STEP 3: Run VAD on each channel separately for speaker attribution
// Mic audio -> "You", System audio -> "Others"
// IMPORTANT: System VAD runs FIRST so "Others" segments arrive at the
// transcription worker before "You" segments. This is required for
// text-based echo deduplication (the worker compares "You" text against
// recent "Others" text to detect speaker bleed-through).
for (vad, audio, device_type) in [
(&mut self.system_vad_processor, &sys_window, DeviceType::System),
(&mut self.mic_vad_processor, &mic_window, DeviceType::Microphone),
] {
match vad.process_audio(audio) {
Ok(speech_segments) => {
for segment in speech_segments {
let duration_ms = segment.end_timestamp_ms - segment.start_timestamp_ms;
if segment.samples.len() >= 800 { // Minimum 50ms at 16kHz
info!("📤 Sending {:?} VAD segment: {:.1}ms, {} samples",
device_type, duration_ms, segment.samples.len());
let transcription_chunk = AudioChunk {
data: segment.samples,
sample_rate: 16000,
timestamp: segment.start_timestamp_ms / 1000.0,
chunk_id: self.chunk_id_counter,
device_type: device_type.clone(),
};
if let Err(e) = self.transcription_sender.send(transcription_chunk) {
warn!("Failed to send VAD segment: {}", e);
} else {
self.chunk_id_counter += 1;
}
} else {
debug!("⏭️ Dropping short VAD segment: {:.1}ms ({} samples < 800)",
duration_ms, segment.samples.len());
}
}
}
Err(e) => {
warn!("⚠️ {:?} VAD error: {}", device_type, e);
}
}
}
// STEP 4: Send mixed audio for recording (WAV file)
if let Some(ref sender) = self.recording_sender_for_mixed {
let recording_chunk = AudioChunk {
data: mixed_with_gain.clone(),
sample_rate: self.sample_rate,
timestamp: chunk.timestamp,
chunk_id: self.chunk_id_counter,
device_type: DeviceType::Microphone, // Mixed audio
};
let _ = sender.send(recording_chunk);
}
}
}
}
Ok(None) => {
info!("Audio pipeline: sender closed after processing {} chunks", self.processed_chunks);
break;
}
Err(_) => {
// Timeout - just continue, VAD handles all segmentation
continue;
}
}
}
// Flush any remaining VAD segments
self.flush_remaining_audio()?;
info!("VAD-driven audio pipeline ended");
Ok(())
}
fn flush_remaining_audio(&mut self) -> Result<()> {
info!("Flushing remaining audio from pipeline (processed {} chunks)", self.processed_chunks);
// Flush both VAD processors and send remaining segments to transcription
// System first so "Others" arrives before "You" (required for echo dedup)
for (vad, device_type) in [
(&mut self.system_vad_processor, DeviceType::System),
(&mut self.mic_vad_processor, DeviceType::Microphone),
] {
match vad.flush() {
Ok(final_segments) => {
for segment in final_segments {
let duration_ms = segment.end_timestamp_ms - segment.start_timestamp_ms;
if segment.samples.len() >= 800 {
info!("📤 Sending final {:?} VAD segment: {:.1}ms, {} samples",
device_type, duration_ms, segment.samples.len());
let transcription_chunk = AudioChunk {
data: segment.samples,
sample_rate: 16000,
timestamp: segment.start_timestamp_ms / 1000.0,
chunk_id: self.chunk_id_counter,
device_type: device_type.clone(),
};
if let Err(e) = self.transcription_sender.send(transcription_chunk) {
warn!("Failed to send final VAD segment: {}", e);
} else {
self.chunk_id_counter += 1;
}
} else {
info!("⏭️ Skipping short final segment: {:.1}ms ({} samples < 800)",
duration_ms, segment.samples.len());
}
}
}
Err(e) => {
warn!("Failed to flush {:?} VAD processor: {}", device_type, e);
}
}
}
Ok(())
}
}
/// Simple audio pipeline manager
pub struct AudioPipelineManager {
pipeline_handle: Option<JoinHandle<Result<()>>>,
audio_sender: Option<mpsc::UnboundedSender<AudioChunk>>,
}
impl AudioPipelineManager {
pub fn new() -> Self {
Self {
pipeline_handle: None,
audio_sender: None,
}
}
/// Start the audio pipeline with device information for adaptive buffering
pub fn start(
&mut self,
state: Arc<RecordingState>,
transcription_sender: mpsc::UnboundedSender<AudioChunk>,
target_chunk_duration_ms: u32,
sample_rate: u32,
recording_sender: Option<mpsc::UnboundedSender<AudioChunk>>,
mic_device_name: String,
mic_device_kind: super::device_detection::InputDeviceKind,
system_device_name: String,
system_device_kind: super::device_detection::InputDeviceKind,
) -> Result<()> {