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}");