Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LICENSE to Examples/Godot, change tabs to spaces #34

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Examples/Godot/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Godot 4+ specific ignores
.godot/
.vs/

# Godot-specific ignores
.import/
export.cfg
export_presets.cfg

# Imported translations (automatically generated from CSV files)
*.translation

# Mono-specific ignores
.mono/
data_*/
mono_crash.*.json
348 changes: 174 additions & 174 deletions Examples/Godot/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@

public partial class Demo : Node2D
{
// User interface
private TextEdit _textEdit;
private Label _label;
private Button _downloadButton;
private Button _transcribeButton;
private Button _speakButton;
private Button _stopButton;
private Container _buttons;
private MenuButton _languageMenu;

private Speech _speech;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_SetupComponents();
_SetupSpeech();
_ChangeLanguage("English");
}

private void _SetupComponents()
{
_label = GetNode<Label>("CanvasLayer/VBoxContainer/StatusLabel");
_transcribeButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/TranscriptButton");
_transcribeButton.Connect("pressed", new Callable(this, "OnTranscriptClick"));
_speakButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/SpeakButton");
_speakButton.Connect("pressed", new Callable(this, "OnSpeakClick"));
_stopButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/StopButton");
_stopButton.Connect("pressed", new Callable(this, "OnStopClick"));
_textEdit = GetNode<TextEdit>("CanvasLayer/VBoxContainer/TextEdit");
_downloadButton = GetNode<Button>("CanvasLayer/VBoxContainer/DownloadButton");
_downloadButton.Connect("pressed", new Callable(this, "OnDownloadClick"));
_buttons = GetNode<Container>("CanvasLayer/VBoxContainer/Buttons");
_languageMenu = GetNode<MenuButton>("CanvasLayer/VBoxContainer/GridContainer/MenuButton");
_languageMenu.GetPopup().Connect("index_pressed", new Callable(this, "OnLanguageMenu"));
}

private void _SetupSpeech()
{
_speech = GetNode<Speech>("Speech");
// User interface
private TextEdit _textEdit;
private Label _label;
private Button _downloadButton;
private Button _transcribeButton;
private Button _speakButton;
private Button _stopButton;
private Container _buttons;
private MenuButton _languageMenu;

private Speech _speech;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_SetupComponents();
_SetupSpeech();
_ChangeLanguage("English");
}

private void _SetupComponents()
{
_label = GetNode<Label>("CanvasLayer/VBoxContainer/StatusLabel");
_transcribeButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/TranscriptButton");
_transcribeButton.Connect("pressed", new Callable(this, "OnTranscriptClick"));
_speakButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/SpeakButton");
_speakButton.Connect("pressed", new Callable(this, "OnSpeakClick"));
_stopButton = GetNode<Button>("CanvasLayer/VBoxContainer/Buttons/StopButton");
_stopButton.Connect("pressed", new Callable(this, "OnStopClick"));
_textEdit = GetNode<TextEdit>("CanvasLayer/VBoxContainer/TextEdit");
_downloadButton = GetNode<Button>("CanvasLayer/VBoxContainer/DownloadButton");
_downloadButton.Connect("pressed", new Callable(this, "OnDownloadClick"));
_buttons = GetNode<Container>("CanvasLayer/VBoxContainer/Buttons");
_languageMenu = GetNode<MenuButton>("CanvasLayer/VBoxContainer/GridContainer/MenuButton");
_languageMenu.GetPopup().Connect("index_pressed", new Callable(this, "OnLanguageMenu"));
}

private void _SetupSpeech()
{
_speech = GetNode<Speech>("Speech");
_speech.SpeakEnd += () =>
{
UpdateButtons();
Expand All @@ -57,147 +57,147 @@ private void _SetupSpeech()
};
_speech.Recognized += (string text) =>
{
_textEdit.Text = text;
_textEdit.Text = text;
};
_speech.DownloadEnd += (bool success) =>
{
if (success)
{
_ModelsDownloaded();
_speech.DownloadEnd += (bool success) =>
{
if (success)
{
_ModelsDownloaded();
}
else
{
else
{
_downloadButton.Disabled = false;
_SetStatusText("Error downloading models");
}
};
}

public override void _ExitTree()
{
}

private void UpdateButtons()
{
_transcribeButton.Disabled = _speech.IsTranscribing;
_speakButton.Disabled = _speech.IsSpeaking;
_stopButton.Disabled = !_speech.IsSpeaking && !_speech.IsTranscribing;
}

public void OnLanguageMenu(int index)
{
if (index == 0)
{
_ChangeLanguage("English");
}
else if (index == 1)
{
_ChangeLanguage("German");
}
else
{
throw new ArgumentException();
}
}

public void OnDownloadClick()
{
_downloadButton.Disabled = true;
_speech.DownloadAllModelFiles();
}

public void OnTranscriptClick()
{
if (!_speech.IsTranscribing)
{
_speech.StartTranscribe();
_SetStatusText("Silent");
UpdateButtons();
}
}

public void OnSpeakClick()
{
string text = _textEdit.Text;
if (!string.IsNullOrWhiteSpace(text))
{
try
{
_speech.SpeakText(text);
}
catch (Exception ex)
{
GD.Print(ex.ToString());
}

UpdateButtons();
}
}

public void OnStopClick()
{
_speech.StopTranscribe();
_speech.CancelSpeak();
_SetStatusText("");
UpdateButtons();
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
var status = _speech.GetDownloadStatus();
if (status != null)
{
_label.Text = string.Format("Loading {0} {1}% ({1}KB)", status.FileName, status.Percent, status.FileSize);
}
}

private void _ChangeLanguage(string language)
{
GD.Print(string.Format("Language={0}", language));
_speech.Language = language;
_languageMenu.Text = language;
if (_speech.CheckAllModelFiles())
{
_ModelsDownloaded();
}
else
{
_downloadButton.Show();
_buttons.Hide();
}
}

private void _ModelsDownloaded()
{
try
{
_speech.LoadAllModels();
GD.Print("SpeechSynthesizer ready");

if (_speech.Language == "English")
{
_textEdit.Text = "The quick brown fox jumps over the lazy dog.";
}
else if (_speech.Language == "German")
{
_textEdit.Text = "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.";
}

_downloadButton.Hide();
_buttons.Show();
UpdateButtons();
_SetStatusText("");
}
catch (Exception)
{
_downloadButton.Disabled = false;
_SetStatusText("Error building models");
}
}

private void _SetStatusText(string text)
{
_label.Text = text;
}
{
}

private void UpdateButtons()
{
_transcribeButton.Disabled = _speech.IsTranscribing;
_speakButton.Disabled = _speech.IsSpeaking;
_stopButton.Disabled = !_speech.IsSpeaking && !_speech.IsTranscribing;
}

public void OnLanguageMenu(int index)
{
if (index == 0)
{
_ChangeLanguage("English");
}
else if (index == 1)
{
_ChangeLanguage("German");
}
else
{
throw new ArgumentException();
}
}

public void OnDownloadClick()
{
_downloadButton.Disabled = true;
_speech.DownloadAllModelFiles();
}

public void OnTranscriptClick()
{
if (!_speech.IsTranscribing)
{
_speech.StartTranscribe();
_SetStatusText("Silent");
UpdateButtons();
}
}

public void OnSpeakClick()
{
string text = _textEdit.Text;
if (!string.IsNullOrWhiteSpace(text))
{
try
{
_speech.SpeakText(text);
}
catch (Exception ex)
{
GD.Print(ex.ToString());
}

UpdateButtons();
}
}

public void OnStopClick()
{
_speech.StopTranscribe();
_speech.CancelSpeak();
_SetStatusText("");
UpdateButtons();
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
var status = _speech.GetDownloadStatus();
if (status != null)
{
_label.Text = string.Format("Loading {0} {1}% ({1}KB)", status.FileName, status.Percent, status.FileSize);
}
}

private void _ChangeLanguage(string language)
{
GD.Print(string.Format("Language={0}", language));
_speech.Language = language;
_languageMenu.Text = language;
if (_speech.CheckAllModelFiles())
{
_ModelsDownloaded();
}
else
{
_downloadButton.Show();
_buttons.Hide();
}
}

private void _ModelsDownloaded()
{
try
{
_speech.LoadAllModels();
GD.Print("SpeechSynthesizer ready");

if (_speech.Language == "English")
{
_textEdit.Text = "The quick brown fox jumps over the lazy dog.";
}
else if (_speech.Language == "German")
{
_textEdit.Text = "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.";
}

_downloadButton.Hide();
_buttons.Show();
UpdateButtons();
_SetStatusText("");
}
catch (Exception)
{
_downloadButton.Disabled = false;
_SetStatusText("Error building models");
}
}

private void _SetStatusText(string text)
{
_label.Text = text;
}
}
Loading