Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

speech-service.js 2.6 KB

You have to be logged in to leave a comment. Sign In
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  1. var uuid = require('node-uuid'),
  2. request = require('request');
  3. var SPEECH_API_KEY = process.env.MICROSOFT_SPEECH_API_KEY;
  4. // The token has an expiry time of 10 minutes https://www.microsoft.com/cognitive-services/en-us/Speech-api/documentation/API-Reference-REST/BingVoiceRecognition
  5. var TOKEN_EXPIRY_IN_SECONDS = 600;
  6. var speechApiAccessToken = '';
  7. exports.getTextFromAudioStream = function (stream) {
  8. return new Promise(
  9. function (resolve, reject) {
  10. if (!speechApiAccessToken) {
  11. try {
  12. authenticate(function () {
  13. streamToText(stream, resolve, reject);
  14. });
  15. } catch (exception) {
  16. reject(exception);
  17. }
  18. } else {
  19. streamToText(stream, resolve, reject);
  20. }
  21. }
  22. );
  23. };
  24. function authenticate(callback) {
  25. var requestData = {
  26. url: 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken',
  27. headers: {
  28. 'content-type': 'application/x-www-form-urlencoded',
  29. 'Ocp-Apim-Subscription-Key': SPEECH_API_KEY
  30. }
  31. };
  32. request.post(requestData, function (error, response, token) {
  33. if (error) {
  34. console.error(error);
  35. } else if (response.statusCode !== 200) {
  36. console.error(token);
  37. } else {
  38. speechApiAccessToken = 'Bearer ' + token;
  39. // We need to refresh the token before it expires.
  40. setTimeout(authenticate, (TOKEN_EXPIRY_IN_SECONDS - 60) * 1000);
  41. if (callback) {
  42. callback();
  43. }
  44. }
  45. });
  46. }
  47. function streamToText(stream, resolve, reject) {
  48. var speechApiUrl = [
  49. 'https://speech.platform.bing.com/recognize?scenarios=smd',
  50. 'appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5',
  51. 'locale=en-US',
  52. 'device.os=wp7',
  53. 'version=3.0',
  54. 'format=json',
  55. 'form=BCSSTT',
  56. 'instanceid=0F8EBADC-3DE7-46FB-B11A-1B3C3C4309F5',
  57. 'requestid=' + uuid.v4()
  58. ].join('&');
  59. var speechRequestData = {
  60. url: speechApiUrl,
  61. headers: {
  62. 'Authorization': speechApiAccessToken,
  63. 'content-type': 'audio/wav; codec=\'audio/pcm\'; samplerate=16000'
  64. }
  65. };
  66. stream.pipe(request.post(speechRequestData, function (error, response, body) {
  67. if (error) {
  68. reject(error);
  69. } else if (response.statusCode !== 200) {
  70. reject(body);
  71. } else {
  72. resolve(JSON.parse(body).header.name);
  73. }
  74. }));
  75. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...