Skip to content

Commit bfa78b1

Browse files
committed
✨ feat: update api
1 parent 4668b58 commit bfa78b1

File tree

4 files changed

+56
-58
lines changed

4 files changed

+56
-58
lines changed

.dumirc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default defineConfig({
5252
'process.env': process.env,
5353
},
5454
favicons: ['https://npm.elemecdn.com/@lobehub/assets-favicons/assets/favicon.ico'],
55-
locales: [{ id: 'en-US', name: 'English' }],
55+
locales: [
56+
{ id: 'en-US', name: 'English' },
57+
{ id: 'zh-CN', name: '简体中文' },
58+
],
5659
// mfsu: isWin ? undefined : {},
5760
mfsu: false,
5861
npmClient: 'pnpm',
Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1+
---
2+
group: TTS
3+
title: EdgeSpeechTTS
4+
---
5+
16
# EdgeSpeechTTS
27

3-
## 简介
8+
## `constructor(options: EdgeSpeechAPI & { locale?: string }): EdgeSpeechTTS`
49

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

7-
## 构造函数
12+
### 示例
813

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

11-
创建一个 `EdgeSpeechTTS` 实例。
16+
Node 环境:
1217

13-
#### 参数
18+
```js
19+
import { EdgeSpeechTTS } from '@lobehub/tts';
20+
import fs from 'fs';
21+
import path from 'path';
22+
23+
// 实例化 EdgeSpeechTTS
24+
const tts = new EdgeSpeechTTS({ locale: 'zh-CN' });
25+
26+
// 创建语音合成请求负载
27+
const payload = {
28+
text: '这是一段语音演示',
29+
voice: 'zh-CN-XiaoxiaoNeural',
30+
};
31+
const speechFile = path.resolve('./speech.mp3');
32+
33+
// 调用 create 方法来合成语音
34+
async function main() {
35+
const mp3Buffer = await tts.create(payload);
36+
await fs.writeFileSync(speechFile, mp3Buffer);
37+
}
38+
39+
main();
40+
```
41+
42+
在此示例中,首先实例化了 `EdgeSpeechTTS` 类,并指定了后端服务的 URL 和语音区域设置。然后创建了一个包含文本和语音选项的请求负载。最后,通过调用 `create` 方法并传入负载来合成语音。如果合成成功,将返回一个包含音频数据的 `AudioBuffer` 对象。如果出现错误,将捕获并处理。
43+
44+
## 参数
1445

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

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

41-
### `fetch(payload: EdgeSpeechPayload): Promise<Response>`
42-
43-
内部方法,用于发送语音合成请求。
44-
45-
#### 参数
46-
47-
- `payload`: `EdgeSpeechPayload` 类型,包含语音合成请求的必要信息。
48-
49-
#### 返回值
50-
51-
返回一个 `Promise`,该 `Promise` 解析为包含音频数据的 `Response` 对象。
52-
53-
#### 异常
54-
55-
如果网络响应不成功,将抛出一个错误。
56-
5772
### `create(payload: EdgeSpeechPayload): Promise<AudioBuffer>`
5873

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

6782
返回一个 `Promise`,该 `Promise` 解析为 `AudioBuffer` 对象,包含合成的音频数据。
68-
69-
## 示例
70-
71-
以下是使用 `EdgeSpeechTTS` 类的示例代码:
72-
73-
```javascript
74-
import { EdgeSpeechTTS } from 'path-to-EdgeSpeechTTS';
75-
76-
// 实例化 EdgeSpeechTTS
77-
const tts = new EdgeSpeechTTS({
78-
backendUrl: 'https://your-backend-service.com/api/speech',
79-
locale: 'en-US',
80-
});
81-
82-
// 创建语音合成请求负载
83-
const payload = {
84-
text: 'Hello, world!',
85-
voice: 'en-US-Standard-B',
86-
// 其他选项...
87-
};
88-
89-
// 调用 create 方法来合成语音
90-
tts
91-
.create(payload)
92-
.then((audioBuffer) => {
93-
// 使用 audioBuffer
94-
})
95-
.catch((error) => {
96-
console.error('语音合成失败:', error);
97-
});
98-
```
99-
100-
在此示例中,首先实例化了 `EdgeSpeechTTS` 类,并指定了后端服务的 URL 和语音区域设置。然后创建了一个包含文本和语音选项的请求负载。最后,通过调用 `create` 方法并传入负载来合成语音。如果合成成功,将返回一个包含音频数据的 `AudioBuffer` 对象。如果出现错误,将捕获并处理。

docs/api/index.zh-CN.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# API Reference
2+
3+
## TTS
4+
5+
- [EdgeSpeechTTS](./edge-speech-tts.zh-CN.md)

src/core/EdgeSpeechTTS/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@ export class EdgeSpeechTTS {
4141
return response;
4242
};
4343

44-
create = async (payload: EdgeSpeechPayload): Promise<AudioBuffer> => {
45-
const response = await this.fetch(payload);
44+
create = async (payload: EdgeSpeechPayload): Promise<Response> => {
45+
return this.fetch(payload);
46+
};
47+
48+
/**
49+
* Browser only
50+
* @param payload
51+
*/
52+
createAudio = async (payload: EdgeSpeechPayload): Promise<AudioBuffer> => {
53+
const res = await this.create(payload);
4654

47-
const arrayBuffer = await response.arrayBuffer();
55+
const arrayBuffer = await res.arrayBuffer();
4856

4957
return arrayBufferConvert(arrayBuffer);
5058
};

0 commit comments

Comments
 (0)