1.0.0 • Published 11 months ago

aliyun-tts-websocket v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

Aliyun-tts-websocket

This repo is developed for aliyun tts websocket. For more information, please see: https://help.aliyun.com/document_detail/324262.html?spm=a2c4g.324262.0.0.7dd87d63BjFUFh

English | 简体中文

Installation

$ npm install aliyun-tts-websocket --save

Usage

With webpack, vite or other build tools:

import AliyunTtsWebSocket from 'aliyun-tts-websocket'

const service = new AliyunTtsWebSocket({
  ws: '', // A websocket address with your token.
  ttsHeaders: {
    appkey: '' // Your aliyun cloud appkey
  }
})

service.start()
service.stop()

Constructor options

nametypedefaultdescription
wsstring''Websocket url.
mediaStreamHandler(stream: MediaStream, send: Function) => voidnullA function which can make you change audio stream and send to websocket.
ttsConfigobjecttts default configSame as the payload of StartTranscription directive.
ttsHeadersobjecttts default headerSame as the header of each directive.

mediaStreamHandler

This function make you convert audio format by your self. PCM is used by default.

Default config value

{
  enable_intermediate_result: true,
  enable_punctuation_prediction: true
}

This params is used for StartTranscription directive's payload. For more information, see: https://help.aliyun.com/document_detail/324262.html?spm=a2c4g.324262.0.0.7dd87d63BjFUFh#sectiondiv-rz2-i36-2gv

Default header value

{
  appkey: ''
}

This params is used for each directive. For more information, see: https://help.aliyun.com/document_detail/324262.html?spm=a2c4g.324262.0.0.7dd87d63BjFUFh#sectiondiv-mkk-cs4-5kh

Notice that only appkey field is required, other fields is not recommended to pass.

Public methods

namedescription
startOpen a websocket connection, send StartTranscription directive to server and weakup microphone device.
stopDisconnect the microphone devece and send StopTranscription directive to serve. The websocket connection will be closed after receive the TranscriptionCompleted event.
onAlias of addEventListener.

Events

Aliyun-tts-websocket extends EventTarget, you can listen events as same as DOM events.

const service = new AliyunTtsWebSocket({
  // ...
})
service.addEventListener('ws:open', function () {
  // do something.
})
service.addEventListener('tts:change', function (event) {
  console.log(event.detail); // get event data.
})
nameevent datadescription
ws:openundefinedWill be called after websocket connected.
ws:messageWebsocket MessageEventWill be called after receive websocket messages.
ws:closeundefinedWill be called after websocket disconnected.
tts:beginsame as SentenceBegin eventWill be called after receive SentenceBegin event.
tts:changesame as TranscriptionResultChanged eventWill be called after receive TranscriptionResultChanged event.
tts:endsame as SentenceEnd eventWill be called after receive SentenceEnd event.
tts:stopsame as TranscriptionCompleted eventWill be called after receive TranscriptionCompleted event.
readyundefinedWill be called after weakup microphone device.

Thanks

Some code is referenced from this article: https://juejin.cn/post/7028003824912564231, thanks!

1.0.0

11 months ago

0.0.1

11 months ago

0.0.1-beta.1

11 months ago