-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoopStream.cs
40 lines (32 loc) · 1.12 KB
/
LoopStream.cs
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
using NAudio.Wave;
namespace kvsPlayer
{
public class LoopStream(WaveStream sourceStream) : WaveStream
{
WaveStream sourceStream = sourceStream;
public bool EnableLooping { get; set; } = true;
public TimeSpan LoopPoint { get; set; }
public override WaveFormat WaveFormat => sourceStream.WaveFormat;
public override long Length => sourceStream.Length;
public override long Position
{
get => sourceStream.Position;
set => sourceStream.Position = value;
}
public override int Read(byte[] buffer, int offset, int count)
{
int totalBytesRead = 0;
while (totalBytesRead < count)
{
int bytesRead = sourceStream.Read(buffer, offset + totalBytesRead, count - totalBytesRead);
if (bytesRead == 0)
{
if (sourceStream.Position == 0 || !EnableLooping) break;
CurrentTime = LoopPoint;
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
}
}