|
| 1 | +# iFLYTEK-MSC-Python-SDK |
| 2 | + |
| 3 | +## 简介 |
| 4 | + |
| 5 | +* 中文 | [English](./README.md) |
| 6 | + |
| 7 | +* 一个讯飞智能语音平台 MSC 的第三方 Python SDK,支持语音唤醒、语音识别、语音合成、语音评测等功能。 |
| 8 | + |
| 9 | +## 快速开始 |
| 10 | +* 安装 MSC Python SDK |
| 11 | + |
| 12 | + ```bash |
| 13 | + $ pip install git+https://github.com/jm12138/iFLYTEK-MSC-Python-SDK |
| 14 | + ``` |
| 15 | + |
| 16 | +* 下载 MSC SDK |
| 17 | + |
| 18 | + * [SDK Download](https://www.xfyun.cn/sdk/dispatcher) |
| 19 | + |
| 20 | +* 语音唤醒 |
| 21 | + |
| 22 | + ```python |
| 23 | + import os |
| 24 | +
|
| 25 | + # Set MSC SDK DLL/SO File Path |
| 26 | + # X86: SDK_DIR/bin/msc.dll |
| 27 | + # X64: SDK_DIR/bin/msc_x64.dll |
| 28 | + os.environ['MSC_SDK_PATH'] = '' |
| 29 | +
|
| 30 | + import msc |
| 31 | + import pyaudio |
| 32 | + from ctypes import string_at, c_void_p |
| 33 | + from threading import Event |
| 34 | +
|
| 35 | + # Audio Stream |
| 36 | + p = pyaudio.PyAudio() |
| 37 | + input_stream = p.open( |
| 38 | + format=pyaudio.paInt16, |
| 39 | + channels=1, |
| 40 | + rate=16000, |
| 41 | + input=True, |
| 42 | + frames_per_buffer=2048 |
| 43 | + ) |
| 44 | +
|
| 45 | + # Set APP ID |
| 46 | + appid = '' |
| 47 | +
|
| 48 | + # Set MSC Client |
| 49 | + client = msc.MSC(params=f"appid={appid}".encode('UTF-8')) |
| 50 | +
|
| 51 | + # Stop Event |
| 52 | + stop_event = Event() |
| 53 | +
|
| 54 | + # KSW Callback Function |
| 55 | + def message_callback( |
| 56 | + sessionID: bytes, |
| 57 | + msg: int, |
| 58 | + param1: int, |
| 59 | + param2: int, |
| 60 | + info: c_void_p, |
| 61 | + userData: c_void_p |
| 62 | + ): |
| 63 | + # Print Result |
| 64 | + print('sessionID: ', sessionID.decode('UTF-8')) |
| 65 | + print('msg: ', msg) |
| 66 | + print('param1: ', param1) |
| 67 | + print('param2: ', param2) |
| 68 | + print('info: ', string_at(info, param2).decode('UTF-8')) |
| 69 | + print('userData: ', userData) |
| 70 | +
|
| 71 | + # Stop |
| 72 | + stop_event.set() |
| 73 | +
|
| 74 | + return 0 |
| 75 | +
|
| 76 | + # Set IVW Res Path |
| 77 | + # SDK_DIR/bin/msc/res/ivw/wakeupresource.jet |
| 78 | + ivw_res_path = '' |
| 79 | +
|
| 80 | + # Start KWS |
| 81 | + client.kws( |
| 82 | + params=f'ivw_res_path=fo|{ivw_res_path}'.encode('UTF-8'), |
| 83 | + message_callback=message_callback, |
| 84 | + stream=input_stream, |
| 85 | + chunk_size=2048, |
| 86 | + user_data=None, |
| 87 | + stop_event=stop_event |
| 88 | + ) |
| 89 | + ``` |
| 90 | + |
| 91 | + pIvwParam = { |
| 92 | + "rlt": [ |
| 93 | + { |
| 94 | + "sid": "undefine", |
| 95 | + "istart": 33, |
| 96 | + "iresid": 119, |
| 97 | + "iresIndex": 2, |
| 98 | + "iduration": 189, |
| 99 | + "nfillerscore": 0, |
| 100 | + "nkeywordscore": 142754, |
| 101 | + "ncm": 1525, |
| 102 | + "keyword": "ding1 dong1 ding1 dong1 " |
| 103 | + } |
| 104 | + ] |
| 105 | + } |
| 106 | + data->rlt = { |
| 107 | + "rlt": [ |
| 108 | + { |
| 109 | + "sid": "undefine", |
| 110 | + "istart": 33, |
| 111 | + "iresid": 119, |
| 112 | + "iresIndex": 2, |
| 113 | + "iduration": 189, |
| 114 | + "nfillerscore": 0, |
| 115 | + "nkeywordscore": 142754, |
| 116 | + "ncm": 1525, |
| 117 | + "keyword": "ding1 dong1 ding1 dong1 " |
| 118 | + } |
| 119 | + ] |
| 120 | + } |
| 121 | + data->datalen=76864 |
| 122 | + sessionID: civw1PdHHBHvGQRgCahtaaBJvtTw1WLl5L |
| 123 | + msg: 1 |
| 124 | + param1: 0 |
| 125 | + param2: 98 |
| 126 | + info: { |
| 127 | + "sst": "wakeup", |
| 128 | + "id": 2, |
| 129 | + "score": 1525, |
| 130 | + "bos": 330, |
| 131 | + "eos": 2220, |
| 132 | + "keyword": "lao3-zhang1-lao3-zhang1" |
| 133 | + } |
| 134 | + userData: None |
| 135 | + wakeup : ivw param = { |
| 136 | + "rlt": [ |
| 137 | + { |
| 138 | + "sid": "undefine", |
| 139 | + "istart": 33, |
| 140 | + "iresid": 119, |
| 141 | + "iresIndex": 2, |
| 142 | + "iduration": 189, |
| 143 | + "nfillerscore": 0, |
| 144 | + "nkeywordscore": 142754, |
| 145 | + "ncm": 1525, |
| 146 | + "keyword": "ding1 dong1 ding1 dong1 " |
| 147 | + } |
| 148 | + ] |
| 149 | + } |
| 150 | + |
| 151 | +* 语音识别 |
| 152 | + |
| 153 | + ```python |
| 154 | + import os |
| 155 | +
|
| 156 | + # Set MSC SDK DLL/SO File Path |
| 157 | + # X86: SDK_DIR/bin/msc.dll |
| 158 | + # X64: SDK_DIR/bin/msc_x64.dll |
| 159 | + os.environ['MSC_SDK_PATH'] = '' |
| 160 | +
|
| 161 | + import msc |
| 162 | + import pyaudio |
| 163 | +
|
| 164 | + # Audio Stream |
| 165 | + p = pyaudio.PyAudio() |
| 166 | + input_stream = p.open( |
| 167 | + format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=2048 |
| 168 | + ) |
| 169 | +
|
| 170 | + # Set APP ID |
| 171 | + appid = "" |
| 172 | +
|
| 173 | + # Set MSC Client |
| 174 | + client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) |
| 175 | +
|
| 176 | + # Set Domain |
| 177 | + domain = "iat" |
| 178 | +
|
| 179 | + # Start ASR |
| 180 | + for item in client.asr( |
| 181 | + params=f"domain={domain}".encode("UTF-8"), |
| 182 | + stream=input_stream, |
| 183 | + chunk_size=2048, |
| 184 | + ): |
| 185 | + print(item.decode("UTF-8")) |
| 186 | + ``` |
| 187 | +
|
| 188 | + {"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":64,"cw":[{"sc":0.0,"w":"今天"}]},{"bg":132,"cw":[{"sc":0.0,"w":"天气"}]},{"bg":164,"cw":[{"sc":0.0,"w":"怎么样"}]}]} |
| 189 | + {"sn":2,"ls":true,"bg":0,"ed":0,"ws":[{"bg":235,"cw":[{"sc":0.0,"w":"?"}]}]} |
| 190 | +
|
| 191 | +* 语音合成 |
| 192 | +
|
| 193 | + ```python |
| 194 | + import os |
| 195 | +
|
| 196 | + # Set MSC SDK DLL/SO File Path |
| 197 | + # X86: SDK_DIR/bin/msc.dll |
| 198 | + # X64: SDK_DIR/bin/msc_x64.dll |
| 199 | + os.environ['MSC_SDK_PATH'] = '' |
| 200 | +
|
| 201 | + import msc |
| 202 | + import pyaudio |
| 203 | +
|
| 204 | + # Audio Stream |
| 205 | + p = pyaudio.PyAudio() |
| 206 | + output_stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, output=True) |
| 207 | +
|
| 208 | + # Set APP ID |
| 209 | + appid = "" |
| 210 | +
|
| 211 | + # Set MSC Client |
| 212 | + client = msc.MSC(params=f"appid={appid}".encode("UTF-8")) |
| 213 | +
|
| 214 | + # Set Text Encoding |
| 215 | + text_encoding = "UTF8" |
| 216 | +
|
| 217 | + # Start TTS |
| 218 | + for item in client.tts( |
| 219 | + params=f"text_encoding={text_encoding}".encode("UTF-8"), |
| 220 | + text="你好,我是您的语音助手,有什么需要帮助的吗?".encode("UTF-8"), |
| 221 | + ): |
| 222 | + output_stream.write(item) |
| 223 | + ``` |
| 224 | +
|
| 225 | +## Documentation |
| 226 | +
|
| 227 | +* [Offical Documents](https://www.xfyun.cn/doc/mscapi/Windows&Linux/wlapi.html) |
0 commit comments