Skip to content

Commit fdc0644

Browse files
authored
Update embedded speech samples (#2201)
1 parent e12b0cc commit fdc0644

File tree

10 files changed

+55
-22
lines changed

10 files changed

+55
-22
lines changed

samples/cpp/embedded-speech/samples/intent_recognition_samples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void RecognizeIntent(bool useKeyword)
181181
{
182182
// NoMatch occurs when no speech was recognized.
183183
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
184-
cout << "NOMATCH: Reason=";
184+
cout << "NO MATCH: Reason=";
185185
switch (reason)
186186
{
187187
case NoMatchReason::NotRecognized:

samples/cpp/embedded-speech/samples/speech_recognition_samples.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void RecognizeSpeech(shared_ptr<SpeechRecognizer> recognizer, bool useKeyword, b
153153
{
154154
// NoMatch occurs when no speech phrase was recognized.
155155
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
156-
cout << "NOMATCH: Reason=";
156+
cout << "NO MATCH: Reason=";
157157
switch (reason)
158158
{
159159
case NoMatchReason::NotRecognized:

samples/cpp/embedded-speech/samples/speech_translation_samples.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,42 @@ void TranslateSpeech(shared_ptr<TranslationRecognizer> recognizer)
7171
// the source language.
7272
if (e.Result->Reason == ResultReason::TranslatingSpeech)
7373
{
74+
// Source (input) language identification is enabled when TranslationRecognizer
75+
// is created with an AutoDetectSourceLanguageConfig argument.
76+
// In case the model does not support this functionality or the language cannot
77+
// be identified, the result Language is "Unknown".
78+
auto sourceLangResult = AutoDetectSourceLanguageResult::FromResult(e.Result);
79+
const auto& sourceLang = sourceLangResult->Language;
80+
7481
for (const auto& translation : e.Result->Translations)
7582
{
7683
auto targetLang = translation.first;
7784
auto outputText = translation.second;
78-
cout << "Translating [" << targetLang << "]: " << outputText << endl;
85+
cout << "Translating [" << sourceLang << " -> " << targetLang << "]: " << outputText << endl;
7986
}
8087
}
8188
};
8289

8390
recognizer->Recognized += [](const TranslationRecognitionEventArgs& e)
8491
{
92+
// Final result. May differ from the last intermediate result.
8593
if (e.Result->Reason == ResultReason::TranslatedSpeech)
8694
{
87-
// Final result. May differ from the last intermediate result.
95+
auto sourceLangResult = AutoDetectSourceLanguageResult::FromResult(e.Result);
96+
const auto& sourceLang = sourceLangResult->Language;
97+
8898
for (const auto& translation : e.Result->Translations)
8999
{
90100
auto targetLang = translation.first;
91101
auto outputText = translation.second;
92-
cout << "TRANSLATED [" << targetLang << "]: " << outputText << endl;
102+
cout << "TRANSLATED [" << sourceLang << " -> " << targetLang << "]: " << outputText << endl;
93103
}
94104
}
95105
else if (e.Result->Reason == ResultReason::NoMatch)
96106
{
97107
// NoMatch occurs when no speech phrase was recognized.
98108
auto reason = NoMatchDetails::FromResult(e.Result)->Reason;
99-
cout << "NOMATCH: Reason=";
109+
cout << "NO MATCH: Reason=";
100110
switch (reason)
101111
{
102112
case NoMatchReason::NotRecognized:
@@ -182,8 +192,9 @@ void TranslateSpeech(shared_ptr<TranslationRecognizer> recognizer)
182192
void EmbeddedSpeechTranslationFromMicrophone()
183193
{
184194
auto speechConfig = CreateEmbeddedSpeechConfig();
195+
auto sourceLangConfig = AutoDetectSourceLanguageConfig::FromOpenRange(); // optional, for input language identification
185196
auto audioConfig = AudioConfig::FromDefaultMicrophoneInput();
186197

187-
auto recognizer = TranslationRecognizer::FromConfig(speechConfig, audioConfig);
198+
auto recognizer = TranslationRecognizer::FromConfig(speechConfig, sourceLangConfig, audioConfig);
188199
TranslateSpeech(recognizer);
189200
}

samples/csharp/dotnetcore/embedded-speech/samples/IntentRecognitionSamples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private static async Task RecognizeIntentAsync(bool useKeyword)
175175
{
176176
// NoMatch occurs when no speech was recognized.
177177
var reason = NoMatchDetails.FromResult(e.Result).Reason;
178-
Console.WriteLine($"NOMATCH: Reason={reason}");
178+
Console.WriteLine($"NO MATCH: Reason={reason}");
179179
}
180180
};
181181

samples/csharp/dotnetcore/embedded-speech/samples/SpeechRecognitionSamples.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private static async Task RecognizeSpeechAsync(SpeechRecognizer recognizer, bool
143143
{
144144
// NoMatch occurs when no speech was recognized.
145145
var reason = NoMatchDetails.FromResult(e.Result).Reason;
146-
Console.WriteLine($"NOMATCH: Reason={reason}");
146+
Console.WriteLine($"NO MATCH: Reason={reason}");
147147
}
148148
};
149149

samples/csharp/dotnetcore/embedded-speech/samples/SpeechTranslationSamples.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,42 @@ private static async Task TranslateSpeechAsync(TranslationRecognizer recognizer)
7171
// the source language.
7272
if (e.Result.Reason == ResultReason.TranslatingSpeech)
7373
{
74+
// Source (input) language identification is enabled when TranslationRecognizer
75+
// is created with an AutoDetectSourceLanguageConfig argument.
76+
// In case the model does not support this functionality or the language cannot
77+
// be identified, the result Language is "Unknown".
78+
var sourceLangResult = AutoDetectSourceLanguageResult.FromResult(e.Result);
79+
var sourceLang = sourceLangResult.Language;
80+
7481
foreach (var translation in e.Result.Translations)
7582
{
7683
var targetLang = translation.Key;
7784
var outputText = translation.Value;
78-
Console.WriteLine($"Translating [{targetLang}]: {outputText}");
85+
Console.WriteLine($"Translating [{sourceLang} -> {targetLang}]: {outputText}");
7986
}
8087
}
8188
};
8289

8390
recognizer.Recognized += (s, e) =>
8491
{
92+
// Final result. May differ from the last intermediate result.
8593
if (e.Result.Reason == ResultReason.TranslatedSpeech)
8694
{
87-
// Final result. May differ from the last intermediate result.
95+
var sourceLangResult = AutoDetectSourceLanguageResult.FromResult(e.Result);
96+
var sourceLang = sourceLangResult.Language;
97+
8898
foreach (var translation in e.Result.Translations)
8999
{
90100
var targetLang = translation.Key;
91101
var outputText = translation.Value;
92-
Console.WriteLine($"TRANSLATED [{targetLang}]: {outputText}");
102+
Console.WriteLine($"TRANSLATED [{sourceLang} -> {targetLang}]: {outputText}");
93103
}
94104
}
95105
else if (e.Result.Reason == ResultReason.NoMatch)
96106
{
97107
// NoMatch occurs when no speech was recognized.
98108
var reason = NoMatchDetails.FromResult(e.Result).Reason;
99-
Console.WriteLine($"NOMATCH: Reason={reason}");
109+
Console.WriteLine($"NO MATCH: Reason={reason}");
100110
}
101111
};
102112

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

151-
using var recognizer = new TranslationRecognizer(speechConfig, audioConfig);
162+
using var recognizer = new TranslationRecognizer(speechConfig, sourceLangConfig, audioConfig);
152163
TranslateSpeechAsync(recognizer).Wait();
153164
}
154165
}

samples/java/android/embedded-speech/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
implementation fileTree(include: ['*.jar'], dir: 'libs')
2626

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

3030
implementation 'androidx.appcompat:appcompat:1.3.1'
3131
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'

samples/java/jre/embedded-speech/src/com/microsoft/cognitiveservices/speech/samples/embedded/IntentRecognitionSamples.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ else if (e.getResult().getReason() == ResultReason.NoMatch)
174174
{
175175
// NoMatch occurs when no speech was recognized.
176176
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
177-
System.out.println("NOMATCH: Reason=" + reason);
177+
System.out.println("NO MATCH: Reason=" + reason);
178178
}
179179
});
180180

samples/java/jre/embedded-speech/src/com/microsoft/cognitiveservices/speech/samples/embedded/SpeechRecognitionSamples.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ else if (e.getResult().getReason() == ResultReason.NoMatch)
160160
{
161161
// NoMatch occurs when no speech was recognized.
162162
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
163-
System.out.println("NOMATCH: Reason=" + reason);
163+
System.out.println("NO MATCH: Reason=" + reason);
164164
}
165165
});
166166

samples/java/jre/embedded-speech/src/com/microsoft/cognitiveservices/speech/samples/embedded/SpeechTranslationSamples.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,42 @@ private static void translateSpeechAsync(TranslationRecognizer recognizer) throw
7878
// the source language.
7979
if (e.getResult().getReason() == ResultReason.TranslatingSpeech)
8080
{
81+
// Source (input) language identification is enabled when TranslationRecognizer
82+
// is created with an AutoDetectSourceLanguageConfig argument.
83+
// In case the model does not support this functionality or the language cannot
84+
// be identified, the result Language is "Unknown".
85+
AutoDetectSourceLanguageResult sourceLangResult = AutoDetectSourceLanguageResult.fromResult(e.getResult());
86+
String sourceLang = sourceLangResult.getLanguage();
87+
8188
for (Map.Entry<String, String> translation : e.getResult().getTranslations().entrySet())
8289
{
8390
String targetLang = translation.getKey();
8491
String outputText = translation.getValue();
85-
System.out.println("Translating [" + targetLang + "]: " + outputText);
92+
System.out.println("Translating [" + sourceLang + " -> " + targetLang + "]: " + outputText);
8693
}
8794
}
8895
});
8996

9097
recognizer.recognized.addEventListener((s, e) ->
9198
{
99+
// Final result. May differ from the last intermediate result.
92100
if (e.getResult().getReason() == ResultReason.TranslatedSpeech)
93101
{
94-
// Final result. May differ from the last intermediate result.
102+
AutoDetectSourceLanguageResult sourceLangResult = AutoDetectSourceLanguageResult.fromResult(e.getResult());
103+
String sourceLang = sourceLangResult.getLanguage();
104+
95105
for (Map.Entry<String, String> translation : e.getResult().getTranslations().entrySet())
96106
{
97107
String targetLang = translation.getKey();
98108
String outputText = translation.getValue();
99-
System.out.println("TRANSLATED [" + targetLang + "]: " + outputText);
109+
System.out.println("TRANSLATED [" + sourceLang + " -> " + targetLang + "]: " + outputText);
100110
}
101111
}
102112
else if (e.getResult().getReason() == ResultReason.NoMatch)
103113
{
104114
// NoMatch occurs when no speech was recognized.
105115
NoMatchReason reason = NoMatchDetails.fromResult(e.getResult()).getReason();
106-
System.out.println("NOMATCH: Reason=" + reason);
116+
System.out.println("NO MATCH: Reason=" + reason);
107117
}
108118
});
109119

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

159-
TranslationRecognizer recognizer = new TranslationRecognizer(speechConfig, audioConfig);
170+
TranslationRecognizer recognizer = new TranslationRecognizer(speechConfig, sourceLangConfig, audioConfig);
160171
translateSpeechAsync(recognizer);
161172

162173
recognizer.close();

0 commit comments

Comments
 (0)