namespace Nonemm.Recorder.Tests; /// A sound system with no hardware: the test lists devices and pushes samples. internal sealed class FakeBackend : AudioBackend { private readonly Dictionary handlers = []; public List Devices { get; } = []; public int PlaybackOpened { get; private set; } public IReadOnlyList ListDevices() => [.. Devices]; public IDisposable OpenCapture(AudioDeviceInfo device, CaptureHandler handler) { handlers[device.Name] = handler; return new Closer(() => handlers.Remove(device.Name)); } public IDisposable OpenPlayback(PlaybackHandler handler) { PlaybackOpened++; return new Closer(() => { }); } public bool IsOpen(string name) => handlers.ContainsKey(name); /// Delivers `frames` of interleaved samples, `value` in each channel `channel`. public void Push(string name, int channels, int frames, int channel, float value) { float[] samples = new float[frames * channels]; for (int frame = 0; frame < frames; frame++) { samples[frame * channels + channel] = value; } handlers[name](samples); } public void Dispose() { } private sealed class Closer(Action close) : IDisposable { public void Dispose() => close(); } }