forked from Azure-Samples/cognitive-services-speech-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation.js
More file actions
69 lines (53 loc) · 2.64 KB
/
translation.js
File metadata and controls
69 lines (53 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// pull in the required packages.
var sdk = require("microsoft-cognitiveservices-speech-sdk");
(function() {
"use strict";
module.exports = {
main: function(settings, audioStream) {
// now create the audio-config pointing to our stream and
// the speech config specifying the language.
var audioConfig = sdk.AudioConfig.fromStreamInput(audioStream);
var translationConfig = sdk.SpeechTranslationConfig.fromSubscription(settings.subscriptionKey, settings.serviceRegion);
// setting the recognition language to English.
translationConfig.speechRecognitionLanguage = settings.language;
// target language is German.
translationConfig.addTargetLanguage("de-DE");
// create the translation recognizer.
var recognizer = new sdk.TranslationRecognizer(translationConfig, audioConfig);
// Before beginning speech recognition, setup the callbacks to be invoked when an event occurs.
// The event recognizing signals that an intermediate recognition result is received.
// You will receive one or more recognizing events as a speech phrase is recognized, with each containing
// more recognized speech. The event will contain the text for the recognition since the last phrase was recognized.
// Both the source language text and the translation text(s) are available.
recognizer.recognizing = function (s, e) {
var str = ("(recognizing) Reason: " + sdk.ResultReason[e.result.reason] + " Text: " + e.result.text + " Translations:");
var language = "de";
str += " [" + language + "] " + e.result.translations.get(language);
console.log(str);
};
// The event recognized signals that a final recognition result is received.
// This is the final event that a phrase has been recognized.
// For continuous recognition, you will get one recognized event for each phrase recognized.
// Both the source language text and the translation text(s) are available.
recognizer.recognized = function (s, e) {
var str = "\r\n(recognized) Reason: " + sdk.ResultReason[e.result.reason] + " Text: " + e.result.text + " Translations:";
var language = "de";
str += " [" + language + "] " + e.result.translations.get(language);
str += "\r\n";
console.log(str);
};
// start the recognizer and wait for a result.
recognizer.recognizeOnceAsync(
function (result) {
recognizer.close();
recognizer = undefined;
},
function (err) {
recognizer.close();
recognizer = undefined;
});
}
}
}());