-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add: TTS * WD EDIT * add: TTS prototypes * fix * fix 100%
- Loading branch information
Showing
43 changed files
with
5,276 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Linq; | ||
using Content.Client._White.TTS; | ||
using Content.Shared.Preferences; | ||
using Content.Shared._White.TTS; | ||
using Robust.Shared.Random; | ||
|
||
// ReSharper disable InconsistentNaming | ||
// ReSharper disable once CheckNamespace | ||
namespace Content.Client.Lobby.UI; | ||
|
||
public sealed partial class HumanoidProfileEditor | ||
{ | ||
private TTSSystem _ttsSystem = default!; | ||
private TTSManager _ttsManager = default!; | ||
private IRobustRandom _random = default!; | ||
|
||
private List<TTSVoicePrototype> _voiceList = default!; | ||
|
||
private readonly string[] _sampleText = | ||
[ | ||
"Помогите, клоун насилует в технических тоннелях!", | ||
"ХоС, ваши сотрудники украли у меня собаку и засунули ее в стиральную машину!", | ||
"Агент синдиката украл пиво из бара и взорвался!", | ||
"Врача! Позовите врача!" | ||
]; | ||
|
||
private void InitializeVoice() | ||
{ | ||
_random = IoCManager.Resolve<IRobustRandom>(); | ||
_ttsManager = IoCManager.Resolve<TTSManager>(); | ||
_ttsSystem = IoCManager.Resolve<IEntityManager>().System<TTSSystem>(); | ||
_voiceList = _prototypeManager.EnumeratePrototypes<TTSVoicePrototype>().Where(o => o.RoundStart).ToList(); | ||
|
||
VoiceButton.OnItemSelected += args => | ||
{ | ||
VoiceButton.SelectId(args.Id); | ||
SetVoice(_voiceList[args.Id].ID); | ||
}; | ||
|
||
VoicePlayButton.OnPressed += _ => { PlayTTS(); }; | ||
} | ||
|
||
private void UpdateTTSVoicesControls() | ||
{ | ||
if (Profile is null) | ||
return; | ||
|
||
VoiceButton.Clear(); | ||
|
||
var firstVoiceChoiceId = 1; | ||
for (var i = 0; i < _voiceList.Count; i++) | ||
{ | ||
var voice = _voiceList[i]; | ||
if (!HumanoidCharacterProfile.CanHaveVoice(voice, Profile.Sex)) | ||
continue; | ||
|
||
var name = Loc.GetString(voice.Name); | ||
VoiceButton.AddItem(name, i); | ||
|
||
if (firstVoiceChoiceId == 1) | ||
firstVoiceChoiceId = i; | ||
} | ||
|
||
var voiceChoiceId = _voiceList.FindIndex(x => x.ID == Profile.Voice); | ||
if (!VoiceButton.TrySelectId(voiceChoiceId) && VoiceButton.TrySelectId(firstVoiceChoiceId)) | ||
SetVoice(_voiceList[firstVoiceChoiceId].ID); | ||
} | ||
|
||
private void PlayTTS() | ||
{ | ||
if (Profile is null) | ||
return; | ||
|
||
_ttsSystem.StopCurrentTTS(PreviewDummy); | ||
_ttsManager.RequestTTS(PreviewDummy, _random.Pick(_sampleText), Profile.Voice); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Content.Shared._White.TTS; | ||
using Robust.Shared.Network; | ||
|
||
namespace Content.Client._White.TTS; | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public sealed class TTSManager | ||
{ | ||
[Dependency] private readonly IClientNetManager _netMgr = default!; | ||
[Dependency] private readonly EntityManager _entityManager = default!; | ||
|
||
public void Initialize() | ||
{ | ||
_netMgr.RegisterNetMessage<MsgRequestTTS>(); | ||
} | ||
|
||
// ReSharper disable once InconsistentNaming | ||
public void RequestTTS(EntityUid uid, string text, string voiceId) | ||
{ | ||
var netEntity = _entityManager.GetNetEntity(uid); | ||
var msg = new MsgRequestTTS { Text = text, Uid = netEntity, VoiceId = voiceId }; | ||
_netMgr.ClientSendMessage(msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System.IO; | ||
using Content.Shared._White; | ||
using Content.Shared._White.TTS.Events; | ||
using Robust.Client.Audio; | ||
using Robust.Shared.Audio; | ||
using Robust.Shared.Audio.Components; | ||
using Robust.Shared.Configuration; | ||
|
||
namespace Content.Client._White.TTS; | ||
|
||
// ReSharper disable InconsistentNaming | ||
public sealed class TTSSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IAudioManager _audioManager = default!; | ||
[Dependency] private readonly IConfigurationManager _cfg = default!; | ||
[Dependency] private readonly AudioSystem _audioSystem = default!; | ||
|
||
private float _volume; | ||
private readonly Dictionary<EntityUid, AudioComponent> _currentlyPlaying = new(); | ||
|
||
private readonly Dictionary<EntityUid, Queue<AudioStreamWithParams>> _enquedStreams = new(); | ||
|
||
// Same as Server.ChatSystem.VoiceRange | ||
private const float VoiceRange = 10; | ||
|
||
public override void Initialize() | ||
{ | ||
_cfg.OnValueChanged(WhiteCVars.TtsVolume, OnTtsVolumeChanged, true); | ||
|
||
SubscribeNetworkEvent<PlayTTSEvent>(OnPlayTTS); | ||
} | ||
|
||
public override void Shutdown() | ||
{ | ||
base.Shutdown(); | ||
_cfg.UnsubValueChanged(WhiteCVars.TtsVolume, OnTtsVolumeChanged); | ||
ClearQueues(); | ||
} | ||
|
||
public override void FrameUpdate(float frameTime) | ||
{ | ||
foreach (var (uid, audioComponent) in _currentlyPlaying) | ||
{ | ||
if (!Deleted(uid) && audioComponent is { Running: true, Playing: true } | ||
|| !_enquedStreams.TryGetValue(uid, out var queue) | ||
|| !queue.TryDequeue(out var toPlay)) | ||
continue; | ||
|
||
var audio = _audioSystem.PlayEntity(toPlay.Stream, uid, toPlay.Params); | ||
if (!audio.HasValue) | ||
continue; | ||
|
||
_currentlyPlaying[uid] = audio.Value.Component; | ||
} | ||
} | ||
|
||
private void OnTtsVolumeChanged(float volume) | ||
{ | ||
_volume = volume; | ||
} | ||
|
||
private void OnPlayTTS(PlayTTSEvent ev) | ||
{ | ||
PlayTTS(GetEntity(ev.Uid), ev.Data, ev.BoostVolume ? _volume + 5 : _volume); | ||
} | ||
|
||
public void PlayTTS(EntityUid uid, byte[] data, float volume) | ||
{ | ||
if (_volume <= -20f) | ||
return; | ||
|
||
var stream = CreateAudioStream(data); | ||
|
||
var audioParams = new AudioParams | ||
{ | ||
Volume = volume, | ||
MaxDistance = VoiceRange | ||
}; | ||
|
||
var audioStream = new AudioStreamWithParams(stream, audioParams); | ||
EnqueueAudio(uid, audioStream); | ||
} | ||
|
||
public void StopCurrentTTS(EntityUid uid) | ||
{ | ||
if (!_currentlyPlaying.TryGetValue(uid, out var audio)) | ||
return; | ||
|
||
_audioSystem.Stop(audio.Owner); | ||
} | ||
|
||
private void EnqueueAudio(EntityUid uid, AudioStreamWithParams audioStream) | ||
{ | ||
if (!_currentlyPlaying.ContainsKey(uid)) | ||
{ | ||
var audio = _audioSystem.PlayEntity(audioStream.Stream, uid, audioStream.Params); | ||
if (!audio.HasValue) | ||
return; | ||
|
||
_currentlyPlaying[uid] = audio.Value.Component; | ||
return; | ||
} | ||
|
||
if (_enquedStreams.TryGetValue(uid, out var queue)) | ||
{ | ||
queue.Enqueue(audioStream); | ||
return; | ||
} | ||
|
||
queue = new Queue<AudioStreamWithParams>(); | ||
queue.Enqueue(audioStream); | ||
_enquedStreams[uid] = queue; | ||
} | ||
|
||
private void ClearQueues() | ||
{ | ||
foreach (var (_, queue) in _enquedStreams) | ||
{ | ||
queue.Clear(); | ||
} | ||
} | ||
|
||
private AudioStream CreateAudioStream(byte[] data) | ||
{ | ||
var dataStream = new MemoryStream(data) { Position = 0 }; | ||
return _audioManager.LoadAudioOggVorbis(dataStream); | ||
} | ||
|
||
private record AudioStreamWithParams(AudioStream Stream, AudioParams Params); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.