实时语音识别与合成

v1.0.0发布于:2024-08-12 18:38

说明文档
回复列表 (0)

介绍

实时语音与识别与合成,基于阿里云的智能语音交互,可以很方便地使用该能力

标识

调用插件的时候需要用到标识,标识是唯一的,不能重复,建议使用英文,不要使用中文,对应插件 plugin.json 中的 key 字段

  • 标识:voice-ali

配置

{
  "accessKeyId": "阿里云的accessKeyId",
  "accessKeySecret": "阿里云的accessKeySecret",
  "appkey": "项目的对应的appkey,可以在智能语音交互控制台获得",
  "expired": 120
}

方法

下面是插件提供的一些方法

  • getToken

获得 Token

 /**
   * 获得Token
   */
  async getToken()
  • textToSpeech

语音合成

 /** 语音合成
   * @param content 文本内容
   * @param callback 回调函数
   * @param params 参数
   */
  async textToSpeech(
    content: string,
    callback: (data: { event: string; buffer: Buffer }) => void,
    params?: {
      text?: string;
      voice?: string;
      format?: string;
      sample_rate?: number;
      volume?: number;
      speech_rate?: number;
      pitch_rate?: number;
      enable_subtitle?: boolean;
      platform?: string;
    }
  )
  • speechToText

语音识别

 /**
   * 语音识别
   * @param buffer 语音数据
   * @param taskId 任务ID
   * @param stop 是否停止
   * @param callback 回调函数
   * @param params 参数
   */
  async speechToText(
    buffer: Buffer,
    callback: (data: { event: string; data: any }) => void,
    taskId: string,
    stop = false,
    params?: {
      format?: string;
      sample_rate?: number;
      enable_intermediate_result?: boolean;
      enable_punctuation_predition?: boolean;
      enable_inverse_text_normalization?: boolean;
    }
  )

调用示例

@Inject()
pluginService: PluginService;

// 获取插件实例
const instance = await this.pluginService.getInstance('voice-ali');

// 语音合成
instance.textToSpeech(
  "COOL为开发者而生",
  (data) => {
    console.log(data);
  }
);

// 语音识别
let audioStream = fs.createReadStream("test.pcm", {
  encoding: "binary",
  highWaterMark: 1024,
});
audioStream.on("data", async (chunk) => {
  instance.speechToText(
    Buffer.from(chunk, "binary"),
    (data) => {
      console.log(data);
    },
    "xxx"
  );
});

更新日志

  • v1.0.0 (2024-08-12)
    • 初始版本