@pie-players/tts-server-core 0.1.0
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 +1 -0
- package/README.md +154 -0
- package/dist/cache.d.ts +118 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +136 -0
- package/dist/cache.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +129 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +54 -0
- package/dist/provider.js.map +1 -0
- package/dist/speech-marks.d.ts +76 -0
- package/dist/speech-marks.d.ts.map +1 -0
- package/dist/speech-marks.js +194 -0
- package/dist/speech-marks.js.map +1 -0
- package/dist/types.d.ts +360 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +49 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -0
- package/src/cache.ts +273 -0
- package/src/index.ts +50 -0
- package/src/provider.ts +200 -0
- package/src/speech-marks.ts +243 -0
- package/src/types.ts +425 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Speech marks utilities
|
|
3
|
+
* @module @pie-players/tts-server-core
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { SpeechMark } from "./types.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Estimate speech marks for text when provider doesn't support them
|
|
10
|
+
*
|
|
11
|
+
* Uses average speaking rate to estimate word timing.
|
|
12
|
+
* Not as accurate as provider-generated marks, but better than nothing.
|
|
13
|
+
*
|
|
14
|
+
* @param text - Text to generate marks for
|
|
15
|
+
* @param avgWordsPerMinute - Average speaking rate (default 150)
|
|
16
|
+
* @returns Estimated speech marks
|
|
17
|
+
*/
|
|
18
|
+
export function estimateSpeechMarks(
|
|
19
|
+
text: string,
|
|
20
|
+
avgWordsPerMinute = 150,
|
|
21
|
+
): SpeechMark[] {
|
|
22
|
+
const words = text.split(/\s+/).filter((w) => w.length > 0);
|
|
23
|
+
const msPerWord = (60 * 1000) / avgWordsPerMinute;
|
|
24
|
+
|
|
25
|
+
const marks: SpeechMark[] = [];
|
|
26
|
+
let charIndex = 0;
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < words.length; i++) {
|
|
29
|
+
const word = words[i];
|
|
30
|
+
|
|
31
|
+
// Find word position in original text (preserves spacing)
|
|
32
|
+
const wordStart = text.indexOf(word, charIndex);
|
|
33
|
+
if (wordStart === -1) {
|
|
34
|
+
// Word not found (shouldn't happen), skip
|
|
35
|
+
charIndex += word.length + 1;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
marks.push({
|
|
40
|
+
time: Math.round(i * msPerWord),
|
|
41
|
+
type: "word",
|
|
42
|
+
start: wordStart,
|
|
43
|
+
end: wordStart + word.length,
|
|
44
|
+
value: word,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
charIndex = wordStart + word.length;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return marks;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Adjust speech marks timing for different speaking rates
|
|
55
|
+
*
|
|
56
|
+
* @param marks - Original speech marks
|
|
57
|
+
* @param rate - Speech rate multiplier (0.25 to 4.0)
|
|
58
|
+
* @returns Adjusted speech marks
|
|
59
|
+
*/
|
|
60
|
+
export function adjustSpeechMarksForRate(
|
|
61
|
+
marks: SpeechMark[],
|
|
62
|
+
rate: number,
|
|
63
|
+
): SpeechMark[] {
|
|
64
|
+
if (rate === 1.0) {
|
|
65
|
+
return marks; // No adjustment needed
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return marks.map((mark) => ({
|
|
69
|
+
...mark,
|
|
70
|
+
time: Math.round(mark.time / rate),
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate speech marks
|
|
76
|
+
* Ensures marks are properly ordered and have valid data
|
|
77
|
+
*
|
|
78
|
+
* @param marks - Speech marks to validate
|
|
79
|
+
* @returns Validation errors (empty array if valid)
|
|
80
|
+
*/
|
|
81
|
+
export function validateSpeechMarks(marks: SpeechMark[]): string[] {
|
|
82
|
+
const errors: string[] = [];
|
|
83
|
+
|
|
84
|
+
if (!marks || marks.length === 0) {
|
|
85
|
+
return errors; // Empty is valid
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
for (let i = 0; i < marks.length; i++) {
|
|
89
|
+
const mark = marks[i];
|
|
90
|
+
|
|
91
|
+
// Check required fields
|
|
92
|
+
if (typeof mark.time !== "number" || mark.time < 0) {
|
|
93
|
+
errors.push(`Mark ${i}: invalid time (${mark.time})`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (typeof mark.start !== "number" || mark.start < 0) {
|
|
97
|
+
errors.push(`Mark ${i}: invalid start (${mark.start})`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (typeof mark.end !== "number" || mark.end <= mark.start) {
|
|
101
|
+
errors.push(`Mark ${i}: invalid end (${mark.end}, start: ${mark.start})`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!mark.value || typeof mark.value !== "string") {
|
|
105
|
+
errors.push(`Mark ${i}: invalid value`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Check ordering (time should be monotonically increasing)
|
|
109
|
+
if (i > 0 && mark.time < marks[i - 1].time) {
|
|
110
|
+
errors.push(
|
|
111
|
+
`Mark ${i}: time (${mark.time}) is less than previous mark (${marks[i - 1].time})`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return errors;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Merge overlapping or adjacent speech marks
|
|
121
|
+
* Useful when combining marks from multiple sources
|
|
122
|
+
*
|
|
123
|
+
* @param marks - Speech marks to merge
|
|
124
|
+
* @returns Merged speech marks
|
|
125
|
+
*/
|
|
126
|
+
export function mergeSpeechMarks(marks: SpeechMark[]): SpeechMark[] {
|
|
127
|
+
if (marks.length <= 1) {
|
|
128
|
+
return marks;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Sort by start position
|
|
132
|
+
const sorted = [...marks].sort((a, b) => a.start - b.start);
|
|
133
|
+
const merged: SpeechMark[] = [sorted[0]];
|
|
134
|
+
|
|
135
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
136
|
+
const current = sorted[i];
|
|
137
|
+
const previous = merged[merged.length - 1];
|
|
138
|
+
|
|
139
|
+
// Check if marks overlap or are adjacent
|
|
140
|
+
if (current.start <= previous.end) {
|
|
141
|
+
// Merge with previous mark
|
|
142
|
+
previous.end = Math.max(previous.end, current.end);
|
|
143
|
+
previous.value = previous.value + " " + current.value;
|
|
144
|
+
previous.time = Math.min(previous.time, current.time); // Use earlier time
|
|
145
|
+
} else {
|
|
146
|
+
// No overlap, add as new mark
|
|
147
|
+
merged.push(current);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return merged;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Filter speech marks by type
|
|
156
|
+
*
|
|
157
|
+
* @param marks - Speech marks to filter
|
|
158
|
+
* @param type - Type to filter by
|
|
159
|
+
* @returns Filtered speech marks
|
|
160
|
+
*/
|
|
161
|
+
export function filterSpeechMarksByType(
|
|
162
|
+
marks: SpeechMark[],
|
|
163
|
+
type: "word" | "sentence" | "ssml",
|
|
164
|
+
): SpeechMark[] {
|
|
165
|
+
return marks.filter((mark) => mark.type === type);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get speech mark at specific time
|
|
170
|
+
*
|
|
171
|
+
* @param marks - Speech marks
|
|
172
|
+
* @param time - Time in milliseconds
|
|
173
|
+
* @returns Speech mark at time, or null if none found
|
|
174
|
+
*/
|
|
175
|
+
export function getSpeechMarkAtTime(
|
|
176
|
+
marks: SpeechMark[],
|
|
177
|
+
time: number,
|
|
178
|
+
): SpeechMark | null {
|
|
179
|
+
// Binary search for efficiency
|
|
180
|
+
let left = 0;
|
|
181
|
+
let right = marks.length - 1;
|
|
182
|
+
let closest: SpeechMark | null = null;
|
|
183
|
+
|
|
184
|
+
while (left <= right) {
|
|
185
|
+
const mid = Math.floor((left + right) / 2);
|
|
186
|
+
const mark = marks[mid];
|
|
187
|
+
|
|
188
|
+
if (mark.time === time) {
|
|
189
|
+
return mark;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Track closest mark
|
|
193
|
+
if (
|
|
194
|
+
!closest ||
|
|
195
|
+
Math.abs(mark.time - time) < Math.abs(closest.time - time)
|
|
196
|
+
) {
|
|
197
|
+
closest = mark;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (mark.time < time) {
|
|
201
|
+
left = mid + 1;
|
|
202
|
+
} else {
|
|
203
|
+
right = mid - 1;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Return closest mark if within reasonable threshold (500ms)
|
|
208
|
+
if (closest && Math.abs(closest.time - time) <= 500) {
|
|
209
|
+
return closest;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Calculate statistics for speech marks
|
|
217
|
+
*
|
|
218
|
+
* @param marks - Speech marks
|
|
219
|
+
* @returns Statistics about the marks
|
|
220
|
+
*/
|
|
221
|
+
export function getSpeechMarksStats(marks: SpeechMark[]) {
|
|
222
|
+
if (marks.length === 0) {
|
|
223
|
+
return {
|
|
224
|
+
count: 0,
|
|
225
|
+
totalDuration: 0,
|
|
226
|
+
avgWordDuration: 0,
|
|
227
|
+
wordsPerMinute: 0,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const wordMarks = filterSpeechMarksByType(marks, "word");
|
|
232
|
+
const totalDuration = marks[marks.length - 1].time;
|
|
233
|
+
const avgWordDuration = totalDuration / wordMarks.length;
|
|
234
|
+
const wordsPerMinute = (wordMarks.length / totalDuration) * 60 * 1000;
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
count: marks.length,
|
|
238
|
+
wordCount: wordMarks.length,
|
|
239
|
+
totalDuration,
|
|
240
|
+
avgWordDuration,
|
|
241
|
+
wordsPerMinute,
|
|
242
|
+
};
|
|
243
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for server-side TTS providers
|
|
3
|
+
* @module @pie-players/tts-server-core
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Speech mark representing a timing event in synthesized speech
|
|
8
|
+
* Unified format across all TTS providers
|
|
9
|
+
*/
|
|
10
|
+
export interface SpeechMark {
|
|
11
|
+
/** Milliseconds from start of audio */
|
|
12
|
+
time: number;
|
|
13
|
+
|
|
14
|
+
/** Type of speech mark */
|
|
15
|
+
type: "word" | "sentence" | "ssml";
|
|
16
|
+
|
|
17
|
+
/** Character index in original text (inclusive) */
|
|
18
|
+
start: number;
|
|
19
|
+
|
|
20
|
+
/** Character index in original text (exclusive) */
|
|
21
|
+
end: number;
|
|
22
|
+
|
|
23
|
+
/** The actual word or text */
|
|
24
|
+
value: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Standard TTS parameters based on W3C Web Speech API and SSML specifications.
|
|
29
|
+
*
|
|
30
|
+
* These parameters are widely supported across TTS providers (browsers, cloud services)
|
|
31
|
+
* and follow established standards:
|
|
32
|
+
* - W3C Web Speech API (SpeechSynthesisUtterance)
|
|
33
|
+
* - W3C SSML 1.1 specification
|
|
34
|
+
* - BCP47 language tags (RFC 5646)
|
|
35
|
+
*
|
|
36
|
+
* @see https://w3c.github.io/speech-api/
|
|
37
|
+
* @see https://www.w3.org/TR/speech-synthesis/
|
|
38
|
+
*/
|
|
39
|
+
export interface StandardTTSParameters {
|
|
40
|
+
/**
|
|
41
|
+
* Text to synthesize (plain text or SSML markup)
|
|
42
|
+
*
|
|
43
|
+
* @standard W3C Web Speech API
|
|
44
|
+
*/
|
|
45
|
+
text: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Voice identifier (provider-specific voice names)
|
|
49
|
+
* Examples: "Joanna" (Polly), "en-US-Standard-A" (Google), browser voice names
|
|
50
|
+
*
|
|
51
|
+
* @standard W3C Web Speech API (concept)
|
|
52
|
+
* @note Voice names are provider-specific but the concept is standard
|
|
53
|
+
*/
|
|
54
|
+
voice?: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Language code using BCP47 format (e.g., 'en-US', 'es-ES', 'fr-FR')
|
|
58
|
+
*
|
|
59
|
+
* @standard BCP47 (RFC 5646), W3C Web Speech API
|
|
60
|
+
* @see https://tools.ietf.org/html/rfc5646
|
|
61
|
+
*/
|
|
62
|
+
language?: string;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Speech rate (speed multiplier)
|
|
66
|
+
* - Range: 0.25 to 4.0
|
|
67
|
+
* - Default: 1.0 (normal speed)
|
|
68
|
+
* - 0.5 = half speed, 2.0 = double speed
|
|
69
|
+
*
|
|
70
|
+
* @standard W3C Web Speech API, SSML <prosody rate>
|
|
71
|
+
*/
|
|
72
|
+
rate?: number;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Pitch adjustment
|
|
76
|
+
* - Range: -20 to +20 semitones (or 0 to 2 as multiplier depending on provider)
|
|
77
|
+
* - Default: 0 (or 1.0 as multiplier)
|
|
78
|
+
* - Negative values = lower pitch, positive = higher pitch
|
|
79
|
+
*
|
|
80
|
+
* @standard W3C Web Speech API, SSML <prosody pitch>
|
|
81
|
+
* @note Some providers use semitones (-20 to +20), others use multipliers (0 to 2)
|
|
82
|
+
*/
|
|
83
|
+
pitch?: number;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Volume level
|
|
87
|
+
* - Range: 0.0 to 1.0
|
|
88
|
+
* - Default: 1.0 (full volume)
|
|
89
|
+
* - 0.0 = silent, 0.5 = half volume
|
|
90
|
+
*
|
|
91
|
+
* @standard W3C Web Speech API, SSML <prosody volume>
|
|
92
|
+
*/
|
|
93
|
+
volume?: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Provider-specific extensions for advanced TTS control.
|
|
98
|
+
*
|
|
99
|
+
* These parameters are NOT part of W3C standards and have varying support
|
|
100
|
+
* across providers. Use with caution for portability.
|
|
101
|
+
*
|
|
102
|
+
* Common extensions include:
|
|
103
|
+
* - Audio format selection (mp3, wav, ogg)
|
|
104
|
+
* - Sample rate control
|
|
105
|
+
* - Engine selection (neural vs standard)
|
|
106
|
+
* - Regional endpoints
|
|
107
|
+
* - Speech marks / word timing
|
|
108
|
+
*
|
|
109
|
+
* @note Providers may ignore unsupported extensions silently or throw errors
|
|
110
|
+
*/
|
|
111
|
+
export interface TTSProviderExtensions {
|
|
112
|
+
/**
|
|
113
|
+
* Audio format for output
|
|
114
|
+
*
|
|
115
|
+
* @extension Common across providers but values vary
|
|
116
|
+
* @support AWS Polly (mp3, ogg, pcm), Google Cloud TTS (mp3, wav, ogg), Azure (mp3, wav, ogg)
|
|
117
|
+
*/
|
|
118
|
+
format?: "mp3" | "wav" | "ogg" | "pcm";
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Sample rate in Hz (e.g., 8000, 16000, 22050, 24000)
|
|
122
|
+
*
|
|
123
|
+
* @extension Common audio parameter
|
|
124
|
+
* @note Higher sample rates = better quality but larger file sizes
|
|
125
|
+
*/
|
|
126
|
+
sampleRate?: number;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Request word-level timing data (speech marks)
|
|
130
|
+
*
|
|
131
|
+
* @extension Provider-specific but common pattern
|
|
132
|
+
* @support AWS Polly (SpeechMarks), Google Cloud TTS (timepoints), Azure (word boundaries)
|
|
133
|
+
* @default true
|
|
134
|
+
*/
|
|
135
|
+
includeSpeechMarks?: boolean;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Provider-specific options (extensibility point)
|
|
139
|
+
*
|
|
140
|
+
* Examples:
|
|
141
|
+
* - AWS Polly: { engine: 'neural' | 'standard', lexiconNames: string[] }
|
|
142
|
+
* - Google Cloud TTS: { audioEncoding: string, effectsProfileId: string[] }
|
|
143
|
+
* - Azure: { voiceType: string, stylesList: string[] }
|
|
144
|
+
*
|
|
145
|
+
* @extension Arbitrary provider-specific data
|
|
146
|
+
*/
|
|
147
|
+
providerOptions?: Record<string, unknown>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Complete synthesis request combining standard parameters and extensions.
|
|
152
|
+
*
|
|
153
|
+
* This interface provides the full set of options for text-to-speech synthesis,
|
|
154
|
+
* clearly separating W3C-standard parameters from provider-specific extensions.
|
|
155
|
+
*
|
|
156
|
+
* @example Basic usage (portable across providers)
|
|
157
|
+
* ```typescript
|
|
158
|
+
* const request: SynthesizeRequest = {
|
|
159
|
+
* text: "Hello world",
|
|
160
|
+
* voice: "Joanna",
|
|
161
|
+
* rate: 1.0,
|
|
162
|
+
* language: "en-US"
|
|
163
|
+
* };
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* @example Advanced usage with extensions (provider-specific)
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const request: SynthesizeRequest = {
|
|
169
|
+
* text: "Hello world",
|
|
170
|
+
* voice: "Joanna",
|
|
171
|
+
* rate: 1.0,
|
|
172
|
+
* // Extensions - may not be portable
|
|
173
|
+
* format: 'mp3',
|
|
174
|
+
* sampleRate: 24000,
|
|
175
|
+
* includeSpeechMarks: true,
|
|
176
|
+
* providerOptions: {
|
|
177
|
+
* engine: 'neural' // AWS Polly specific
|
|
178
|
+
* }
|
|
179
|
+
* };
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export interface SynthesizeRequest
|
|
183
|
+
extends StandardTTSParameters,
|
|
184
|
+
TTSProviderExtensions {}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Response from speech synthesis
|
|
188
|
+
*/
|
|
189
|
+
export interface SynthesizeResponse {
|
|
190
|
+
/** Audio data (Buffer for server, base64 string for client) */
|
|
191
|
+
audio: Buffer | string;
|
|
192
|
+
|
|
193
|
+
/** MIME type of audio (e.g., 'audio/mpeg') */
|
|
194
|
+
contentType: string;
|
|
195
|
+
|
|
196
|
+
/** Speech marks for word-level timing */
|
|
197
|
+
speechMarks: SpeechMark[];
|
|
198
|
+
|
|
199
|
+
/** Metadata about the synthesis */
|
|
200
|
+
metadata: SynthesizeMetadata;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Metadata about synthesized speech
|
|
205
|
+
*/
|
|
206
|
+
export interface SynthesizeMetadata {
|
|
207
|
+
/** Provider that generated the audio */
|
|
208
|
+
providerId: string;
|
|
209
|
+
|
|
210
|
+
/** Voice ID used */
|
|
211
|
+
voice: string;
|
|
212
|
+
|
|
213
|
+
/** Audio duration in seconds */
|
|
214
|
+
duration: number;
|
|
215
|
+
|
|
216
|
+
/** Character count of input text */
|
|
217
|
+
charCount: number;
|
|
218
|
+
|
|
219
|
+
/** Whether response was served from cache */
|
|
220
|
+
cached: boolean;
|
|
221
|
+
|
|
222
|
+
/** ISO timestamp of synthesis */
|
|
223
|
+
timestamp?: string;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Voice definition
|
|
228
|
+
*/
|
|
229
|
+
export interface Voice {
|
|
230
|
+
/** Unique voice identifier */
|
|
231
|
+
id: string;
|
|
232
|
+
|
|
233
|
+
/** Human-readable name */
|
|
234
|
+
name: string;
|
|
235
|
+
|
|
236
|
+
/** Language name (e.g., "English", "Spanish") */
|
|
237
|
+
language: string;
|
|
238
|
+
|
|
239
|
+
/** Language code (e.g., "en-US", "es-ES") */
|
|
240
|
+
languageCode: string;
|
|
241
|
+
|
|
242
|
+
/** Gender of voice */
|
|
243
|
+
gender?: "male" | "female" | "neutral";
|
|
244
|
+
|
|
245
|
+
/** Voice quality level */
|
|
246
|
+
quality: "standard" | "premium" | "neural";
|
|
247
|
+
|
|
248
|
+
/** Supported features */
|
|
249
|
+
supportedFeatures: VoiceFeatures;
|
|
250
|
+
|
|
251
|
+
/** Provider-specific metadata */
|
|
252
|
+
providerMetadata?: Record<string, unknown>;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Voice feature flags
|
|
257
|
+
*/
|
|
258
|
+
export interface VoiceFeatures {
|
|
259
|
+
/** Supports SSML markup */
|
|
260
|
+
ssml: boolean;
|
|
261
|
+
|
|
262
|
+
/** Supports emotional expression */
|
|
263
|
+
emotions: boolean;
|
|
264
|
+
|
|
265
|
+
/** Supports speaking styles */
|
|
266
|
+
styles: boolean;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Options for listing voices
|
|
271
|
+
*/
|
|
272
|
+
export interface GetVoicesOptions {
|
|
273
|
+
/** Filter by language code */
|
|
274
|
+
language?: string;
|
|
275
|
+
|
|
276
|
+
/** Filter by quality level */
|
|
277
|
+
quality?: "standard" | "premium" | "neural";
|
|
278
|
+
|
|
279
|
+
/** Filter by gender */
|
|
280
|
+
gender?: "male" | "female" | "neutral";
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Provider capabilities split into standard features and extensions.
|
|
285
|
+
*
|
|
286
|
+
* This interface helps consumers understand what features are universally
|
|
287
|
+
* supported (W3C standards) vs provider-specific extensions.
|
|
288
|
+
*/
|
|
289
|
+
export interface ServerProviderCapabilities {
|
|
290
|
+
/**
|
|
291
|
+
* Standard W3C features that should be widely supported
|
|
292
|
+
*/
|
|
293
|
+
standard: {
|
|
294
|
+
/**
|
|
295
|
+
* Supports SSML markup (W3C SSML 1.1)
|
|
296
|
+
*
|
|
297
|
+
* @standard W3C SSML 1.1
|
|
298
|
+
* @support Most cloud TTS providers, limited browser support
|
|
299
|
+
*/
|
|
300
|
+
supportsSSML: boolean;
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Supports pitch control via rate parameter or SSML <prosody>
|
|
304
|
+
*
|
|
305
|
+
* @standard W3C Web Speech API, SSML <prosody pitch>
|
|
306
|
+
* @note May be via API parameter or SSML only
|
|
307
|
+
*/
|
|
308
|
+
supportsPitch: boolean;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Supports rate (speed) control via rate parameter or SSML <prosody>
|
|
312
|
+
*
|
|
313
|
+
* @standard W3C Web Speech API, SSML <prosody rate>
|
|
314
|
+
*/
|
|
315
|
+
supportsRate: boolean;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Supports volume control via volume parameter or SSML <prosody>
|
|
319
|
+
*
|
|
320
|
+
* @standard W3C Web Speech API, SSML <prosody volume>
|
|
321
|
+
* @note Often better handled client-side for server TTS
|
|
322
|
+
*/
|
|
323
|
+
supportsVolume: boolean;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Supports multiple voices (voice selection)
|
|
327
|
+
*
|
|
328
|
+
* @standard W3C Web Speech API (concept)
|
|
329
|
+
*/
|
|
330
|
+
supportsMultipleVoices: boolean;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Maximum text length in characters
|
|
334
|
+
*
|
|
335
|
+
* @note Varies by provider: Polly=3000, Google=5000, browser=~32k
|
|
336
|
+
*/
|
|
337
|
+
maxTextLength: number;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Provider-specific extensions
|
|
342
|
+
*/
|
|
343
|
+
extensions: {
|
|
344
|
+
/**
|
|
345
|
+
* Supports word-level timing data (speech marks)
|
|
346
|
+
*
|
|
347
|
+
* @extension Provider-specific but common
|
|
348
|
+
* @support AWS Polly ✅, Google Cloud TTS ✅, Azure TTS ✅, Browser ⚠️
|
|
349
|
+
* @note Format and precision vary by provider
|
|
350
|
+
*/
|
|
351
|
+
supportsSpeechMarks: boolean;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Supported audio output formats
|
|
355
|
+
*
|
|
356
|
+
* @extension Common but not standardized
|
|
357
|
+
*/
|
|
358
|
+
supportedFormats: ("mp3" | "wav" | "ogg" | "pcm")[];
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Supports sample rate configuration
|
|
362
|
+
*
|
|
363
|
+
* @extension Common audio parameter
|
|
364
|
+
*/
|
|
365
|
+
supportsSampleRate: boolean;
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Provider-specific features (extensibility point)
|
|
369
|
+
*
|
|
370
|
+
* Examples:
|
|
371
|
+
* - AWS Polly: { engines: ['neural', 'standard'], lexicons: true }
|
|
372
|
+
* - Google Cloud TTS: { audioProfiles: true, voiceEffects: true }
|
|
373
|
+
* - Azure: { styles: true, emotions: true }
|
|
374
|
+
*
|
|
375
|
+
* @extension Arbitrary provider capabilities
|
|
376
|
+
*/
|
|
377
|
+
providerSpecific?: Record<string, unknown>;
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* TTS error codes
|
|
383
|
+
*/
|
|
384
|
+
export enum TTSErrorCode {
|
|
385
|
+
INVALID_REQUEST = "INVALID_REQUEST",
|
|
386
|
+
INVALID_VOICE = "INVALID_VOICE",
|
|
387
|
+
INVALID_PROVIDER = "INVALID_PROVIDER",
|
|
388
|
+
TEXT_TOO_LONG = "TEXT_TOO_LONG",
|
|
389
|
+
PROVIDER_ERROR = "PROVIDER_ERROR",
|
|
390
|
+
NETWORK_ERROR = "NETWORK_ERROR",
|
|
391
|
+
AUTHENTICATION_ERROR = "AUTHENTICATION_ERROR",
|
|
392
|
+
RATE_LIMIT_EXCEEDED = "RATE_LIMIT_EXCEEDED",
|
|
393
|
+
INITIALIZATION_ERROR = "INITIALIZATION_ERROR",
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* TTS error with structured information
|
|
398
|
+
*/
|
|
399
|
+
export class TTSError extends Error {
|
|
400
|
+
constructor(
|
|
401
|
+
public code: TTSErrorCode,
|
|
402
|
+
message: string,
|
|
403
|
+
public details?: Record<string, unknown>,
|
|
404
|
+
public providerId?: string,
|
|
405
|
+
) {
|
|
406
|
+
super(message);
|
|
407
|
+
this.name = "TTSError";
|
|
408
|
+
|
|
409
|
+
// Maintains proper stack trace for where error was thrown (V8 only)
|
|
410
|
+
if (Error.captureStackTrace) {
|
|
411
|
+
Error.captureStackTrace(this, TTSError);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
toJSON() {
|
|
416
|
+
return {
|
|
417
|
+
error: {
|
|
418
|
+
code: this.code,
|
|
419
|
+
message: this.message,
|
|
420
|
+
details: this.details,
|
|
421
|
+
provider: this.providerId,
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2022"],
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"resolveJsonModule": true
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
20
|
+
}
|