index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const { request } = require('./utils.js');
  18. const RealTime = require('./asrRealtimeSdk');
  19. /**
  20. * 获取语音识别相关接口
  21. * @param {object} params - 参数包装对象
  22. * @param {string} params.name - 对应图像识别 API的Action值 https://cloud.tencent.com/document/product/1093/35637
  23. * @param {object} params.payload - API需要的参数
  24. * @return {Promise<object>} API返回的有效数据
  25. */
  26. async function getAsrResult(params) {
  27. if (!params.name) {
  28. throw new Error('缺少API Action参数');
  29. }
  30. try {
  31. const result = await request(params.name, params.payload);
  32. return result;
  33. } catch (e) {
  34. throw new Error(e);
  35. }
  36. }
  37. /**
  38. * 实时语音识别
  39. * @param {object} params
  40. * @param {object} params.data // 音频文件
  41. * @param {object} params.params // 参数
  42. * @return {Promise<object>} 语音识别结果
  43. */
  44. async function realTimeVoice({ data: voiceData, params }) {
  45. try {
  46. // 创建调用实例
  47. const asrReq = new RealTime(params);
  48. const chunk = Buffer.from(voiceData, 'base64');
  49. const result = await asrReq.sendRequest(chunk);
  50. return result;
  51. } catch (e) {
  52. throw new Error(e);
  53. }
  54. }
  55. module.exports = {
  56. getAsrResult,
  57. realTimeVoice
  58. };