@mastra/voice-playai 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +36 -0
- package/LICENSE +44 -0
- package/README.md +129 -0
- package/dist/_tsup-dts-rollup.d.ts +43 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +219 -0
- package/package.json +33 -0
- package/src/index.test.ts +144 -0
- package/src/index.ts +255 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
> @mastra/voice-playai@0.1.0-alpha.2 build /home/runner/work/mastra/mastra/voice/playai
|
|
3
|
+
> tsup src/index.ts --format esm --experimental-dts --clean --treeshake
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.3.6
|
|
8
|
+
[34mTSC[39m Build start
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 5348ms
|
|
10
|
+
[34mDTS[39m Build start
|
|
11
|
+
[34mCLI[39m Target: es2022
|
|
12
|
+
Analysis will use the bundled TypeScript version 5.7.3
|
|
13
|
+
[36mWriting package typings: /home/runner/work/mastra/mastra/voice/playai/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
|
+
[32mDTS[39m ⚡️ Build success in 5115ms
|
|
15
|
+
[34mCLI[39m Cleaning output folder
|
|
16
|
+
[34mESM[39m Build start
|
|
17
|
+
[32mESM[39m [1mdist/index.js [22m[32m5.97 KB[39m
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 364ms
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @mastra/voice-playai
|
|
2
|
+
|
|
3
|
+
## 0.1.0-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 41d0166: deprecate @mastra/speech-playai for @mastra/voice-playai
|
|
8
|
+
- Updated dependencies [7fceae1]
|
|
9
|
+
- Updated dependencies [f626fbb]
|
|
10
|
+
- @mastra/core@0.4.2-alpha.0
|
|
11
|
+
|
|
12
|
+
## 0.1.0 (2024-XX-XX)
|
|
13
|
+
|
|
14
|
+
This package replaces the deprecated @mastra/speech-playai package. All functionality has been migrated to this new package with a more consistent naming scheme.
|
|
15
|
+
|
|
16
|
+
### Changes from @mastra/speech-playai
|
|
17
|
+
|
|
18
|
+
- Package renamed from @mastra/speech-playai to @mastra/voice-playai
|
|
19
|
+
- API changes:
|
|
20
|
+
- `PlayAITTS` class renamed to `PlayAIVoice`
|
|
21
|
+
- `generate()` and `stream()` methods combined into `speak()`
|
|
22
|
+
- `voices()` method renamed to `getSpeakers()`
|
|
23
|
+
- Constructor configuration simplified
|
|
24
|
+
- Added support for text stream input
|
|
25
|
+
- Default speaker configuration added
|
|
26
|
+
- All core functionality remains the same
|
|
27
|
+
- Import paths should be updated from '@mastra/speech-playai' to '@mastra/voice-playai'
|
|
28
|
+
|
|
29
|
+
### Key Features Preserved
|
|
30
|
+
|
|
31
|
+
- High-quality voice synthesis
|
|
32
|
+
- Multiple voice options with rich metadata
|
|
33
|
+
- Streaming support
|
|
34
|
+
- Natural and expressive speech output
|
|
35
|
+
|
|
36
|
+
For a complete history of changes prior to the rename, please see the changelog of the original package.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
**Acceptance**
|
|
4
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
5
|
+
|
|
6
|
+
**Copyright License**
|
|
7
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
|
|
8
|
+
|
|
9
|
+
**Limitations**
|
|
10
|
+
You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
|
|
11
|
+
|
|
12
|
+
You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
|
|
13
|
+
|
|
14
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
|
|
15
|
+
|
|
16
|
+
**Patents**
|
|
17
|
+
The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
18
|
+
|
|
19
|
+
**Notices**
|
|
20
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
|
|
21
|
+
|
|
22
|
+
If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
|
|
23
|
+
|
|
24
|
+
**No Other Rights**
|
|
25
|
+
These terms do not imply any licenses other than those expressly granted in these terms.
|
|
26
|
+
|
|
27
|
+
**Termination**
|
|
28
|
+
If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
|
|
29
|
+
|
|
30
|
+
**No Liability**
|
|
31
|
+
As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
|
|
32
|
+
|
|
33
|
+
**Definitions**
|
|
34
|
+
The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
|
|
35
|
+
|
|
36
|
+
_you_ refers to the individual or entity agreeing to these terms.
|
|
37
|
+
|
|
38
|
+
_your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
39
|
+
|
|
40
|
+
_your licenses_ are all the licenses granted to you for the software under these terms.
|
|
41
|
+
|
|
42
|
+
_use_ means anything you do with the software requiring one of your licenses.
|
|
43
|
+
|
|
44
|
+
_trademark_ means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @mastra/voice-playai
|
|
2
|
+
|
|
3
|
+
PlayAI Voice integration for Mastra, providing Text-to-Speech (TTS) capabilities using PlayAI's voice synthesis technology.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/voice-playai
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
The module requires the following environment variables:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
PLAYAI_API_KEY=your_api_key
|
|
17
|
+
PLAYAI_USER_ID=your_user_id
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { PlayAIVoice } from '@mastra/voice-playai';
|
|
24
|
+
|
|
25
|
+
// Initialize with configuration
|
|
26
|
+
const voice = new PlayAIVoice({
|
|
27
|
+
speechModel: {
|
|
28
|
+
name: 'PlayDialog', // Optional, defaults to 'PlayDialog'
|
|
29
|
+
apiKey: 'your-api-key', // Optional, can use PLAYAI_API_KEY env var
|
|
30
|
+
userId: 'your-user-id', // Optional, can use PLAYAI_USER_ID env var
|
|
31
|
+
},
|
|
32
|
+
speaker: 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json', // Optional, defaults to first available voice
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Or use with defaults (using env vars)
|
|
36
|
+
const defaultVoice = new PlayAIVoice();
|
|
37
|
+
|
|
38
|
+
// List available speakers
|
|
39
|
+
const speakers = await voice.getSpeakers();
|
|
40
|
+
|
|
41
|
+
// Generate speech from text
|
|
42
|
+
const stream = await voice.speak('Hello from Mastra!');
|
|
43
|
+
|
|
44
|
+
// Or generate speech from a text stream
|
|
45
|
+
const textStream = getTextStream(); // Your text stream source
|
|
46
|
+
const audioStream = await voice.speak(textStream);
|
|
47
|
+
|
|
48
|
+
// The stream can be piped to a destination
|
|
49
|
+
stream.pipe(destination);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- High-quality Text-to-Speech synthesis
|
|
55
|
+
- Multiple voice options
|
|
56
|
+
- Streaming support for both input and output
|
|
57
|
+
- Natural and expressive speech output
|
|
58
|
+
- Voice customization options
|
|
59
|
+
|
|
60
|
+
## Available Voices
|
|
61
|
+
|
|
62
|
+
PlayAI offers a diverse selection of voices with different characteristics:
|
|
63
|
+
|
|
64
|
+
### Conversational Style
|
|
65
|
+
|
|
66
|
+
- Angelo (US, Young Male)
|
|
67
|
+
- ID: `s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json`
|
|
68
|
+
- Arsenio (US African American, Middle-aged Male)
|
|
69
|
+
- ID: `s3://voice-cloning-zero-shot/65977f5e-a22a-4b36-861b-ecede19bdd65/original/manifest.json`
|
|
70
|
+
- Cillian (Irish, Middle-aged Male)
|
|
71
|
+
- ID: `s3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json`
|
|
72
|
+
- Timo (US, Middle-aged Male)
|
|
73
|
+
- ID: `s3://voice-cloning-zero-shot/677a4ae3-252f-476e-85ce-eeed68e85951/original/manifest.json`
|
|
74
|
+
- Dexter (US, Middle-aged Male)
|
|
75
|
+
- ID: `s3://voice-cloning-zero-shot/b27bc13e-996f-4841-b584-4d35801aea98/original/manifest.json`
|
|
76
|
+
- Miles (US African American, Young Male)
|
|
77
|
+
- ID: `s3://voice-cloning-zero-shot/29dd9a52-bd32-4a6e-bff1-bbb98dcc286a/original/manifest.json`
|
|
78
|
+
- Briggs (US Southern/Oklahoma, Elderly Male)
|
|
79
|
+
- ID: `s3://voice-cloning-zero-shot/71cdb799-1e03-41c6-8a05-f7cd55134b0b/original/manifest.json`
|
|
80
|
+
- Deedee (US African American, Middle-aged Female)
|
|
81
|
+
- ID: `s3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json`
|
|
82
|
+
- Nia (US, Young Female)
|
|
83
|
+
- ID: `s3://voice-cloning-zero-shot/831bd330-85c6-4333-b2b4-10c476ea3491/original/manifest.json`
|
|
84
|
+
- Inara (US African American, Middle-aged Female)
|
|
85
|
+
- ID: `s3://voice-cloning-zero-shot/adb83b67-8d75-48ff-ad4d-a0840d231ef1/original/manifest.json`
|
|
86
|
+
- Constanza (US Latin American, Young Female)
|
|
87
|
+
- ID: `s3://voice-cloning-zero-shot/b0aca4d7-1738-4848-a80b-307ac44a7298/original/manifest.json`
|
|
88
|
+
|
|
89
|
+
### Narrative Style
|
|
90
|
+
|
|
91
|
+
- Gideon (British, Elderly Male)
|
|
92
|
+
- ID: `s3://voice-cloning-zero-shot/5a3a1168-7793-4b2c-8f90-aff2b5232131/original/manifest.json`
|
|
93
|
+
- Casper (US, Middle-aged Male)
|
|
94
|
+
- ID: `s3://voice-cloning-zero-shot/1bbc6986-fadf-4bd8-98aa-b86fed0476e9/original/manifest.json`
|
|
95
|
+
- Mitch (Australian, Middle-aged Male)
|
|
96
|
+
- ID: `s3://voice-cloning-zero-shot/c14e50f2-c5e3-47d1-8c45-fa4b67803d19/original/manifest.json`
|
|
97
|
+
- Ava (Australian, Middle-aged Female)
|
|
98
|
+
- ID: `s3://voice-cloning-zero-shot/50381567-ff7b-46d2-bfdc-a9584a85e08d/original/manifest.json`
|
|
99
|
+
|
|
100
|
+
Each voice includes metadata about accent, gender, age, and speaking style. Use the voice ID when specifying a speaker.
|
|
101
|
+
|
|
102
|
+
## API Reference
|
|
103
|
+
|
|
104
|
+
### Constructor
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
new PlayAIVoice({
|
|
108
|
+
speechModel?: {
|
|
109
|
+
name?: 'PlayDialog' | 'Play3.0-mini', // Default: 'PlayDialog'
|
|
110
|
+
apiKey?: string, // Optional, can use PLAYAI_API_KEY env var
|
|
111
|
+
userId?: string, // Optional, can use PLAYAI_USER_ID env var
|
|
112
|
+
},
|
|
113
|
+
speaker?: string // Optional, defaults to first available voice ID
|
|
114
|
+
})
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Methods
|
|
118
|
+
|
|
119
|
+
#### `speak(input: string | NodeJS.ReadableStream, options?: { speaker?: string })`
|
|
120
|
+
|
|
121
|
+
Converts text to speech. The speaker option should be a voice ID. Returns a readable stream of audio data.
|
|
122
|
+
|
|
123
|
+
#### `getSpeakers()`
|
|
124
|
+
|
|
125
|
+
Returns a list of available speakers with their details including voice IDs.
|
|
126
|
+
|
|
127
|
+
#### `listen()`
|
|
128
|
+
|
|
129
|
+
Not supported - PlayAI does not provide speech recognition.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
2
|
+
|
|
3
|
+
export declare const PLAYAI_VOICES: PlayAIVoiceInfo[];
|
|
4
|
+
|
|
5
|
+
declare interface PlayAIConfig {
|
|
6
|
+
name?: 'PlayDialog' | 'Play3.0-mini';
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
userId?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare class PlayAIVoice extends MastraVoice {
|
|
12
|
+
private baseUrl;
|
|
13
|
+
private userId;
|
|
14
|
+
constructor({ speechModel, speaker }?: {
|
|
15
|
+
speechModel?: PlayAIConfig;
|
|
16
|
+
speaker?: string;
|
|
17
|
+
});
|
|
18
|
+
private makeRequest;
|
|
19
|
+
private streamToString;
|
|
20
|
+
speak(input: string | NodeJS.ReadableStream, options?: {
|
|
21
|
+
speaker?: string;
|
|
22
|
+
}): Promise<NodeJS.ReadableStream>;
|
|
23
|
+
listen(_input: NodeJS.ReadableStream, _options?: Record<string, unknown>): Promise<string | NodeJS.ReadableStream>;
|
|
24
|
+
getSpeakers(): Promise<{
|
|
25
|
+
voiceId: string;
|
|
26
|
+
name: string;
|
|
27
|
+
accent: string;
|
|
28
|
+
gender: "M" | "F";
|
|
29
|
+
age: "Young" | "Middle" | "Old";
|
|
30
|
+
style: "Conversational" | "Narrative";
|
|
31
|
+
}[]>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare interface PlayAIVoiceInfo {
|
|
35
|
+
name: string;
|
|
36
|
+
accent: string;
|
|
37
|
+
gender: 'M' | 'F';
|
|
38
|
+
age: 'Young' | 'Middle' | 'Old';
|
|
39
|
+
style: 'Conversational' | 'Narrative';
|
|
40
|
+
id: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { }
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
2
|
+
import { PassThrough } from 'stream';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var PLAYAI_VOICES = [
|
|
6
|
+
{
|
|
7
|
+
name: "Angelo",
|
|
8
|
+
accent: "US",
|
|
9
|
+
gender: "M",
|
|
10
|
+
age: "Young",
|
|
11
|
+
style: "Conversational",
|
|
12
|
+
id: "s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "Arsenio",
|
|
16
|
+
accent: "US African American",
|
|
17
|
+
gender: "M",
|
|
18
|
+
age: "Middle",
|
|
19
|
+
style: "Conversational",
|
|
20
|
+
id: "s3://voice-cloning-zero-shot/65977f5e-a22a-4b36-861b-ecede19bdd65/original/manifest.json"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "Cillian",
|
|
24
|
+
accent: "Irish",
|
|
25
|
+
gender: "M",
|
|
26
|
+
age: "Middle",
|
|
27
|
+
style: "Conversational",
|
|
28
|
+
id: "s3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "Timo",
|
|
32
|
+
accent: "US",
|
|
33
|
+
gender: "M",
|
|
34
|
+
age: "Middle",
|
|
35
|
+
style: "Conversational",
|
|
36
|
+
id: "s3://voice-cloning-zero-shot/677a4ae3-252f-476e-85ce-eeed68e85951/original/manifest.json"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: "Dexter",
|
|
40
|
+
accent: "US",
|
|
41
|
+
gender: "M",
|
|
42
|
+
age: "Middle",
|
|
43
|
+
style: "Conversational",
|
|
44
|
+
id: "s3://voice-cloning-zero-shot/b27bc13e-996f-4841-b584-4d35801aea98/original/manifest.json"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "Miles",
|
|
48
|
+
accent: "US African American",
|
|
49
|
+
gender: "M",
|
|
50
|
+
age: "Young",
|
|
51
|
+
style: "Conversational",
|
|
52
|
+
id: "s3://voice-cloning-zero-shot/29dd9a52-bd32-4a6e-bff1-bbb98dcc286a/original/manifest.json"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "Briggs",
|
|
56
|
+
accent: "US Southern (Oklahoma)",
|
|
57
|
+
gender: "M",
|
|
58
|
+
age: "Old",
|
|
59
|
+
style: "Conversational",
|
|
60
|
+
id: "s3://voice-cloning-zero-shot/71cdb799-1e03-41c6-8a05-f7cd55134b0b/original/manifest.json"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "Deedee",
|
|
64
|
+
accent: "US African American",
|
|
65
|
+
gender: "F",
|
|
66
|
+
age: "Middle",
|
|
67
|
+
style: "Conversational",
|
|
68
|
+
id: "s3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "Nia",
|
|
72
|
+
accent: "US",
|
|
73
|
+
gender: "F",
|
|
74
|
+
age: "Young",
|
|
75
|
+
style: "Conversational",
|
|
76
|
+
id: "s3://voice-cloning-zero-shot/831bd330-85c6-4333-b2b4-10c476ea3491/original/manifest.json"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "Inara",
|
|
80
|
+
accent: "US African American",
|
|
81
|
+
gender: "F",
|
|
82
|
+
age: "Middle",
|
|
83
|
+
style: "Conversational",
|
|
84
|
+
id: "s3://voice-cloning-zero-shot/adb83b67-8d75-48ff-ad4d-a0840d231ef1/original/manifest.json"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: "Constanza",
|
|
88
|
+
accent: "US Latin American",
|
|
89
|
+
gender: "F",
|
|
90
|
+
age: "Young",
|
|
91
|
+
style: "Conversational",
|
|
92
|
+
id: "s3://voice-cloning-zero-shot/b0aca4d7-1738-4848-a80b-307ac44a7298/original/manifest.json"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: "Gideon",
|
|
96
|
+
accent: "British",
|
|
97
|
+
gender: "M",
|
|
98
|
+
age: "Old",
|
|
99
|
+
style: "Narrative",
|
|
100
|
+
id: "s3://voice-cloning-zero-shot/5a3a1168-7793-4b2c-8f90-aff2b5232131/original/manifest.json"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "Casper",
|
|
104
|
+
accent: "US",
|
|
105
|
+
gender: "M",
|
|
106
|
+
age: "Middle",
|
|
107
|
+
style: "Narrative",
|
|
108
|
+
id: "s3://voice-cloning-zero-shot/1bbc6986-fadf-4bd8-98aa-b86fed0476e9/original/manifest.json"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "Mitch",
|
|
112
|
+
accent: "Australian",
|
|
113
|
+
gender: "M",
|
|
114
|
+
age: "Middle",
|
|
115
|
+
style: "Narrative",
|
|
116
|
+
id: "s3://voice-cloning-zero-shot/c14e50f2-c5e3-47d1-8c45-fa4b67803d19/original/manifest.json"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: "Ava",
|
|
120
|
+
accent: "Australian",
|
|
121
|
+
gender: "F",
|
|
122
|
+
age: "Middle",
|
|
123
|
+
style: "Narrative",
|
|
124
|
+
id: "s3://voice-cloning-zero-shot/50381567-ff7b-46d2-bfdc-a9584a85e08d/original/manifest.json"
|
|
125
|
+
}
|
|
126
|
+
];
|
|
127
|
+
var PlayAIVoice = class extends MastraVoice {
|
|
128
|
+
baseUrl = "https://api.play.ai/api/v1";
|
|
129
|
+
userId;
|
|
130
|
+
constructor({ speechModel, speaker } = {}) {
|
|
131
|
+
super({
|
|
132
|
+
speechModel: {
|
|
133
|
+
name: speechModel?.name ?? "PlayDialog",
|
|
134
|
+
apiKey: speechModel?.apiKey ?? process.env.PLAYAI_API_KEY
|
|
135
|
+
},
|
|
136
|
+
speaker: speaker ?? PLAYAI_VOICES[0]?.id
|
|
137
|
+
});
|
|
138
|
+
const userId = speechModel?.userId ?? process.env.PLAYAI_USER_ID;
|
|
139
|
+
if (!userId) {
|
|
140
|
+
throw new Error("userId is required");
|
|
141
|
+
}
|
|
142
|
+
this.userId = userId;
|
|
143
|
+
}
|
|
144
|
+
async makeRequest(endpoint, payload, method = "POST") {
|
|
145
|
+
const headers = new Headers({
|
|
146
|
+
Authorization: `Bearer ${this.speechModel?.apiKey}`,
|
|
147
|
+
"Content-Type": "application/json",
|
|
148
|
+
"X-USER-ID": this.userId
|
|
149
|
+
});
|
|
150
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
151
|
+
method,
|
|
152
|
+
headers,
|
|
153
|
+
body: payload ? JSON.stringify(payload) : void 0
|
|
154
|
+
});
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
const error = await response.json();
|
|
157
|
+
throw new Error(`PlayAI API Error: ${error.message || response.statusText}`);
|
|
158
|
+
}
|
|
159
|
+
return response;
|
|
160
|
+
}
|
|
161
|
+
async streamToString(stream) {
|
|
162
|
+
const chunks = [];
|
|
163
|
+
for await (const chunk of stream) {
|
|
164
|
+
chunks.push(Buffer.from(chunk));
|
|
165
|
+
}
|
|
166
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
167
|
+
}
|
|
168
|
+
async speak(input, options) {
|
|
169
|
+
const text = typeof input === "string" ? input : await this.streamToString(input);
|
|
170
|
+
return this.traced(async () => {
|
|
171
|
+
const payload = {
|
|
172
|
+
text,
|
|
173
|
+
voice: options?.speaker || this.speaker,
|
|
174
|
+
model: this.speechModel?.name
|
|
175
|
+
};
|
|
176
|
+
const response = await this.makeRequest("/tts/stream", payload);
|
|
177
|
+
if (!response.body) {
|
|
178
|
+
throw new Error("No response body received");
|
|
179
|
+
}
|
|
180
|
+
const stream = new PassThrough();
|
|
181
|
+
const reader = response.body.getReader();
|
|
182
|
+
(async () => {
|
|
183
|
+
try {
|
|
184
|
+
while (true) {
|
|
185
|
+
const { done, value } = await reader.read();
|
|
186
|
+
if (done) {
|
|
187
|
+
stream.end();
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
stream.write(value);
|
|
191
|
+
}
|
|
192
|
+
} catch (error) {
|
|
193
|
+
stream.destroy(error);
|
|
194
|
+
}
|
|
195
|
+
})();
|
|
196
|
+
return stream;
|
|
197
|
+
}, "voice.playai.speak")();
|
|
198
|
+
}
|
|
199
|
+
async listen(_input, _options) {
|
|
200
|
+
throw new Error("PlayAI does not support speech recognition");
|
|
201
|
+
}
|
|
202
|
+
async getSpeakers() {
|
|
203
|
+
return this.traced(
|
|
204
|
+
() => Promise.resolve(
|
|
205
|
+
PLAYAI_VOICES.map((voice) => ({
|
|
206
|
+
voiceId: voice.id,
|
|
207
|
+
name: voice.name,
|
|
208
|
+
accent: voice.accent,
|
|
209
|
+
gender: voice.gender,
|
|
210
|
+
age: voice.age,
|
|
211
|
+
style: voice.style
|
|
212
|
+
}))
|
|
213
|
+
),
|
|
214
|
+
"voice.playai.voices"
|
|
215
|
+
)();
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export { PLAYAI_VOICES, PlayAIVoice };
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/voice-playai",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "Mastra PlayAI voice integration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"zod": "^3.24.1",
|
|
19
|
+
"@mastra/core": "^0.4.2-alpha.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@microsoft/api-extractor": "^7.49.2",
|
|
23
|
+
"@types/node": "^22.13.1",
|
|
24
|
+
"tsup": "^8.0.1",
|
|
25
|
+
"typescript": "^5.7.3",
|
|
26
|
+
"vitest": "^2.1.8"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
|
|
30
|
+
"build:watch": "pnpm build --watch",
|
|
31
|
+
"test": "vitest run"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createWriteStream, mkdirSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { writeFile } from 'fs/promises';
|
|
7
|
+
|
|
8
|
+
import { PlayAIVoice, PLAYAI_VOICES } from './index.js';
|
|
9
|
+
|
|
10
|
+
describe('PlayAI Voice Integration Tests', () => {
|
|
11
|
+
const voice = new PlayAIVoice({
|
|
12
|
+
speechModel: {
|
|
13
|
+
name: 'PlayDialog',
|
|
14
|
+
apiKey: process.env.PLAYAI_API_KEY!,
|
|
15
|
+
userId: process.env.PLAYAI_USER_ID!,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
const outputDir = path.join(process.cwd(), 'test-outputs');
|
|
19
|
+
let voiceId: string;
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
// Create output directory if it doesn't exist
|
|
23
|
+
try {
|
|
24
|
+
mkdirSync(outputDir, { recursive: true });
|
|
25
|
+
} catch (err) {
|
|
26
|
+
// Ignore if directory already exists
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const speakers = await voice.getSpeakers();
|
|
30
|
+
voiceId = speakers.find(
|
|
31
|
+
v => v.voiceId === 's3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json',
|
|
32
|
+
)!.voiceId;
|
|
33
|
+
expect(voiceId).toBeDefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should verify available speakers', async () => {
|
|
37
|
+
const speakers = await voice.getSpeakers();
|
|
38
|
+
expect(speakers.length).toBeGreaterThan(0);
|
|
39
|
+
expect(speakers[0]).toHaveProperty('voiceId');
|
|
40
|
+
expect(speakers[0].voiceId).toBe(PLAYAI_VOICES[0].id);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should allow immediate playback while streaming', async () => {
|
|
44
|
+
// Create a longer text to ensure we get multiple chunks
|
|
45
|
+
const longText = 'This is a longer text that will be streamed. '.repeat(5);
|
|
46
|
+
|
|
47
|
+
const audioStream = await voice.speak(longText, {
|
|
48
|
+
speaker: voiceId,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Create a write stream to simulate real-time playback
|
|
52
|
+
const outputPath = path.join(outputDir, 'playai-streaming-output.mp3');
|
|
53
|
+
const writeStream = createWriteStream(outputPath);
|
|
54
|
+
|
|
55
|
+
let firstChunkTime: number | null = null;
|
|
56
|
+
let lastChunkTime: number | null = null;
|
|
57
|
+
let totalChunks = 0;
|
|
58
|
+
|
|
59
|
+
for await (const chunk of audioStream) {
|
|
60
|
+
if (!firstChunkTime) {
|
|
61
|
+
firstChunkTime = Date.now();
|
|
62
|
+
}
|
|
63
|
+
lastChunkTime = Date.now();
|
|
64
|
+
totalChunks++;
|
|
65
|
+
writeStream.write(chunk);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
writeStream.end();
|
|
69
|
+
expect(firstChunkTime).toBeDefined();
|
|
70
|
+
expect(lastChunkTime).toBeDefined();
|
|
71
|
+
expect(lastChunkTime! - firstChunkTime!).toBeGreaterThan(100); // Should take some time to receive all chunks
|
|
72
|
+
console.log(`Total streaming time: ${lastChunkTime! - firstChunkTime!}ms for ${totalChunks} chunks`);
|
|
73
|
+
}, 30000);
|
|
74
|
+
|
|
75
|
+
it('should test speak method', async () => {
|
|
76
|
+
const audioStream = await voice.speak('Hello from PlayAI!', {
|
|
77
|
+
speaker: voiceId,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const chunks: Buffer[] = [];
|
|
81
|
+
for await (const chunk of audioStream) {
|
|
82
|
+
chunks.push(Buffer.from(chunk));
|
|
83
|
+
}
|
|
84
|
+
const audioBuffer = Buffer.concat(chunks);
|
|
85
|
+
|
|
86
|
+
await writeFile(path.join(outputDir, 'playai-generate-output.mp3'), audioBuffer);
|
|
87
|
+
expect(audioBuffer.length).toBeGreaterThan(0);
|
|
88
|
+
}, 30000);
|
|
89
|
+
|
|
90
|
+
it('should handle stream input in speak method', async () => {
|
|
91
|
+
// Create a readable stream from text
|
|
92
|
+
const textStream = Readable.from(['Hello', ' from', ' stream', ' input!']);
|
|
93
|
+
|
|
94
|
+
const audioStream = await voice.speak(textStream, {
|
|
95
|
+
speaker: voiceId,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const chunks: Buffer[] = [];
|
|
99
|
+
for await (const chunk of audioStream) {
|
|
100
|
+
chunks.push(Buffer.from(chunk));
|
|
101
|
+
}
|
|
102
|
+
const audioBuffer = Buffer.concat(chunks);
|
|
103
|
+
|
|
104
|
+
await writeFile(path.join(outputDir, 'playai-stream-input-output.mp3'), audioBuffer);
|
|
105
|
+
expect(audioBuffer.length).toBeGreaterThan(0);
|
|
106
|
+
}, 30000);
|
|
107
|
+
|
|
108
|
+
it('should handle errors gracefully', async () => {
|
|
109
|
+
// Test with invalid voice ID
|
|
110
|
+
await expect(
|
|
111
|
+
voice.speak('Hello', {
|
|
112
|
+
speaker: 'invalid-voice-id',
|
|
113
|
+
}),
|
|
114
|
+
).rejects.toThrow();
|
|
115
|
+
|
|
116
|
+
// Test with empty text
|
|
117
|
+
await expect(
|
|
118
|
+
voice.speak('', {
|
|
119
|
+
speaker: voiceId,
|
|
120
|
+
}),
|
|
121
|
+
).rejects.toThrow();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should work with default configuration', async () => {
|
|
125
|
+
// Create instance with no args
|
|
126
|
+
const defaultVoice = new PlayAIVoice();
|
|
127
|
+
|
|
128
|
+
// Should use default model name and get API key and userId from env
|
|
129
|
+
const audioStream = await defaultVoice.speak('Testing default configuration');
|
|
130
|
+
|
|
131
|
+
const chunks: Buffer[] = [];
|
|
132
|
+
for await (const chunk of audioStream) {
|
|
133
|
+
chunks.push(Buffer.from(chunk));
|
|
134
|
+
}
|
|
135
|
+
const audioBuffer = Buffer.concat(chunks);
|
|
136
|
+
|
|
137
|
+
await writeFile(path.join(outputDir, 'playai-default-config-output.mp3'), audioBuffer);
|
|
138
|
+
expect(audioBuffer.length).toBeGreaterThan(0);
|
|
139
|
+
|
|
140
|
+
// Verify default speaker was used
|
|
141
|
+
const speakers = await defaultVoice.getSpeakers();
|
|
142
|
+
expect(speakers.length).toBeGreaterThan(0);
|
|
143
|
+
}, 30000);
|
|
144
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
2
|
+
import { PassThrough } from 'stream';
|
|
3
|
+
|
|
4
|
+
interface PlayAIVoiceInfo {
|
|
5
|
+
name: string;
|
|
6
|
+
accent: string;
|
|
7
|
+
gender: 'M' | 'F';
|
|
8
|
+
age: 'Young' | 'Middle' | 'Old';
|
|
9
|
+
style: 'Conversational' | 'Narrative';
|
|
10
|
+
id: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const PLAYAI_VOICES: PlayAIVoiceInfo[] = [
|
|
14
|
+
{
|
|
15
|
+
name: 'Angelo',
|
|
16
|
+
accent: 'US',
|
|
17
|
+
gender: 'M',
|
|
18
|
+
age: 'Young',
|
|
19
|
+
style: 'Conversational',
|
|
20
|
+
id: 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'Arsenio',
|
|
24
|
+
accent: 'US African American',
|
|
25
|
+
gender: 'M',
|
|
26
|
+
age: 'Middle',
|
|
27
|
+
style: 'Conversational',
|
|
28
|
+
id: 's3://voice-cloning-zero-shot/65977f5e-a22a-4b36-861b-ecede19bdd65/original/manifest.json',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'Cillian',
|
|
32
|
+
accent: 'Irish',
|
|
33
|
+
gender: 'M',
|
|
34
|
+
age: 'Middle',
|
|
35
|
+
style: 'Conversational',
|
|
36
|
+
id: 's3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'Timo',
|
|
40
|
+
accent: 'US',
|
|
41
|
+
gender: 'M',
|
|
42
|
+
age: 'Middle',
|
|
43
|
+
style: 'Conversational',
|
|
44
|
+
id: 's3://voice-cloning-zero-shot/677a4ae3-252f-476e-85ce-eeed68e85951/original/manifest.json',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'Dexter',
|
|
48
|
+
accent: 'US',
|
|
49
|
+
gender: 'M',
|
|
50
|
+
age: 'Middle',
|
|
51
|
+
style: 'Conversational',
|
|
52
|
+
id: 's3://voice-cloning-zero-shot/b27bc13e-996f-4841-b584-4d35801aea98/original/manifest.json',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'Miles',
|
|
56
|
+
accent: 'US African American',
|
|
57
|
+
gender: 'M',
|
|
58
|
+
age: 'Young',
|
|
59
|
+
style: 'Conversational',
|
|
60
|
+
id: 's3://voice-cloning-zero-shot/29dd9a52-bd32-4a6e-bff1-bbb98dcc286a/original/manifest.json',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'Briggs',
|
|
64
|
+
accent: 'US Southern (Oklahoma)',
|
|
65
|
+
gender: 'M',
|
|
66
|
+
age: 'Old',
|
|
67
|
+
style: 'Conversational',
|
|
68
|
+
id: 's3://voice-cloning-zero-shot/71cdb799-1e03-41c6-8a05-f7cd55134b0b/original/manifest.json',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'Deedee',
|
|
72
|
+
accent: 'US African American',
|
|
73
|
+
gender: 'F',
|
|
74
|
+
age: 'Middle',
|
|
75
|
+
style: 'Conversational',
|
|
76
|
+
id: 's3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'Nia',
|
|
80
|
+
accent: 'US',
|
|
81
|
+
gender: 'F',
|
|
82
|
+
age: 'Young',
|
|
83
|
+
style: 'Conversational',
|
|
84
|
+
id: 's3://voice-cloning-zero-shot/831bd330-85c6-4333-b2b4-10c476ea3491/original/manifest.json',
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: 'Inara',
|
|
88
|
+
accent: 'US African American',
|
|
89
|
+
gender: 'F',
|
|
90
|
+
age: 'Middle',
|
|
91
|
+
style: 'Conversational',
|
|
92
|
+
id: 's3://voice-cloning-zero-shot/adb83b67-8d75-48ff-ad4d-a0840d231ef1/original/manifest.json',
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: 'Constanza',
|
|
96
|
+
accent: 'US Latin American',
|
|
97
|
+
gender: 'F',
|
|
98
|
+
age: 'Young',
|
|
99
|
+
style: 'Conversational',
|
|
100
|
+
id: 's3://voice-cloning-zero-shot/b0aca4d7-1738-4848-a80b-307ac44a7298/original/manifest.json',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: 'Gideon',
|
|
104
|
+
accent: 'British',
|
|
105
|
+
gender: 'M',
|
|
106
|
+
age: 'Old',
|
|
107
|
+
style: 'Narrative',
|
|
108
|
+
id: 's3://voice-cloning-zero-shot/5a3a1168-7793-4b2c-8f90-aff2b5232131/original/manifest.json',
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'Casper',
|
|
112
|
+
accent: 'US',
|
|
113
|
+
gender: 'M',
|
|
114
|
+
age: 'Middle',
|
|
115
|
+
style: 'Narrative',
|
|
116
|
+
id: 's3://voice-cloning-zero-shot/1bbc6986-fadf-4bd8-98aa-b86fed0476e9/original/manifest.json',
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: 'Mitch',
|
|
120
|
+
accent: 'Australian',
|
|
121
|
+
gender: 'M',
|
|
122
|
+
age: 'Middle',
|
|
123
|
+
style: 'Narrative',
|
|
124
|
+
id: 's3://voice-cloning-zero-shot/c14e50f2-c5e3-47d1-8c45-fa4b67803d19/original/manifest.json',
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'Ava',
|
|
128
|
+
accent: 'Australian',
|
|
129
|
+
gender: 'F',
|
|
130
|
+
age: 'Middle',
|
|
131
|
+
style: 'Narrative',
|
|
132
|
+
id: 's3://voice-cloning-zero-shot/50381567-ff7b-46d2-bfdc-a9584a85e08d/original/manifest.json',
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
interface PlayAIConfig {
|
|
137
|
+
name?: 'PlayDialog' | 'Play3.0-mini';
|
|
138
|
+
apiKey?: string;
|
|
139
|
+
userId?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export class PlayAIVoice extends MastraVoice {
|
|
143
|
+
private baseUrl = 'https://api.play.ai/api/v1';
|
|
144
|
+
private userId: string;
|
|
145
|
+
|
|
146
|
+
constructor({ speechModel, speaker }: { speechModel?: PlayAIConfig; speaker?: string } = {}) {
|
|
147
|
+
super({
|
|
148
|
+
speechModel: {
|
|
149
|
+
name: speechModel?.name ?? 'PlayDialog',
|
|
150
|
+
apiKey: speechModel?.apiKey ?? process.env.PLAYAI_API_KEY,
|
|
151
|
+
},
|
|
152
|
+
speaker: speaker ?? PLAYAI_VOICES[0]?.id,
|
|
153
|
+
});
|
|
154
|
+
const userId = speechModel?.userId ?? process.env.PLAYAI_USER_ID;
|
|
155
|
+
if (!userId) {
|
|
156
|
+
throw new Error('userId is required');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
this.userId = userId;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private async makeRequest(endpoint: string, payload?: any, method: 'GET' | 'POST' = 'POST') {
|
|
163
|
+
const headers = new Headers({
|
|
164
|
+
Authorization: `Bearer ${this.speechModel?.apiKey}`,
|
|
165
|
+
'Content-Type': 'application/json',
|
|
166
|
+
'X-USER-ID': this.userId,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
170
|
+
method,
|
|
171
|
+
headers,
|
|
172
|
+
body: payload ? JSON.stringify(payload) : undefined,
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (!response.ok) {
|
|
176
|
+
const error = await response.json();
|
|
177
|
+
|
|
178
|
+
// @ts-expect-error - PlayAI API returns an error object but we don't type it
|
|
179
|
+
throw new Error(`PlayAI API Error: ${error.message || response.statusText}`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return response;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private async streamToString(stream: NodeJS.ReadableStream): Promise<string> {
|
|
186
|
+
const chunks: Buffer[] = [];
|
|
187
|
+
for await (const chunk of stream) {
|
|
188
|
+
chunks.push(Buffer.from(chunk));
|
|
189
|
+
}
|
|
190
|
+
return Buffer.concat(chunks).toString('utf-8');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async speak(input: string | NodeJS.ReadableStream, options?: { speaker?: string }): Promise<NodeJS.ReadableStream> {
|
|
194
|
+
const text = typeof input === 'string' ? input : await this.streamToString(input);
|
|
195
|
+
|
|
196
|
+
return this.traced(async () => {
|
|
197
|
+
const payload = {
|
|
198
|
+
text,
|
|
199
|
+
voice: options?.speaker || this.speaker,
|
|
200
|
+
model: this.speechModel?.name,
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const response = await this.makeRequest('/tts/stream', payload);
|
|
204
|
+
if (!response.body) {
|
|
205
|
+
throw new Error('No response body received');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Create a PassThrough stream for the audio
|
|
209
|
+
const stream = new PassThrough();
|
|
210
|
+
|
|
211
|
+
// Process the stream
|
|
212
|
+
const reader = response.body.getReader();
|
|
213
|
+
(async () => {
|
|
214
|
+
try {
|
|
215
|
+
while (true) {
|
|
216
|
+
const { done, value } = await reader.read();
|
|
217
|
+
if (done) {
|
|
218
|
+
stream.end();
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
stream.write(value);
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
stream.destroy(error as Error);
|
|
225
|
+
}
|
|
226
|
+
})();
|
|
227
|
+
|
|
228
|
+
return stream;
|
|
229
|
+
}, 'voice.playai.speak')();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async listen(
|
|
233
|
+
_input: NodeJS.ReadableStream,
|
|
234
|
+
_options?: Record<string, unknown>,
|
|
235
|
+
): Promise<string | NodeJS.ReadableStream> {
|
|
236
|
+
throw new Error('PlayAI does not support speech recognition');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async getSpeakers() {
|
|
240
|
+
return this.traced(
|
|
241
|
+
() =>
|
|
242
|
+
Promise.resolve(
|
|
243
|
+
PLAYAI_VOICES.map(voice => ({
|
|
244
|
+
voiceId: voice.id,
|
|
245
|
+
name: voice.name,
|
|
246
|
+
accent: voice.accent,
|
|
247
|
+
gender: voice.gender,
|
|
248
|
+
age: voice.age,
|
|
249
|
+
style: voice.style,
|
|
250
|
+
})),
|
|
251
|
+
),
|
|
252
|
+
'voice.playai.voices',
|
|
253
|
+
)();
|
|
254
|
+
}
|
|
255
|
+
}
|
package/tsconfig.json
ADDED