@readium/speech 0.7.1 → 0.8.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.
@@ -0,0 +1,21 @@
1
+ import { ReadiumSpeechEngineProvider } from '../provider';
2
+ import { ReadiumSpeechPlaybackEngine } from '../engine';
3
+ import { ReadiumSpeechVoice } from '../voices/types';
4
+ export interface FallbackEngineProviderOptions {
5
+ primary: ReadiumSpeechEngineProvider;
6
+ fallback: ReadiumSpeechEngineProvider;
7
+ onFailure?: "fallback" | "error" | "fallbackAndRecover";
8
+ healthCheckIntervalMs?: number;
9
+ }
10
+ export declare class FallbackEngineProvider implements ReadiumSpeechEngineProvider {
11
+ readonly id: string;
12
+ readonly name: string;
13
+ private readonly primary;
14
+ private readonly fallback;
15
+ private readonly onFailure;
16
+ private readonly healthCheckIntervalMs?;
17
+ constructor(options: FallbackEngineProviderOptions);
18
+ getVoices(forceRefresh?: boolean): Promise<ReadiumSpeechVoice[]>;
19
+ createEngine(voice?: ReadiumSpeechVoice | string): Promise<ReadiumSpeechPlaybackEngine>;
20
+ destroy(): Promise<void>;
21
+ }
@@ -0,0 +1,68 @@
1
+ import { ReadiumSpeechPlaybackEngine } from '../engine';
2
+ import { ReadiumSpeechEngineProvider } from '../provider';
3
+ import { ReadiumSpeechPlaybackEvent, ReadiumSpeechPlaybackState } from '../navigator';
4
+ import { ReadiumSpeechUtterance } from '../utterance';
5
+ import { ReadiumSpeechVoice } from '../voices/types';
6
+ export interface FallbackSpeechEngineOptions {
7
+ primaryEngine: ReadiumSpeechPlaybackEngine;
8
+ primaryProvider: ReadiumSpeechEngineProvider;
9
+ fallbackProvider: ReadiumSpeechEngineProvider;
10
+ onFailure?: "fallback" | "error" | "fallbackAndRecover";
11
+ healthCheckIntervalMs?: number;
12
+ }
13
+ export declare class FallbackSpeechEngine implements ReadiumSpeechPlaybackEngine {
14
+ private activeEngine;
15
+ private readonly primaryProvider;
16
+ private readonly fallbackProvider;
17
+ private readonly onFailure;
18
+ private readonly healthCheckIntervalMs;
19
+ private hasFallenBack;
20
+ private healthCheckTimer;
21
+ private primaryReachable;
22
+ private currentUtterances;
23
+ private lastVoiceRequest;
24
+ private desiredIndex;
25
+ private desiredPlaying;
26
+ private engineStarted;
27
+ private swapInFlight;
28
+ private teardownEpoch;
29
+ private loadToken;
30
+ private readonly events;
31
+ private unbindActiveEngine;
32
+ constructor(options: FallbackSpeechEngineOptions);
33
+ initialize(): Promise<unknown>;
34
+ loadUtterances(contents: ReadiumSpeechUtterance[], startIndex?: number): void;
35
+ setVoice(voice: ReadiumSpeechVoice | string): void;
36
+ getCurrentVoice(): ReadiumSpeechVoice | null;
37
+ getAvailableVoices(): Promise<ReadiumSpeechVoice[]>;
38
+ setSpeakInContentLanguage(enabled: boolean): void;
39
+ getSpeakInContentLanguage(): boolean;
40
+ speak(utteranceIndex?: number): void;
41
+ pause(): void;
42
+ resume(): void;
43
+ stop(): void;
44
+ setRate(rate: number): void;
45
+ getRate(): number;
46
+ setPitch(pitch: number): void;
47
+ getPitch(): number;
48
+ setVolume(volume: number): void;
49
+ getVolume(): number;
50
+ private isEngineTrusted;
51
+ getState(): ReadiumSpeechPlaybackState;
52
+ getCurrentUtteranceIndex(): number;
53
+ setCurrentUtteranceIndex(index: number, onComplete?: (success: boolean) => void): void;
54
+ getUtteranceCount(): number;
55
+ on(event: ReadiumSpeechPlaybackEvent["type"], callback: (event: ReadiumSpeechPlaybackEvent) => void): () => void;
56
+ private emitEvent;
57
+ private bindActiveEngine;
58
+ private handleError;
59
+ private copyPlaybackParameters;
60
+ private startEngineWhenReady;
61
+ private startHealthCheck;
62
+ private maybeRecoverNow;
63
+ private swapToFallback;
64
+ private recoverToPrimary;
65
+ private performSwap;
66
+ private pickBestFallbackVoice;
67
+ destroy(): Promise<void>;
68
+ }
@@ -0,0 +1,3 @@
1
+ export * from './fallbackSpeechEngine';
2
+ export * from './fallbackEngineProvider';
3
+ export * from './recoverableFailure';
@@ -0,0 +1,10 @@
1
+ import { ReadiumSpeechPlaybackEvent } from '../navigator';
2
+ export interface ErrorEventDetail {
3
+ message: string;
4
+ recoverable: boolean;
5
+ status?: number;
6
+ type?: string;
7
+ title?: string;
8
+ instance?: string;
9
+ }
10
+ export declare function isRecoverableFailure(event: ReadiumSpeechPlaybackEvent): boolean;
@@ -17,4 +17,13 @@ export declare class SpeechServerError extends Error {
17
17
  instance?: string;
18
18
  });
19
19
  }
20
+ export declare class SpeechServerStallError extends SpeechServerError {
21
+ constructor(message: string);
22
+ }
23
+ export declare class SpeechServerAudioDecodeError extends Error {
24
+ constructor(message: string);
25
+ }
26
+ export declare class SpeechServerNetworkError extends Error {
27
+ constructor(message: string);
28
+ }
20
29
  export declare function toSpeechServerError(response: Response): Promise<SpeechServerError>;
@@ -15,6 +15,7 @@ export interface SpeechServerEngineOptions {
15
15
  readyBufferChars?: number;
16
16
  overLengthText?: "split" | "error";
17
17
  format?: SpeechServerFormatOptions;
18
+ timeoutMs?: number;
18
19
  }
19
20
  export declare class SpeechServerEngine implements ReadiumSpeechPlaybackEngine {
20
21
  private endpoints;
@@ -26,17 +27,19 @@ export declare class SpeechServerEngine implements ReadiumSpeechPlaybackEngine {
26
27
  private currentUtterances;
27
28
  private currentUtteranceIndex;
28
29
  private playbackState;
29
- private eventListeners;
30
+ private readonly events;
30
31
  private speakInContentLanguage;
31
32
  private speakGeneration;
32
33
  private loadGeneration;
33
34
  private readonly prefetchWindow;
34
35
  private readonly readyBufferChars;
35
36
  private readonly overLengthText;
37
+ private readonly timeoutMs;
36
38
  private readonly formatOptions;
37
39
  private readonly canPlayType;
38
40
  private prefetchCache;
39
41
  private prefetchChainTail;
42
+ private activeControllers;
40
43
  private audioContext;
41
44
  private masterGain;
42
45
  private scheduledChunks;
@@ -46,7 +49,8 @@ export declare class SpeechServerEngine implements ReadiumSpeechPlaybackEngine {
46
49
  private volume;
47
50
  constructor(options: SpeechServerEngineOptions);
48
51
  setAvailableVoices(voices: ReadiumSpeechVoice[]): void;
49
- loadUtterances(contents: ReadiumSpeechUtterance[]): void;
52
+ private fetchNetwork;
53
+ loadUtterances(contents: ReadiumSpeechUtterance[], startIndex?: number): void;
50
54
  private bufferUntilReady;
51
55
  private indexCoveringChars;
52
56
  setVoice(voice: ReadiumSpeechVoice | string): void;
@@ -65,6 +69,7 @@ export declare class SpeechServerEngine implements ReadiumSpeechPlaybackEngine {
65
69
  private synthesizeStream;
66
70
  private synthesizeChunk;
67
71
  private ensureAudioContext;
72
+ private awaitWithStallDeadline;
68
73
  private scheduleChunksStreaming;
69
74
  private handleUtteranceEnded;
70
75
  private startBoundaryPolling;
@@ -10,7 +10,7 @@ export declare class SpeechServerEngineProvider implements ReadiumSpeechEnginePr
10
10
  private fetchImpl;
11
11
  private voices;
12
12
  constructor(options: SpeechServerEngineProviderOptions);
13
- getVoices(): Promise<ReadiumSpeechVoice[]>;
13
+ getVoices(forceRefresh?: boolean): Promise<ReadiumSpeechVoice[]>;
14
14
  createEngine(voice?: ReadiumSpeechVoice | string): Promise<ReadiumSpeechPlaybackEngine>;
15
15
  destroy(): Promise<void>;
16
16
  }
@@ -84,11 +84,6 @@ export declare class WebSpeechVoiceManager {
84
84
  * @returns A tuple of [language, region] where language is lowercase and region is UPPERCASE
85
85
  */
86
86
  static extractLangRegionFromBCP47(lang: string): [string, string | undefined];
87
- /**
88
- * Get display name for a language code
89
- * @private
90
- */
91
- private static getLanguageDisplayName;
92
87
  /**
93
88
  * Clean voice name by removing specific formatting
94
89
  * @private
@@ -188,39 +183,16 @@ export declare class WebSpeechVoiceManager {
188
183
  * @returns Filtered array with very low quality voices removed
189
184
  */
190
185
  filterOutVeryLowQualityVoices(voices?: ReadiumSpeechVoice[]): ReadiumSpeechVoice[];
191
- /**
192
- * Get the numeric value for a quality level
193
- * @param quality Quality level
194
- * @returns Numeric value (higher = better quality, 0 for undefined/null)
195
- */
196
- private static getQualityValue;
197
- /**
198
- * Sort two voices by quality, using JSON order as fallback for undefined/null quality
199
- * @param a First voice
200
- * @param b Second voice
201
- * @param jsonOrderMaps Optional map of language codes to voice order maps
202
- * @param baseLang Base language code to use for looking up the order map
203
- * @returns Comparison result (-1, 0, or 1)
204
- */
205
- private static sortByQuality;
206
186
  /**
207
187
  * Sort voices by quality, respecting JSON name order, then alphabetically for undefined/null quality
208
188
  * @param voices Array of voices to sort
209
189
  * @returns Sorted array of voices
210
190
  */
211
191
  sortVoicesByQuality(voices?: ReadiumSpeechVoice[]): Promise<ReadiumSpeechVoice[]>;
212
- /**
213
- * Group voices by language based on processed preferred languages
214
- */
215
- private static groupVoicesByLanguage;
216
192
  /**
217
193
  * Sort regions by default then alphabetically, sort voices by quality
218
194
  */
219
195
  private static sortByDefaultRegion;
220
- /**
221
- * Sort voices alphabetically by language, then region, then quality
222
- */
223
- private static sortAlphabetically;
224
196
  /**
225
197
  * Sort voices by language preference, then alphabetically
226
198
  * @param voices Array of voices to sort
@@ -228,10 +200,6 @@ export declare class WebSpeechVoiceManager {
228
200
  * @returns Sorted array of voices
229
201
  */
230
202
  sortVoicesByLanguages(preferredLanguages?: string[], voices?: ReadiumSpeechVoice[]): Promise<ReadiumSpeechVoice[]>;
231
- /**
232
- * Sort languages by region preference, then voices by quality
233
- */
234
- private static sortByPreferredRegion;
235
203
  /**
236
204
  * Sort voices by region preference, then alphabetically
237
205
  * @param voices Array of voices to sort
@@ -9,7 +9,7 @@ export declare class WebSpeechEngine implements ReadiumSpeechPlaybackEngine {
9
9
  private currentUtterances;
10
10
  private currentUtteranceIndex;
11
11
  private playbackState;
12
- private eventListeners;
12
+ private readonly events;
13
13
  private voiceManager;
14
14
  private voices;
15
15
  private defaultVoice;
@@ -49,7 +49,7 @@ export declare class WebSpeechEngine implements ReadiumSpeechPlaybackEngine {
49
49
  setSpeakInContentLanguage(enabled: boolean): void;
50
50
  getSpeakInContentLanguage(): boolean;
51
51
  private toPlainText;
52
- loadUtterances(contents: ReadiumSpeechUtterance[]): void;
52
+ loadUtterances(contents: ReadiumSpeechUtterance[], startIndex?: number): void;
53
53
  setVoice(voice: ReadiumSpeechVoice | string): Promise<void>;
54
54
  getAvailableVoices(): Promise<ReadiumSpeechVoice[]>;
55
55
  speak(utteranceIndex?: number): void;
package/build/engine.d.ts CHANGED
@@ -3,7 +3,7 @@ import { ReadiumSpeechUtterance } from './utterance';
3
3
  import { ReadiumSpeechVoice } from './voices/types';
4
4
  export interface ReadiumSpeechPlaybackEngine {
5
5
  initialize?(): Promise<unknown>;
6
- loadUtterances(contents: ReadiumSpeechUtterance[]): void;
6
+ loadUtterances(contents: ReadiumSpeechUtterance[], startIndex?: number): void;
7
7
  setVoice(voice: ReadiumSpeechVoice | string): void;
8
8
  getCurrentVoice(): ReadiumSpeechVoice | null;
9
9
  getAvailableVoices(): Promise<ReadiumSpeechVoice[]>;