Skip to content

Commit

Permalink
Update embedded speech samples (#2201)
Browse files Browse the repository at this point in the history
  • Loading branch information
pankopon authored Jan 2, 2024
1 parent e12b0cc commit fdc0644
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void RecognizeIntent(bool useKeyword)
{
// NoMatch occurs when no speech was recognized.
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
cout << "NOMATCH: Reason=";
cout << "NO MATCH: Reason=";
switch (reason)
{
case NoMatchReason::NotRecognized:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void RecognizeSpeech(shared_ptr<SpeechRecognizer> recognizer, bool useKeyword, b
{
// NoMatch occurs when no speech phrase was recognized.
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
cout << "NOMATCH: Reason=";
cout << "NO MATCH: Reason=";
switch (reason)
{
case NoMatchReason::NotRecognized:
Expand Down
21 changes: 16 additions & 5 deletions samples/cpp/embedded-speech/samples/speech_translation_samples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,42 @@ void TranslateSpeech(shared_ptr<TranslationRecognizer> recognizer)
// the source language.
if (e.Result->Reason == ResultReason::TranslatingSpeech)
{
// Source (input) language identification is enabled when TranslationRecognizer
// is created with an AutoDetectSourceLanguageConfig argument.
// In case the model does not support this functionality or the language cannot
// be identified, the result Language is "Unknown".
auto sourceLangResult = AutoDetectSourceLanguageResult::FromResult(e.Result);
const auto& sourceLang = sourceLangResult->Language;

for (const auto& translation : e.Result->Translations)
{
auto targetLang = translation.first;
auto outputText = translation.second;
cout << "Translating [" << targetLang << "]: " << outputText << endl;
cout << "Translating [" << sourceLang << " -> " << targetLang << "]: " << outputText << endl;
}
}
};

recognizer->Recognized += [](const TranslationRecognitionEventArgs& e)
{
// Final result. May differ from the last intermediate result.
if (e.Result->Reason == ResultReason::TranslatedSpeech)
{
// Final result. May differ from the last intermediate result.
auto sourceLangResult = AutoDetectSourceLanguageResult::FromResult(e.Result);
const auto& sourceLang = sourceLangResult->Language;

for (const auto& translation : e.Result->Translations)
{
auto targetLang = translation.first;
auto outputText = translation.second;
cout << "TRANSLATED [" << targetLang << "]: " << outputText << endl;
cout << "TRANSLATED [" << sourceLang << " -> " << targetLang << "]: " << outputText << endl;
}
}
else if (e.Result->Reason == ResultReason::NoMatch)
{
// NoMatch occurs when no speech phrase was recognized.
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
cout << "NOMATCH: Reason=";
cout << "NO MATCH: Reason=";
switch (reason)
{
case NoMatchReason::NotRecognized:
Expand Down Expand Up @@ -182,8 +192,9 @@ void TranslateSpeech(shared_ptr<TranslationRecognizer> recognizer)
void EmbeddedSpeechTranslationFromMicrophone()
{
auto speechConfig = CreateEmbeddedSpeechConfig();
auto sourceLangConfig = AutoDetectSourceLanguageConfig::FromOpenRange(); // optional, for input language identification
auto audioConfig = AudioConfig::FromDefaultMicrophoneInput();

auto recognizer = TranslationRecognizer::FromConfig(speechConfig, audioConfig);
auto recognizer = TranslationRecognizer::FromConfig(speechConfig, sourceLangConfig, audioConfig);
TranslateSpeech(recognizer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private static async Task RecognizeIntentAsync(bool useKeyword)
{
// NoMatch occurs when no speech was recognized.
var reason = NoMatchDetails.FromResult(e.Result).Reason;
Console.WriteLine($"NOMATCH: Reason={reason}");
Console.WriteLine($"NO MATCH: Reason={reason}");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static async Task RecognizeSpeechAsync(SpeechRecognizer recognizer, bool
{
// NoMatch occurs when no speech was recognized.
var reason = NoMatchDetails.FromResult(e.Result).Reason;
Console.WriteLine($"NOMATCH: Reason={reason}");
Console.WriteLine($"NO MATCH: Reason={reason}");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,42 @@ private static async Task TranslateSpeechAsync(TranslationRecognizer recognizer)
// the source language.
if (e.Result.Reason == ResultReason.TranslatingSpeech)
{
// Source (input) language identification is enabled when TranslationRecognizer
// is created with an AutoDetectSourceLanguageConfig argument.
// In case the model does not support this functionality or the language cannot
// be identified, the result Language is "Unknown".
var sourceLangResult = AutoDetectSourceLanguageResult.FromResult(e.Result);
var sourceLang = sourceLangResult.Language;

foreach (var translation in e.Result.Translations)
{
var targetLang = translation.Key;
var outputText = translation.Value;
Console.WriteLine($"Translating [{targetLang}]: {outputText}");
Console.WriteLine($"Translating [{sourceLang} -> {targetLang}]: {outputText}");
}
}
};

recognizer.Recognized += (s, e) =>
{
// Final result. May differ from the last intermediate result.
if (e.Result.Reason == ResultReason.TranslatedSpeech)
{
// Final result. May differ from the last intermediate result.
var sourceLangResult = AutoDetectSourceLanguageResult.FromResult(e.Result);
var sourceLang = sourceLangResult.Language;

foreach (var translation in e.Result.Translations)
{
var targetLang = translation.Key;
var outputText = translation.Value;
Console.WriteLine($"TRANSLATED [{targetLang}]: {outputText}");
Console.WriteLine($"TRANSLATED [{sourceLang} -> {targetLang}]: {outputText}");
}
}
else if (e.Result.Reason == ResultReason.NoMatch)
{
// NoMatch occurs when no speech was recognized.
var reason = NoMatchDetails.FromResult(e.Result).Reason;
Console.WriteLine($"NOMATCH: Reason={reason}");
Console.WriteLine($"NO MATCH: Reason={reason}");
}
};

Expand Down Expand Up @@ -146,9 +156,10 @@ private static async Task TranslateSpeechAsync(TranslationRecognizer recognizer)
public static void EmbeddedTranslationFromMicrophone()
{
var speechConfig = Settings.CreateEmbeddedSpeechConfig();
var sourceLangConfig = AutoDetectSourceLanguageConfig.FromOpenRange(); // optional, for input language identification
using var audioConfig = AudioConfig.FromDefaultMicrophoneInput();

using var recognizer = new TranslationRecognizer(speechConfig, audioConfig);
using var recognizer = new TranslationRecognizer(speechConfig, sourceLangConfig, audioConfig);
TranslateSpeechAsync(recognizer).Wait();
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/java/android/embedded-speech/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')

// Speech SDK
implementation 'com.microsoft.cognitiveservices.speech:client-sdk-embedded:1.34.0'
implementation 'com.microsoft.cognitiveservices.speech:client-sdk-embedded:1.34.0@aar'

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ else if (e.getResult().getReason() == ResultReason.NoMatch)
{
// NoMatch occurs when no speech was recognized.
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
System.out.println("NOMATCH: Reason=" + reason);
System.out.println("NO MATCH: Reason=" + reason);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ else if (e.getResult().getReason() == ResultReason.NoMatch)
{
// NoMatch occurs when no speech was recognized.
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
System.out.println("NOMATCH: Reason=" + reason);
System.out.println("NO MATCH: Reason=" + reason);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,42 @@ private static void translateSpeechAsync(TranslationRecognizer recognizer) throw
// the source language.
if (e.getResult().getReason() == ResultReason.TranslatingSpeech)
{
// Source (input) language identification is enabled when TranslationRecognizer
// is created with an AutoDetectSourceLanguageConfig argument.
// In case the model does not support this functionality or the language cannot
// be identified, the result Language is "Unknown".
AutoDetectSourceLanguageResult sourceLangResult = AutoDetectSourceLanguageResult.fromResult(e.getResult());
String sourceLang = sourceLangResult.getLanguage();

for (Map.Entry<String, String> translation : e.getResult().getTranslations().entrySet())
{
String targetLang = translation.getKey();
String outputText = translation.getValue();
System.out.println("Translating [" + targetLang + "]: " + outputText);
System.out.println("Translating [" + sourceLang + " -> " + targetLang + "]: " + outputText);
}
}
});

recognizer.recognized.addEventListener((s, e) ->
{
// Final result. May differ from the last intermediate result.
if (e.getResult().getReason() == ResultReason.TranslatedSpeech)
{
// Final result. May differ from the last intermediate result.
AutoDetectSourceLanguageResult sourceLangResult = AutoDetectSourceLanguageResult.fromResult(e.getResult());
String sourceLang = sourceLangResult.getLanguage();

for (Map.Entry<String, String> translation : e.getResult().getTranslations().entrySet())
{
String targetLang = translation.getKey();
String outputText = translation.getValue();
System.out.println("TRANSLATED [" + targetLang + "]: " + outputText);
System.out.println("TRANSLATED [" + sourceLang + " -> " + targetLang + "]: " + outputText);
}
}
else if (e.getResult().getReason() == ResultReason.NoMatch)
{
// NoMatch occurs when no speech was recognized.
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
System.out.println("NOMATCH: Reason=" + reason);
System.out.println("NO MATCH: Reason=" + reason);
}
});

Expand Down Expand Up @@ -154,9 +164,10 @@ else if (e.getResult().getReason() == ResultReason.NoMatch)
public static void embeddedTranslationFromMicrophone() throws InterruptedException, ExecutionException
{
EmbeddedSpeechConfig speechConfig = Settings.createEmbeddedSpeechConfig();
AutoDetectSourceLanguageConfig sourceLangConfig = AutoDetectSourceLanguageConfig.fromOpenRange(); // optional, for input language identification
AudioConfig audioConfig = AudioConfig.fromDefaultMicrophoneInput();

TranslationRecognizer recognizer = new TranslationRecognizer(speechConfig, audioConfig);
TranslationRecognizer recognizer = new TranslationRecognizer(speechConfig, sourceLangConfig, audioConfig);
translateSpeechAsync(recognizer);

recognizer.close();
Expand Down

0 comments on commit fdc0644

Please sign in to comment.