@mastra/voice-playai 0.0.0-storage-20250225005900 → 0.0.0-switch-to-core-20250424015131
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/{LICENSE → LICENSE.md} +3 -1
- package/dist/_tsup-dts-rollup.d.cts +43 -0
- package/dist/index.cjs +226 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.js +7 -3
- package/package.json +21 -10
- package/.turbo/turbo-build.log +0 -19
- package/CHANGELOG.md +0 -46
- package/src/index.test.ts +0 -144
- package/src/index.ts +0 -255
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -8
package/{LICENSE → LICENSE.md}
RENAMED
|
@@ -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.cjs
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var stream = require('stream');
|
|
4
|
+
var voice = require('@mastra/core/voice');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var PLAYAI_VOICES = [
|
|
8
|
+
{
|
|
9
|
+
name: "Angelo",
|
|
10
|
+
accent: "US",
|
|
11
|
+
gender: "M",
|
|
12
|
+
age: "Young",
|
|
13
|
+
style: "Conversational",
|
|
14
|
+
id: "s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: "Arsenio",
|
|
18
|
+
accent: "US African American",
|
|
19
|
+
gender: "M",
|
|
20
|
+
age: "Middle",
|
|
21
|
+
style: "Conversational",
|
|
22
|
+
id: "s3://voice-cloning-zero-shot/65977f5e-a22a-4b36-861b-ecede19bdd65/original/manifest.json"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "Cillian",
|
|
26
|
+
accent: "Irish",
|
|
27
|
+
gender: "M",
|
|
28
|
+
age: "Middle",
|
|
29
|
+
style: "Conversational",
|
|
30
|
+
id: "s3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "Timo",
|
|
34
|
+
accent: "US",
|
|
35
|
+
gender: "M",
|
|
36
|
+
age: "Middle",
|
|
37
|
+
style: "Conversational",
|
|
38
|
+
id: "s3://voice-cloning-zero-shot/677a4ae3-252f-476e-85ce-eeed68e85951/original/manifest.json"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "Dexter",
|
|
42
|
+
accent: "US",
|
|
43
|
+
gender: "M",
|
|
44
|
+
age: "Middle",
|
|
45
|
+
style: "Conversational",
|
|
46
|
+
id: "s3://voice-cloning-zero-shot/b27bc13e-996f-4841-b584-4d35801aea98/original/manifest.json"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "Miles",
|
|
50
|
+
accent: "US African American",
|
|
51
|
+
gender: "M",
|
|
52
|
+
age: "Young",
|
|
53
|
+
style: "Conversational",
|
|
54
|
+
id: "s3://voice-cloning-zero-shot/29dd9a52-bd32-4a6e-bff1-bbb98dcc286a/original/manifest.json"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "Briggs",
|
|
58
|
+
accent: "US Southern (Oklahoma)",
|
|
59
|
+
gender: "M",
|
|
60
|
+
age: "Old",
|
|
61
|
+
style: "Conversational",
|
|
62
|
+
id: "s3://voice-cloning-zero-shot/71cdb799-1e03-41c6-8a05-f7cd55134b0b/original/manifest.json"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "Deedee",
|
|
66
|
+
accent: "US African American",
|
|
67
|
+
gender: "F",
|
|
68
|
+
age: "Middle",
|
|
69
|
+
style: "Conversational",
|
|
70
|
+
id: "s3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "Nia",
|
|
74
|
+
accent: "US",
|
|
75
|
+
gender: "F",
|
|
76
|
+
age: "Young",
|
|
77
|
+
style: "Conversational",
|
|
78
|
+
id: "s3://voice-cloning-zero-shot/831bd330-85c6-4333-b2b4-10c476ea3491/original/manifest.json"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: "Inara",
|
|
82
|
+
accent: "US African American",
|
|
83
|
+
gender: "F",
|
|
84
|
+
age: "Middle",
|
|
85
|
+
style: "Conversational",
|
|
86
|
+
id: "s3://voice-cloning-zero-shot/adb83b67-8d75-48ff-ad4d-a0840d231ef1/original/manifest.json"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "Constanza",
|
|
90
|
+
accent: "US Latin American",
|
|
91
|
+
gender: "F",
|
|
92
|
+
age: "Young",
|
|
93
|
+
style: "Conversational",
|
|
94
|
+
id: "s3://voice-cloning-zero-shot/b0aca4d7-1738-4848-a80b-307ac44a7298/original/manifest.json"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "Gideon",
|
|
98
|
+
accent: "British",
|
|
99
|
+
gender: "M",
|
|
100
|
+
age: "Old",
|
|
101
|
+
style: "Narrative",
|
|
102
|
+
id: "s3://voice-cloning-zero-shot/5a3a1168-7793-4b2c-8f90-aff2b5232131/original/manifest.json"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: "Casper",
|
|
106
|
+
accent: "US",
|
|
107
|
+
gender: "M",
|
|
108
|
+
age: "Middle",
|
|
109
|
+
style: "Narrative",
|
|
110
|
+
id: "s3://voice-cloning-zero-shot/1bbc6986-fadf-4bd8-98aa-b86fed0476e9/original/manifest.json"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: "Mitch",
|
|
114
|
+
accent: "Australian",
|
|
115
|
+
gender: "M",
|
|
116
|
+
age: "Middle",
|
|
117
|
+
style: "Narrative",
|
|
118
|
+
id: "s3://voice-cloning-zero-shot/c14e50f2-c5e3-47d1-8c45-fa4b67803d19/original/manifest.json"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "Ava",
|
|
122
|
+
accent: "Australian",
|
|
123
|
+
gender: "F",
|
|
124
|
+
age: "Middle",
|
|
125
|
+
style: "Narrative",
|
|
126
|
+
id: "s3://voice-cloning-zero-shot/50381567-ff7b-46d2-bfdc-a9584a85e08d/original/manifest.json"
|
|
127
|
+
}
|
|
128
|
+
];
|
|
129
|
+
var PlayAIVoice = class extends voice.MastraVoice {
|
|
130
|
+
baseUrl = "https://api.play.ai/api/v1";
|
|
131
|
+
userId;
|
|
132
|
+
constructor({ speechModel, speaker } = {}) {
|
|
133
|
+
super({
|
|
134
|
+
speechModel: {
|
|
135
|
+
name: speechModel?.name ?? "PlayDialog",
|
|
136
|
+
apiKey: speechModel?.apiKey ?? process.env.PLAYAI_API_KEY
|
|
137
|
+
},
|
|
138
|
+
speaker: speaker ?? PLAYAI_VOICES[0]?.id
|
|
139
|
+
});
|
|
140
|
+
const userId = speechModel?.userId ?? process.env.PLAYAI_USER_ID;
|
|
141
|
+
if (!userId) {
|
|
142
|
+
throw new Error("userId is required");
|
|
143
|
+
}
|
|
144
|
+
this.userId = userId;
|
|
145
|
+
}
|
|
146
|
+
async makeRequest(endpoint, payload, method = "POST") {
|
|
147
|
+
const headers = new Headers({
|
|
148
|
+
Authorization: `Bearer ${this.speechModel?.apiKey}`,
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
"X-USER-ID": this.userId
|
|
151
|
+
});
|
|
152
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
153
|
+
method,
|
|
154
|
+
headers,
|
|
155
|
+
body: payload ? JSON.stringify(payload) : void 0
|
|
156
|
+
});
|
|
157
|
+
if (!response.ok) {
|
|
158
|
+
const error = await response.json();
|
|
159
|
+
throw new Error(`PlayAI API Error: ${error.message || response.statusText}`);
|
|
160
|
+
}
|
|
161
|
+
return response;
|
|
162
|
+
}
|
|
163
|
+
async streamToString(stream) {
|
|
164
|
+
const chunks = [];
|
|
165
|
+
for await (const chunk of stream) {
|
|
166
|
+
if (typeof chunk === "string") {
|
|
167
|
+
chunks.push(Buffer.from(chunk));
|
|
168
|
+
} else {
|
|
169
|
+
chunks.push(chunk);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
173
|
+
}
|
|
174
|
+
async speak(input, options) {
|
|
175
|
+
const text = typeof input === "string" ? input : await this.streamToString(input);
|
|
176
|
+
return this.traced(async () => {
|
|
177
|
+
const payload = {
|
|
178
|
+
text,
|
|
179
|
+
voice: options?.speaker || this.speaker,
|
|
180
|
+
model: this.speechModel?.name
|
|
181
|
+
};
|
|
182
|
+
const response = await this.makeRequest("/tts/stream", payload);
|
|
183
|
+
if (!response.body) {
|
|
184
|
+
throw new Error("No response body received");
|
|
185
|
+
}
|
|
186
|
+
const stream$1 = new stream.PassThrough();
|
|
187
|
+
const reader = response.body.getReader();
|
|
188
|
+
void (async () => {
|
|
189
|
+
try {
|
|
190
|
+
while (true) {
|
|
191
|
+
const { done, value } = await reader.read();
|
|
192
|
+
if (done) {
|
|
193
|
+
stream$1.end();
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
stream$1.write(value);
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
stream$1.destroy(error);
|
|
200
|
+
}
|
|
201
|
+
})();
|
|
202
|
+
return stream$1;
|
|
203
|
+
}, "voice.playai.speak")();
|
|
204
|
+
}
|
|
205
|
+
async listen(_input, _options) {
|
|
206
|
+
throw new Error("PlayAI does not support speech recognition");
|
|
207
|
+
}
|
|
208
|
+
async getSpeakers() {
|
|
209
|
+
return this.traced(
|
|
210
|
+
() => Promise.resolve(
|
|
211
|
+
PLAYAI_VOICES.map((voice) => ({
|
|
212
|
+
voiceId: voice.id,
|
|
213
|
+
name: voice.name,
|
|
214
|
+
accent: voice.accent,
|
|
215
|
+
gender: voice.gender,
|
|
216
|
+
age: voice.age,
|
|
217
|
+
style: voice.style
|
|
218
|
+
}))
|
|
219
|
+
),
|
|
220
|
+
"voice.playai.voices"
|
|
221
|
+
)();
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
exports.PLAYAI_VOICES = PLAYAI_VOICES;
|
|
226
|
+
exports.PlayAIVoice = PlayAIVoice;
|
package/dist/index.d.cts
ADDED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MastraVoice } from '@mastra/core/voice';
|
|
2
1
|
import { PassThrough } from 'stream';
|
|
2
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
3
3
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
var PLAYAI_VOICES = [
|
|
@@ -161,7 +161,11 @@ var PlayAIVoice = class extends MastraVoice {
|
|
|
161
161
|
async streamToString(stream) {
|
|
162
162
|
const chunks = [];
|
|
163
163
|
for await (const chunk of stream) {
|
|
164
|
-
|
|
164
|
+
if (typeof chunk === "string") {
|
|
165
|
+
chunks.push(Buffer.from(chunk));
|
|
166
|
+
} else {
|
|
167
|
+
chunks.push(chunk);
|
|
168
|
+
}
|
|
165
169
|
}
|
|
166
170
|
return Buffer.concat(chunks).toString("utf-8");
|
|
167
171
|
}
|
|
@@ -179,7 +183,7 @@ var PlayAIVoice = class extends MastraVoice {
|
|
|
179
183
|
}
|
|
180
184
|
const stream = new PassThrough();
|
|
181
185
|
const reader = response.body.getReader();
|
|
182
|
-
(async () => {
|
|
186
|
+
void (async () => {
|
|
183
187
|
try {
|
|
184
188
|
while (true) {
|
|
185
189
|
const { done, value } = await reader.read();
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/voice-playai",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-switch-to-core-20250424015131",
|
|
4
4
|
"description": "Mastra PlayAI voice integration",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
6
9
|
"main": "dist/index.js",
|
|
7
10
|
"types": "dist/index.d.ts",
|
|
8
11
|
"exports": {
|
|
@@ -10,24 +13,32 @@
|
|
|
10
13
|
"import": {
|
|
11
14
|
"types": "./dist/index.d.ts",
|
|
12
15
|
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
13
20
|
}
|
|
14
21
|
},
|
|
15
22
|
"./package.json": "./package.json"
|
|
16
23
|
},
|
|
24
|
+
"license": "Elastic-2.0",
|
|
17
25
|
"dependencies": {
|
|
18
|
-
"zod": "^3.24.
|
|
19
|
-
"@mastra/core": "
|
|
26
|
+
"zod": "^3.24.2",
|
|
27
|
+
"@mastra/core": "0.0.0-switch-to-core-20250424015131"
|
|
20
28
|
},
|
|
21
29
|
"devDependencies": {
|
|
22
|
-
"@microsoft/api-extractor": "^7.
|
|
23
|
-
"@types/node": "^
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
30
|
+
"@microsoft/api-extractor": "^7.52.2",
|
|
31
|
+
"@types/node": "^20.17.27",
|
|
32
|
+
"eslint": "^9.23.0",
|
|
33
|
+
"tsup": "^8.4.0",
|
|
34
|
+
"typescript": "^5.8.2",
|
|
35
|
+
"vitest": "^2.1.9",
|
|
36
|
+
"@internal/lint": "0.0.2"
|
|
27
37
|
},
|
|
28
38
|
"scripts": {
|
|
29
|
-
"build": "tsup src/index.ts --format esm --experimental-dts --clean --treeshake",
|
|
39
|
+
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
|
30
40
|
"build:watch": "pnpm build --watch",
|
|
31
|
-
"test": "vitest run"
|
|
41
|
+
"test": "vitest run",
|
|
42
|
+
"lint": "eslint ."
|
|
32
43
|
}
|
|
33
44
|
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
> @mastra/voice-playai@0.1.0-alpha.2 build /Users/ward/projects/mastra/mastra/voice/playai
|
|
4
|
-
> tsup src/index.ts --format esm --experimental-dts --clean --treeshake
|
|
5
|
-
|
|
6
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
7
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
-
[34mCLI[39m tsup v8.3.6
|
|
9
|
-
[34mTSC[39m Build start
|
|
10
|
-
[32mTSC[39m ⚡️ Build success in 1284ms
|
|
11
|
-
[34mDTS[39m Build start
|
|
12
|
-
[34mCLI[39m Target: es2022
|
|
13
|
-
Analysis will use the bundled TypeScript version 5.7.3
|
|
14
|
-
[36mWriting package typings: /Users/ward/projects/mastra/mastra/voice/playai/dist/_tsup-dts-rollup.d.ts[39m
|
|
15
|
-
[32mDTS[39m ⚡️ Build success in 1173ms
|
|
16
|
-
[34mCLI[39m Cleaning output folder
|
|
17
|
-
[34mESM[39m Build start
|
|
18
|
-
[32mESM[39m [1mdist/index.js [22m[32m5.97 KB[39m
|
|
19
|
-
[32mESM[39m ⚡️ Build success in 119ms
|
package/CHANGELOG.md
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# @mastra/voice-playai
|
|
2
|
-
|
|
3
|
-
## 0.0.0-storage-20250225005900
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- 41d0166: deprecate @mastra/speech-playai for @mastra/voice-playai
|
|
8
|
-
- Updated dependencies [7fceae1]
|
|
9
|
-
- Updated dependencies [f626fbb]
|
|
10
|
-
- Updated dependencies [8db2a28]
|
|
11
|
-
- @mastra/core@0.0.0-storage-20250225005900
|
|
12
|
-
|
|
13
|
-
## 0.1.0-alpha.2
|
|
14
|
-
|
|
15
|
-
### Patch Changes
|
|
16
|
-
|
|
17
|
-
- 41d0166: deprecate @mastra/speech-playai for @mastra/voice-playai
|
|
18
|
-
- Updated dependencies [7fceae1]
|
|
19
|
-
- Updated dependencies [f626fbb]
|
|
20
|
-
- @mastra/core@0.4.2-alpha.0
|
|
21
|
-
|
|
22
|
-
## 0.1.0 (2024-XX-XX)
|
|
23
|
-
|
|
24
|
-
This package replaces the deprecated @mastra/speech-playai package. All functionality has been migrated to this new package with a more consistent naming scheme.
|
|
25
|
-
|
|
26
|
-
### Changes from @mastra/speech-playai
|
|
27
|
-
|
|
28
|
-
- Package renamed from @mastra/speech-playai to @mastra/voice-playai
|
|
29
|
-
- API changes:
|
|
30
|
-
- `PlayAITTS` class renamed to `PlayAIVoice`
|
|
31
|
-
- `generate()` and `stream()` methods combined into `speak()`
|
|
32
|
-
- `voices()` method renamed to `getSpeakers()`
|
|
33
|
-
- Constructor configuration simplified
|
|
34
|
-
- Added support for text stream input
|
|
35
|
-
- Default speaker configuration added
|
|
36
|
-
- All core functionality remains the same
|
|
37
|
-
- Import paths should be updated from '@mastra/speech-playai' to '@mastra/voice-playai'
|
|
38
|
-
|
|
39
|
-
### Key Features Preserved
|
|
40
|
-
|
|
41
|
-
- High-quality voice synthesis
|
|
42
|
-
- Multiple voice options with rich metadata
|
|
43
|
-
- Streaming support
|
|
44
|
-
- Natural and expressive speech output
|
|
45
|
-
|
|
46
|
-
For a complete history of changes prior to the rename, please see the changelog of the original package.
|
package/src/index.test.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,255 +0,0 @@
|
|
|
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
DELETED