@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/dist/types.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Core types for server-side TTS providers
3
+ * @module @pie-players/tts-server-core
4
+ */
5
+ /**
6
+ * TTS error codes
7
+ */
8
+ export var TTSErrorCode;
9
+ (function (TTSErrorCode) {
10
+ TTSErrorCode["INVALID_REQUEST"] = "INVALID_REQUEST";
11
+ TTSErrorCode["INVALID_VOICE"] = "INVALID_VOICE";
12
+ TTSErrorCode["INVALID_PROVIDER"] = "INVALID_PROVIDER";
13
+ TTSErrorCode["TEXT_TOO_LONG"] = "TEXT_TOO_LONG";
14
+ TTSErrorCode["PROVIDER_ERROR"] = "PROVIDER_ERROR";
15
+ TTSErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
16
+ TTSErrorCode["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
17
+ TTSErrorCode["RATE_LIMIT_EXCEEDED"] = "RATE_LIMIT_EXCEEDED";
18
+ TTSErrorCode["INITIALIZATION_ERROR"] = "INITIALIZATION_ERROR";
19
+ })(TTSErrorCode || (TTSErrorCode = {}));
20
+ /**
21
+ * TTS error with structured information
22
+ */
23
+ export class TTSError extends Error {
24
+ code;
25
+ details;
26
+ providerId;
27
+ constructor(code, message, details, providerId) {
28
+ super(message);
29
+ this.code = code;
30
+ this.details = details;
31
+ this.providerId = providerId;
32
+ this.name = "TTSError";
33
+ // Maintains proper stack trace for where error was thrown (V8 only)
34
+ if (Error.captureStackTrace) {
35
+ Error.captureStackTrace(this, TTSError);
36
+ }
37
+ }
38
+ toJSON() {
39
+ return {
40
+ error: {
41
+ code: this.code,
42
+ message: this.message,
43
+ details: this.details,
44
+ provider: this.providerId,
45
+ },
46
+ };
47
+ }
48
+ }
49
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAyXH;;GAEG;AACH,MAAM,CAAN,IAAY,YAUX;AAVD,WAAY,YAAY;IACvB,mDAAmC,CAAA;IACnC,+CAA+B,CAAA;IAC/B,qDAAqC,CAAA;IACrC,+CAA+B,CAAA;IAC/B,iDAAiC,CAAA;IACjC,+CAA+B,CAAA;IAC/B,6DAA6C,CAAA;IAC7C,2DAA2C,CAAA;IAC3C,6DAA6C,CAAA;AAC9C,CAAC,EAVW,YAAY,KAAZ,YAAY,QAUvB;AAED;;GAEG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IAE1B;IAEA;IACA;IAJR,YACQ,IAAkB,EACzB,OAAe,EACR,OAAiC,EACjC,UAAmB;QAE1B,KAAK,CAAC,OAAO,CAAC,CAAC;QALR,SAAI,GAAJ,IAAI,CAAc;QAElB,YAAO,GAAP,OAAO,CAA0B;QACjC,eAAU,GAAV,UAAU,CAAS;QAG1B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;QAEvB,oEAAoE;QACpE,IAAI,KAAK,CAAC,iBAAiB,EAAE,CAAC;YAC7B,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACzC,CAAC;IACF,CAAC;IAED,MAAM;QACL,OAAO;YACN,KAAK,EAAE;gBACN,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,QAAQ,EAAE,IAAI,CAAC,UAAU;aACzB;SACD,CAAC;IACH,CAAC;CACD"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@pie-players/tts-server-core",
3
+ "version": "0.1.0",
4
+ "description": "Core interfaces and types for server-side TTS providers",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "build": "tsc",
16
+ "dev": "tsc --watch",
17
+ "test": "vitest",
18
+ "test:coverage": "vitest --coverage"
19
+ },
20
+ "keywords": [
21
+ "tts",
22
+ "text-to-speech",
23
+ "speech-synthesis",
24
+ "server-side",
25
+ "speech-marks"
26
+ ],
27
+ "author": "PIE Framework",
28
+ "license": "MIT",
29
+ "devDependencies": {
30
+ "typescript": "^5.3.3",
31
+ "vitest": "^1.0.4"
32
+ }
33
+ }
package/src/cache.ts ADDED
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Caching interface for TTS results
3
+ * @module @pie-players/tts-server-core
4
+ */
5
+
6
+ import type { SynthesizeResponse } from "./types.js";
7
+
8
+ /**
9
+ * Cache key components for TTS synthesis
10
+ */
11
+ export interface CacheKeyComponents {
12
+ /** Provider identifier */
13
+ providerId: string;
14
+
15
+ /** Text to synthesize */
16
+ text: string;
17
+
18
+ /** Voice ID */
19
+ voice: string;
20
+
21
+ /** Language code */
22
+ language?: string;
23
+
24
+ /** Speech rate */
25
+ rate?: number;
26
+
27
+ /** Audio format */
28
+ format?: string;
29
+ }
30
+
31
+ /**
32
+ * Cache interface for TTS providers
33
+ */
34
+ export interface ITTSCache {
35
+ /**
36
+ * Get cached synthesis result
37
+ *
38
+ * @param key - Cache key
39
+ * @returns Cached result or null if not found
40
+ */
41
+ get(key: string): Promise<SynthesizeResponse | null>;
42
+
43
+ /**
44
+ * Store synthesis result in cache
45
+ *
46
+ * @param key - Cache key
47
+ * @param value - Synthesis response to cache
48
+ * @param ttl - Time to live in seconds (optional)
49
+ */
50
+ set(key: string, value: SynthesizeResponse, ttl?: number): Promise<void>;
51
+
52
+ /**
53
+ * Check if key exists in cache
54
+ *
55
+ * @param key - Cache key
56
+ * @returns True if key exists
57
+ */
58
+ has(key: string): Promise<boolean>;
59
+
60
+ /**
61
+ * Delete cached result
62
+ *
63
+ * @param key - Cache key
64
+ */
65
+ delete(key: string): Promise<void>;
66
+
67
+ /**
68
+ * Clear all cached results
69
+ */
70
+ clear(): Promise<void>;
71
+
72
+ /**
73
+ * Get cache statistics
74
+ */
75
+ getStats?(): Promise<CacheStats>;
76
+ }
77
+
78
+ /**
79
+ * Cache statistics
80
+ */
81
+ export interface CacheStats {
82
+ /** Total cache hits */
83
+ hits: number;
84
+
85
+ /** Total cache misses */
86
+ misses: number;
87
+
88
+ /** Hit rate (0.0 to 1.0) */
89
+ hitRate: number;
90
+
91
+ /** Number of keys in cache */
92
+ keyCount: number;
93
+
94
+ /** Total size in bytes (if available) */
95
+ sizeBytes?: number;
96
+ }
97
+
98
+ /**
99
+ * Generate cache key from components
100
+ *
101
+ * @param components - Cache key components
102
+ * @returns Cache key string
103
+ */
104
+ export function generateCacheKey(components: CacheKeyComponents): string {
105
+ const {
106
+ providerId,
107
+ text,
108
+ voice,
109
+ language = "",
110
+ rate = 1.0,
111
+ format = "mp3",
112
+ } = components;
113
+
114
+ // Create deterministic key from components
115
+ const keyParts = [
116
+ "tts",
117
+ providerId,
118
+ voice,
119
+ language,
120
+ rate.toFixed(2),
121
+ format,
122
+ text,
123
+ ];
124
+
125
+ // Use simple concatenation with delimiter
126
+ // In production, consider using a hash function for shorter keys
127
+ return keyParts.join(":");
128
+ }
129
+
130
+ /**
131
+ * Generate SHA-256 hash for cache key
132
+ * Useful for creating shorter keys from long text
133
+ *
134
+ * @param text - Text to hash
135
+ * @returns Hex string hash
136
+ */
137
+ export async function hashText(text: string): Promise<string> {
138
+ // Use Web Crypto API (available in modern Node.js and browsers)
139
+ const encoder = new TextEncoder();
140
+ const data = encoder.encode(text);
141
+ const hashBuffer = await crypto.subtle.digest("SHA-256", data);
142
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
143
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
144
+ }
145
+
146
+ /**
147
+ * Generate short cache key using hash
148
+ *
149
+ * @param components - Cache key components
150
+ * @returns Promise resolving to cache key
151
+ */
152
+ export async function generateHashedCacheKey(
153
+ components: CacheKeyComponents,
154
+ ): Promise<string> {
155
+ const {
156
+ providerId,
157
+ text,
158
+ voice,
159
+ language = "",
160
+ rate = 1.0,
161
+ format = "mp3",
162
+ } = components;
163
+
164
+ // Hash the text to keep key length reasonable
165
+ const textHash = await hashText(text);
166
+
167
+ const keyParts = [
168
+ "tts",
169
+ providerId,
170
+ voice,
171
+ language,
172
+ rate.toFixed(2),
173
+ format,
174
+ textHash,
175
+ ];
176
+
177
+ return keyParts.join(":");
178
+ }
179
+
180
+ /**
181
+ * In-memory cache implementation
182
+ * Simple LRU cache for development/testing
183
+ */
184
+ export class MemoryCache implements ITTSCache {
185
+ private cache = new Map<
186
+ string,
187
+ { value: SynthesizeResponse; expires: number }
188
+ >();
189
+ private hits = 0;
190
+ private misses = 0;
191
+ private maxSize: number;
192
+
193
+ constructor(maxSize = 100) {
194
+ this.maxSize = maxSize;
195
+ }
196
+
197
+ async get(key: string): Promise<SynthesizeResponse | null> {
198
+ const entry = this.cache.get(key);
199
+
200
+ if (!entry) {
201
+ this.misses++;
202
+ return null;
203
+ }
204
+
205
+ // Check expiration
206
+ if (Date.now() > entry.expires) {
207
+ this.cache.delete(key);
208
+ this.misses++;
209
+ return null;
210
+ }
211
+
212
+ this.hits++;
213
+
214
+ // Update metadata to mark as served from cache
215
+ const result = { ...entry.value };
216
+ result.metadata = { ...result.metadata, cached: true };
217
+
218
+ return result;
219
+ }
220
+
221
+ async set(
222
+ key: string,
223
+ value: SynthesizeResponse,
224
+ ttl = 86400,
225
+ ): Promise<void> {
226
+ // Enforce max size (simple LRU)
227
+ if (this.cache.size >= this.maxSize) {
228
+ // Delete oldest entry (first key)
229
+ const firstKey = this.cache.keys().next().value;
230
+ if (firstKey) {
231
+ this.cache.delete(firstKey);
232
+ }
233
+ }
234
+
235
+ this.cache.set(key, {
236
+ value,
237
+ expires: Date.now() + ttl * 1000,
238
+ });
239
+ }
240
+
241
+ async has(key: string): Promise<boolean> {
242
+ const entry = this.cache.get(key);
243
+ if (!entry) return false;
244
+
245
+ // Check expiration
246
+ if (Date.now() > entry.expires) {
247
+ this.cache.delete(key);
248
+ return false;
249
+ }
250
+
251
+ return true;
252
+ }
253
+
254
+ async delete(key: string): Promise<void> {
255
+ this.cache.delete(key);
256
+ }
257
+
258
+ async clear(): Promise<void> {
259
+ this.cache.clear();
260
+ this.hits = 0;
261
+ this.misses = 0;
262
+ }
263
+
264
+ async getStats(): Promise<CacheStats> {
265
+ const total = this.hits + this.misses;
266
+ return {
267
+ hits: this.hits,
268
+ misses: this.misses,
269
+ hitRate: total > 0 ? this.hits / total : 0,
270
+ keyCount: this.cache.size,
271
+ };
272
+ }
273
+ }
package/src/index.ts ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Core types and interfaces for server-side TTS providers
3
+ * @module @pie-players/tts-server-core
4
+ */
5
+
6
+ // Export cache interfaces
7
+ export type {
8
+ CacheKeyComponents,
9
+ CacheStats,
10
+ ITTSCache,
11
+ } from "./cache.js";
12
+ export {
13
+ generateCacheKey,
14
+ generateHashedCacheKey,
15
+ hashText,
16
+ MemoryCache,
17
+ } from "./cache.js";
18
+
19
+ // Export provider interfaces
20
+ export type {
21
+ ITTSServerProvider,
22
+ TTSServerConfig,
23
+ } from "./provider.js";
24
+
25
+ export { BaseTTSProvider } from "./provider.js";
26
+
27
+ // Export speech marks utilities
28
+ export {
29
+ adjustSpeechMarksForRate,
30
+ estimateSpeechMarks,
31
+ filterSpeechMarksByType,
32
+ getSpeechMarkAtTime,
33
+ getSpeechMarksStats,
34
+ mergeSpeechMarks,
35
+ validateSpeechMarks,
36
+ } from "./speech-marks.js";
37
+ // Export types
38
+ export type {
39
+ GetVoicesOptions,
40
+ ServerProviderCapabilities,
41
+ SpeechMark,
42
+ StandardTTSParameters,
43
+ SynthesizeMetadata,
44
+ SynthesizeRequest,
45
+ SynthesizeResponse,
46
+ TTSProviderExtensions,
47
+ Voice,
48
+ VoiceFeatures,
49
+ } from "./types.js";
50
+ export { TTSError, TTSErrorCode } from "./types.js";
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Server-side TTS Provider interface
3
+ * @module @pie-players/tts-server-core
4
+ */
5
+
6
+ import type {
7
+ GetVoicesOptions,
8
+ ServerProviderCapabilities,
9
+ SynthesizeRequest,
10
+ SynthesizeResponse,
11
+ Voice,
12
+ } from "./types.js";
13
+
14
+ /**
15
+ * Base configuration for TTS providers
16
+ */
17
+ export interface TTSServerConfig {
18
+ /** Provider-specific configuration */
19
+ [key: string]: unknown;
20
+ }
21
+
22
+ /**
23
+ * Server-side TTS Provider interface
24
+ *
25
+ * All server-side TTS providers must implement this interface.
26
+ * Providers handle synthesis requests and return audio with speech marks.
27
+ *
28
+ * ## Initialization Performance
29
+ *
30
+ * The `initialize()` method MUST be fast and lightweight:
31
+ * - Should only validate config and create API clients
32
+ * - MUST NOT fetch voices or make expensive API calls
33
+ * - MUST NOT perform test synthesis requests
34
+ *
35
+ * Use `getVoices()` explicitly when voice discovery is needed (e.g., in demo/admin UIs).
36
+ * Runtime synthesis should work with hardcoded voice IDs without querying available voices.
37
+ *
38
+ * @example Fast initialization (runtime)
39
+ * ```typescript
40
+ * const provider = new PollyServerProvider();
41
+ * await provider.initialize({ region: 'us-east-1', defaultVoice: 'Joanna' });
42
+ * // Ready to synthesize immediately - no voices query
43
+ * await provider.synthesize({ text: 'Hello', voice: 'Joanna' });
44
+ * ```
45
+ *
46
+ * @example Explicit voice discovery (admin/demo UIs)
47
+ * ```typescript
48
+ * const provider = new PollyServerProvider();
49
+ * await provider.initialize({ region: 'us-east-1' });
50
+ * const voices = await provider.getVoices(); // Explicit, separate call
51
+ * ```
52
+ */
53
+ export interface ITTSServerProvider {
54
+ /**
55
+ * Unique provider identifier (e.g., 'aws-polly', 'google-cloud-tts')
56
+ */
57
+ readonly providerId: string;
58
+
59
+ /**
60
+ * Human-readable provider name
61
+ */
62
+ readonly providerName: string;
63
+
64
+ /**
65
+ * Provider version
66
+ */
67
+ readonly version: string;
68
+
69
+ /**
70
+ * Initialize the provider with configuration.
71
+ *
72
+ * MUST be fast and lightweight - only validates config and creates clients.
73
+ * MUST NOT fetch voices or make expensive API calls during initialization.
74
+ *
75
+ * @param config - Provider-specific configuration
76
+ * @throws {TTSError} If initialization fails
77
+ * @performance Should complete in <100ms
78
+ */
79
+ initialize(config: TTSServerConfig): Promise<void>;
80
+
81
+ /**
82
+ * Synthesize speech from text
83
+ *
84
+ * @param request - Synthesis request parameters
85
+ * @returns Audio data and speech marks
86
+ * @throws {TTSError} If synthesis fails
87
+ */
88
+ synthesize(request: SynthesizeRequest): Promise<SynthesizeResponse>;
89
+
90
+ /**
91
+ * Get available voices (explicit, secondary query).
92
+ *
93
+ * This is an EXPLICIT operation for voice discovery in demo/admin UIs.
94
+ * NOT called during initialization - call separately when needed.
95
+ *
96
+ * @param options - Optional filters for voices
97
+ * @returns List of available voices
98
+ * @throws {TTSError} If voice listing fails
99
+ * @note May take 200-500ms depending on provider
100
+ */
101
+ getVoices(options?: GetVoicesOptions): Promise<Voice[]>;
102
+
103
+ /**
104
+ * Get provider capabilities (synchronous, fast).
105
+ *
106
+ * Returns static capability information without API calls.
107
+ *
108
+ * @returns Provider feature support
109
+ * @performance Should complete in <1ms (synchronous)
110
+ */
111
+ getCapabilities(): ServerProviderCapabilities;
112
+
113
+ /**
114
+ * Clean up provider resources
115
+ * Called when provider is no longer needed
116
+ */
117
+ destroy(): Promise<void>;
118
+ }
119
+
120
+ /**
121
+ * Abstract base class for TTS providers
122
+ * Provides common functionality and helpers
123
+ */
124
+ export abstract class BaseTTSProvider implements ITTSServerProvider {
125
+ abstract readonly providerId: string;
126
+ abstract readonly providerName: string;
127
+ abstract readonly version: string;
128
+
129
+ protected config: TTSServerConfig = {};
130
+ protected initialized = false;
131
+
132
+ abstract initialize(config: TTSServerConfig): Promise<void>;
133
+ abstract synthesize(request: SynthesizeRequest): Promise<SynthesizeResponse>;
134
+ abstract getVoices(options?: GetVoicesOptions): Promise<Voice[]>;
135
+ abstract getCapabilities(): ServerProviderCapabilities;
136
+
137
+ async destroy(): Promise<void> {
138
+ this.initialized = false;
139
+ this.config = {};
140
+ }
141
+
142
+ /**
143
+ * Ensure provider is initialized before operations
144
+ * @throws {TTSError} If provider not initialized
145
+ */
146
+ protected ensureInitialized(): void {
147
+ if (!this.initialized) {
148
+ throw new Error(`Provider ${this.providerId} not initialized`);
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Validate synthesis request
154
+ * @throws {TTSError} If request is invalid
155
+ */
156
+ protected validateRequest(
157
+ request: SynthesizeRequest,
158
+ capabilities: ServerProviderCapabilities,
159
+ ): void {
160
+ if (!request.text || request.text.trim().length === 0) {
161
+ throw new Error("Text is required and cannot be empty");
162
+ }
163
+
164
+ if (request.text.length > capabilities.standard.maxTextLength) {
165
+ throw new Error(
166
+ `Text length (${request.text.length}) exceeds maximum (${capabilities.standard.maxTextLength})`,
167
+ );
168
+ }
169
+
170
+ if (
171
+ request.format &&
172
+ !capabilities.extensions.supportedFormats.includes(request.format)
173
+ ) {
174
+ throw new Error(
175
+ `Format '${request.format}' not supported. Supported formats: ${capabilities.extensions.supportedFormats.join(", ")}`,
176
+ );
177
+ }
178
+
179
+ if (
180
+ request.rate !== undefined &&
181
+ (request.rate < 0.25 || request.rate > 4.0)
182
+ ) {
183
+ throw new Error("Rate must be between 0.25 and 4.0");
184
+ }
185
+
186
+ if (
187
+ request.pitch !== undefined &&
188
+ (request.pitch < -20 || request.pitch > 20)
189
+ ) {
190
+ throw new Error("Pitch must be between -20 and 20");
191
+ }
192
+
193
+ if (
194
+ request.volume !== undefined &&
195
+ (request.volume < 0 || request.volume > 1)
196
+ ) {
197
+ throw new Error("Volume must be between 0 and 1");
198
+ }
199
+ }
200
+ }