-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Offline Speech Recognition #2089 (#2258) Fix build Remove Task Update according to comments Fix tizen Discard changes to samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj Discard changes to global.json Fix tizen Update ISpeechToText.shared.cs Co-authored-by: Shaun Lawrence <[email protected]> Update ISpeechToText.shared.cs Co-authored-by: Shaun Lawrence <[email protected]> Update samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/OfflineSpeechToTextViewModel.cs Fix xml comment Update sample
- Loading branch information
1 parent
725260b
commit c184ea2
Showing
34 changed files
with
1,129 additions
and
360 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
82 changes: 82 additions & 0 deletions
82
samples/CommunityToolkit.Maui.Sample/Pages/Essentials/OfflineSpeechToTextPage.xaml
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,82 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages" | ||
x:Class="CommunityToolkit.Maui.Sample.Pages.Essentials.OfflineSpeechToTextPage" | ||
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Essentials" | ||
xmlns:essentials="clr-namespace:CommunityToolkit.Maui.Sample.Pages.Essentials" | ||
x:TypeArguments="vm:OfflineSpeechToTextViewModel" | ||
x:DataType="vm:OfflineSpeechToTextViewModel" | ||
Title="OfflineSpeechToText"> | ||
|
||
<ScrollView> | ||
<VerticalStackLayout | ||
Spacing="20" | ||
Padding="30,0"> | ||
|
||
<Label | ||
Text="OfflineSpeechToText allows the user to convert speech to text in real time in offline." | ||
HorizontalTextAlignment="Center"/> | ||
|
||
<Label | ||
Text="State" | ||
FontAttributes="Bold"/> | ||
|
||
<Label | ||
Text="{Binding State}" | ||
FontSize="18" | ||
HorizontalOptions="Center" | ||
HorizontalTextAlignment="Center" | ||
MinimumHeightRequest="100" /> | ||
|
||
<Label | ||
Text="Language Output" | ||
FontAttributes="Bold"/> | ||
|
||
<Label | ||
Text="{Binding RecognitionText}" | ||
FontSize="18" | ||
HorizontalOptions="Center" | ||
HorizontalTextAlignment="Center" | ||
MinimumHeightRequest="100" /> | ||
|
||
<Border | ||
StrokeThickness="2" | ||
Stroke="#808080" | ||
StrokeShape="RoundRectangle 8,8,8,8" | ||
Padding="12"> | ||
<Border.Content> | ||
<Grid RowDefinitions="*,60" | ||
ColumnDefinitions="*,*" | ||
RowSpacing="12" | ||
ColumnSpacing="12"> | ||
|
||
<Button | ||
Grid.Row="0" | ||
Grid.Column="0" | ||
Text="StartListenAsync" | ||
Command="{Binding StartListenCommand}" | ||
HorizontalOptions="End" /> | ||
|
||
<Button | ||
Grid.Row="0" | ||
Grid.Column="1" | ||
Text="StopListenAsync" | ||
Command="{Binding StopListenCommand}" | ||
HorizontalOptions="Start" /> | ||
|
||
<Label | ||
Grid.Row="1" | ||
Grid.ColumnSpan="2" | ||
Text="The `StartListenAsync` API starts the speech-to-text service and shares the results using `RecognitionResultUpdated` event and `RecognitionResultCompleted` event." | ||
HorizontalOptions="Center" | ||
HorizontalTextAlignment="Center" | ||
FontSize="12"/> | ||
|
||
</Grid> | ||
</Border.Content> | ||
</Border> | ||
</VerticalStackLayout> | ||
</ScrollView> | ||
|
||
</pages:BasePage> |
11 changes: 11 additions & 0 deletions
11
samples/CommunityToolkit.Maui.Sample/Pages/Essentials/OfflineSpeechToTextPage.xaml.cs
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,11 @@ | ||
using CommunityToolkit.Maui.Sample.ViewModels.Essentials; | ||
|
||
namespace CommunityToolkit.Maui.Sample.Pages.Essentials; | ||
|
||
public partial class OfflineSpeechToTextPage : BasePage<OfflineSpeechToTextViewModel> | ||
{ | ||
public OfflineSpeechToTextPage(OfflineSpeechToTextViewModel viewModel) : base(viewModel) | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/OfflineSpeechToTextViewModel.cs
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.Globalization; | ||
using CommunityToolkit.Maui.Alerts; | ||
using CommunityToolkit.Maui.Media; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
|
||
namespace CommunityToolkit.Maui.Sample.ViewModels.Essentials; | ||
|
||
public partial class OfflineSpeechToTextViewModel : BaseViewModel | ||
{ | ||
readonly ISpeechToText speechToText; | ||
|
||
public SpeechToTextState? State => speechToText.CurrentState; | ||
|
||
[ObservableProperty] | ||
string? recognitionText = "Welcome to .NET MAUI Community Toolkit!"; | ||
|
||
public OfflineSpeechToTextViewModel() | ||
{ | ||
// For demo purposes. You can resolve dependency from the DI container, | ||
speechToText = OfflineSpeechToText.Default; | ||
|
||
speechToText.StateChanged += HandleSpeechToTextStateChanged; | ||
speechToText.RecognitionResultCompleted += HandleRecognitionResultCompleted; | ||
} | ||
|
||
[RelayCommand] | ||
async Task StartListen() | ||
{ | ||
var isGranted = await speechToText.RequestPermissions(CancellationToken.None); | ||
if (!isGranted) | ||
{ | ||
await Toast.Make("Permission not granted").Show(CancellationToken.None); | ||
return; | ||
} | ||
|
||
const string beginSpeakingPrompt = "Begin speaking..."; | ||
|
||
RecognitionText = beginSpeakingPrompt; | ||
|
||
speechToText.RecognitionResultUpdated += HandleRecognitionResultUpdated; | ||
|
||
await speechToText.StartListenAsync(new SpeechToTextOptions | ||
{ | ||
Culture = CultureInfo.CurrentCulture, | ||
ShouldReportPartialResults = true | ||
}, CancellationToken.None); | ||
|
||
if (RecognitionText is beginSpeakingPrompt) | ||
{ | ||
RecognitionText = string.Empty; | ||
} | ||
} | ||
|
||
[RelayCommand] | ||
Task StopListen() | ||
{ | ||
speechToText.RecognitionResultUpdated -= HandleRecognitionResultUpdated; | ||
|
||
return speechToText.StopListenAsync(CancellationToken.None); | ||
} | ||
|
||
void HandleRecognitionResultUpdated(object? sender, SpeechToTextRecognitionResultUpdatedEventArgs e) | ||
{ | ||
RecognitionText += e.RecognitionResult; | ||
} | ||
|
||
void HandleRecognitionResultCompleted(object? sender, SpeechToTextRecognitionResultCompletedEventArgs e) | ||
{ | ||
RecognitionText = e.RecognitionResult.IsSuccessful ? e.RecognitionResult.Text : e.RecognitionResult.Exception.Message; | ||
} | ||
|
||
void HandleSpeechToTextStateChanged(object? sender, SpeechToTextStateChangedEventArgs e) | ||
{ | ||
OnPropertyChanged(nameof(State)); | ||
} | ||
} |
Oops, something went wrong.