Skip to content

Commit 89437aa

Browse files
committed
Remove locking from AudioBuffer
1 parent af3d8ec commit 89437aa

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

Runtime/Scripts/Internal/AudioBuffer.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
namespace LiveKit
55
{
66
/// <summary>
7-
/// A thread-safe buffer for buffering audio samples.
7+
/// A ring buffer for audio samples.
88
/// </summary>
99
internal class AudioBuffer
1010
{
1111
private readonly uint _bufferDurationMs;
1212
private RingBuffer _buffer;
1313
private uint _channels;
1414
private uint _sampleRate;
15-
private object _lock = new object();
1615

1716
/// <summary>
1817
/// Initializes a new audio sample buffer for holding samples for a given duration.
@@ -52,23 +51,20 @@ static short FloatToS16(float v)
5251

5352
private void Capture(short[] data, uint channels, uint sampleRate)
5453
{
55-
lock (_lock)
54+
if (_buffer == null || channels != _channels || sampleRate != _sampleRate)
5655
{
57-
if (_buffer == null || channels != _channels || sampleRate != _sampleRate)
58-
{
59-
var size = (int)(channels * sampleRate * (_bufferDurationMs / 1000f));
60-
_buffer?.Dispose();
61-
_buffer = new RingBuffer(size * sizeof(short));
62-
_channels = channels;
63-
_sampleRate = sampleRate;
64-
}
65-
unsafe
56+
var size = (int)(channels * sampleRate * (_bufferDurationMs / 1000f));
57+
_buffer?.Dispose();
58+
_buffer = new RingBuffer(size * sizeof(short));
59+
_channels = channels;
60+
_sampleRate = sampleRate;
61+
}
62+
unsafe
63+
{
64+
fixed (short* pData = data)
6665
{
67-
fixed (short* pData = data)
68-
{
69-
var byteData = new ReadOnlySpan<byte>(pData, data.Length * sizeof(short));
70-
_buffer.Write(byteData);
71-
}
66+
var byteData = new ReadOnlySpan<byte>(pData, data.Length * sizeof(short));
67+
_buffer.Write(byteData);
7268
}
7369
}
7470
}
@@ -80,22 +76,19 @@ private void Capture(short[] data, uint channels, uint sampleRate)
8076
/// <returns>An AudioFrame containing the read audio samples or if there is not enough samples, null.</returns>
8177
internal AudioFrame ReadDuration(uint durationMs)
8278
{
83-
lock (_lock)
84-
{
85-
if (_buffer == null) return null;
79+
if (_buffer == null) return null;
8680

87-
var samplesForDuration = (uint)(_sampleRate * (durationMs / 1000f));
88-
var requiredLength = samplesForDuration * _channels * sizeof(short);
89-
if (_buffer.AvailableRead() < requiredLength) return null;
81+
var samplesForDuration = (uint)(_sampleRate * (durationMs / 1000f));
82+
var requiredLength = samplesForDuration * _channels * sizeof(short);
83+
if (_buffer.AvailableRead() < requiredLength) return null;
9084

91-
var frame = new AudioFrame(_sampleRate, _channels, samplesForDuration);
92-
unsafe
93-
{
94-
var frameData = new Span<byte>(frame.Data.ToPointer(), frame.Length);
95-
_buffer.Read(frameData);
96-
}
97-
return frame;
85+
var frame = new AudioFrame(_sampleRate, _channels, samplesForDuration);
86+
unsafe
87+
{
88+
var frameData = new Span<byte>(frame.Data.ToPointer(), frame.Length);
89+
_buffer.Read(frameData);
9890
}
91+
return frame;
9992
}
10093
}
10194
}

0 commit comments

Comments
 (0)