Skip to content

Commit 266730d

Browse files
committed
Trust Unity audio selection, use sample rate detected in mic start
1 parent a754aa5 commit 266730d

3 files changed

Lines changed: 9 additions & 34 deletions

File tree

Runtime/Scripts/MicrophoneSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private IEnumerator StartMicrophone()
8282
_deviceName,
8383
loop: true,
8484
lengthSec: 1,
85-
frequency: (int)DefaultMicrophoneSampleRate
85+
frequency: (int)_expectedSampleRate
8686
);
8787
}
8888
catch (Exception e)

Runtime/Scripts/RtcAudioSource.cs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,11 @@ private sealed class PendingAudioFrame
4646
/// </remarks>
4747
public abstract event Action<float[], int, int> AudioRead;
4848

49-
#if UNITY_IOS && !UNITY_EDITOR
50-
// iOS microphone sample rate is 24k
51-
public static uint DefaultMicrophoneSampleRate = 24000;
52-
53-
public static uint DefaultSampleRate = 48000;
54-
#else
55-
public static uint DefaultSampleRate = 48000;
56-
public static uint DefaultMicrophoneSampleRate = DefaultSampleRate;
57-
#endif
58-
public static uint DefaultChannels = 2;
59-
6049
private readonly RtcAudioSourceType _sourceType;
6150
public RtcAudioSourceType SourceType => _sourceType;
6251
private readonly int _debugId = Interlocked.Increment(ref nextDebugId);
63-
private readonly uint _expectedSampleRate;
64-
private readonly uint _expectedChannels;
52+
internal readonly uint _expectedSampleRate;
53+
internal readonly uint _expectedChannels;
6554

6655
internal readonly FfiHandle Handle;
6756
protected AudioSourceInfo _info;
@@ -128,26 +117,12 @@ protected RtcAudioSource(RtcAudioSourceType audioSourceType, uint sampleRate, ui
128117
// (e.g. batch mode without an audio device).
129118
private (uint sampleRate, uint channels) ResolveDeviceFormat()
130119
{
131-
uint sampleRate = _sourceType == RtcAudioSourceType.AudioSourceMicrophone
132-
? DefaultMicrophoneSampleRate
133-
: DefaultSampleRate;
134-
uint channels = DefaultChannels;
120+
var config = UnityEngine.AudioSettings.GetConfiguration();
121+
var sampleRate = (uint)config.sampleRate;
122+
var configuredChannels = SpeakerModeChannels(config.speakerMode);
123+
var channels = configuredChannels;
135124

136-
try
137-
{
138-
var config = UnityEngine.AudioSettings.GetConfiguration();
139-
if (config.sampleRate > 0)
140-
sampleRate = (uint)config.sampleRate;
141-
var configuredChannels = SpeakerModeChannels(config.speakerMode);
142-
if (configuredChannels > 0)
143-
channels = configuredChannels;
144-
145-
Utils.Info($"Configured native audio source with sampleRate {sampleRate} and channels {channels}");
146-
}
147-
catch (Exception e)
148-
{
149-
Utils.Warning($"{DebugTag} could not read Unity audio configuration, using defaults: {e.Message}");
150-
}
125+
Utils.Info($"Configured native audio source with sampleRate {sampleRate} and channels {channels}");
151126

152127
return (sampleRate, channels);
153128
}

Samples~/Meet/Assets/Runtime/MeetManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ private IEnumerator PublishLocalMicrophone()
453453
{
454454
if (_audioObjects.ContainsKey(LocalAudioTrackName)) yield break;
455455

456-
// Start the microphone here for early iOS permission request
456+
// Start the microphone here for early iOS permission request and android getting access to Microphone.devices
457457
Microphone.Start(null, true, 10, 44100);
458458

459459
var audioObject = new GameObject($"My Microphone: {Microphone.devices[0]}");

0 commit comments

Comments
 (0)