@mastra/voice-playai 0.0.0-storage-20250225005900 → 0.0.0-stream-vnext-usage-20250908171242
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/CHANGELOG.md +1510 -3
- package/LICENSE.md +15 -0
- package/dist/index.cjs +236 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +47 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -3
- package/dist/index.js.map +1 -0
- package/package.json +37 -13
- package/.turbo/turbo-build.log +0 -19
- package/LICENSE +0 -44
- package/dist/_tsup-dts-rollup.d.ts +0 -43
- 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/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