!2768 feat(#I56W9J): add Logger on Speech component

* chore: 更新版本 6.0.6
* doc: 增加注释信息
* feat: 增加日志功能
This commit is contained in:
Argo 2022-05-11 06:27:12 +00:00
parent 51d69b9c18
commit 70c175f588
3 changed files with 56 additions and 20 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<Version>6.0.5</Version>
<Version>6.0.6</Version>
</PropertyGroup>
<PropertyGroup>

View File

@ -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,20 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable
private Baidu.Aip.Speech.Asr Client { get; }
private ILogger Logger { get; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="options"></param>
/// <param name="runtime"></param>
public BaiduRecognizerProvider(IOptionsMonitor<BaiduSpeechOption> options, IJSRuntime runtime)
/// <param name="logger"></param>
public BaiduRecognizerProvider(IOptionsMonitor<BaiduSpeechOption> options, IJSRuntime runtime, ILogger<BaiduRecognizerProvider> logger)
{
JSRuntime = runtime;
SpeechOption = options.CurrentValue;
Client = new Baidu.Aip.Speech.Asr(SpeechOption.AppId, SpeechOption.ApiKey, SpeechOption.Secret);
Logger = logger;
}
/// <summary>
@ -46,18 +51,19 @@ public class BaiduRecognizerProvider : IRecognizerProvider, IAsyncDisposable
/// <exception cref="InvalidOperationException"></exception>
public async Task InvokeAsync(RecognizerOption option)
{
if (string.IsNullOrEmpty(option.MethodName))
if (!string.IsNullOrEmpty(option.MethodName))
{
throw new InvalidOperationException();
Option = option;
if (Module == null)
{
var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js";
Logger.LogInformation($"load module {moduleName}");
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", moduleName);
}
Interop ??= DotNetObjectReference.Create(this);
await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds);
Logger.LogInformation($"{Option.MethodName}");
}
Option = option;
if (Module == null)
{
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "/_content/BootstrapBlazor.BaiduSpeech/js/recognizer.js");
}
Interop ??= DotNetObjectReference.Create(this);
await Module.InvokeVoidAsync(Option.MethodName, Interop, nameof(RecognizeCallback), Option.AutoRecoginzerElapsedMilliseconds);
}
/// <summary>
@ -66,17 +72,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<int>("err_no");
if (err_no == 0)
{
sb.Append(item.ToString());
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
{
Logger.LogError($"err_no: {err_no}");
}
data = sb.ToString();
}
if (Option.Callback != null)

View File

@ -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; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="options"></param>
/// <param name="runtime"></param>
public BaiduSynthesizerProvider(IOptionsMonitor<BaiduSpeechOption> options, IJSRuntime runtime)
/// <param name="logger"></param>
public BaiduSynthesizerProvider(IOptionsMonitor<BaiduSpeechOption> options, IJSRuntime runtime, ILogger<BaiduSynthesizerProvider> logger)
{
JSRuntime = runtime;
SpeechOption = options.CurrentValue;
Client = new Baidu.Aip.Speech.Tts(SpeechOption.ApiKey, SpeechOption.Secret);
Logger = logger;
}
/// <summary>
@ -49,19 +54,34 @@ public class BaiduSynthesizerProvider : ISynthesizerProvider, IAsyncDisposable
// 加载模块
if (Module == null)
{
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js");
var moduleName = "./_content/BootstrapBlazor.BaiduSpeech/js/synthesizer.js";
Logger.LogInformation($"load module {moduleName}");
Module = await JSRuntime.InvokeAsync<IJSObjectReference>("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");
}
}