Skip to content

Commit cc3e7fe

Browse files
committed
javascript synthesizer events etc
1 parent 9c65935 commit cc3e7fe

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

articles/cognitive-services/Speech-Service/includes/how-to/speech-synthesis/cpp.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ This time, save the result to a [`SpeechSynthesisResult`](/cpp/cognitive-service
100100
void synthesizeSpeech()
101101
{
102102
auto speechConfig = SpeechConfig::FromSubscription("YourSpeechKey", "YourSpeechRegion");
103-
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig, NULL);
103+
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig);
104104

105105
auto result = synthesizer->SpeakTextAsync("Getting the response as an in-memory stream.").get();
106106
auto stream = AudioDataStream::FromResult(result);
@@ -132,7 +132,7 @@ void synthesizeSpeech()
132132
auto speechConfig = SpeechConfig::FromSubscription("YourSpeechKey", "YourSpeechRegion");
133133
speechConfig->SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat::Riff24Khz16BitMonoPcm);
134134

135-
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig, NULL);
135+
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig);
136136
auto result = synthesizer->SpeakTextAsync("A simple test to write to a file.").get();
137137

138138
auto stream = AudioDataStream::FromResult(result);
@@ -164,7 +164,7 @@ Next, you need to change the speech synthesis request to reference your XML file
164164
void synthesizeSpeech()
165165
{
166166
auto speechConfig = SpeechConfig::FromSubscription("YourSpeechKey", "YourSpeechRegion");
167-
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig, NULL);
167+
auto synthesizer = SpeechSynthesizer::FromConfig(speechConfig);
168168

169169
std::ifstream file("./ssml.xml");
170170
std::string ssml, line;

articles/cognitive-services/Speech-Service/includes/how-to/speech-synthesis/csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class Program
212212
</speak>";
213213

214214
// Required for sentence-level WordBoundary events
215-
speechConfig.SetProperty("SpeechServiceResponse_RequestSentenceBoundary", "true");
215+
speechConfig.SetProperty(PropertyId.SpeechServiceResponse_RequestSentenceBoundary, "true");
216216

217217
using (var speechSynthesizer = new SpeechSynthesizer(speechConfig))
218218
{

articles/cognitive-services/Speech-Service/includes/how-to/speech-synthesis/javascript.md

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,100 @@ While using the [SpeechSynthesizer](/javascript/api/microsoft-cognitiveservices-
341341

342342
[!INCLUDE [Event types](events.md)]
343343

344-
Here's an example that shows how to subscribe to the `bookmarkReached` event for speech synthesis.
344+
Here's an example that shows how to subscribe to events for speech synthesis. You can follow the instructions in the [quickstart](../../../get-started-text-to-speech.md?pivots=javascript), but replace the contents of that `SpeechSynthesis.js` file with the following JavaScript code.
345345

346346
```javascript
347-
synthesizer.bookmarkReached = function (s, e) {
348-
window.console.log("(Bookmark reached), Audio offset: " + e.audioOffset / 10000 + "ms, bookmark text: " + e.text);
349-
}
347+
(function() {
348+
349+
"use strict";
350+
351+
var sdk = require("microsoft-cognitiveservices-speech-sdk");
352+
353+
var audioFile = "YourAudioFile.wav";
354+
const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
355+
const audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile);
356+
357+
var speechSynthesisVoiceName = "en-US-JennyNeural";
358+
var ssml = `<speak version='1.0' xml:lang='en-US' xmlns='http://www.w3.org/2001/10/synthesis' xmlns:mstts='http://www.w3.org/2001/mstts'> \r\n \
359+
<voice name='${speechSynthesisVoiceName}'> \r\n \
360+
<mstts:viseme type='redlips_front'/> \r\n \
361+
The rainbow has seven colors: <bookmark mark='colors_list_begin'/>Red, orange, yellow, green, blue, indigo, and violet.<bookmark mark='colors_list_end'/>. \r\n \
362+
</voice> \r\n \
363+
</speak>`;
364+
365+
// Required for WordBoundary event sentences.
366+
speechConfig.setProperty(sdk.PropertyId.SpeechServiceResponse_RequestSentenceBoundary, "true");
367+
368+
// Create the speech speechSynthesizer.
369+
var speechSynthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
370+
371+
speechSynthesizer.bookmarkReached = function (s, e) {
372+
var str = `BookmarkReached event: \
373+
\r\n\tAudioOffset: ${(e.audioOffset + 5000) / 10000}ms \
374+
\r\n\tText: \"${e.Text}\".`;
375+
console.log(str);
376+
}
377+
378+
speechSynthesizer.synthesisCanceled = function (s, e) {
379+
console.log("SynthesisCanceled event");
380+
};
381+
382+
speechSynthesizer.synthesisCompleted = function (s, e) {
383+
var str = `SynthesisCompleted event: \
384+
\r\n\tAudioData: ${e.result.audioData.byteLength} bytes \
385+
\r\n\tAudioDuration: ${e.result.audioDuration}`;
386+
console.log(str);
387+
};
388+
389+
speechSynthesizer.synthesisStarted = function (s, e) {
390+
console.log("SynthesisStarted event");
391+
};
392+
393+
speechSynthesizer.synthesizing = function (s, e) {
394+
var str = `Synthesizing event: \
395+
\r\n\tAudioData: ${e.result.audioData.byteLength} bytes`;
396+
console.log(str);
397+
};
398+
399+
speechSynthesizer.visemeReceived = function(s, e) {
400+
var str = `VisemeReceived event: \
401+
\r\n\tAudioOffset: ${(e.audioOffset + 5000) / 10000}ms \
402+
\r\n\tVisemeId: ${e.visemeId}`;
403+
console.log(str);
404+
}
405+
406+
speechSynthesizer.wordBoundary = function (s, e) {
407+
// Word, Punctuation, or Sentence
408+
var str = `WordBoundary event: \
409+
\r\n\tBoundaryType: ${e.boundaryType} \
410+
\r\n\tAudioOffset: ${(e.audioOffset + 5000) / 10000}ms \
411+
\r\n\tDuration: ${e.duration} \
412+
\r\n\tText: \"${e.text}\" \
413+
\r\n\tTextOffset: ${e.textOffset} \
414+
\r\n\tWordLength: ${e.wordLength}`;
415+
console.log(str);
416+
};
417+
418+
// Synthesize the SSML
419+
console.log(`SSML to synthesize: \r\n ${ssml}`)
420+
console.log(`Synthesize to: ${audioFile}`);
421+
speechSynthesizer.speakSsmlAsync(ssml,
422+
function (result) {
423+
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
424+
console.log("SynthesizingAudioCompleted result");
425+
} else {
426+
console.error("Speech synthesis canceled, " + result.errorDetails +
427+
"\nDid you set the speech resource key and region values?");
428+
}
429+
speechSynthesizer.close();
430+
speechSynthesizer = null;
431+
},
432+
function (err) {
433+
console.trace("err - " + err);
434+
speechSynthesizer.close();
435+
speechSynthesizer = null;
436+
});
437+
}());
350438
```
351439

352440
You can find more text-to-speech samples at [GitHub](https://aka.ms/csspeech/samples).

0 commit comments

Comments
 (0)