Skip to content

Commit

Permalink
Azure recognizer language detection added
Browse files Browse the repository at this point in the history
  • Loading branch information
PaciStardust committed Sep 30, 2022
1 parent 63ac1f1 commit 9efdae8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
3 changes: 2 additions & 1 deletion OscMultitool/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,11 @@ public int TranslationMaxTextLength
//Azure
public string AzureRegion { get; set; } = string.Empty;
public string AzureKey { get; set; } = string.Empty;
public string AzureLanguage { get; set; } = string.Empty;
public string AzureSpeechLanguage { get; set; } = string.Empty;
public string AzureCustomEndpoint { get; set; } = string.Empty;
public string AzureVoice { get; set; } = string.Empty;
public List<string> AzurePhrases { get; set; } = new();
public List<string> AzureRecognitionLanguages { get; set; } = new();

//Usage
public bool TranslateTts { get; set; } = false;
Expand Down
4 changes: 2 additions & 2 deletions OscMultitool/Services/Api/Synthesizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static void ReloadClient()
if (!string.IsNullOrWhiteSpace(Config.Api.AzureVoice))
speechCfg.SpeechSynthesisVoiceName = Config.Api.AzureVoice;

if (!string.IsNullOrWhiteSpace(Config.Api.AzureLanguage))
speechCfg.SpeechSynthesisLanguage = Config.Api.AzureLanguage;
if (!string.IsNullOrWhiteSpace(Config.Api.AzureSpeechLanguage))
speechCfg.SpeechSynthesisLanguage = Config.Api.AzureSpeechLanguage;

speechCfg.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm);

Expand Down
33 changes: 24 additions & 9 deletions OscMultitool/Services/Speech/Recognizers/RecognizerAzure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ protected override bool StartInternal()
if (!string.IsNullOrWhiteSpace(Config.Api.AzureCustomEndpoint))
speechConfig.EndpointId = Config.Api.AzureCustomEndpoint;

if (!string.IsNullOrWhiteSpace(Config.Api.AzureLanguage))
speechConfig.SpeechRecognitionLanguage = Config.Api.AzureLanguage;

rec = new(speechConfig, audioConfig);
if (Config.Api.AzureRecognitionLanguages.Count > 1)
{
speechConfig.SetProperty(PropertyId.SpeechServiceConnection_ContinuousLanguageIdPriority, "Latency");
var autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.FromLanguages(Config.Api.AzureRecognitionLanguages.ToArray());
rec = new(speechConfig, autoDetectSourceLanguageConfig, audioConfig);
}
else
{
if (Config.Api.AzureRecognitionLanguages.Count == 1)
speechConfig.SpeechRecognitionLanguage = Config.Api.AzureRecognitionLanguages[0];
rec = new(speechConfig, audioConfig);
}

var phraseList = PhraseListGrammar.FromRecognizer(rec);
foreach(var phrase in Config.Api.AzurePhrases)
phraseList.AddPhrase(phrase);
if (Config.Api.AzurePhrases.Count != 0)
{
var phraseList = PhraseListGrammar.FromRecognizer(rec);
foreach (var phrase in Config.Api.AzurePhrases)
phraseList.AddPhrase(phrase);
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -123,9 +134,13 @@ private async Task StartRecognizing()
#region Events
private void OnRecognized(object? sender, SpeechRecognitionEventArgs e)
{
Logger.Log("Got Message: " + e.Result.Text);
var result = e.Result.Text;
if (string.IsNullOrWhiteSpace(result))
return;

Logger.Log("Got Message: " + result);

var message = Denoise(e.Result.Text);
var message = Denoise(result);
if (string.IsNullOrWhiteSpace(message))
return;

Expand Down
15 changes: 9 additions & 6 deletions OscMultitool/Ui/Pages/PageApi.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@
<TextBlock Style="{DynamicResource HTextBlock}" Text="Custom endpoint (Optional)" Margin="0,4,0,0"/>
<TextBox Text="{Binding Api.AzureCustomEndpoint, Source={x:Static local:Config.Data}}" Tag="Custom endpoint..." Style="{StaticResource HTextBoxLong}" PreviewKeyDown="TextBox_PreviewKeyDown"/>

<WrapPanel Margin="0,4,0,0">
<TextBox HorizontalAlignment="Left" Text="{Binding Api.AzureLanguage, Source={x:Static local:Config.Data}}" Tag="Language..." Style="{StaticResource HTextBoxShort}" PreviewKeyDown="TextBox_PreviewKeyDown"/>
<Label Content="Used Language (Optional)" VerticalAlignment="Center"/>
</WrapPanel>

<Label Content="Speech Recognition Options (Optional)" Margin="0,8,0,0"/>
<StackPanel Style="{StaticResource HStackIndent}">
<Button Content="Edit phrase list" Click="Button_EditPhrases" HorizontalAlignment="Left"/>
<WrapPanel>
<Button Content="Edit phrases" Click="Button_EditPhrases"/>
<Button Content="Edit languages" Click="Button_EditLanguages" Margin="16,0,0,0"/>
</WrapPanel>
</StackPanel>

<Label Content="Text-to-Speech Options (Optional)" Margin="0,8,0,0"/>
<StackPanel Style="{StaticResource HStackIndent}">
<CheckBox Content="Use Azure TTS" IsChecked="{Binding Api.UseAzureTts, Source={x:Static local:Config.Data}}"/>
<TextBlock Style="{DynamicResource HTextBlock}" Text="Output Speech Voice" Margin="0,4,0,0"/>
<TextBox Text="{Binding Api.AzureVoice, Source={x:Static local:Config.Data}}" Tag="Speech Voice..." Style="{StaticResource HTextBoxLong}" PreviewKeyDown="TextBox_PreviewKeyDown"/>

<WrapPanel Margin="0,4,0,0">
<TextBox HorizontalAlignment="Left" Text="{Binding Api.AzureSpeechLanguage, Source={x:Static local:Config.Data}}" Tag="Language..." Style="{StaticResource HTextBoxShort}" PreviewKeyDown="TextBox_PreviewKeyDown"/>
<Label Content="Used Language" VerticalAlignment="Center"/>
</WrapPanel>
</StackPanel>
</StackPanel>
</StackPanel>
Expand Down
3 changes: 3 additions & 0 deletions OscMultitool/Ui/Pages/PageApi.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private void Button_ReloadSynthesizer(object sender, RoutedEventArgs e)

private void Button_EditPhrases(object sender, RoutedEventArgs e)
=> UiHelper.OpenListEditor("Edit phrases", "Phrase", Config.Api.AzurePhrases, "New Phrase");

private void Button_EditLanguages(object sender, RoutedEventArgs e)
=> UiHelper.OpenListEditor("Edit languages", "Language", Config.Api.AzureRecognitionLanguages, "New Language");
#endregion

#region SelectionChanged
Expand Down
4 changes: 2 additions & 2 deletions OscMultitool/Ui/Pages/PageConfig.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<CheckBox Content="Automatically check for updates" IsChecked="{Binding Debug.CheckUpdates, Source={x:Static local:Config.Data}}" Margin="0,4,0,0"/>
<WrapPanel Margin="0,4,0,0">
<Button Content="Open Docs" HorizontalAlignment="Left" Click="Button_OpenDocs"/>
<Button Margin="8,0,0,0" Content="Open Config" HorizontalAlignment="Left" Click="Button_OpenConfig"/>
<Button Margin="8,0,0,0" Content="Check Update" HorizontalAlignment="Left" Click="Button_CheckUpdate"/>
<Button Margin="16,0,0,0" Content="Open Config" HorizontalAlignment="Left" Click="Button_OpenConfig"/>
<Button Margin="16,0,0,0" Content="Check Update" HorizontalAlignment="Left" Click="Button_CheckUpdate"/>
</WrapPanel>
<Button Margin="0,8,0,0" Content="Reload devices" Foreground="{x:Static ui:UiHelper.ColorInvalid}" HorizontalAlignment="Left" Click="Button_ReloadDevices"/>
</StackPanel>
Expand Down

0 comments on commit 9efdae8

Please sign in to comment.