@@ -49,10 +49,7 @@ public static class BasisLocalMicrophoneDriver
4949 public static int rmsIndex = 0 ;
5050 public static float averageRms ;
5151
52- #if ! UNITY_ANDROID && ! UNITY_IOS && ! UNITY_STANDALONE_LINUX
5352 public static RNNoise . NET . Denoiser Denoiser = new RNNoise . NET . Denoiser ( ) ;
54- #endif
55-
5653 public static int minFreq = 48000 ;
5754 public static int maxFreq = 48000 ;
5855
@@ -108,9 +105,7 @@ private static bool IsPaused
108105
109106 // Temp buffers for denoiser wet/dry and chunking
110107 private static float [ ] _denoiseDry ; // copy of pre-denoise frame
111- #if ! UNITY_ANDROID && ! UNITY_IOS && ! UNITY_STANDALONE_LINUX
112108 private static float [ ] _tmp480 ; // 480-sample scratch (allocated on demand)
113- #endif
114109 // ---------------------------------------------------------
115110
116111 public static bool Initialize ( )
@@ -150,11 +145,9 @@ public static void DeInitialize()
150145 {
151146 VAJ . processBufferArray . Dispose ( ) ;
152147 }
153- #if ! UNITY_ANDROID && ! UNITY_IOS && ! UNITY_STANDALONE_LINUX
154148 Denoiser ? . Dispose ( ) ;
155149 Denoiser = null ;
156150 _tmp480 = null ;
157- #endif
158151 clip = null ;
159152 microphoneBufferArray = null ;
160153 processBufferArray = null ;
@@ -328,49 +321,38 @@ public static void ResetMicrophones(string newMicrophone)
328321 Array . Clear ( rmsValues , 0 , rmsValues . Length ) ;
329322 rmsIndex = 0 ;
330323 averageRms = 0f ;
331-
332324 warmupSamples = SampleRate * 2 ;
333325 inWarmup = true ;
334-
335326 Array . Clear ( microphoneBufferArray , 0 , microphoneBufferArray . Length ) ;
336327 Array . Clear ( processBufferArray , 0 , processBufferArray . Length ) ;
337328 Array . Clear ( _denoiseDry , 0 , _denoiseDry . Length ) ;
338-
339- #if ! UNITY_ANDROID && ! UNITY_IOS && ! UNITY_STANDALONE_LINUX
340- if ( Denoiser == null ) Denoiser = new RNNoise . NET . Denoiser ( ) ;
341- #endif
342-
329+ Denoiser ??= new RNNoise . NET . Denoiser ( ) ;
343330 MicrophoneIsStarted = true ;
344-
345331 PacketSize = SampleRate * 4 ;
346-
347332 // Re-apply current UI volume with dB mapping
348333 ChangeMicrophoneVolume ( SMDMicrophone . SelectedVolumeMicrophone ) ;
349-
350334 MicrophoneDevice = newMicrophone ;
351335 }
352336 }
353-
354337 private static void StopSelectedMicrophone_Internal ( )
355338 {
356339 if ( string . IsNullOrEmpty ( MicrophoneDevice ) )
340+ {
357341 return ;
342+ }
358343
359344 if ( Microphone . IsRecording ( MicrophoneDevice ) )
360345 {
361346 Microphone . End ( MicrophoneDevice ) ;
362347 BasisDebug . Log ( "Stopped Microphone " + MicrophoneDevice ) ;
363348 }
364-
365349 MicrophoneDevice = null ;
366350 MicrophoneIsStarted = false ;
367-
368351 if ( clip != null )
369352 {
370353 clip = null ;
371354 }
372355 }
373-
374356 private static void ClearStateAfterStop ( )
375357 {
376358 head = 0 ;
@@ -385,9 +367,11 @@ private static void ClearStateAfterStop()
385367 rmsIndex = 0 ;
386368 averageRms = 0f ;
387369 }
388- if ( _denoiseDry != null ) Array . Clear ( _denoiseDry , 0 , _denoiseDry . Length ) ;
370+ if ( _denoiseDry != null )
371+ {
372+ Array . Clear ( _denoiseDry , 0 , _denoiseDry . Length ) ;
373+ }
389374 }
390-
391375 private static void StopSelectedMicrophone ( )
392376 {
393377 lock ( processingLock )
@@ -397,14 +381,12 @@ private static void StopSelectedMicrophone()
397381 ClearStateAfterStop ( ) ;
398382 }
399383 }
400-
401384 public static void HandleBasisVolumeAdjustmentJob ( )
402385 {
403386 if ( handle . IsCompleted == false )
404387 {
405388 handle . Complete ( ) ;
406389 }
407-
408390 if ( VAJ . processBufferArray . IsCreated )
409391 {
410392 if ( VAJ . processBufferArray . Length != processBufferArray . Length )
@@ -417,17 +399,14 @@ public static void HandleBasisVolumeAdjustmentJob()
417399 {
418400 VAJ . processBufferArray = new NativeArray < float > ( processBufferArray , Allocator . Persistent ) ;
419401 }
420-
421402 VAJ . Volume = Volume ;
422403 VAJ . LimitThreshold = LimitThreshold ;
423404 VAJ . LimitKnee = LimitKnee ;
424405 }
425-
426406 public static void ToggleIsPaused ( )
427407 {
428408 IsPaused = ! IsPaused ;
429409 }
430-
431410 public static void MicrophoneUpdate ( )
432411 {
433412 if ( ! MicrophoneIsStarted || string . IsNullOrEmpty ( MicrophoneDevice ) || clip == null )
@@ -461,7 +440,6 @@ public static void MicrophoneUpdate()
461440 MainThreadOnHasSilence ? . Invoke ( ) ;
462441 }
463442 }
464-
465443 private static void StartProcessingThread ( )
466444 {
467445 processingTokenSource = new CancellationTokenSource ( ) ;
@@ -486,7 +464,6 @@ private static void StartProcessingThread()
486464 processingThread . IsBackground = true ;
487465 processingThread . Start ( ) ;
488466 }
489-
490467 public static void StopProcessingThread ( )
491468 {
492469 processingTokenSource ? . Cancel ( ) ;
@@ -500,7 +477,6 @@ public static void StopProcessingThread()
500477 processingTokenSource ? . Dispose ( ) ;
501478 processingTokenSource = null ;
502479 }
503-
504480 public static void ProcessAudioData ( int posSnapshot )
505481 {
506482 if ( inWarmup )
@@ -546,18 +522,13 @@ public static void ProcessAudioData(int posSnapshot)
546522 }
547523 }
548524 }
549-
550525 // --- User gain + limiter in Burst job ---
551- AdjustVolume ( ) ; // uses VAJ.Volume (linear) and limiter
552-
553- // --- Optional denoise with wet/dry + makeup ---
526+ AdjustVolume ( ) ;
554527 if ( UseDenoiser )
555528 {
556529 ApplyDeNoise ( ) ;
557530 }
558-
559531 RollingRMS ( ) ;
560-
561532 if ( IsTransmitWorthy ( ) )
562533 {
563534 OnHasAudio ? . Invoke ( ) ;
@@ -574,7 +545,6 @@ public static void ProcessAudioData(int posSnapshot)
574545 dataLength -= SampleRate ;
575546 }
576547 }
577-
578548 public static void AdjustVolume ( )
579549 {
580550 // keep VAJ fields up to date (in case UI changed them at runtime)
@@ -587,7 +557,6 @@ public static void AdjustVolume()
587557 handle . Complete ( ) ;
588558 VAJ . processBufferArray . CopyTo ( processBufferArray ) ;
589559 }
590-
591560 public static float GetRMS ( )
592561 {
593562 double sum = 0.0 ;
@@ -598,12 +567,10 @@ public static float GetRMS()
598567 }
599568 return Mathf . Sqrt ( ( float ) ( sum / SampleRate ) ) ;
600569 }
601-
602570 public static int GetDataLength ( int len , int h , int pos )
603571 {
604572 return ( pos < h ) ? ( len - h + pos ) : ( pos - h ) ;
605573 }
606-
607574 /// <summary>
608575 /// UI volume in [0..1] is mapped to dB, then converted to linear. Range: −60 dB … +18 dB.
609576 /// </summary>
@@ -619,9 +586,10 @@ public static void ChangeMicrophoneVolume(float ui)
619586 }
620587 public static void ApplyDeNoise ( )
621588 {
622- #if ! UNITY_ANDROID && ! UNITY_IOS && ! UNITY_STANDALONE_LINUX
623589 if ( _denoiseDry == null || _denoiseDry . Length != processBufferArray . Length )
590+ {
624591 CreateOrResizeArray ( processBufferArray . Length , ref _denoiseDry ) ;
592+ }
625593
626594 // copy dry
627595 Array . Copy ( processBufferArray , _denoiseDry , SampleRate ) ;
@@ -643,14 +611,11 @@ public static void ApplyDeNoise()
643611 // copy chunk to temp, zero-pad if last chunk shorter
644612 Array . Clear ( _tmp480 , 0 , hop ) ;
645613 Array . Copy ( processBufferArray , o , _tmp480 , 0 , n ) ;
646-
647614 Denoiser ? . Denoise ( _tmp480 ) ;
648-
649615 Array . Copy ( _tmp480 , 0 , processBufferArray , o , n ) ;
650616 o += n ;
651617 }
652618 }
653-
654619 // wet/dry + makeup
655620 float makeup = DbToAmp ( DenoiseMakeupDb ) ;
656621 float wet = Mathf . Clamp01 ( DenoiseWet ) ;
@@ -662,26 +627,19 @@ public static void ApplyDeNoise()
662627 processBufferArray [ Index ] = Mathf . Lerp ( _denoiseDry [ Index ] , den , wet ) ;
663628 }
664629 }
665- #endif
666630 }
667-
668631 public static void RollingRMS ( )
669632 {
670633 float rms = GetRMS ( ) ;
671634 rmsValues [ rmsIndex ] = rms ;
672635 rmsIndex = ( rmsIndex + 1 ) % LocalOpusSettings . rmsWindowSize ;
673636 averageRms = rmsValues . Average ( ) ;
674637 }
675-
676638 public static bool IsTransmitWorthy ( )
677639 {
678640 return averageRms > LocalOpusSettings . silenceThreshold ;
679641 }
680-
681- // ---------- Helpers ----------
682-
683642 private static float DbToAmp ( float db ) => Mathf . Pow ( 10f , db / 20f ) ;
684-
685643 private static void UpdateAgc ( float frameRms )
686644 {
687645 if ( frameRms <= 1e-6f )
0 commit comments