Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Runtime/Scripts/MicrophoneSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<float[], int, int> AudioRead;

Expand All @@ -27,10 +28,14 @@ sealed public class MicrophoneSource : RtcAudioSource
/// get the list of available devices.</param>
/// <param name="sourceObject">The GameObject to attach the AudioSource to. The object must be kept in the scene
/// for the duration of the source's lifetime.</param>
public MicrophoneSource(string deviceName, GameObject sourceObject) : base(2, RtcAudioSourceType.AudioSourceMicrophone)
/// <param name="channels">Number of audio channels (default: 2 for stereo). Configurable at runtime.</param>
/// <param name="sampleRate">Sample rate in Hz (default: 48000). Configurable at runtime.</param>
public MicrophoneSource(string deviceName, GameObject sourceObject, int channels = 2, uint sampleRate = 48000)
: base(channels, RtcAudioSourceType.AudioSourceMicrophone, sampleRate)
{
_deviceName = deviceName;
_sourceObject = sourceObject;
_sampleRate = sampleRate;
}

/// <summary>
Expand All @@ -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");

Expand All @@ -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");
Expand Down
9 changes: 6 additions & 3 deletions Runtime/Scripts/RtcAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NewAudioSourceRequest>();
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}");

Expand Down
Loading