4
4
namespace LiveKit
5
5
{
6
6
/// <summary>
7
- /// A thread-safe buffer for buffering audio samples.
7
+ /// A ring buffer for audio samples.
8
8
/// </summary>
9
9
internal class AudioBuffer
10
10
{
11
11
private readonly uint _bufferDurationMs ;
12
12
private RingBuffer _buffer ;
13
13
private uint _channels ;
14
14
private uint _sampleRate ;
15
- private object _lock = new object ( ) ;
16
15
17
16
/// <summary>
18
17
/// Initializes a new audio sample buffer for holding samples for a given duration.
@@ -52,23 +51,20 @@ static short FloatToS16(float v)
52
51
53
52
private void Capture ( short [ ] data , uint channels , uint sampleRate )
54
53
{
55
- lock ( _lock )
54
+ if ( _buffer == null || channels != _channels || sampleRate != _sampleRate )
56
55
{
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 )
66
65
{
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 ) ;
72
68
}
73
69
}
74
70
}
@@ -80,22 +76,19 @@ private void Capture(short[] data, uint channels, uint sampleRate)
80
76
/// <returns>An AudioFrame containing the read audio samples or if there is not enough samples, null.</returns>
81
77
internal AudioFrame ReadDuration ( uint durationMs )
82
78
{
83
- lock ( _lock )
84
- {
85
- if ( _buffer == null ) return null ;
79
+ if ( _buffer == null ) return null ;
86
80
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 ;
90
84
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 ) ;
98
90
}
91
+ return frame ;
99
92
}
100
93
}
101
94
}
0 commit comments