From 4abb74d45e3ef68c12b41fb69052555b63f6f1a0 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 11 May 2022 12:40:45 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/BaiduRecognizerProvider.cs | 47 ++++++++++++------- .../Services/BaiduSynthesizerProvider.cs | 26 ++++++++-- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs index 73556028a..cdf111bc3 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.JSInterop; using System.Text; @@ -26,16 +27,19 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable private Baidu.Aip.Speech.Asr Client { get; } + private ILogger Logger { get; } + /// /// 构造函数 /// /// /// - public BaiduRecognizerProvider(IOptionsMonitor options, IJSRuntime runtime) + public BaiduRecognizerProvider(IOptionsMonitor options, IJSRuntime runtime, ILogger logger) { JSRuntime = runtime; SpeechOption = options.CurrentValue; Client = new Baidu.Aip.Speech.Asr(SpeechOption.AppId, SpeechOption.ApiKey, SpeechOption.Secret); + Logger = logger; } /// @@ -46,18 +50,19 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable /// public async Task InvokeAsync(RecognizerOption option) { - if (string.IsNullOrEmpty(option.MethodName)) - { - throw new InvalidOperationException(); - } - - Option = option; - if (Module == null) + if (!string.IsNullOrEmpty(option.MethodName)) { - Module = await JSRuntime.InvokeAsync("import", "/_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js"); + Option = option; + if (Module == null) + { + var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js"; + Logger.LogInformation($"load module {moduleName}"); + Module = await JSRuntime.InvokeAsync("import", moduleName); + } + Interop ??= DotNetObjectReference.Create(this); + await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds); + Logger.LogInformation($"{Option.MethodName}"); } - Interop ??= DotNetObjectReference.Create(this); - await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds); } /// @@ -66,17 +71,27 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable [JSInvokable] public async Task RecognizeCallback(RecognizerStatus status, byte[]? bytes) { + Logger.LogInformation($"RecognizerStatus: {status}"); string data = "Error"; if (status == RecognizerStatus.Finished) { var result = Client.Recognize(bytes, "wav", 16000); - var sb = new StringBuilder(); - var text = result["result"].ToArray(); - foreach (var item in text) + var err_no = result.Value("err_no"); + if (err_no == 0) + { + var sb = new StringBuilder(); + var text = result["result"].ToArray(); + foreach (var item in text) + { + sb.Append(item.ToString()); + } + data = sb.ToString(); + Logger.LogInformation($"recognizer: {data}"); + } + else { - sb.Append(item.ToString()); + Logger.LogError($"err_no: {err_no}"); } - data = sb.ToString(); } if (Option.Callback != null) diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs index 1aa03b8b2..8f8c35587 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduSynthesizerProvider.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.JSInterop; @@ -25,16 +26,20 @@ public class BaiduSynthesizerProvider : ISynthesizerProvider, IAsyncDisposable private Baidu.Aip.Speech.Tts Client { get; } + private ILogger Logger { get; } + /// /// 构造函数 /// /// /// - public BaiduSynthesizerProvider(IOptionsMonitor options, IJSRuntime runtime) + /// + public BaiduSynthesizerProvider(IOptionsMonitor options, IJSRuntime runtime, ILogger logger) { JSRuntime = runtime; SpeechOption = options.CurrentValue; Client = new Baidu.Aip.Speech.Tts(SpeechOption.ApiKey, SpeechOption.Secret); + Logger = logger; } /// @@ -49,19 +54,34 @@ public class BaiduSynthesizerProvider : ISynthesizerProvider, IAsyncDisposable // 加载模块 if (Module == null) { - Module = await JSRuntime.InvokeAsync("import", "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js"); + var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js"; + Logger.LogInformation($"load module {moduleName}"); + Module = await JSRuntime.InvokeAsync("import", moduleName); } Interop ??= DotNetObjectReference.Create(this); if (Option.MethodName == "bb_baidu_speech_synthesizerOnce" && !string.IsNullOrEmpty(Option.Text)) { var result = Client.Synthesis(Option.Text); - await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback), result.Data); + if (result.Success) + { + await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback), result.Data); + } + else + { + + } + Logger.LogInformation($"bb_baidu_speech_synthesizerOnce {result.Success}"); + if (!result.Success) + { + Logger.LogError($"{result.ErrorCode}: {result.ErrorMsg}"); + } } else if (Option.MethodName == "bb_baidu_close_synthesizer") { // 停止语音 await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(Callback)); + Logger.LogInformation("bb_baidu_close_synthesizer"); } } -- Gitee From e1faa3647479a1e3c1af39f2786cb6b68aa4fc69 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 11 May 2022 12:42:46 +0800 Subject: [PATCH 2/3] =?UTF-8?q?doc:=20=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/BaiduRecognizerProvider.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs index cdf111bc3..4345e142b 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/Services/BaiduRecognizerProvider.cs @@ -34,6 +34,7 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable /// /// /// + /// public BaiduRecognizerProvider(IOptionsMonitor options, IJSRuntime runtime, ILogger logger) { JSRuntime = runtime; -- Gitee From f9c9fdc54d2de237124b174cb45012c0d5c74dca Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 11 May 2022 14:25:04 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=206.0.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.BaiduSpeech.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj index 7edb69a00..d7523b5da 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj +++ b/src/Extensions/Components/BootstrapBlazor.BaiduSpeech/BootstrapBlazor.BaiduSpeech.csproj @@ -1,7 +1,7 @@ - 6.0.5 + 6.0.6 -- Gitee