This repository has been archived on 2025-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Cryville.EEW.Unity/Assets/Cryville.EEW.Unity/TTSWorker.cs
2025-05-06 20:35:08 +08:00

48 lines
1.2 KiB
C#

using SpeechLib;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace Cryville.EEW.Unity {
class TTSWorker : Core.Audio.TTSWorker {
readonly SpVoiceClass _voice;
public TTSWorker() : base(CreateSoundPlayer()) {
try {
_voice = new SpVoiceClass();
}
catch { }
}
static SoundPlayer CreateSoundPlayer() {
try {
return new SoundPlayer();
}
catch (InvalidOperationException) {
return null;
}
}
protected override bool IsSpeaking() {
if (_voice == null) return false;
_voice.GetStatus(out var status, out _);
return (status.dwRunningState & (uint)SpeechRunState.SRSEIsSpeaking) != 0;
}
protected override Task Speak(CultureInfo culture, string content, CancellationToken cancellationToken) {
if (_voice == null) return Task.CompletedTask;
_voice.Speak(
string.Format(CultureInfo.InvariantCulture, "<LANG LANGID=\"{0:x}\">{1}</LANG>", culture.LCID, content),
SpeechVoiceSpeakFlags.SVSFlagsAsync | SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak
);
return Task.CompletedTask;
}
protected override void StopCurrent() {
if (_voice == null) return;
_voice.Skip("SENTENCE", int.MaxValue);
}
}
}