-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added speech recognition, but it's not source gen'd
- Loading branch information
1 parent
f622c6a
commit 8a077d3
Showing
40 changed files
with
1,437 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@typeparam T where T : class | ||
|
||
@if (Value is not null) | ||
{ | ||
<pre class="bg-dark @(_textClass) fs-4 p-4"> | ||
<code> | ||
@{ | ||
var opts = new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
}; | ||
} | ||
@Value?.ToJson(opts) | ||
</code> | ||
</pre> | ||
} | ||
|
||
@code { | ||
[Parameter, EditorRequired] | ||
public T? Value { get; set; } = default!; | ||
|
||
[Parameter] | ||
public bool IsError { get; set; } | ||
|
||
string _textClass => IsError ? "text-warning" : "text-info"; | ||
} |
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,29 @@ | ||
@page "/listen" | ||
|
||
<PageTitle>Speech-to-text</PageTitle> | ||
|
||
<p>This page demonstrates the source generated <code>Blazor.SpeechRecongition.WebAssembly</code> package.</p> | ||
|
||
<div class="mb-3 w-75"> | ||
<label for="text" class="form-label"> | ||
Speech-to-text | ||
</label> | ||
<textarea class="form-control" id="text" | ||
readonly=@_isRecognizingSpeech rows="6" @bind=@_transcript> | ||
</textarea> | ||
</div> | ||
<Code Value=@_errorEvent T=SpeechRecognitionErrorEvent IsError=true /> | ||
|
||
<button type="button" class="btn btn-lg btn-primary" | ||
@onclick=@OnRecognizeSpeechClick> | ||
@if (_isRecognizingSpeech) | ||
{ | ||
<span class="oi oi-media-stop"></span> | ||
<span class="ps-2">Stop</span> | ||
} | ||
else | ||
{ | ||
<span class="oi oi-microphone"></span> | ||
<span class="ps-2">Listen</span> | ||
} | ||
</button> |
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,76 @@ | ||
// Copyright (c) David Pine. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Blazor.ExampleConsumer.Pages; | ||
|
||
public sealed partial class ListenToMe : IAsyncDisposable | ||
{ | ||
bool _isRecognizingSpeech = false; | ||
SpeechRecognitionErrorEvent? _errorEvent; | ||
string? _transcript; | ||
|
||
[Inject] | ||
public ISpeechRecognitionService SpeechRecognition { get; set; } = null!; | ||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
await SpeechRecognition.InitializeModuleAsync(); | ||
} | ||
} | ||
|
||
void OnRecognizeSpeechClick() | ||
{ | ||
if (_isRecognizingSpeech) | ||
{ | ||
SpeechRecognition.CancelSpeechRecognition(false); | ||
} | ||
else | ||
{ | ||
var bcp47Tag = CurrentUICulture.Name; | ||
SpeechRecognition.RecognizeSpeech( | ||
bcp47Tag, | ||
OnRecognized, | ||
OnError, | ||
OnStarted, | ||
OnEnded); | ||
} | ||
} | ||
|
||
void OnStarted() | ||
{ | ||
_isRecognizingSpeech = true; | ||
StateHasChanged(); | ||
} | ||
|
||
void OnEnded() | ||
{ | ||
_isRecognizingSpeech = false; | ||
StateHasChanged(); | ||
} | ||
|
||
void OnError(SpeechRecognitionErrorEvent errorEvent) | ||
{ | ||
_errorEvent = errorEvent; | ||
StateHasChanged(); | ||
} | ||
|
||
void OnRecognized(string transcript) | ||
{ | ||
_transcript = _transcript switch | ||
{ | ||
null => transcript, | ||
_ => $"{_transcript.Trim()} {transcript}".Trim() | ||
}; | ||
StateHasChanged(); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (SpeechRecognition is not null) | ||
{ | ||
await SpeechRecognition.DisposeAsync(); | ||
} | ||
} | ||
} |
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
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.