From e2b812a695eb69ea1cf6cc3afb3410ad2d1652f7 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 3 Jul 2024 13:09:02 +0800 Subject: [PATCH] Add `Type` for `RtcAudioSource`. (#41) * Set a special sample rate for the iOS microphone. * fix. --- Runtime/Scripts/AudioSource.cs | 40 +++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/Runtime/Scripts/AudioSource.cs b/Runtime/Scripts/AudioSource.cs index 1fb2f90..a674c2a 100644 --- a/Runtime/Scripts/AudioSource.cs +++ b/Runtime/Scripts/AudioSource.cs @@ -8,15 +8,33 @@ namespace LiveKit { + public enum RtcAudioSourceType + { + AudioSourceCustom = 0, + // if the source is a microphone, + // we don't want to play the audio locally + AudioSourceMicrophone = 1, + } + public class RtcAudioSource { #if UNITY_IOS - public static uint DefaultSampleRate = 24000; + // iOS microphone sample rate is 24k, + // please make sure when you using + // sourceType is AudioSourceMicrophone + public static uint DefaultMirophoneSampleRate = 24000; + + public static uint DefaultSampleRate = 48000; #else public static uint DefaultSampleRate = 48000; + public static uint DefaultMirophoneSampleRate = DefaultSampleRate; #endif public static uint DefaultChannels = 2; + private RtcAudioSourceType _sourceType; + + public RtcAudioSourceType SourceType => _sourceType; + private AudioSource _audioSource; private AudioFilter _audioFilter; @@ -27,13 +45,22 @@ public class RtcAudioSource private Thread _readAudioThread; private ThreadSafeQueue _frameQueue = new ThreadSafeQueue(); - public RtcAudioSource(AudioSource source) + public RtcAudioSource(AudioSource source, RtcAudioSourceType audioSourceType = RtcAudioSourceType.AudioSourceCustom) { + _sourceType = audioSourceType; + using var request = FFIBridge.Instance.NewRequest(); var newAudioSource = request.request; newAudioSource.Type = AudioSourceType.AudioSourceNative; newAudioSource.NumChannels = DefaultChannels; - newAudioSource.SampleRate = DefaultSampleRate; + if(_sourceType == RtcAudioSourceType.AudioSourceMicrophone) + { + newAudioSource.SampleRate = DefaultMirophoneSampleRate; + } + else + { + newAudioSource.SampleRate = DefaultSampleRate; + } newAudioSource.Options = request.TempResource(); newAudioSource.Options.EchoCancellation = true; newAudioSource.Options.AutoGainControl = true; @@ -93,8 +120,11 @@ static short FloatToS16(float v) { frameData[i] = FloatToS16(data[i]); } - // Don't play the audio locally - Array.Clear(data, 0, data.Length); + if (_sourceType == RtcAudioSourceType.AudioSourceMicrophone) + { + // Don't play the audio locally, to avoid echo. + Array.Clear(data, 0, data.Length); + } } _frameQueue.Enqueue(frame); }