Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Added language selection and API key fields
Browse files Browse the repository at this point in the history
  • Loading branch information
demonixis committed Dec 20, 2022
1 parent 3d5f877 commit 69c9ae1
Show file tree
Hide file tree
Showing 17 changed files with 2,239 additions and 740 deletions.
21 changes: 11 additions & 10 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b842c3e13cce4feeb4d1390f5e39ce03, type: 3}
m_Name:
m_EditorClassIdentifier:
_samplerate: 22000
--- !u!82 &1033768237
AudioSource:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1711,61 +1712,61 @@ MonoBehaviour:
Min: 75
Max: 135
Sequence:
Frequency: 2
Frequency: 3
- Servo: 1
RandomRange: 1
Min: 75
Max: 135
Sequence:
Frequency: 2
Frequency: 3
- Servo: 5
RandomRange: 1
Min: 75
Max: 115
Sequence:
Frequency: 2
Frequency: 5
- Servo: 6
RandomRange: 1
Min: 75
Max: 115
Sequence:
Frequency: 2
Frequency: 5
- Servo: 13
RandomRange: 1
Min: 70
Max: 120
Sequence:
Frequency: 4
Frequency: 5
- Servo: 19
RandomRange: 1
Min: 0
Max: 180
Sequence:
Frequency: 4
Frequency: 5
- Servo: 18
RandomRange: 1
Min: 0
Max: 180
Sequence:
Frequency: 2.5
Frequency: 5
- Servo: 15
RandomRange: 1
Min: 80
Max: 110
Sequence:
Frequency: 3
Frequency: 5
- Servo: 16
RandomRange: 1
Min: 80
Max: 110
Sequence:
Frequency: 3
Frequency: 5
- Servo: 17
RandomRange: 1
Min: 120
Max: 140
Sequence:
Frequency: 3
Frequency: 5
--- !u!1 &1314284959
GameObject:
m_ObjectHideFlags: 0
Expand Down
9 changes: 4 additions & 5 deletions Assets/Scripts/Robot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public sealed class Robot : MonoBehaviour
private static Robot _instance;
private const string ServiceListFilename = "services.json";
private const string SystemListFilename = "systems.json";

private BrainSpeechProxy _brainSpeechProxy;
private List<RobotService> _currentServices;
private List<Action> _waitingStartCallbacks;

Expand Down Expand Up @@ -52,6 +50,7 @@ public static Robot Instance
/// </summary>
public RobotService[] AllServices => FindObjectsOfType<RobotService>(true);

public BrainSpeechProxy BrainSpeechProxy { get; private set; }
public bool Started { get; private set; }

public event Action<Robot> RobotInitialized;
Expand All @@ -67,7 +66,7 @@ private void Awake()
return;
}

_brainSpeechProxy = new BrainSpeechProxy();
BrainSpeechProxy = new BrainSpeechProxy();
_currentServices = new List<RobotService>();
_waitingStartCallbacks = new List<Action>();
}
Expand Down Expand Up @@ -115,7 +114,7 @@ private void InitializeServices()

if (!serviceList.IsValid())
serviceList = ServiceList.New();

// Select service selected by the user
var chatbotService = SelectService<ChatbotService>(serviceList.Chatbot);
var voiceRecognition = SelectService<VoiceRecognitionService>(serviceList.VoiceRecognition);
Expand All @@ -125,7 +124,7 @@ private void InitializeServices()
SelectService<NavigationService>(serviceList.Navigation);
SelectService<ComputerVisionService>(serviceList.ComputerVision);

_brainSpeechProxy.Setup(chatbotService, voiceRecognition, speechSynthesis);
BrainSpeechProxy.Setup(chatbotService, voiceRecognition, speechSynthesis);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Services/Brain/AIMLNetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void InitializeBrain()
_aimlBot.isAcceptingUserInput = true;
}

public override void SetCulture(string culture)
public override void SetLanguage(string culture)
{
if (!IsLanguageSupported(culture))
{
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/Services/Brain/BrainSpeechProxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Demonixis.InMoov.Chatbots;
using Demonixis.InMoov.Settings;

namespace Demonixis.InMoov.Services.Speech
{
Expand All @@ -17,6 +18,16 @@ public void Setup(ChatbotService chatbotService, VoiceRecognitionService voiceRe

_chatbot.ResponseReady += OnChatbotResponse;
_voiceRecognition.PhraseDetected += OnVoiceRecognized;

var settings = GlobalSettings.Get();
SetLanguage(settings.Language);
}

public void SetLanguage(string language)
{
_chatbot.SetLanguage(language);
_voiceRecognition.SetLanguage(language);
_speechSynthesis.SetLanguage(language);
}

private void OnChatbotResponse(string response)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Services/Brain/ChatbotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class ChatbotService : RobotService

public event Action<string> ResponseReady;

public abstract void SetCulture(string culture);
public abstract void SetLanguage(string culture);

public void SubmitResponse(string inputSpeech)
{
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Services/Brain/OpenAIChatbot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OpenAIChatbot : ChatbotService

public override void Initialize()
{
var settings = GlobalSettings.GetInstance();
var settings = GlobalSettings.Get();
var key = settings.OpenAIKey;

if (string.IsNullOrEmpty(key))
Expand All @@ -28,7 +28,7 @@ public override void Initialize()
base.Initialize();
}

public override void SetCulture(string culture)
public override void SetLanguage(string culture)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ public override void Shutdown()
private IEnumerator SpeechLoop(string message)
{
NotifySpeechState(true, message);

yield return CoroutineFactory.WaitForSeconds(GetSpeakTime(message));

var waitTime = GetSpeakTime(message);
yield return CoroutineFactory.WaitForSeconds(waitTime);

NotifySpeechState(false, null);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Services/Speech/SAMSpeechSynthesis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public sealed class SAMSpeechSynthesis : SpeechSynthesisService
{
private AudioSource _audioSource;

[SerializeField] private int _samplerate = 44100;
[SerializeField] private int _samplerate = 22000;

private void Start()
{
Expand Down
11 changes: 9 additions & 2 deletions Assets/Scripts/Services/Speech/SpeechSynthesisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@ public class SpeechSynthesisService : RobotService
public event Action<string> SpeechStarted;
public event Action SpeechFinished;

public virtual void SetCulture(string culture)
public virtual void SetLanguage(string culture)
{
}

public virtual string[] GetVoices() => null;

public virtual void SelectVoice(string voiceName)
{

}

public virtual void Speak(string message)
{
}

public float GetSpeakTime(string sentence, int wordsPerMinute = 40)
public float GetSpeakTime(string sentence, int wordsPerMinute = 100)
{
var words = sentence.Split(' ');
return words.Length * 60.0f / wordsPerMinute;
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Services/Speech/VoiceRSS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public override void Initialize()
{
base.Initialize();

var settings = GlobalSettings.GetInstance();
var settings = GlobalSettings.Get();
_ttsManager.APIKey = settings.VoiceRSSKey;

if (string.IsNullOrEmpty(_ttsManager.APIKey))
Expand All @@ -34,7 +34,7 @@ public override void Initialize()
}
}

public override void SetCulture(string culture)
public override void SetLanguage(string culture)
{
switch (culture)
{
Expand Down
11 changes: 3 additions & 8 deletions Assets/Scripts/Services/Speech/VoiceRecognitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ private void InternalInitialize()
{
var robot = Robot.Instance;
var speech = robot.GetService<SpeechSynthesisService>();
speech.SpeechStarted += _ => OnSpeechLocked(true);
speech.SpeechFinished += () => OnSpeechLocked(false);
speech.SpeechStarted += _ => _isLocked = true;
speech.SpeechFinished += () => _isLocked = false;
}

public virtual void SetCulture(string culture)
public virtual void SetLanguage(string culture)
{
}

protected virtual void OnSpeechLocked(bool locked)
{
_isLocked = locked;
}

protected void NotifyPhraseDetected(string phrase)
{
PhraseDetected?.Invoke(phrase);
Expand Down
48 changes: 40 additions & 8 deletions Assets/Scripts/Services/Speech/VoskVoiceRecognitionService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.IO;
using UnityEngine;
using Yetibyte.Unity.SpeechRecognition;

Expand All @@ -6,25 +7,51 @@ namespace Demonixis.InMoov.Services.Speech
[RequireComponent(typeof(VoskListener))]
public sealed class VoskVoiceRecognitionService : VoiceRecognitionService
{
private VoskListener _voskListener;

public override void Initialize()
{
base.Initialize();

var vosk = GetComponent<VoskListener>();
vosk.LoadModel();
vosk.StartListening();
vosk.ResultFound += Vosk_ResultFound;
_voskListener = GetComponent<VoskListener>();
_voskListener.LoadModel();
_voskListener.StartListening();
_voskListener.ResultFound += Vosk_ResultFound;
}

public override void SetLanguage(string culture)
{
if (culture.Contains("fr-"))
TryLoadModel("-fr-");
else if (culture.Contains("en-"))
TryLoadModel("-en-");
}

private void TryLoadModel(string langPattern)
{
var model = FindVoskModel(langPattern);
if (model == null) return;
_voskListener.ReloadModel(model);
}

public override void SetCulture(string culture)
private string FindVoskModel(string langPattern)
{

var voskPath = $"{Application.streamingAssetsPath}/VoskModels/";
var voskModels = Directory.GetDirectories(voskPath);

foreach (var model in voskModels)
{
if (model.Contains(langPattern))
return model;
}

return null;
}

public override void SetPaused(bool paused)
{
base.SetPaused(paused);

var vosk = GetComponent<VoskListener>();
if (paused)
vosk.StopListening();
Expand All @@ -34,7 +61,12 @@ public override void SetPaused(bool paused)

private void Vosk_ResultFound(object sender, VoskResultEventArgs e)
{
if (!CanListen) return;
if (!CanListen)
{
Debug.Log("Can't listen");
return;
}

NotifyPhraseDetected(e.Result.Text);
}

Expand Down
Loading

0 comments on commit 69c9ae1

Please sign in to comment.