Skip to content

Commit

Permalink
✨ feat: update api
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Nov 17, 2023
1 parent 4668b58 commit bfa78b1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 58 deletions.
5 changes: 4 additions & 1 deletion .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export default defineConfig({
'process.env': process.env,
},
favicons: ['https://npm.elemecdn.com/@lobehub/assets-favicons/assets/favicon.ico'],
locales: [{ id: 'en-US', name: 'English' }],
locales: [
{ id: 'en-US', name: 'English' },
{ id: 'zh-CN', name: '简体中文' },
],
// mfsu: isWin ? undefined : {},
mfsu: false,
npmClient: 'pnpm',
Expand Down
90 changes: 36 additions & 54 deletions docs/edge-speech-tts.md → docs/api/edge-speech-tts.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
---
group: TTS
title: EdgeSpeechTTS
---

# EdgeSpeechTTS

## 简介
## `constructor(options: EdgeSpeechAPI & { locale?: string }): EdgeSpeechTTS`

`EdgeSpeechTTS` 类是一个用于将文本转换为语音的工具,它可以在边缘运行时环境中使用。该类提供了一系列方法来获取语音选项,创建语音合成请求,并处理返回的音频数据。

## 构造函数
### 示例

### `constructor(options: EdgeSpeechAPI & { locale?: string }): EdgeSpeechTTS`
以下是使用 `EdgeSpeechTTS` 类的示例代码:

创建一个 `EdgeSpeechTTS` 实例。
Node 环境:

#### 参数
```js
import { EdgeSpeechTTS } from '@lobehub/tts';
import fs from 'fs';
import path from 'path';

// 实例化 EdgeSpeechTTS
const tts = new EdgeSpeechTTS({ locale: 'zh-CN' });

// 创建语音合成请求负载
const payload = {
text: '这是一段语音演示',
voice: 'zh-CN-XiaoxiaoNeural',
};
const speechFile = path.resolve('./speech.mp3');

// 调用 create 方法来合成语音
async function main() {
const mp3Buffer = await tts.create(payload);
await fs.writeFileSync(speechFile, mp3Buffer);
}

main();
```

在此示例中,首先实例化了 `EdgeSpeechTTS` 类,并指定了后端服务的 URL 和语音区域设置。然后创建了一个包含文本和语音选项的请求负载。最后,通过调用 `create` 方法并传入负载来合成语音。如果合成成功,将返回一个包含音频数据的 `AudioBuffer` 对象。如果出现错误,将捕获并处理。

## 参数

- `options`: 对象,可选。
- `backendUrl`: 字符串,指定后端服务的 URL。如果提供,将使用此 URL 发送请求。
Expand Down Expand Up @@ -38,22 +69,6 @@

返回一个包含当前可用语音选项的对象。

### `fetch(payload: EdgeSpeechPayload): Promise<Response>`

内部方法,用于发送语音合成请求。

#### 参数

- `payload`: `EdgeSpeechPayload` 类型,包含语音合成请求的必要信息。

#### 返回值

返回一个 `Promise`,该 `Promise` 解析为包含音频数据的 `Response` 对象。

#### 异常

如果网络响应不成功,将抛出一个错误。

### `create(payload: EdgeSpeechPayload): Promise<AudioBuffer>`

使用给定的请求负载创建语音合成。
Expand All @@ -65,36 +80,3 @@
#### 返回值

返回一个 `Promise`,该 `Promise` 解析为 `AudioBuffer` 对象,包含合成的音频数据。

## 示例

以下是使用 `EdgeSpeechTTS` 类的示例代码:

```javascript
import { EdgeSpeechTTS } from 'path-to-EdgeSpeechTTS';

// 实例化 EdgeSpeechTTS
const tts = new EdgeSpeechTTS({
backendUrl: 'https://your-backend-service.com/api/speech',
locale: 'en-US',
});

// 创建语音合成请求负载
const payload = {
text: 'Hello, world!',
voice: 'en-US-Standard-B',
// 其他选项...
};

// 调用 create 方法来合成语音
tts
.create(payload)
.then((audioBuffer) => {
// 使用 audioBuffer
})
.catch((error) => {
console.error('语音合成失败:', error);
});
```

在此示例中,首先实例化了 `EdgeSpeechTTS` 类,并指定了后端服务的 URL 和语音区域设置。然后创建了一个包含文本和语音选项的请求负载。最后,通过调用 `create` 方法并传入负载来合成语音。如果合成成功,将返回一个包含音频数据的 `AudioBuffer` 对象。如果出现错误,将捕获并处理。
5 changes: 5 additions & 0 deletions docs/api/index.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# API Reference

## TTS

- [EdgeSpeechTTS](./edge-speech-tts.zh-CN.md)
14 changes: 11 additions & 3 deletions src/core/EdgeSpeechTTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ export class EdgeSpeechTTS {
return response;
};

create = async (payload: EdgeSpeechPayload): Promise<AudioBuffer> => {
const response = await this.fetch(payload);
create = async (payload: EdgeSpeechPayload): Promise<Response> => {
return this.fetch(payload);
};

/**
* Browser only
* @param payload
*/
createAudio = async (payload: EdgeSpeechPayload): Promise<AudioBuffer> => {
const res = await this.create(payload);

const arrayBuffer = await response.arrayBuffer();
const arrayBuffer = await res.arrayBuffer();

return arrayBufferConvert(arrayBuffer);
};
Expand Down

0 comments on commit bfa78b1

Please sign in to comment.