2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
अलीबाबा द्वारा प्रदत्ताः डेमो कोड् सर्वे जावास्क्रिप्ट् सन्ति, अतः भवान् स्वकीयं निर्मातुम् अर्हति । सन्दर्भरूपेण अहं Alibaba sentence dictation Nls इत्यस्य कृते typescript module लिखितवान् । VUE3 इत्यस्य संयुक्तं API रूपम्
startClient: डिक्टेशनं आरभत इति ध्यानं कुर्वन्तु यत् अग्रिमः सोपानः यथाशीघ्रं पहिचानं आँकडासंचरणं च सक्षमं कर्तुं भवितुमर्हति, अन्यथा 6 सेकेण्ड् अनन्तरं बन्दं भविष्यति।
startRecognition: मान्यताव्यवहारं आरभत, मान्यता-कॉलबैक्-मध्ये पारयन्तु, भवन्तः वर्णाः मुद्रयितुं वा स्क्रीन-उपरि प्रदर्शयितुं वा शक्नुवन्ति
sendSound: द्विचक्रीय PCM आँकडा (स्वरूपं 16MHz16bit) प्रेषयन्तु
stopRecognition: मान्यताव्यवहारस्य समाप्तिः
- /**
- * 阿里语音,一句话识别模块for ccframe
- *
- * 无心跳设计,非长连接推送,因此在需要使用的时候才进行连接
- *
- * @Jim 2024/07/08
- */
- import * as utils from '@/utils/index'
- import { nextTick } from 'vue'
- // import Global from '@/utils/constants'
-
- const NLS_SERVER_URL = 'wss://nls-gateway.aliyuncs.com/ws/v1'
- const NLS_MODE = 'SpeechRecognizer' // 一句话识别
- const WEBSOCKET_MAX_RETRY = 3
- const RECONNECT_INTERVAL = 3000
-
- interface INlsConfig {
- url?: string
- appkey: string // 应用的key
- token: string // 从服务器获得,要缓存
- }
-
- let client: (UniNamespace.SocketTask & { readyState?: WsState }) | undefined
- const clientId = utils.uuid(utils.UUIDFormat.StandardCompact)
- let taskId: string = ''
- let config: INlsConfig
- let reconnectAttempts = 0
- let taskStarted = false
-
- enum WsState {
- CONNECTING,
- OPEN,
- CLOSING,
- CLOSED
- }
-
- /**
- *
- * @param action
- * @returns 请求json
- */
- const buildMsg: (action: string, payload: Record<string, any>) => string = (
- action,
- payload = {}
- ) => {
- if (taskId.length === 0) {
- taskId = utils.uuid(utils.UUIDFormat.StandardCompact)
- }
- const msg = {
- header: {
- message_id: utils.uuid(utils.UUIDFormat.StandardCompact),
- task_id: taskId,
- namespace: NLS_MODE,
- name: action,
- appkey: config.appkey
- },
- payload,
- context: {
- sdk: {
- name: 'nls-wx-sdk',
- version: '0.0.1',
- language: 'wxjs'
- }
- }
- }
- return JSON.stringify(msg, null, 0)
- }
-
- /**
- * 开启连接,开启后立即要传,否则会被关闭.
- * @param config
- * @param callback
- */
- export const startClient = (
- conf?: INlsConfig,
- startCallback?: () => void,
- recognizedCallback?: (text: string) => void
- ) => {
- if (client && client.readyState !== WsState.CLOSED) {
- // 关闭原连接
- client.close({})
- }
-
- client = uni.connectSocket({
- url: conf.url ?? NLS_SERVER_URL,
- tcpNoDelay: true,
- header: {
- 'X-NLS-Token': conf?.token ?? config.token
- },
- success: (res) => {
- if (!config) config = conf
- console.log(`connected to ${NLS_SERVER_URL} success`)
- },
- fail: (res) => {
- console.log(`connect to ${NLS_SERVER_URL} failed:${res.errMsg}`)
- }
- })
- client.readyState = WsState.CONNECTING
-
- client.onMessage((res) => {
- if (typeof res.data === 'string') {
- const msgObj = JSON.parse(res.data)
- switch (msgObj?.header?.name) {
- case 'RecognitionStarted': {
- console.log('started')
- break
- }
- case 'RecognitionResultChanged': {
- if (recognizedCallback) {
- const text = msgObj?.payload?.result
- if (text) {
- recognizedCallback(text)
- }
- }
- console.log('changed')
- break
- }
- case 'RecognitionCompleted': {
- const text = msgObj?.payload?.result
- if (text) {
- recognizedCallback(text)
- }
- taskStarted = false // 结束识别
- break
- }
- case 'TaskFailed': {
- taskStarted = false // 结束识别
- break
- }
- }
- }
- console.log('recv:' + res.data)
- })
-
- client.onOpen(() => {
- reconnectAttempts = 0
- client.readyState = WsState.OPEN
- if (startCallback) nextTick(startCallback)
- })
-
- client.onError((error) => {
- console.error('WebSocket error:', error)
- if (reconnectAttempts < WEBSOCKET_MAX_RETRY) {
- setTimeout(() => startClient(), RECONNECT_INTERVAL)
- } else {
- console.error('Max reconnect attempts reached')
- }
- })
-
- client.onClose(() => {
- client.readyState = WsState.CLOSED
- console.log('connection closed')
- })
- }
-
- export const startRecognition = () => {
- if (client && client.readyState === WsState.OPEN)
- client.send({
- data: buildMsg('StartRecognition', {
- format: 'opus',
- sample_rate: 16000,
- enable_intermediate_result: true,
- enable_punctuation_prediction: true,
- enable_inverse_text_normalization: true
- }),
- success: (res) => {
- taskStarted = true
- }
- })
- }
-
- export const stopRecognition = () => {
- if (client && client.readyState === WsState.OPEN)
- client.send({
- data: buildMsg('StopRecognition', {
- format: 'opus',
- sample_rate: 16000,
- enable_intermediate_result: true,
- enable_punctuation_prediction: true,
- enable_inverse_text_normalization: true
- }),
- complete: () => {
- taskStarted = false // 不管是否成功,都不发送音频了
- }
- })
- }
-
- export const sendSound = (msgBytes: ArrayBuffer) => {
- if (client && client.readyState === WsState.OPEN && taskStarted)
- client.send({
- data: msgBytes,
- success: (res) => {
- console.log('send ' + msgBytes.byteLength + ' success')
- }
- })
- }
util’s uuid tool इत्यस्य कृते मम पूर्वलेखं पश्यन्तुhttps://mp.csdn.net/mp_blog/निर्माण/संपादक/140267684https://mp.csdn.net/mp_blog/निर्माण/संपादक/140267684