|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | +// |
| 5 | +// Microsoft Cognitive Services (formerly Project Oxford): https://www.microsoft.com/cognitive-services |
| 6 | +// |
| 7 | +// Microsoft Cognitive Services (formerly Project Oxford) GitHub: |
| 8 | +// https://github.com/Microsoft/Cognitive-Speech-TTS |
| 9 | +// |
| 10 | +// Copyright (c) Microsoft Corporation |
| 11 | +// All rights reserved. |
| 12 | +// |
| 13 | +// MIT License: |
| 14 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 15 | +// a copy of this software and associated documentation files (the |
| 16 | +// "Software"), to deal in the Software without restriction, including |
| 17 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 18 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 19 | +// permit persons to whom the Software is furnished to do so, subject to |
| 20 | +// the following conditions: |
| 21 | +// |
| 22 | +// The above copyright notice and this permission notice shall be |
| 23 | +// included in all copies or substantial portions of the Software. |
| 24 | +// |
| 25 | +// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, |
| 26 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 29 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 30 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | +// |
| 33 | + |
| 34 | +const request = require("request"); |
| 35 | +const fs = require("fs"); |
| 36 | + |
| 37 | +var subscriptionKey = "{SubscriptionKey}" // replace this with your subscription key |
| 38 | +var region = "{Region}" // replace this with the region corresponding to your subscription key, e.g.westus, eastasia |
| 39 | + |
| 40 | +// build pronunciation assessment parameters |
| 41 | +var referenceText = "Good morning."; |
| 42 | +var pronAssessmentParamsJson = `{"ReferenceText":"${referenceText}","GradingSystem":"HundredMark","Dimension":"Comprehensive"}`; |
| 43 | +var pronAssessmentParams = Buffer.from(pronAssessmentParamsJson, 'utf-8').toString('base64'); |
| 44 | + |
| 45 | +// build request |
| 46 | +var options = { |
| 47 | + method: 'POST', |
| 48 | + baseUrl: `https://${region}.stt.speech.microsoft.com/`, |
| 49 | + url: 'speech/recognition/conversation/cognitiveservices/v1?language=en-us', |
| 50 | + headers: { |
| 51 | + 'Accept': 'application/json;text/xml', |
| 52 | + 'Connection': 'Keep-Alive', |
| 53 | + 'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000', |
| 54 | + 'Transfer-Encoding': 'chunked', |
| 55 | + 'Expect': '100-continue', |
| 56 | + 'Ocp-Apim-Subscription-Key': subscriptionKey, |
| 57 | + 'Pronunciation-Assessment': pronAssessmentParams |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +var uploadFinishTime; |
| 62 | + |
| 63 | +var req = request.post(options); |
| 64 | +req.on("response", (resp) => { |
| 65 | + resp.on("data", (chunk) => { |
| 66 | + var result = chunk.toString('utf-8'); |
| 67 | + console.log("Pronunciation assessment result:\n"); |
| 68 | + console.log(result); // the result is a JSON string, you can parse it with JSON.parse() when consuming it |
| 69 | + var getResponseTime = Date.now(); |
| 70 | + console.log(`\nLatency = ${getResponseTime - uploadFinishTime}ms`); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +// a common wave header, with zero audio length |
| 75 | +// since stream data doesn't contain header, but the API requires header to fetch format information, so you need post this header as first chunk for each query |
| 76 | +const waveHeader16K16BitMono = Buffer.from([82, 73, 70, 70, 78, 128, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 18, 0, 0, 0, 1, 0, 1, 0, 128, 62, 0, 0, 0, 125, 0, 0, 2, 0, 16, 0, 0, 0, 100, 97, 116, 97, 0, 0, 0, 0]); |
| 77 | +req.write(waveHeader16K16BitMono); |
| 78 | + |
| 79 | +// send request with chunked data |
| 80 | +var audioStream = fs.createReadStream("../GoodMorning.pcm", { highWaterMark: 1024 }); |
| 81 | +audioStream.on("data", (data) => { |
| 82 | + sleep(data.length / 32); // to simulate human speaking rate |
| 83 | +}); |
| 84 | +audioStream.on("end", () => { |
| 85 | + uploadFinishTime = Date.now(); |
| 86 | +}); |
| 87 | + |
| 88 | +audioStream.pipe(req); |
| 89 | + |
| 90 | +function sleep(milliseconds) { |
| 91 | + var startTime = Date.now(); |
| 92 | + var endTime = Date.now(); |
| 93 | + while (endTime < startTime + milliseconds) { |
| 94 | + endTime = Date.now(); |
| 95 | + } |
| 96 | +} |
0 commit comments