-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from tsubakimoto/classlib
Create shared classlib
- Loading branch information
Showing
12 changed files
with
338 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using Microsoft.CognitiveServices.Speech; | ||
global using Microsoft.CognitiveServices.Speech.Audio; | ||
global using Microsoft.CognitiveServices.Speech.Translation; |
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.33.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,18 @@ | ||
namespace SpeechTranslatorShared; | ||
|
||
public abstract class TranslationRecognizerWorkerBase | ||
{ | ||
public abstract void OnRecognizing(TranslationRecognitionEventArgs e); | ||
|
||
public abstract void OnRecognized(TranslationRecognitionEventArgs e); | ||
|
||
public abstract void OnCanceled(TranslationRecognitionCanceledEventArgs e); | ||
|
||
public abstract void OnSpeechStartDetected(RecognitionEventArgs e); | ||
|
||
public abstract void OnSpeechEndDetected(RecognitionEventArgs e); | ||
|
||
public abstract void OnSessionStarted(SessionEventArgs e); | ||
|
||
public abstract void OnSessionStopped(SessionEventArgs e); | ||
} |
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 @@ | ||
namespace SpeechTranslatorShared; | ||
|
||
public class Translator | ||
{ | ||
private readonly SpeechTranslationConfig _speechTranslationConfig; | ||
|
||
public Translator(Uri endpointUrl, string subscriptionKey, string recognitionLanguage = "en-US", string targetLanguage = "ja-JP") | ||
{ | ||
if (endpointUrl is null) | ||
{ | ||
throw new ArgumentNullException(nameof(endpointUrl)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(subscriptionKey)) | ||
{ | ||
throw new ArgumentException($"'{nameof(subscriptionKey)}' を NULL または空にすることはできません。", nameof(subscriptionKey)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(recognitionLanguage)) | ||
{ | ||
throw new ArgumentException($"'{nameof(recognitionLanguage)}' を NULL または空にすることはできません。", nameof(recognitionLanguage)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(targetLanguage)) | ||
{ | ||
throw new ArgumentException($"'{nameof(targetLanguage)}' を NULL または空にすることはできません。", nameof(targetLanguage)); | ||
} | ||
|
||
_speechTranslationConfig = SpeechTranslationConfig.FromEndpoint(endpointUrl, subscriptionKey); | ||
_speechTranslationConfig.SpeechRecognitionLanguage = recognitionLanguage; | ||
_speechTranslationConfig.AddTargetLanguage(targetLanguage); | ||
_speechTranslationConfig.SetProperty(PropertyId.SpeechServiceConnection_TranslationVoice, "de-DE-Hedda"); | ||
} | ||
|
||
public async Task MultiLingualTranslation(TranslationRecognizerWorkerBase worker) | ||
{ | ||
if (worker is null) | ||
{ | ||
throw new ArgumentNullException(nameof(worker)); | ||
} | ||
|
||
var autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig.FromLanguages([_speechTranslationConfig.SpeechRecognitionLanguage]); | ||
var stopTranslation = new TaskCompletionSource<int>(); | ||
|
||
using (var audioInput = AudioConfig.FromDefaultMicrophoneInput()) | ||
using (var recognizer = new TranslationRecognizer(_speechTranslationConfig, autoDetectSourceLanguageConfig, audioInput)) | ||
{ | ||
recognizer.Recognizing += (s, e) => worker.OnRecognizing(e); | ||
|
||
recognizer.Recognized += (s, e) => worker.OnRecognized(e); | ||
|
||
recognizer.Canceled += (s, e) => | ||
{ | ||
stopTranslation.TrySetResult(0); | ||
worker.OnCanceled(e); | ||
}; | ||
|
||
recognizer.SpeechStartDetected += (s, e) => worker.OnSpeechStartDetected(e); | ||
|
||
recognizer.SpeechEndDetected += (s, e) => worker.OnSpeechEndDetected(e); | ||
|
||
recognizer.SessionStarted += (s, e) => worker.OnSessionStarted(e); | ||
|
||
recognizer.SessionStopped += (s, e) => | ||
{ | ||
stopTranslation.TrySetResult(0); | ||
worker.OnSessionStopped(e); | ||
}; | ||
|
||
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition. | ||
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false); | ||
|
||
Task.WaitAny(new[] { stopTranslation.Task }); | ||
await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} |
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,9 @@ | ||
global using System.Text; | ||
|
||
global using Microsoft.CognitiveServices.Speech; | ||
global using Microsoft.CognitiveServices.Speech.Translation; | ||
global using Microsoft.Extensions.Configuration; | ||
|
||
global using SpeechTranslatorConsole; | ||
|
||
global using SpeechTranslatorShared; |
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.