From 9da83efbc8f180bab3a3d6a5f1c9a3c3eace2939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Vinent?= Date: Wed, 10 Dec 2025 23:27:37 +0100 Subject: [PATCH] Make audio source sample rate and channels configurable Fix audio capture failures on Meta Quest devices caused by sample_rate and num_channels mismatch with hardcoded defaults. Changes: - RtcAudioSource: Add optional uint? sampleRate parameter with platform-specific fallbacks - MicrophoneSource: Accept configurable channels and sampleRate at runtime - Allows device-specific audio configuration to match actual capabilities Fixes: InvalidState error - sample_rate and num_channels don't match --- Runtime/Scripts/MicrophoneSource.cs | 12 ++++++++---- Runtime/Scripts/RtcAudioSource.cs | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Runtime/Scripts/MicrophoneSource.cs b/Runtime/Scripts/MicrophoneSource.cs index 923df981..da680681 100644 --- a/Runtime/Scripts/MicrophoneSource.cs +++ b/Runtime/Scripts/MicrophoneSource.cs @@ -14,6 +14,7 @@ sealed public class MicrophoneSource : RtcAudioSource { private readonly GameObject _sourceObject; private readonly string _deviceName; + private readonly uint _sampleRate; public override event Action AudioRead; @@ -27,10 +28,14 @@ sealed public class MicrophoneSource : RtcAudioSource /// get the list of available devices. /// The GameObject to attach the AudioSource to. The object must be kept in the scene /// for the duration of the source's lifetime. - public MicrophoneSource(string deviceName, GameObject sourceObject) : base(2, RtcAudioSourceType.AudioSourceMicrophone) + /// Number of audio channels (default: 2 for stereo). Configurable at runtime. + /// Sample rate in Hz (default: 48000). Configurable at runtime. + public MicrophoneSource(string deviceName, GameObject sourceObject, int channels = 2, uint sampleRate = 48000) + : base(channels, RtcAudioSourceType.AudioSourceMicrophone, sampleRate) { _deviceName = deviceName; _sourceObject = sourceObject; + _sampleRate = sampleRate; } /// @@ -48,7 +53,6 @@ public override void Start() base.Start(); if (_started) return; - if (!Application.HasUserAuthorization(mode: UserAuthorization.Microphone)) throw new InvalidOperationException("Microphone access not authorized"); @@ -60,11 +64,11 @@ public override void Start() private IEnumerator StartMicrophone() { - var clip = Microphone.Start( + var clip = Microphone.Start( _deviceName, loop: true, lengthSec: 1, - frequency: (int)DefaultMicrophoneSampleRate + frequency: (int)_sampleRate // Uses configurable sample rate instead of hardcoded default ); if (clip == null) throw new InvalidOperationException("Microphone start failed"); diff --git a/Runtime/Scripts/RtcAudioSource.cs b/Runtime/Scripts/RtcAudioSource.cs index ef512733..f2bc9bf8 100644 --- a/Runtime/Scripts/RtcAudioSource.cs +++ b/Runtime/Scripts/RtcAudioSource.cs @@ -60,16 +60,19 @@ public abstract class RtcAudioSource : IRtcSource, IDisposable private bool _started = false; private bool _disposed = false; - protected RtcAudioSource(int channels = 2, RtcAudioSourceType audioSourceType = RtcAudioSourceType.AudioSourceCustom) + protected RtcAudioSource(int channels = 2, RtcAudioSourceType audioSourceType = RtcAudioSourceType.AudioSourceCustom, uint? sampleRate = null) { _sourceType = audioSourceType; + // Use provided sample rate, or fall back to defaults + uint actualSampleRate = sampleRate ?? + (_sourceType == RtcAudioSourceType.AudioSourceMicrophone ? DefaultMicrophoneSampleRate : DefaultSampleRate); + using var request = FFIBridge.Instance.NewRequest(); var newAudioSource = request.request; newAudioSource.Type = AudioSourceType.AudioSourceNative; newAudioSource.NumChannels = (uint)channels; - newAudioSource.SampleRate = _sourceType == RtcAudioSourceType.AudioSourceMicrophone ? - DefaultMicrophoneSampleRate : DefaultSampleRate; + newAudioSource.SampleRate = actualSampleRate; UnityEngine.Debug.Log($"NewAudioSource: {newAudioSource.NumChannels} {newAudioSource.SampleRate}");