1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
using System;
namespace Ryujinx.Audio.SoundIo
{
/// <summary>
/// A thread-safe variable-size circular buffer
/// </summary>
internal class SoundIoRingBuffer
{
private byte[] m_Buffer;
private int m_Size;
private int m_HeadOffset;
private int m_TailOffset;
/// <summary>
/// Gets the available bytes in the ring buffer
/// </summary>
public int Length
{
get { return m_Size; }
}
/// <summary>
/// Constructs a new instance of a <see cref="SoundIoRingBuffer"/>
/// </summary>
public SoundIoRingBuffer()
{
m_Buffer = new byte[2048];
}
/// <summary>
/// Constructs a new instance of a <see cref="SoundIoRingBuffer"/> with the specified capacity
/// </summary>
/// <param name="capacity">The number of entries that the <see cref="SoundIoRingBuffer"/> can initially contain</param>
public SoundIoRingBuffer(int capacity)
{
m_Buffer = new byte[capacity];
}
/// <summary>
/// Clears the ring buffer
/// </summary>
public void Clear()
{
m_Size = 0;
m_HeadOffset = 0;
m_TailOffset = 0;
}
/// <summary>
/// Clears the specified amount of bytes from the ring buffer
/// </summary>
/// <param name="size">The amount of bytes to clear from the ring buffer</param>
public void Clear(int size)
{
lock (this)
{
if (size > m_Size)
{
size = m_Size;
}
if (size == 0)
{
return;
}
m_HeadOffset = (m_HeadOffset + size) % m_Buffer.Length;
m_Size -= size;
if (m_Size == 0)
{
m_HeadOffset = 0;
m_TailOffset = 0;
}
return;
}
}
/// <summary>
/// Extends the capacity of the ring buffer
/// </summary>
private void SetCapacity(int capacity)
{
byte[] buffer = new byte[capacity];
if (m_Size > 0)
{
if (m_HeadOffset < m_TailOffset)
{
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, 0, m_Size);
}
else
{
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, 0, m_Buffer.Length - m_HeadOffset);
Buffer.BlockCopy(m_Buffer, 0, buffer, m_Buffer.Length - m_HeadOffset, m_TailOffset);
}
}
m_Buffer = buffer;
m_HeadOffset = 0;
m_TailOffset = m_Size;
}
/// <summary>
/// Writes a sequence of bytes to the ring buffer
/// </summary>
/// <param name="buffer">A byte array containing the data to write</param>
/// <param name="index">The zero-based byte offset in <paramref name="buffer" /> from which to begin copying bytes to the ring buffer</param>
/// <param name="count">The number of bytes to write</param>
public void Write<T>(T[] buffer, int index, int count)
{
if (count == 0)
{
return;
}
lock (this)
{
if ((m_Size + count) > m_Buffer.Length)
{
SetCapacity((m_Size + count + 2047) & ~2047);
}
if (m_HeadOffset < m_TailOffset)
{
int tailLength = m_Buffer.Length - m_TailOffset;
if (tailLength >= count)
{
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, count);
}
else
{
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, tailLength);
Buffer.BlockCopy(buffer, index + tailLength, m_Buffer, 0, count - tailLength);
}
}
else
{
Buffer.BlockCopy(buffer, index, m_Buffer, m_TailOffset, count);
}
m_Size += count;
m_TailOffset = (m_TailOffset + count) % m_Buffer.Length;
}
}
/// <summary>
/// Reads a sequence of bytes from the ring buffer and advances the position within the ring buffer by the number of bytes read
/// </summary>
/// <param name="buffer">The buffer to write the data into</param>
/// <param name="index">The zero-based byte offset in <paramref name="buffer" /> at which the read bytes will be placed</param>
/// <param name="count">The maximum number of bytes to read</param>
/// <returns>The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the ring buffer is empty</returns>
public int Read<T>(T[] buffer, int index, int count)
{
lock (this)
{
if (count > m_Size)
{
count = m_Size;
}
if (count == 0)
{
return 0;
}
if (m_HeadOffset < m_TailOffset)
{
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, count);
}
else
{
int tailLength = m_Buffer.Length - m_HeadOffset;
if (tailLength >= count)
{
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, count);
}
else
{
Buffer.BlockCopy(m_Buffer, m_HeadOffset, buffer, index, tailLength);
Buffer.BlockCopy(m_Buffer, 0, buffer, index + tailLength, count - tailLength);
}
}
m_Size -= count;
m_HeadOffset = (m_HeadOffset + count) % m_Buffer.Length;
if (m_Size == 0)
{
m_HeadOffset = 0;
m_TailOffset = 0;
}
return count;
}
}
}
}
|