Extract CRingBuffer
This commit is contained in:
1 parent
a46681c22b
commit
2e209e0f70
3 files changed
+80
-77
No files matched your search
@@ -0,0 +1,79 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Vocaluxe.Lib.Sound.Playback
|
||||||
|
{
|
||||||
|
class CRingBuffer
|
||||||
|
{
|
||||||
|
private readonly byte[] _Data;
|
||||||
|
private readonly int _Size;
|
||||||
|
private int _ReadPos;
|
||||||
|
private int _WritePos;
|
||||||
|
private int _BytesNotRead;
|
||||||
|
|
||||||
|
public int BytesNotRead
|
||||||
|
{
|
||||||
|
get { return _BytesNotRead; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public CRingBuffer(int size)
|
||||||
|
{
|
||||||
|
_Size = size;
|
||||||
|
_Data = new byte[size];
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
_ReadPos = 0;
|
||||||
|
_WritePos = 0;
|
||||||
|
_BytesNotRead = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(byte[] data)
|
||||||
|
{
|
||||||
|
int start = 0;
|
||||||
|
int end = data.Length;
|
||||||
|
if (end - start > _Size)
|
||||||
|
start = end - _Size;
|
||||||
|
int lenTotal = end - start;
|
||||||
|
int len = Math.Min(lenTotal, _Size - _WritePos);
|
||||||
|
Buffer.BlockCopy(data, start, _Data, _WritePos, len);
|
||||||
|
_WritePos += len;
|
||||||
|
if (_WritePos >= _Size)
|
||||||
|
{
|
||||||
|
_WritePos = 0;
|
||||||
|
start += len;
|
||||||
|
len = end - start;
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
Buffer.BlockCopy(data, start, _Data, _WritePos, len);
|
||||||
|
_WritePos += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_BytesNotRead += lenTotal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Read(byte[] data)
|
||||||
|
{
|
||||||
|
int lenTotal = Math.Min(data.Length, _BytesNotRead);
|
||||||
|
if (lenTotal == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int len = Math.Min(lenTotal, _Size - _ReadPos);
|
||||||
|
Buffer.BlockCopy(_Data, _ReadPos, data, 0, len);
|
||||||
|
_ReadPos += len;
|
||||||
|
if (_ReadPos >= _Size)
|
||||||
|
{
|
||||||
|
_ReadPos = 0;
|
||||||
|
int start = len;
|
||||||
|
len = lenTotal - len;
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
Buffer.BlockCopy(_Data, _ReadPos, data, start, len);
|
||||||
|
_ReadPos += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_BytesNotRead -= lenTotal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,8 +15,6 @@
|
|||||||
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
|
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Vocaluxe.Lib.Sound.Playback
|
namespace Vocaluxe.Lib.Sound.Playback
|
||||||
{
|
{
|
||||||
public enum EStreamAction
|
public enum EStreamAction
|
||||||
@@ -75,79 +73,4 @@ namespace Vocaluxe.Lib.Sound.Playback
|
|||||||
void Update();
|
void Update();
|
||||||
#endregion Stream Handling
|
#endregion Stream Handling
|
||||||
}
|
}
|
||||||
|
|
||||||
class CRingBuffer
|
|
||||||
{
|
|
||||||
private readonly byte[] _Data;
|
|
||||||
private readonly int _Size;
|
|
||||||
private int _ReadPos;
|
|
||||||
private int _WritePos;
|
|
||||||
private int _BytesNotRead;
|
|
||||||
|
|
||||||
public int BytesNotRead
|
|
||||||
{
|
|
||||||
get { return _BytesNotRead; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public CRingBuffer(int size)
|
|
||||||
{
|
|
||||||
_Size = size;
|
|
||||||
_Data = new byte[size];
|
|
||||||
Reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Reset()
|
|
||||||
{
|
|
||||||
_ReadPos = 0;
|
|
||||||
_WritePos = 0;
|
|
||||||
_BytesNotRead = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Write(byte[] data)
|
|
||||||
{
|
|
||||||
int start = 0;
|
|
||||||
int end = data.Length;
|
|
||||||
if (end - start > _Size)
|
|
||||||
start = end - _Size;
|
|
||||||
int lenTotal = end - start;
|
|
||||||
int len = Math.Min(lenTotal, _Size - _WritePos);
|
|
||||||
Buffer.BlockCopy(data, start, _Data, _WritePos, len);
|
|
||||||
_WritePos += len;
|
|
||||||
if (_WritePos >= _Size)
|
|
||||||
{
|
|
||||||
_WritePos = 0;
|
|
||||||
start += len;
|
|
||||||
len = end - start;
|
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
Buffer.BlockCopy(data, start, _Data, _WritePos, len);
|
|
||||||
_WritePos += len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_BytesNotRead += lenTotal;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Read(byte[] data)
|
|
||||||
{
|
|
||||||
int lenTotal = Math.Min(data.Length, _BytesNotRead);
|
|
||||||
if (lenTotal == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int len = Math.Min(lenTotal, _Size - _ReadPos);
|
|
||||||
Buffer.BlockCopy(_Data, _ReadPos, data, 0, len);
|
|
||||||
_ReadPos += len;
|
|
||||||
if (_ReadPos >= _Size)
|
|
||||||
{
|
|
||||||
_ReadPos = 0;
|
|
||||||
int start = len;
|
|
||||||
len = lenTotal - len;
|
|
||||||
if (len > 0)
|
|
||||||
{
|
|
||||||
Buffer.BlockCopy(_Data, _ReadPos, data, start, len);
|
|
||||||
_ReadPos += len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_BytesNotRead -= lenTotal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -219,6 +219,7 @@
|
|||||||
<Compile Include="Base\CPlaylistElement.cs" />
|
<Compile Include="Base\CPlaylistElement.cs" />
|
||||||
<Compile Include="Base\CRecord.cs" />
|
<Compile Include="Base\CRecord.cs" />
|
||||||
<Compile Include="COSFunctions.cs" />
|
<Compile Include="COSFunctions.cs" />
|
||||||
|
<Compile Include="Lib\Sound\Playback\CRingBuffer.cs" />
|
||||||
<Compile Include="Lib\Sound\Playback\GstreamerSharp\CGstreamerSharpAudioStream.cs" />
|
<Compile Include="Lib\Sound\Playback\GstreamerSharp\CGstreamerSharpAudioStream.cs" />
|
||||||
<Compile Include="Lib\Sound\Playback\OpenAL\COpenAlStream.cs" />
|
<Compile Include="Lib\Sound\Playback\OpenAL\COpenAlStream.cs" />
|
||||||
<Compile Include="Lib\Sound\Playback\PortAudio\CPortAudioStream.cs" />
|
<Compile Include="Lib\Sound\Playback\PortAudio\CPortAudioStream.cs" />
|
||||||
|
|||||||
Reference in new issue
Block a user