84 lines
1.9 KiB
C#
84 lines
1.9 KiB
C#
|
|
using Robovoice.Core;
|
||
|
|
|
||
|
|
namespace Robovoice.Stt.File;
|
||
|
|
|
||
|
|
public sealed class FileSttSource : ISttSource
|
||
|
|
{
|
||
|
|
private readonly object _lock = new();
|
||
|
|
private string[] _lines = Array.Empty<string>();
|
||
|
|
private int _currentIndex;
|
||
|
|
private bool _disposed;
|
||
|
|
|
||
|
|
public event TranscriptEventHandler? TranscriptReceived;
|
||
|
|
|
||
|
|
public string FilePath { get; set; } = string.Empty;
|
||
|
|
public int LineCount { get; private set; }
|
||
|
|
public int CurrentIndex => _currentIndex;
|
||
|
|
|
||
|
|
public void LoadFile(string path)
|
||
|
|
{
|
||
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
||
|
|
FilePath = path;
|
||
|
|
_lines = System.IO.File.ReadAllLines(path);
|
||
|
|
LineCount = _lines.Length;
|
||
|
|
_currentIndex = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
public string? GetPendingLine()
|
||
|
|
{
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
if (_lines.Length == 0) return null;
|
||
|
|
int idx = _currentIndex % _lines.Length;
|
||
|
|
return _lines[idx];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EmitNext()
|
||
|
|
{
|
||
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
||
|
|
string? line = null;
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
if (_lines.Length > 0)
|
||
|
|
{
|
||
|
|
int idx = _currentIndex % _lines.Length;
|
||
|
|
line = _lines[idx];
|
||
|
|
_currentIndex++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(line))
|
||
|
|
{
|
||
|
|
TranscriptReceived?.Invoke(this, new TranscriptEventArgs
|
||
|
|
{
|
||
|
|
Message = new TranscriptMessage(TranscriptType.Final, line!),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Reset()
|
||
|
|
{
|
||
|
|
lock (_lock)
|
||
|
|
{
|
||
|
|
_currentIndex = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public Task StartAsync(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
return Task.CompletedTask;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Task StopAsync(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
return Task.CompletedTask;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ValueTask DisposeAsync()
|
||
|
|
{
|
||
|
|
_disposed = true;
|
||
|
|
return ValueTask.CompletedTask;
|
||
|
|
}
|
||
|
|
}
|