44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using SFML.Audio;
|
|
using SFML.System;
|
|
|
|
namespace FluidSim.Audio
|
|
{
|
|
internal class RingBufferStream : SoundStream
|
|
{
|
|
private readonly RingBuffer ringBuffer;
|
|
|
|
public RingBufferStream(RingBuffer buffer)
|
|
{
|
|
ringBuffer = buffer;
|
|
// 2 channels, 44.1 kHz, standard stereo mapping
|
|
Initialize(2, 44100, new[] { SoundChannel.FrontLeft, SoundChannel.FrontRight });
|
|
}
|
|
|
|
protected override bool OnGetData(out short[] samples)
|
|
{
|
|
const int monoBlockSize = 512; // number of mono samples we'll read
|
|
float[] temp = new float[monoBlockSize];
|
|
int read = ringBuffer.Read(temp, monoBlockSize);
|
|
samples = new short[monoBlockSize * 2];
|
|
|
|
if (read > 0)
|
|
{
|
|
for (int i = 0; i < read; i++)
|
|
{
|
|
float clamped = Math.Clamp(temp[i], -1f, 1f);
|
|
short final = (short)(clamped * short.MaxValue);
|
|
samples[i * 2] = final; // left
|
|
samples[i * 2 + 1] = final; // right
|
|
}
|
|
}
|
|
for (int i = read * 2; i < samples.Length; i++)
|
|
samples[i] = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
protected override void OnSeek(Time timeOffset) =>
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|