f7ea07b78e
- Stream audio directly to device via BufferedWaveProvider (no temp WAVs) - Add Noise/Speed/NoiseW sliders with live value labels - Sliders reinit engine on MouseUp (not Scroll) to avoid locking - Add text input field with Speak button for direct synthesis - Test button uses text input if non-empty - Fix sentence terminator auto-append (espeak-ng final-word drop) - Fix UTF-8 text marshaling (piper_synthesize_start keeps text ref) - Fix voice catalogue language column (JSON snake_case mapping) - Use piper_default_synthesize_options for model-specific defaults - Fix nullable warnings in designer files - Fix unused _playing field in AudioOutput
109 lines
2.9 KiB
C#
109 lines
2.9 KiB
C#
using NAudio.Wave;
|
|
|
|
namespace Robovoice.App;
|
|
|
|
internal sealed class AudioOutput : IDisposable
|
|
{
|
|
private WaveOutEvent? _waveOut;
|
|
private BufferedWaveProvider? _bufferProvider;
|
|
private int _sampleRate;
|
|
private string _deviceName = string.Empty;
|
|
private bool _disposed;
|
|
|
|
public Action<string>? Log { get; set; }
|
|
|
|
public void Start(int sampleRate, string? deviceName = null)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
|
|
_sampleRate = sampleRate;
|
|
_deviceName = deviceName ?? string.Empty;
|
|
|
|
Stop();
|
|
|
|
int deviceNumber = FindDevice(_deviceName);
|
|
_waveOut = new WaveOutEvent { DeviceNumber = deviceNumber, DesiredLatency = 200 };
|
|
|
|
_bufferProvider = new BufferedWaveProvider(
|
|
WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, 1))
|
|
{
|
|
BufferDuration = TimeSpan.FromSeconds(60),
|
|
DiscardOnBufferOverflow = true,
|
|
ReadFully = true,
|
|
};
|
|
|
|
_waveOut.Init(_bufferProvider);
|
|
_waveOut.PlaybackStopped += OnPlaybackStopped;
|
|
_waveOut.Play();
|
|
}
|
|
|
|
public void WriteSamples(float[] samples)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
if (_bufferProvider is null) return;
|
|
|
|
byte[] bytes = new byte[samples.Length * sizeof(float)];
|
|
Buffer.BlockCopy(samples, 0, bytes, 0, bytes.Length);
|
|
_bufferProvider.AddSamples(bytes, 0, bytes.Length);
|
|
}
|
|
|
|
public void Flush()
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
if (_bufferProvider is null || _sampleRate == 0) return;
|
|
|
|
int padSamples = (int)(_sampleRate * 0.5);
|
|
WriteSamples(new float[padSamples]);
|
|
}
|
|
|
|
private void OnPlaybackStopped(object? sender, StoppedEventArgs e)
|
|
{
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (_waveOut is not null)
|
|
{
|
|
_waveOut.PlaybackStopped -= OnPlaybackStopped;
|
|
_waveOut.Stop();
|
|
_waveOut.Dispose();
|
|
_waveOut = null;
|
|
}
|
|
_bufferProvider?.ClearBuffer();
|
|
_bufferProvider = null;
|
|
}
|
|
|
|
private static int FindDevice(string? deviceName)
|
|
{
|
|
if (string.IsNullOrEmpty(deviceName))
|
|
return -1;
|
|
|
|
for (int i = 0; i < WaveOut.DeviceCount; i++)
|
|
{
|
|
var caps = WaveOut.GetCapabilities(i);
|
|
if (caps.ProductName.Contains(deviceName, StringComparison.OrdinalIgnoreCase))
|
|
return i;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public static IReadOnlyList<(int Index, string Name)> GetDevices()
|
|
{
|
|
var devices = new List<(int, string)>();
|
|
for (int i = 0; i < WaveOut.DeviceCount; i++)
|
|
{
|
|
var caps = WaveOut.GetCapabilities(i);
|
|
devices.Add((i, caps.ProductName));
|
|
}
|
|
return devices;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
Stop();
|
|
_disposed = true;
|
|
}
|
|
}
|