schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
/**
 * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * @file AipSpeech
 * @author baiduAip
 */
const BaseClient = require('./client/baseClient');
 
const RequestInfo = require('./client/requestInfo');
 
const objectTools = require('./util/objectTools');
 
const HttpClientVoiceASR = require('./http/httpClientVoiceASR');
 
const HttpClientVoiceTTS = require('./http/httpClientVoiceTTS');
 
const code = require('./const/code');
 
const httpHeader = require('./const/httpHeader');
 
const METHOD_POST = 'POST';
 
const CONTENT_TYPE_JSON = 'application/json';
 
const HOST_VOP = 'vop.baidu.com';
const HOST_TSN = 'tsn.baidu.com';
const PATH_VOP = '/server_api';
const PATH_VOP_PRO = '/pro_api';
const PATH_TTS = '/text2audio';
 
/**
 * AipSpeech类,构造调用语音接口
 *
 * @class
 * @extends BaseClient
 * @constructor
 * @param {string} appid appid.
 * @param {string} ak  access key.
 * @param {string} sk  security key.
 */
class AipSpeech extends BaseClient {
    constructor(appId, ak, sk) {
        // 在speech.baidu.com上创建的应用需要跳过此项权限检查
        super(appId, ak, sk, {isSkipScopeCheck: true});
    }
 
    recognizePro(buffer, format, rate, options) {
        let param = {
            speech: buffer && buffer.toString(code.BASE64),
            format: format,
            rate: rate,
            channel: 1,
            len: buffer && buffer.toString(code.BIN).length
        };
 
        return this.asrImplPro(objectTools.merge(param, options));
    }
 
    recognize(buffer, format, rate, options) {
        let param = {
            speech: buffer && buffer.toString(code.BASE64),
            format: format,
            rate: rate,
            channel: 1,
            len: buffer && buffer.toString(code.BIN).length
        };
 
        return this.asrImpl(objectTools.merge(param, options));
    }
 
    recognizeByUrl(url, callback, format, rate, options) {
        let param = {
            url: url,
            format: format,
            rate: rate,
            channel: 1,
            callback: callback
        };
        return this.asrImpl(objectTools.merge(param, options));
    }
 
    asrImpl(param) {
        return this.asrImplPath(param, PATH_VOP);
    }
 
    asrImplPro(param) {
        return this.asrImplPath(param, PATH_VOP_PRO);
    }
 
    asrImplPath(param, url) {
        let httpClient = new HttpClientVoiceASR();
        let requestInfo = new RequestInfo(url, param, METHOD_POST, false, {
                [httpHeader.CONTENT_TYPE]: CONTENT_TYPE_JSON
            });
        requestInfo.setHost(HOST_VOP);
        return this.doRequest(requestInfo, httpClient);
    }
 
    text2audio(text, options) {
        let param = {
            tex: text,
            lan: 'zh',
            ctp: 1
        };
        return this.ttsImpl(objectTools.merge(param, options));
    }
 
    ttsImpl(param) {
        let httpClient = new HttpClientVoiceTTS();
        let requestInfo = new RequestInfo(PATH_TTS,
            param, METHOD_POST, true);
 
        requestInfo.setHost(HOST_TSN);
 
        return this.doRequest(requestInfo, httpClient);
    }
 
}
 
module.exports = AipSpeech;