45 lines
974 B
C#
45 lines
974 B
C#
namespace FluidSim.Audio;
|
|
|
|
public class SoundEngine : IDisposable
|
|
{
|
|
private readonly RingBuffer ringBuffer;
|
|
private readonly RingBufferStream stream;
|
|
private bool isPlaying;
|
|
|
|
public SoundEngine(int bufferCapacity = 16384)
|
|
{
|
|
ringBuffer = new RingBuffer(bufferCapacity);
|
|
stream = new RingBufferStream(ringBuffer);
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
if (isPlaying) return;
|
|
stream.Play();
|
|
isPlaying = true;
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (!isPlaying) return;
|
|
stream.Stop();
|
|
isPlaying = false;
|
|
float[] drain = new float[ringBuffer.Count];
|
|
ringBuffer.Read(drain, drain.Length);
|
|
}
|
|
|
|
public int WriteSamples(float[] data, int count) =>
|
|
ringBuffer.Write(data, count);
|
|
|
|
public float Volume
|
|
{
|
|
get => stream.Volume;
|
|
set => stream.Volume = value;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Stop();
|
|
stream.Dispose();
|
|
}
|
|
} |