TRTC Web SDKAPI 文档事件错误码类型教程更新日志
En

Tutorial: 开启美声效果

开启美声效果

功能描述

本文主要介绍如何使用美声插件。

前提条件

自 2023 年 4 月 1 日起,使用该功能需开通 TRTC 包月套餐 旗舰版及更高版本。

  • TRTC Web SDK 版本 >= 5.10。
  • 支持的浏览器: Chrome 66+, Edge 79+, Safari 15+, Firefox 76+, Chrome for Android 126+。

实现流程

1. 引入并注册插件

import { VoiceChanger } from 'trtc-sdk-v5/plugins/voice-changer';
let trtc = TRTC.create({ plugins: [VoiceChanger] });

2. 开启麦克风

await trtc.startLocalAudio();

3. 使用美声插件

await trtc.startPlugin('VoiceChanger', {
  voiceType: 1,
  sdkAppId: 123456,
  userId: 'user_123',
  userSig: 'XXXXXXXX'
});
// 开启后可调用 updatePlugin 更新参数
await trtc.updatePlugin('VoiceChanger', {
  voiceType: 2,
});
// 关闭麦克风采集前关闭美声插件
await trtc.stopPlugin('VoiceChanger');
await trtc.stopLocalAudio();

API 说明

trtc.startPlugin('VoiceChanger', options)

用于开启美声效果

options

参数名 类型 Attributes Description
sdkAppId number 当前应用的 sdkAppId
userId string 当前用户的 userId
userSig string 当前用户的 userSig
voiceType number 可选。1-熊孩子 2-萝莉 3-大叔 4-重金属 5-感冒 6-外语腔 7-困兽 8-肥宅 9-强电流 10-重机械 11-空灵 12-一群小黄人 13-自然男声 14-自然女声。传入时 pitch、formantRatio 将被忽略。
pitch number 可选。音调偏移量,取值范围 [-1, 1],对应 ±12 个半音,缺省为 0,仅当 voiceType 为空时生效。
formantRatio number 可选。共振峰搬移量,取值范围 [-1, 1],正值共振峰上移、负值下移,缺省为 0,仅当 voiceType 为空时生效。

注意:voiceType、pitch、formantRatio 至少需要传入一项,否则接口会抛出参数错误。

Example:

await trtc.startLocalAudio();
await trtc.startPlugin('VoiceChanger', {
  voiceType: 1,
  sdkAppId: 123456,
  userId: 'user_123',
  userSig: 'XXXXXXXX'
});
// 不传 voiceType,使用自定义音调和共振峰
await trtc.startPlugin('VoiceChanger', {
  pitch: 0.2,
  formantRatio: -0.2,
  sdkAppId: 123456,
  userId: 'user_123',
  userSig: 'XXXXXXXX'
});

trtc.updatePlugin('VoiceChanger', options)

修改美声效果

options

参数名 类型 Attributes Description
voiceType number 可选。1-熊孩子 2-萝莉 3-大叔 4-重金属 5-感冒 6-外语腔 7-困兽 8-肥宅 9-强电流 10-重机械 11-空灵 12-一群小黄人 13-自然男声 14-自然女声。传入时 pitch、formantRatio 将被忽略。
pitch number 可选。音调偏移量,取值范围 [-1, 1],对应 ±12 个半音,仅当 voiceType 为空时生效;若当前为预设音色,会先切换到自定义变声再应用。
formantRatio number 可选。共振峰搬移量,取值范围 [-1, 1],正值共振峰上移、负值下移,仅当 voiceType 为空时生效;若当前为预设音色,会先切换到自定义变声再应用。

注意:voiceType、pitch、formantRatio 至少需要传入一项,否则接口会抛出参数错误。

Example:

await trtc.updatePlugin('VoiceChanger', {
  voiceType: 2,
});
await trtc.updatePlugin('VoiceChanger', {
  pitch: 0.2,
  formantRatio: -0.2,
});

trtc.stopPlugin('VoiceChanger')

关闭美声效果

// 关闭麦克风采集前关闭美声插件
await trtc.stopPlugin('VoiceChanger');
await trtc.stopLocalAudio();