Attempting to generate sound, and set cells automatically
This commit is contained in:
45
Audio/SoundEngine.cs
Normal file
45
Audio/SoundEngine.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user