@ssml-builder-js/azure-tts-client 2.12.0 → 2.13.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/index.mjs CHANGED
@@ -174,12 +174,23 @@ async function synthesizeSsml(ssml, config) {
174
174
  ...(bookmarks ?? []).map((bookmark) => bookmark.audioOffsetMs)
175
175
  );
176
176
  const durationMs = result.audioDuration ? ticksToMilliseconds(result.audioDuration) : eventDurationMs;
177
+ const requestId = result.resultId;
178
+ const addSourceMetadata = (event) => ({
179
+ ...event,
180
+ ...config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {},
181
+ ...requestId ? { requestId } : {}
182
+ });
183
+ const sourceBoundaries = boundaries.map((boundary) => addSourceMetadata(boundary));
184
+ const sourceVisemes = visemes.map((viseme) => addSourceMetadata(viseme));
185
+ const sourceBookmarks = bookmarks.map((bookmark) => addSourceMetadata(bookmark));
177
186
  resolve({
178
187
  audioData: result.audioData,
179
188
  durationMs,
180
- ...boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {},
181
- ...visemes.length > 0 ? { visemes } : {},
182
- ...bookmarks.length > 0 ? { bookmarks } : {}
189
+ ...config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {},
190
+ ...requestId ? { requestId } : {},
191
+ ...sourceBoundaries.length > 0 ? { boundaries: sourceBoundaries, wordBoundary: sourceBoundaries, wordBoundaries: sourceBoundaries } : {},
192
+ ...sourceVisemes.length > 0 ? { visemes: sourceVisemes } : {},
193
+ ...sourceBookmarks.length > 0 ? { bookmarks: sourceBookmarks } : {}
183
194
  });
184
195
  };
185
196
  try {
@@ -199,10 +210,109 @@ async function synthesizeSsml(ssml, config) {
199
210
  }
200
211
  });
201
212
  }
213
+ async function synthesizeSsmlChunks(chunks, config) {
214
+ const results = [];
215
+ const totalChunks = chunks.length;
216
+ for (const [index, chunk] of chunks.entries()) {
217
+ const input = typeof chunk === "string" ? { ssml: chunk } : chunk;
218
+ const result = await synthesizeSsml(input.ssml, {
219
+ ...config,
220
+ ...input.originalTextRange ? { sourceTextRange: input.originalTextRange } : {},
221
+ onProgress: void 0
222
+ });
223
+ results.push(result);
224
+ config.onProgress?.({
225
+ currentChunk: index + 1,
226
+ totalChunks,
227
+ percent: totalChunks === 0 ? 100 : Math.round((index + 1) / totalChunks * 100)
228
+ });
229
+ }
230
+ return mergeSynthesisResults(results);
231
+ }
232
+ function mergeSynthesisResults(results) {
233
+ const audioLength = results.reduce((total, result) => total + result.audioData.byteLength, 0);
234
+ const audioData = new Uint8Array(audioLength);
235
+ const boundaries = [];
236
+ const visemes = [];
237
+ const bookmarks = [];
238
+ let byteOffset = 0;
239
+ let durationOffset = 0;
240
+ for (const result of results) {
241
+ audioData.set(new Uint8Array(result.audioData), byteOffset);
242
+ byteOffset += result.audioData.byteLength;
243
+ const chunkBoundaries = result.boundaries && result.boundaries.length > 0 ? result.boundaries : result.wordBoundary ?? result.wordBoundaries ?? [];
244
+ for (const boundary of chunkBoundaries) {
245
+ const textRange = boundary.textRange ?? result.textRange;
246
+ const requestId = boundary.requestId ?? result.requestId;
247
+ boundaries.push({
248
+ ...boundary,
249
+ audioOffsetMs: boundary.audioOffsetMs + durationOffset,
250
+ ...textRange ? { textRange: { ...textRange } } : {},
251
+ ...requestId ? { requestId } : {}
252
+ });
253
+ }
254
+ for (const viseme of result.visemes ?? []) {
255
+ const textRange = viseme.textRange ?? result.textRange;
256
+ const requestId = viseme.requestId ?? result.requestId;
257
+ visemes.push({
258
+ ...viseme,
259
+ audioOffsetMs: viseme.audioOffsetMs + durationOffset,
260
+ ...textRange ? { textRange: { ...textRange } } : {},
261
+ ...requestId ? { requestId } : {}
262
+ });
263
+ }
264
+ for (const bookmark of result.bookmarks ?? []) {
265
+ const textRange = bookmark.textRange ?? result.textRange;
266
+ const requestId = bookmark.requestId ?? result.requestId;
267
+ bookmarks.push({
268
+ ...bookmark,
269
+ audioOffsetMs: bookmark.audioOffsetMs + durationOffset,
270
+ ...textRange ? { textRange: { ...textRange } } : {},
271
+ ...requestId ? { requestId } : {}
272
+ });
273
+ }
274
+ durationOffset += Math.max(0, result.durationMs);
275
+ }
276
+ return {
277
+ audioData: audioData.buffer,
278
+ durationMs: durationOffset,
279
+ ...boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {},
280
+ ...visemes.length > 0 ? { visemes } : {},
281
+ ...bookmarks.length > 0 ? { bookmarks } : {},
282
+ ...results.length === 1 && results[0]?.requestId ? { requestId: results[0].requestId } : {},
283
+ ...results.length === 1 && results[0]?.textRange ? { textRange: { ...results[0].textRange } } : {}
284
+ };
285
+ }
202
286
  async function synthesizeSpeech(ssml, config) {
203
287
  return (await synthesizeSsml(ssml, config)).audioData;
204
288
  }
205
289
 
290
+ // src/safe.ts
291
+ import { validateAzureSsml } from "@ssml-builder-js/ssml-core";
292
+ async function synthesizeSsmlSafe(client, ssml, options = {}) {
293
+ const validationOptions = options.validation ?? options;
294
+ const diagnostics = await Promise.resolve(validateAzureSsml(ssml, validationOptions));
295
+ const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
296
+ if (errors.length > 0) {
297
+ return {
298
+ ok: false,
299
+ success: false,
300
+ status: "validation-error",
301
+ error: {
302
+ kind: "validation",
303
+ message: "SSML validation failed; the Azure Speech API was not called.",
304
+ diagnostics: errors
305
+ }
306
+ };
307
+ }
308
+ try {
309
+ return { ok: true, success: true, status: "success", value: await client.synthesizeSsml(ssml) };
310
+ } catch (error) {
311
+ const azureError = error instanceof AzureTtsError ? error : createSpeechSdkError(error);
312
+ return { ok: false, success: false, status: "azure-api-error", error: azureError };
313
+ }
314
+ }
315
+
206
316
  // src/client.ts
207
317
  var ENDPOINT_TEMPLATE = "https://{region}.tts.speech.microsoft.com/cognitiveservices/v1";
208
318
  var _options;
@@ -224,6 +334,22 @@ var AzureTtsClient = class {
224
334
  __privateGet(this, _options).logger?.debug?.("Using Azure TTS endpoint:", endpoint);
225
335
  return synthesizeSsml(ssml, { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs });
226
336
  }
337
+ async synthesizeChunks(chunks, options = {}) {
338
+ const { region, subscriptionKey, outputFormat, signal, timeoutMs } = __privateGet(this, _options);
339
+ const endpoint = __privateGet(this, _options).endpoint?.trim() || ENDPOINT_TEMPLATE.replace("{region}", region);
340
+ return synthesizeSsmlChunks(chunks, {
341
+ endpoint,
342
+ region,
343
+ subscriptionKey,
344
+ outputFormat,
345
+ signal,
346
+ timeoutMs,
347
+ onProgress: options.onProgress ?? __privateGet(this, _options).onProgress
348
+ });
349
+ }
350
+ async synthesizeSsmlSafe(ssml, options = {}) {
351
+ return synthesizeSsmlSafe(this, ssml, options);
352
+ }
227
353
  };
228
354
  _options = new WeakMap();
229
355
 
@@ -309,7 +435,10 @@ export {
309
435
  AzureTtsError,
310
436
  AzureTtsSdkError,
311
437
  fetchAzureVoiceCatalog,
438
+ mergeSynthesisResults,
312
439
  synthesizeSpeech,
313
- synthesizeSsml
440
+ synthesizeSsml,
441
+ synthesizeSsmlChunks,
442
+ synthesizeSsmlSafe
314
443
  };
315
444
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/errors.ts","../src/synthesis.ts","../src/speechConfig.ts","../src/outputFormats.ts","../src/client.ts","../src/voiceCatalog.ts"],"sourcesContent":["export class AzureTtsError extends Error {\n readonly status: number;\n readonly statusText: string;\n readonly responseBody: string;\n readonly requestId: string | null;\n\n constructor(status: number, statusText: string, responseBody: string, requestId: string | null) {\n super(`Azure TTS request failed: ${status} ${statusText}`);\n this.name = \"AzureTtsError\";\n this.status = status;\n this.statusText = statusText;\n this.responseBody = responseBody;\n this.requestId = requestId;\n }\n}\n\nexport class AzureTtsSdkError extends AzureTtsError {\n readonly errorDetails: string;\n\n constructor(errorDetails: string) {\n super(0, \"Speech SDK\", errorDetails, null);\n this.name = \"AzureTtsSdkError\";\n this.message = `Azure TTS synthesis failed: ${errorDetails}`;\n this.errorDetails = errorDetails;\n }\n}\n\nexport function createSpeechSdkError(error: unknown): AzureTtsSdkError {\n const message = error instanceof Error ? error.message : String(error);\n return new AzureTtsSdkError(message);\n}\n","import * as SpeechSDK from \"microsoft-cognitiveservices-speech-sdk\";\nimport { createSpeechSdkError } from \"./errors.ts\";\nimport { createSpeechConfig } from \"./speechConfig.ts\";\nimport type { SsmlSynthesisResult, TtsConfig } from \"./types.ts\";\n\nfunction closeSpeechResources(speechConfig: SpeechSDK.SpeechConfig, synthesizer: SpeechSDK.SpeechSynthesizer): void {\n try {\n synthesizer.close();\n } catch {}\n\n try {\n speechConfig.close();\n } catch {}\n}\n\nconst ticksToMilliseconds = (ticks: number): number => Math.max(0, ticks) / 10_000;\n\nexport async function synthesizeSsml(ssml: string, config: TtsConfig): Promise<SsmlSynthesisResult> {\n if (config.signal?.aborted) {\n throw createSpeechSdkError(\"Speech synthesis was cancelled.\");\n }\n\n const speechConfig = createSpeechConfig(config);\n const synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, null);\n\n return await new Promise<SsmlSynthesisResult>((resolve, reject) => {\n let resourcesClosed = false;\n let settled = false;\n let timeout: ReturnType<typeof setTimeout> | undefined;\n let abortHandler: (() => void) | undefined;\n const cleanup = () => {\n if (timeout) clearTimeout(timeout);\n if (abortHandler) config.signal?.removeEventListener(\"abort\", abortHandler);\n };\n const closeResources = () => {\n if (resourcesClosed) return;\n resourcesClosed = true;\n closeSpeechResources(speechConfig, synthesizer);\n };\n const rejectWithError = (error: unknown) => {\n if (settled) return;\n settled = true;\n cleanup();\n closeResources();\n reject(createSpeechSdkError(error));\n };\n\n const boundaries: SsmlSynthesisResult[\"boundaries\"] = [];\n const visemes: SsmlSynthesisResult[\"visemes\"] = [];\n const bookmarks: SsmlSynthesisResult[\"bookmarks\"] = [];\n synthesizer.wordBoundary = (_sender, event) => {\n boundaries.push({\n text: event.text,\n audioOffsetMs: ticksToMilliseconds(event.audioOffset),\n durationMs: ticksToMilliseconds(event.duration),\n });\n };\n synthesizer.visemeReceived = (_sender, event) => {\n visemes.push({ visemeId: event.visemeId, audioOffsetMs: ticksToMilliseconds(event.audioOffset) });\n };\n synthesizer.bookmarkReached = (_sender, event) => {\n bookmarks.push({ name: event.text, audioOffsetMs: ticksToMilliseconds(event.audioOffset) });\n };\n\n const cb = (result: SpeechSDK.SpeechSynthesisResult) => {\n if (settled) return;\n const { reason, errorDetails } = result;\n if (reason !== SpeechSDK.ResultReason.SynthesizingAudioCompleted) {\n const err = errorDetails || `Speech synthesis failed with reason ${reason}.`;\n rejectWithError(err);\n return;\n }\n settled = true;\n cleanup();\n closeResources();\n const eventDurationMs = Math.max(\n 0,\n ...(boundaries ?? []).map((boundary) => boundary.audioOffsetMs + boundary.durationMs),\n ...(visemes ?? []).map((viseme) => viseme.audioOffsetMs),\n ...(bookmarks ?? []).map((bookmark) => bookmark.audioOffsetMs),\n );\n const durationMs = result.audioDuration ? ticksToMilliseconds(result.audioDuration) : eventDurationMs;\n resolve({\n audioData: result.audioData,\n durationMs,\n ...(boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {}),\n ...(visemes.length > 0 ? { visemes } : {}),\n ...(bookmarks.length > 0 ? { bookmarks } : {}),\n });\n };\n\n try {\n if (config.signal) {\n abortHandler = () => rejectWithError(\"Speech synthesis was cancelled.\");\n config.signal.addEventListener(\"abort\", abortHandler, { once: true });\n }\n if (config.timeoutMs !== undefined && config.timeoutMs > 0) {\n timeout = setTimeout(\n () => rejectWithError(`Speech synthesis timed out after ${config.timeoutMs} ms.`),\n config.timeoutMs,\n );\n }\n synthesizer.speakSsmlAsync(ssml, cb, rejectWithError);\n } catch (error) {\n rejectWithError(error);\n }\n });\n}\n\n/** Backward-compatible audio-only synthesis helper. */\nexport async function synthesizeSpeech(ssml: string, config: TtsConfig): Promise<ArrayBuffer> {\n return (await synthesizeSsml(ssml, config)).audioData;\n}\n","import { SpeechConfig } from \"microsoft-cognitiveservices-speech-sdk\";\nimport { DEFAULT_OUTPUT_FORMAT, resolveOutputFormat } from \"./outputFormats.ts\";\nimport type { TtsConfig } from \"./types.ts\";\n\nexport function resolveEndpoint(config: TtsConfig): string {\n const endpoint = config.endpoint?.trim() || \"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1\";\n return endpoint.replace(/\\{region\\}/g, encodeURIComponent(config.region));\n}\n\nexport function createSpeechConfig(config: TtsConfig): SpeechConfig {\n const { outputFormat = DEFAULT_OUTPUT_FORMAT, subscriptionKey } = config;\n\n const endpoint = new URL(resolveEndpoint(config));\n const speechConfig = SpeechConfig.fromEndpoint(endpoint, subscriptionKey);\n speechConfig.speechSynthesisOutputFormat = resolveOutputFormat(outputFormat);\n return speechConfig;\n}\n","import * as SpeechSDK from \"microsoft-cognitiveservices-speech-sdk\";\n\nexport const DEFAULT_OUTPUT_FORMAT = \"audio-16khz-128kbitrate-mono-mp3\";\n\nconst OUTPUT_FORMATS: Record<string, SpeechSDK.SpeechSynthesisOutputFormat> = {\n \"raw-8khz-8bit-mono-mulaw\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz8BitMonoMULaw,\n \"riff-16khz-16kbps-mono-siren\": SpeechSDK.SpeechSynthesisOutputFormat.Riff16Khz16KbpsMonoSiren,\n \"audio-16khz-16kbps-mono-siren\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz16KbpsMonoSiren,\n \"audio-16khz-32kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3,\n \"audio-16khz-128kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz128KBitRateMonoMp3,\n \"audio-16khz-64kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz64KBitRateMonoMp3,\n \"audio-24khz-48kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3,\n \"audio-24khz-96kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz96KBitRateMonoMp3,\n \"audio-24khz-160kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3,\n \"raw-16khz-16bit-mono-truesilk\": SpeechSDK.SpeechSynthesisOutputFormat.Raw16Khz16BitMonoTrueSilk,\n \"riff-16khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff16Khz16BitMonoPcm,\n \"riff-8khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz16BitMonoPcm,\n \"riff-24khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm,\n \"riff-8khz-8bit-mono-mulaw\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz8BitMonoMULaw,\n \"raw-16khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm,\n \"raw-24khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw24Khz16BitMonoPcm,\n \"raw-8khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz16BitMonoPcm,\n \"ogg-16khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg16Khz16BitMonoOpus,\n \"ogg-24khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg24Khz16BitMonoOpus,\n \"raw-48khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw48Khz16BitMonoPcm,\n \"riff-48khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff48Khz16BitMonoPcm,\n \"audio-48khz-96kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio48Khz96KBitRateMonoMp3,\n \"audio-48khz-192kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3,\n \"ogg-48khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg48Khz16BitMonoOpus,\n \"webm-16khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm16Khz16BitMonoOpus,\n \"webm-24khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm24Khz16BitMonoOpus,\n \"webm-24khz-16bit-24kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm24Khz16Bit24KbpsMonoOpus,\n \"raw-24khz-16bit-mono-truesilk\": SpeechSDK.SpeechSynthesisOutputFormat.Raw24Khz16BitMonoTrueSilk,\n \"raw-8khz-8bit-mono-alaw\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz8BitMonoALaw,\n \"riff-8khz-8bit-mono-alaw\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz8BitMonoALaw,\n \"audio-16khz-16bit-32kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz16Bit32KbpsMonoOpus,\n \"audio-24khz-16bit-48kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz16Bit48KbpsMonoOpus,\n \"audio-24khz-16bit-24kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz16Bit24KbpsMonoOpus,\n \"raw-22050hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw22050Hz16BitMonoPcm,\n \"riff-22050hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff22050Hz16BitMonoPcm,\n \"raw-44100hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw44100Hz16BitMonoPcm,\n \"riff-44100hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff44100Hz16BitMonoPcm,\n \"amr-wb-16000hz\": SpeechSDK.SpeechSynthesisOutputFormat.AmrWb16000Hz,\n \"g722-16khz-64kbps\": SpeechSDK.SpeechSynthesisOutputFormat.G72216Khz64Kbps,\n};\n\nexport function resolveOutputFormat(outputFormat: string): SpeechSDK.SpeechSynthesisOutputFormat {\n const resolvedFormat = OUTPUT_FORMATS[outputFormat];\n if (resolvedFormat === undefined) {\n throw new Error(`Unsupported Azure Speech output format: ${outputFormat}`);\n }\n\n return resolvedFormat;\n}\n","import { synthesizeSpeech, synthesizeSsml } from \"./synthesis.ts\";\nimport type { AzureTtsClientOptions, SsmlSynthesisResult } from \"./types.ts\";\n\nconst ENDPOINT_TEMPLATE = \"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1\";\n\nexport class AzureTtsClient {\n readonly #options: AzureTtsClientOptions;\n\n constructor(options: AzureTtsClientOptions) {\n this.#options = options;\n }\n\n async synthesize(ssml: string): Promise<ArrayBuffer> {\n const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;\n const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace(\"{region}\", region);\n this.#options.logger?.debug?.(\"Using Azure TTS endpoint:\", endpoint);\n\n const config = { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs };\n return synthesizeSpeech(ssml, config);\n }\n\n async synthesizeSsml(ssml: string): Promise<SsmlSynthesisResult> {\n const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;\n const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace(\"{region}\", region);\n this.#options.logger?.debug?.(\"Using Azure TTS endpoint:\", endpoint);\n\n return synthesizeSsml(ssml, { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs });\n }\n}\n","const AZURE_VOICE_API_VERSION = \"2025-10-01\";\n\nexport interface FetchAzureVoiceCatalogOptions {\n apiKey: string;\n region: string | string[];\n}\n\nexport interface AzureVoiceCatalogVoice {\n name: string;\n locale: string;\n secondaryLocales?: readonly string[];\n styles?: readonly string[];\n regions: readonly string[];\n status?: \"ga\" | \"preview\" | \"deprecated\";\n}\n\nexport interface FetchedAzureVoiceCatalogMetadata {\n voiceCount: number;\n generatedAt: string;\n apiVersion: string;\n regions: readonly string[];\n}\n\nexport interface AzureVoiceCatalog {\n voices: readonly AzureVoiceCatalogVoice[];\n metadata: FetchedAzureVoiceCatalogMetadata;\n}\n\ninterface AzureVoiceApiRecord {\n Locale?: unknown;\n Name?: unknown;\n SecondaryLocaleList?: unknown;\n ShortName?: unknown;\n Status?: unknown;\n StyleList?: unknown;\n}\n\nfunction stringValue(value: unknown): string | undefined {\n return typeof value === \"string\" && value.trim() ? value.trim() : undefined;\n}\n\nfunction stringList(value: unknown): string[] {\n if (!Array.isArray(value)) return [];\n return [...new Set(value.map(stringValue).filter((item): item is string => item !== undefined))];\n}\n\nfunction normalizeStatus(value: unknown): AzureVoiceCatalogVoice[\"status\"] {\n const status = stringValue(value)?.toLowerCase();\n if (status === \"preview\" || status === \"deprecated\" || status === \"ga\") return status;\n return undefined;\n}\n\nfunction normalizeRegions(region: string | string[]): string[] {\n const regions = Array.isArray(region) ? region : [region];\n const result = [...new Set(regions.map((item) => item.trim()).filter(Boolean))];\n if (result.length === 0) throw new TypeError(\"At least one Azure Speech region is required.\");\n return result;\n}\n\nasync function fetchRegionVoices(region: string, apiKey: string): Promise<AzureVoiceApiRecord[]> {\n const endpoint = `https://${encodeURIComponent(region)}.tts.speech.microsoft.com/cognitiveservices/voices/list`;\n const response = await fetch(endpoint, {\n headers: {\n Accept: \"application/json\",\n \"Ocp-Apim-Subscription-Key\": apiKey,\n },\n });\n if (!response.ok) {\n throw new Error(`Azure List Voices API request failed for region \"${region}\" with HTTP ${response.status}.`);\n }\n const payload: unknown = await response.json();\n if (!Array.isArray(payload)) throw new Error(`Azure List Voices API returned an invalid response for \"${region}\".`);\n return payload.filter((item): item is AzureVoiceApiRecord => Boolean(item && typeof item === \"object\"));\n}\n\n/** Fetches and deduplicates the current Azure Speech voice catalog for one or more regions. */\nexport async function fetchAzureVoiceCatalog(options: FetchAzureVoiceCatalogOptions): Promise<AzureVoiceCatalog> {\n if (!options || typeof options.apiKey !== \"string\" || !options.apiKey.trim())\n throw new TypeError(\"An Azure Speech API key is required.\");\n const regions = normalizeRegions(options.region);\n const payloads = await Promise.all(regions.map((region) => fetchRegionVoices(region, options.apiKey)));\n const voices = new Map<string, AzureVoiceCatalogVoice>();\n\n for (let regionIndex = 0; regionIndex < payloads.length; regionIndex += 1) {\n const region = regions[regionIndex];\n for (const record of payloads[regionIndex]) {\n const name = stringValue(record.ShortName) ?? stringValue(record.Name);\n const locale = stringValue(record.Locale);\n if (!name || !locale) continue;\n const key = name.toLowerCase();\n const existing = voices.get(key);\n const secondaryLocales = stringList(record.SecondaryLocaleList);\n const styles = stringList(record.StyleList);\n const status = normalizeStatus(record.Status);\n const merged: AzureVoiceCatalogVoice = {\n name: existing?.name ?? name,\n locale: existing?.locale ?? locale,\n regions: [...new Set([...(existing?.regions ?? []), region])],\n };\n const mergedSecondaryLocales = [...new Set([...(existing?.secondaryLocales ?? []), ...secondaryLocales])];\n if (mergedSecondaryLocales.length > 0) merged.secondaryLocales = mergedSecondaryLocales;\n const mergedStyles = [...new Set([...(existing?.styles ?? []), ...styles])];\n if (mergedStyles.length > 0) merged.styles = mergedStyles;\n if (status) merged.status = status;\n else if (existing?.status) merged.status = existing.status;\n voices.set(key, merged);\n }\n }\n\n const sortedVoices = [...voices.values()].sort((first, second) => first.name.localeCompare(second.name));\n return {\n voices: sortedVoices,\n metadata: {\n voiceCount: sortedVoices.length,\n generatedAt: new Date().toISOString(),\n apiVersion: AZURE_VOICE_API_VERSION,\n regions,\n },\n };\n}\n"],"mappings":";;;;;;;;;AAAO,IAAM,gBAAN,cAA4B,MAAM;AAAA,EAMvC,YAAY,QAAgB,YAAoB,cAAsB,WAA0B;AAC9F,UAAM,6BAA6B,MAAM,IAAI,UAAU,EAAE;AACzD,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,aAAa;AAClB,SAAK,eAAe;AACpB,SAAK,YAAY;AAAA,EACnB;AACF;AAEO,IAAM,mBAAN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safe.ts","../src/client.ts","../src/voiceCatalog.ts"],"sourcesContent":["export class AzureTtsError extends Error {\n readonly status: number;\n readonly statusText: string;\n readonly responseBody: string;\n readonly requestId: string | null;\n\n constructor(status: number, statusText: string, responseBody: string, requestId: string | null) {\n super(`Azure TTS request failed: ${status} ${statusText}`);\n this.name = \"AzureTtsError\";\n this.status = status;\n this.statusText = statusText;\n this.responseBody = responseBody;\n this.requestId = requestId;\n }\n}\n\nexport class AzureTtsSdkError extends AzureTtsError {\n readonly errorDetails: string;\n\n constructor(errorDetails: string) {\n super(0, \"Speech SDK\", errorDetails, null);\n this.name = \"AzureTtsSdkError\";\n this.message = `Azure TTS synthesis failed: ${errorDetails}`;\n this.errorDetails = errorDetails;\n }\n}\n\nexport function createSpeechSdkError(error: unknown): AzureTtsSdkError {\n const message = error instanceof Error ? error.message : String(error);\n return new AzureTtsSdkError(message);\n}\n","import * as SpeechSDK from \"microsoft-cognitiveservices-speech-sdk\";\nimport { createSpeechSdkError } from \"./errors.ts\";\nimport { createSpeechConfig } from \"./speechConfig.ts\";\nimport type { SsmlSynthesisChunk, SsmlSynthesisResult, TtsConfig } from \"./types.ts\";\n\nfunction closeSpeechResources(speechConfig: SpeechSDK.SpeechConfig, synthesizer: SpeechSDK.SpeechSynthesizer): void {\n try {\n synthesizer.close();\n } catch {}\n\n try {\n speechConfig.close();\n } catch {}\n}\n\nconst ticksToMilliseconds = (ticks: number): number => Math.max(0, ticks) / 10_000;\n\nexport async function synthesizeSsml(ssml: string, config: TtsConfig): Promise<SsmlSynthesisResult> {\n if (config.signal?.aborted) {\n throw createSpeechSdkError(\"Speech synthesis was cancelled.\");\n }\n\n const speechConfig = createSpeechConfig(config);\n const synthesizer = new SpeechSDK.SpeechSynthesizer(speechConfig, null);\n\n return await new Promise<SsmlSynthesisResult>((resolve, reject) => {\n let resourcesClosed = false;\n let settled = false;\n let timeout: ReturnType<typeof setTimeout> | undefined;\n let abortHandler: (() => void) | undefined;\n const cleanup = () => {\n if (timeout) clearTimeout(timeout);\n if (abortHandler) config.signal?.removeEventListener(\"abort\", abortHandler);\n };\n const closeResources = () => {\n if (resourcesClosed) return;\n resourcesClosed = true;\n closeSpeechResources(speechConfig, synthesizer);\n };\n const rejectWithError = (error: unknown) => {\n if (settled) return;\n settled = true;\n cleanup();\n closeResources();\n reject(createSpeechSdkError(error));\n };\n\n const boundaries: SsmlSynthesisResult[\"boundaries\"] = [];\n const visemes: SsmlSynthesisResult[\"visemes\"] = [];\n const bookmarks: SsmlSynthesisResult[\"bookmarks\"] = [];\n synthesizer.wordBoundary = (_sender, event) => {\n boundaries.push({\n text: event.text,\n audioOffsetMs: ticksToMilliseconds(event.audioOffset),\n durationMs: ticksToMilliseconds(event.duration),\n });\n };\n synthesizer.visemeReceived = (_sender, event) => {\n visemes.push({ visemeId: event.visemeId, audioOffsetMs: ticksToMilliseconds(event.audioOffset) });\n };\n synthesizer.bookmarkReached = (_sender, event) => {\n bookmarks.push({ name: event.text, audioOffsetMs: ticksToMilliseconds(event.audioOffset) });\n };\n\n const cb = (result: SpeechSDK.SpeechSynthesisResult) => {\n if (settled) return;\n const { reason, errorDetails } = result;\n if (reason !== SpeechSDK.ResultReason.SynthesizingAudioCompleted) {\n const err = errorDetails || `Speech synthesis failed with reason ${reason}.`;\n rejectWithError(err);\n return;\n }\n settled = true;\n cleanup();\n closeResources();\n const eventDurationMs = Math.max(\n 0,\n ...(boundaries ?? []).map((boundary) => boundary.audioOffsetMs + boundary.durationMs),\n ...(visemes ?? []).map((viseme) => viseme.audioOffsetMs),\n ...(bookmarks ?? []).map((bookmark) => bookmark.audioOffsetMs),\n );\n const durationMs = result.audioDuration ? ticksToMilliseconds(result.audioDuration) : eventDurationMs;\n const requestId = (result as SpeechSDK.SpeechSynthesisResult & { resultId?: string }).resultId;\n const addSourceMetadata = <T extends { audioOffsetMs: number }>(event: T): T => ({\n ...event,\n ...(config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {}),\n ...(requestId ? { requestId } : {}),\n });\n const sourceBoundaries = boundaries.map((boundary) => addSourceMetadata(boundary));\n const sourceVisemes = visemes.map((viseme) => addSourceMetadata(viseme));\n const sourceBookmarks = bookmarks.map((bookmark) => addSourceMetadata(bookmark));\n resolve({\n audioData: result.audioData,\n durationMs,\n ...(config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {}),\n ...(requestId ? { requestId } : {}),\n ...(sourceBoundaries.length > 0\n ? { boundaries: sourceBoundaries, wordBoundary: sourceBoundaries, wordBoundaries: sourceBoundaries }\n : {}),\n ...(sourceVisemes.length > 0 ? { visemes: sourceVisemes } : {}),\n ...(sourceBookmarks.length > 0 ? { bookmarks: sourceBookmarks } : {}),\n });\n };\n\n try {\n if (config.signal) {\n abortHandler = () => rejectWithError(\"Speech synthesis was cancelled.\");\n config.signal.addEventListener(\"abort\", abortHandler, { once: true });\n }\n if (config.timeoutMs !== undefined && config.timeoutMs > 0) {\n timeout = setTimeout(\n () => rejectWithError(`Speech synthesis timed out after ${config.timeoutMs} ms.`),\n config.timeoutMs,\n );\n }\n synthesizer.speakSsmlAsync(ssml, cb, rejectWithError);\n } catch (error) {\n rejectWithError(error);\n }\n });\n}\n\n/** Synthesizes chunks sequentially, annotates synchronization events, and merges the results. */\nexport async function synthesizeSsmlChunks(\n chunks: readonly (SsmlSynthesisChunk | string)[],\n config: TtsConfig,\n): Promise<SsmlSynthesisResult> {\n const results: SsmlSynthesisResult[] = [];\n const totalChunks = chunks.length;\n for (const [index, chunk] of chunks.entries()) {\n const input = typeof chunk === \"string\" ? { ssml: chunk } : chunk;\n const result = await synthesizeSsml(input.ssml, {\n ...config,\n ...(input.originalTextRange ? { sourceTextRange: input.originalTextRange } : {}),\n onProgress: undefined,\n });\n results.push(result);\n config.onProgress?.({\n currentChunk: index + 1,\n totalChunks,\n percent: totalChunks === 0 ? 100 : Math.round(((index + 1) / totalChunks) * 100),\n });\n }\n return mergeSynthesisResults(results);\n}\n\n/** Concatenates audio buffers and shifts all synchronization events by prior chunk durations. */\nexport function mergeSynthesisResults(results: readonly SsmlSynthesisResult[]): SsmlSynthesisResult {\n const audioLength = results.reduce((total, result) => total + result.audioData.byteLength, 0);\n const audioData = new Uint8Array(audioLength);\n const boundaries: NonNullable<SsmlSynthesisResult[\"boundaries\"]> = [];\n const visemes: NonNullable<SsmlSynthesisResult[\"visemes\"]> = [];\n const bookmarks: NonNullable<SsmlSynthesisResult[\"bookmarks\"]> = [];\n let byteOffset = 0;\n let durationOffset = 0;\n\n for (const result of results) {\n audioData.set(new Uint8Array(result.audioData), byteOffset);\n byteOffset += result.audioData.byteLength;\n const chunkBoundaries =\n result.boundaries && result.boundaries.length > 0\n ? result.boundaries\n : (result.wordBoundary ?? result.wordBoundaries ?? []);\n for (const boundary of chunkBoundaries) {\n const textRange = boundary.textRange ?? result.textRange;\n const requestId = boundary.requestId ?? result.requestId;\n boundaries.push({\n ...boundary,\n audioOffsetMs: boundary.audioOffsetMs + durationOffset,\n ...(textRange ? { textRange: { ...textRange } } : {}),\n ...(requestId ? { requestId } : {}),\n });\n }\n for (const viseme of result.visemes ?? []) {\n const textRange = viseme.textRange ?? result.textRange;\n const requestId = viseme.requestId ?? result.requestId;\n visemes.push({\n ...viseme,\n audioOffsetMs: viseme.audioOffsetMs + durationOffset,\n ...(textRange ? { textRange: { ...textRange } } : {}),\n ...(requestId ? { requestId } : {}),\n });\n }\n for (const bookmark of result.bookmarks ?? []) {\n const textRange = bookmark.textRange ?? result.textRange;\n const requestId = bookmark.requestId ?? result.requestId;\n bookmarks.push({\n ...bookmark,\n audioOffsetMs: bookmark.audioOffsetMs + durationOffset,\n ...(textRange ? { textRange: { ...textRange } } : {}),\n ...(requestId ? { requestId } : {}),\n });\n }\n durationOffset += Math.max(0, result.durationMs);\n }\n\n return {\n audioData: audioData.buffer,\n durationMs: durationOffset,\n ...(boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {}),\n ...(visemes.length > 0 ? { visemes } : {}),\n ...(bookmarks.length > 0 ? { bookmarks } : {}),\n ...(results.length === 1 && results[0]?.requestId ? { requestId: results[0].requestId } : {}),\n ...(results.length === 1 && results[0]?.textRange ? { textRange: { ...results[0].textRange } } : {}),\n };\n}\n\n/** Backward-compatible audio-only synthesis helper. */\nexport async function synthesizeSpeech(ssml: string, config: TtsConfig): Promise<ArrayBuffer> {\n return (await synthesizeSsml(ssml, config)).audioData;\n}\n","import { SpeechConfig } from \"microsoft-cognitiveservices-speech-sdk\";\nimport { DEFAULT_OUTPUT_FORMAT, resolveOutputFormat } from \"./outputFormats.ts\";\nimport type { TtsConfig } from \"./types.ts\";\n\nexport function resolveEndpoint(config: TtsConfig): string {\n const endpoint = config.endpoint?.trim() || \"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1\";\n return endpoint.replace(/\\{region\\}/g, encodeURIComponent(config.region));\n}\n\nexport function createSpeechConfig(config: TtsConfig): SpeechConfig {\n const { outputFormat = DEFAULT_OUTPUT_FORMAT, subscriptionKey } = config;\n\n const endpoint = new URL(resolveEndpoint(config));\n const speechConfig = SpeechConfig.fromEndpoint(endpoint, subscriptionKey);\n speechConfig.speechSynthesisOutputFormat = resolveOutputFormat(outputFormat);\n return speechConfig;\n}\n","import * as SpeechSDK from \"microsoft-cognitiveservices-speech-sdk\";\n\nexport const DEFAULT_OUTPUT_FORMAT = \"audio-16khz-128kbitrate-mono-mp3\";\n\nconst OUTPUT_FORMATS: Record<string, SpeechSDK.SpeechSynthesisOutputFormat> = {\n \"raw-8khz-8bit-mono-mulaw\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz8BitMonoMULaw,\n \"riff-16khz-16kbps-mono-siren\": SpeechSDK.SpeechSynthesisOutputFormat.Riff16Khz16KbpsMonoSiren,\n \"audio-16khz-16kbps-mono-siren\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz16KbpsMonoSiren,\n \"audio-16khz-32kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3,\n \"audio-16khz-128kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz128KBitRateMonoMp3,\n \"audio-16khz-64kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz64KBitRateMonoMp3,\n \"audio-24khz-48kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3,\n \"audio-24khz-96kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz96KBitRateMonoMp3,\n \"audio-24khz-160kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3,\n \"raw-16khz-16bit-mono-truesilk\": SpeechSDK.SpeechSynthesisOutputFormat.Raw16Khz16BitMonoTrueSilk,\n \"riff-16khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff16Khz16BitMonoPcm,\n \"riff-8khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz16BitMonoPcm,\n \"riff-24khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm,\n \"riff-8khz-8bit-mono-mulaw\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz8BitMonoMULaw,\n \"raw-16khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm,\n \"raw-24khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw24Khz16BitMonoPcm,\n \"raw-8khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz16BitMonoPcm,\n \"ogg-16khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg16Khz16BitMonoOpus,\n \"ogg-24khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg24Khz16BitMonoOpus,\n \"raw-48khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw48Khz16BitMonoPcm,\n \"riff-48khz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff48Khz16BitMonoPcm,\n \"audio-48khz-96kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio48Khz96KBitRateMonoMp3,\n \"audio-48khz-192kbitrate-mono-mp3\": SpeechSDK.SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3,\n \"ogg-48khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Ogg48Khz16BitMonoOpus,\n \"webm-16khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm16Khz16BitMonoOpus,\n \"webm-24khz-16bit-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm24Khz16BitMonoOpus,\n \"webm-24khz-16bit-24kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Webm24Khz16Bit24KbpsMonoOpus,\n \"raw-24khz-16bit-mono-truesilk\": SpeechSDK.SpeechSynthesisOutputFormat.Raw24Khz16BitMonoTrueSilk,\n \"raw-8khz-8bit-mono-alaw\": SpeechSDK.SpeechSynthesisOutputFormat.Raw8Khz8BitMonoALaw,\n \"riff-8khz-8bit-mono-alaw\": SpeechSDK.SpeechSynthesisOutputFormat.Riff8Khz8BitMonoALaw,\n \"audio-16khz-16bit-32kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio16Khz16Bit32KbpsMonoOpus,\n \"audio-24khz-16bit-48kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz16Bit48KbpsMonoOpus,\n \"audio-24khz-16bit-24kbps-mono-opus\": SpeechSDK.SpeechSynthesisOutputFormat.Audio24Khz16Bit24KbpsMonoOpus,\n \"raw-22050hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw22050Hz16BitMonoPcm,\n \"riff-22050hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff22050Hz16BitMonoPcm,\n \"raw-44100hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Raw44100Hz16BitMonoPcm,\n \"riff-44100hz-16bit-mono-pcm\": SpeechSDK.SpeechSynthesisOutputFormat.Riff44100Hz16BitMonoPcm,\n \"amr-wb-16000hz\": SpeechSDK.SpeechSynthesisOutputFormat.AmrWb16000Hz,\n \"g722-16khz-64kbps\": SpeechSDK.SpeechSynthesisOutputFormat.G72216Khz64Kbps,\n};\n\nexport function resolveOutputFormat(outputFormat: string): SpeechSDK.SpeechSynthesisOutputFormat {\n const resolvedFormat = OUTPUT_FORMATS[outputFormat];\n if (resolvedFormat === undefined) {\n throw new Error(`Unsupported Azure Speech output format: ${outputFormat}`);\n }\n\n return resolvedFormat;\n}\n","import { validateAzureSsml, type AzureValidationOptions, type SsmlDiagnostic } from \"@ssml-builder-js/ssml-core\";\nimport { AzureTtsError, createSpeechSdkError } from \"./errors.ts\";\nimport type { AzureTtsClient } from \"./client.ts\";\nimport type { SsmlSynthesisResult } from \"./types.ts\";\n\nexport interface SsmlValidationError {\n readonly kind: \"validation\";\n readonly message: string;\n readonly diagnostics: readonly SsmlDiagnostic[];\n}\n\nexport type Result<T, E> =\n | { readonly ok: true; readonly success: true; readonly status: \"success\"; readonly value: T }\n | {\n readonly ok: false;\n readonly success: false;\n readonly status: \"validation-error\" | \"azure-api-error\";\n readonly error: E;\n };\n\nexport type SynthesisResult<T, E> = Result<T, E>;\n\nexport type Success<T> = Extract<Result<T, never>, { readonly ok: true }>;\nexport type ValidationErrorResult = Extract<\n Result<never, SsmlValidationError>,\n { readonly status: \"validation-error\" }\n>;\nexport type AzureApiErrorResult = Extract<Result<never, AzureTtsError>, { readonly status: \"azure-api-error\" }>;\n\nexport type SsmlSynthesisSafeResult =\n | Result<SsmlSynthesisResult, never>\n | Result<never, SsmlValidationError>\n | Result<never, AzureTtsError>;\n\nexport interface SynthesizeSsmlSafeOptions extends AzureValidationOptions {\n /** Optional nested form for callers that want to keep validation settings grouped. */\n validation?: AzureValidationOptions;\n}\n\ninterface SynthesisClient {\n synthesizeSsml(ssml: string): Promise<SsmlSynthesisResult>;\n}\n\n/** Validates SSML before invoking Azure and converts validation/API failures to one result shape. */\nexport async function synthesizeSsmlSafe(\n client: Pick<AzureTtsClient, \"synthesizeSsml\"> | SynthesisClient,\n ssml: string,\n options: SynthesizeSsmlSafeOptions = {},\n): Promise<SsmlSynthesisSafeResult> {\n const validationOptions = options.validation ?? options;\n const diagnostics = await Promise.resolve(validateAzureSsml(ssml, validationOptions));\n const errors = diagnostics.filter((diagnostic) => diagnostic.severity === \"error\");\n if (errors.length > 0) {\n return {\n ok: false,\n success: false,\n status: \"validation-error\",\n error: {\n kind: \"validation\",\n message: \"SSML validation failed; the Azure Speech API was not called.\",\n diagnostics: errors,\n },\n };\n }\n\n try {\n return { ok: true, success: true, status: \"success\", value: await client.synthesizeSsml(ssml) };\n } catch (error) {\n const azureError = error instanceof AzureTtsError ? error : createSpeechSdkError(error);\n return { ok: false, success: false, status: \"azure-api-error\", error: azureError };\n }\n}\n","import { synthesizeSpeech, synthesizeSsml, synthesizeSsmlChunks } from \"./synthesis.ts\";\nimport { synthesizeSsmlSafe } from \"./safe.ts\";\nimport type { SynthesizeSsmlSafeOptions } from \"./safe.ts\";\nimport type {\n AzureTtsClientOptions,\n SsmlSynthesisChunk,\n SsmlSynthesisResult,\n SynthesizeChunksOptions,\n} from \"./types.ts\";\n\nconst ENDPOINT_TEMPLATE = \"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1\";\n\nexport class AzureTtsClient {\n readonly #options: AzureTtsClientOptions;\n\n constructor(options: AzureTtsClientOptions) {\n this.#options = options;\n }\n\n async synthesize(ssml: string): Promise<ArrayBuffer> {\n const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;\n const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace(\"{region}\", region);\n this.#options.logger?.debug?.(\"Using Azure TTS endpoint:\", endpoint);\n\n const config = { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs };\n return synthesizeSpeech(ssml, config);\n }\n\n async synthesizeSsml(ssml: string): Promise<SsmlSynthesisResult> {\n const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;\n const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace(\"{region}\", region);\n this.#options.logger?.debug?.(\"Using Azure TTS endpoint:\", endpoint);\n\n return synthesizeSsml(ssml, { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs });\n }\n\n async synthesizeChunks(\n chunks: readonly (SsmlSynthesisChunk | string)[],\n options: SynthesizeChunksOptions = {},\n ): Promise<SsmlSynthesisResult> {\n const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;\n const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace(\"{region}\", region);\n return synthesizeSsmlChunks(chunks, {\n endpoint,\n region,\n subscriptionKey,\n outputFormat,\n signal,\n timeoutMs,\n onProgress: options.onProgress ?? this.#options.onProgress,\n });\n }\n\n async synthesizeSsmlSafe(ssml: string, options: SynthesizeSsmlSafeOptions = {}) {\n return synthesizeSsmlSafe(this, ssml, options);\n }\n}\n","const AZURE_VOICE_API_VERSION = \"2025-10-01\";\n\nexport interface FetchAzureVoiceCatalogOptions {\n apiKey: string;\n region: string | string[];\n}\n\nexport interface AzureVoiceCatalogVoice {\n name: string;\n locale: string;\n secondaryLocales?: readonly string[];\n styles?: readonly string[];\n regions: readonly string[];\n status?: \"ga\" | \"preview\" | \"deprecated\";\n}\n\nexport interface FetchedAzureVoiceCatalogMetadata {\n voiceCount: number;\n generatedAt: string;\n apiVersion: string;\n regions: readonly string[];\n}\n\nexport interface AzureVoiceCatalog {\n voices: readonly AzureVoiceCatalogVoice[];\n metadata: FetchedAzureVoiceCatalogMetadata;\n}\n\ninterface AzureVoiceApiRecord {\n Locale?: unknown;\n Name?: unknown;\n SecondaryLocaleList?: unknown;\n ShortName?: unknown;\n Status?: unknown;\n StyleList?: unknown;\n}\n\nfunction stringValue(value: unknown): string | undefined {\n return typeof value === \"string\" && value.trim() ? value.trim() : undefined;\n}\n\nfunction stringList(value: unknown): string[] {\n if (!Array.isArray(value)) return [];\n return [...new Set(value.map(stringValue).filter((item): item is string => item !== undefined))];\n}\n\nfunction normalizeStatus(value: unknown): AzureVoiceCatalogVoice[\"status\"] {\n const status = stringValue(value)?.toLowerCase();\n if (status === \"preview\" || status === \"deprecated\" || status === \"ga\") return status;\n return undefined;\n}\n\nfunction normalizeRegions(region: string | string[]): string[] {\n const regions = Array.isArray(region) ? region : [region];\n const result = [...new Set(regions.map((item) => item.trim()).filter(Boolean))];\n if (result.length === 0) throw new TypeError(\"At least one Azure Speech region is required.\");\n return result;\n}\n\nasync function fetchRegionVoices(region: string, apiKey: string): Promise<AzureVoiceApiRecord[]> {\n const endpoint = `https://${encodeURIComponent(region)}.tts.speech.microsoft.com/cognitiveservices/voices/list`;\n const response = await fetch(endpoint, {\n headers: {\n Accept: \"application/json\",\n \"Ocp-Apim-Subscription-Key\": apiKey,\n },\n });\n if (!response.ok) {\n throw new Error(`Azure List Voices API request failed for region \"${region}\" with HTTP ${response.status}.`);\n }\n const payload: unknown = await response.json();\n if (!Array.isArray(payload)) throw new Error(`Azure List Voices API returned an invalid response for \"${region}\".`);\n return payload.filter((item): item is AzureVoiceApiRecord => Boolean(item && typeof item === \"object\"));\n}\n\n/** Fetches and deduplicates the current Azure Speech voice catalog for one or more regions. */\nexport async function fetchAzureVoiceCatalog(options: FetchAzureVoiceCatalogOptions): Promise<AzureVoiceCatalog> {\n if (!options || typeof options.apiKey !== \"string\" || !options.apiKey.trim())\n throw new TypeError(\"An Azure Speech API key is required.\");\n const regions = normalizeRegions(options.region);\n const payloads = await Promise.all(regions.map((region) => fetchRegionVoices(region, options.apiKey)));\n const voices = new Map<string, AzureVoiceCatalogVoice>();\n\n for (let regionIndex = 0; regionIndex < payloads.length; regionIndex += 1) {\n const region = regions[regionIndex];\n for (const record of payloads[regionIndex]) {\n const name = stringValue(record.ShortName) ?? stringValue(record.Name);\n const locale = stringValue(record.Locale);\n if (!name || !locale) continue;\n const key = name.toLowerCase();\n const existing = voices.get(key);\n const secondaryLocales = stringList(record.SecondaryLocaleList);\n const styles = stringList(record.StyleList);\n const status = normalizeStatus(record.Status);\n const merged: AzureVoiceCatalogVoice = {\n name: existing?.name ?? name,\n locale: existing?.locale ?? locale,\n regions: [...new Set([...(existing?.regions ?? []), region])],\n };\n const mergedSecondaryLocales = [...new Set([...(existing?.secondaryLocales ?? []), ...secondaryLocales])];\n if (mergedSecondaryLocales.length > 0) merged.secondaryLocales = mergedSecondaryLocales;\n const mergedStyles = [...new Set([...(existing?.styles ?? []), ...styles])];\n if (mergedStyles.length > 0) merged.styles = mergedStyles;\n if (status) merged.status = status;\n else if (existing?.status) merged.status = existing.status;\n voices.set(key, merged);\n }\n }\n\n const sortedVoices = [...voices.values()].sort((first, second) => first.name.localeCompare(second.name));\n return {\n voices: sortedVoices,\n metadata: {\n voiceCount: sortedVoices.length,\n generatedAt: new Date().toISOString(),\n apiVersion: AZURE_VOICE_API_VERSION,\n regions,\n },\n };\n}\n"],"mappings":";;;;;;;;;AAAO,IAAM,gBAAN,cAA4B,MAAM;AAAA,EAMvC,YAAY,QAAgB,YAAoB,cAAsB,WAA0B;AAC9F,UAAM,6BAA6B,MAAM,IAAI,UAAU,EAAE;AACzD,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,aAAa;AAClB,SAAK,eAAe;AACpB,SAAK,YAAY;AAAA,EACnB;AACF;AAEO,IAAM,mBAAN,cAA+B,cAAc;AAAA,EAGlD,YAAY,cAAsB;AAChC,UAAM,GAAG,cAAc,cAAc,IAAI;AACzC,SAAK,OAAO;AACZ,SAAK,UAAU,+BAA+B,YAAY;AAC1D,SAAK,eAAe;AAAA,EACtB;AACF;AAEO,SAAS,qBAAqB,OAAkC;AACrE,QAAM,UAAU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SAAS;AAAA,eACnB,UAAU,OAAQ,QAAO,SAAS,SAAS;AACpD,aAAO,IAAI,KAAK,MAAM;AAAA,IACxB;AAAA,EACF;AAEA,QAAM,eAAe,CAAC,GAAG,OAAO,OAAO,CAAC,EAAE,KAAK,CAAC,OAAO,WAAW,MAAM,KAAK,cAAc,OAAO,IAAI,CAAC;AACvG,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,UAAU;AAAA,MACR,YAAY,aAAa;AAAA,MACzB,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,MACpC,YAAY;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AACF;","names":["SpeechSDK"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssml-builder-js/azure-tts-client",
3
- "version": "2.12.0",
3
+ "version": "2.13.0",
4
4
  "description": "Azure Text-to-Speech client using the Microsoft Speech SDK",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,6 +35,7 @@
35
35
  "typescript": "^6.0.3"
36
36
  },
37
37
  "dependencies": {
38
+ "@ssml-builder-js/ssml-core": "^2.13.0",
38
39
  "microsoft-cognitiveservices-speech-sdk": "1.51.0"
39
40
  }
40
41
  }
package/src/client.ts CHANGED
@@ -1,5 +1,12 @@
1
- import { synthesizeSpeech, synthesizeSsml } from "./synthesis.ts";
2
- import type { AzureTtsClientOptions, SsmlSynthesisResult } from "./types.ts";
1
+ import { synthesizeSpeech, synthesizeSsml, synthesizeSsmlChunks } from "./synthesis.ts";
2
+ import { synthesizeSsmlSafe } from "./safe.ts";
3
+ import type { SynthesizeSsmlSafeOptions } from "./safe.ts";
4
+ import type {
5
+ AzureTtsClientOptions,
6
+ SsmlSynthesisChunk,
7
+ SsmlSynthesisResult,
8
+ SynthesizeChunksOptions,
9
+ } from "./types.ts";
3
10
 
4
11
  const ENDPOINT_TEMPLATE = "https://{region}.tts.speech.microsoft.com/cognitiveservices/v1";
5
12
 
@@ -26,4 +33,25 @@ export class AzureTtsClient {
26
33
 
27
34
  return synthesizeSsml(ssml, { endpoint, region, subscriptionKey, outputFormat, signal, timeoutMs });
28
35
  }
36
+
37
+ async synthesizeChunks(
38
+ chunks: readonly (SsmlSynthesisChunk | string)[],
39
+ options: SynthesizeChunksOptions = {},
40
+ ): Promise<SsmlSynthesisResult> {
41
+ const { region, subscriptionKey, outputFormat, signal, timeoutMs } = this.#options;
42
+ const endpoint = this.#options.endpoint?.trim() || ENDPOINT_TEMPLATE.replace("{region}", region);
43
+ return synthesizeSsmlChunks(chunks, {
44
+ endpoint,
45
+ region,
46
+ subscriptionKey,
47
+ outputFormat,
48
+ signal,
49
+ timeoutMs,
50
+ onProgress: options.onProgress ?? this.#options.onProgress,
51
+ });
52
+ }
53
+
54
+ async synthesizeSsmlSafe(ssml: string, options: SynthesizeSsmlSafeOptions = {}) {
55
+ return synthesizeSsmlSafe(this, ssml, options);
56
+ }
29
57
  }
package/src/index.ts CHANGED
@@ -9,12 +9,27 @@ export type {
9
9
  SsmlSynthesisBoundary,
10
10
  SsmlSynthesisResult,
11
11
  SsmlSynthesisViseme,
12
+ SsmlSynthesisChunk,
13
+ SynthesisProgressEvent,
14
+ SynthesizeChunksOptions,
12
15
  TtsConfig,
13
16
  } from "./types.ts";
14
17
  export { AzureTtsError, AzureTtsSdkError } from "./errors.ts";
15
18
  export { AzureTtsClient } from "./client.ts";
16
19
  export { synthesizeSpeech } from "./synthesis.ts";
17
20
  export { synthesizeSsml } from "./synthesis.ts";
21
+ export { mergeSynthesisResults, synthesizeSsmlChunks } from "./synthesis.ts";
22
+ export { synthesizeSsmlSafe } from "./safe.ts";
23
+ export type {
24
+ AzureApiErrorResult,
25
+ Result,
26
+ SsmlSynthesisSafeResult,
27
+ SsmlValidationError as AzureSsmlValidationError,
28
+ Success,
29
+ SynthesisResult,
30
+ SynthesizeSsmlSafeOptions,
31
+ ValidationErrorResult,
32
+ } from "./safe.ts";
18
33
  export { fetchAzureVoiceCatalog } from "./voiceCatalog.ts";
19
34
  export type {
20
35
  AzureVoiceCatalog,
package/src/safe.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { validateAzureSsml, type AzureValidationOptions, type SsmlDiagnostic } from "@ssml-builder-js/ssml-core";
2
+ import { AzureTtsError, createSpeechSdkError } from "./errors.ts";
3
+ import type { AzureTtsClient } from "./client.ts";
4
+ import type { SsmlSynthesisResult } from "./types.ts";
5
+
6
+ export interface SsmlValidationError {
7
+ readonly kind: "validation";
8
+ readonly message: string;
9
+ readonly diagnostics: readonly SsmlDiagnostic[];
10
+ }
11
+
12
+ export type Result<T, E> =
13
+ | { readonly ok: true; readonly success: true; readonly status: "success"; readonly value: T }
14
+ | {
15
+ readonly ok: false;
16
+ readonly success: false;
17
+ readonly status: "validation-error" | "azure-api-error";
18
+ readonly error: E;
19
+ };
20
+
21
+ export type SynthesisResult<T, E> = Result<T, E>;
22
+
23
+ export type Success<T> = Extract<Result<T, never>, { readonly ok: true }>;
24
+ export type ValidationErrorResult = Extract<
25
+ Result<never, SsmlValidationError>,
26
+ { readonly status: "validation-error" }
27
+ >;
28
+ export type AzureApiErrorResult = Extract<Result<never, AzureTtsError>, { readonly status: "azure-api-error" }>;
29
+
30
+ export type SsmlSynthesisSafeResult =
31
+ | Result<SsmlSynthesisResult, never>
32
+ | Result<never, SsmlValidationError>
33
+ | Result<never, AzureTtsError>;
34
+
35
+ export interface SynthesizeSsmlSafeOptions extends AzureValidationOptions {
36
+ /** Optional nested form for callers that want to keep validation settings grouped. */
37
+ validation?: AzureValidationOptions;
38
+ }
39
+
40
+ interface SynthesisClient {
41
+ synthesizeSsml(ssml: string): Promise<SsmlSynthesisResult>;
42
+ }
43
+
44
+ /** Validates SSML before invoking Azure and converts validation/API failures to one result shape. */
45
+ export async function synthesizeSsmlSafe(
46
+ client: Pick<AzureTtsClient, "synthesizeSsml"> | SynthesisClient,
47
+ ssml: string,
48
+ options: SynthesizeSsmlSafeOptions = {},
49
+ ): Promise<SsmlSynthesisSafeResult> {
50
+ const validationOptions = options.validation ?? options;
51
+ const diagnostics = await Promise.resolve(validateAzureSsml(ssml, validationOptions));
52
+ const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
53
+ if (errors.length > 0) {
54
+ return {
55
+ ok: false,
56
+ success: false,
57
+ status: "validation-error",
58
+ error: {
59
+ kind: "validation",
60
+ message: "SSML validation failed; the Azure Speech API was not called.",
61
+ diagnostics: errors,
62
+ },
63
+ };
64
+ }
65
+
66
+ try {
67
+ return { ok: true, success: true, status: "success", value: await client.synthesizeSsml(ssml) };
68
+ } catch (error) {
69
+ const azureError = error instanceof AzureTtsError ? error : createSpeechSdkError(error);
70
+ return { ok: false, success: false, status: "azure-api-error", error: azureError };
71
+ }
72
+ }
package/src/synthesis.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as SpeechSDK from "microsoft-cognitiveservices-speech-sdk";
2
2
  import { createSpeechSdkError } from "./errors.ts";
3
3
  import { createSpeechConfig } from "./speechConfig.ts";
4
- import type { SsmlSynthesisResult, TtsConfig } from "./types.ts";
4
+ import type { SsmlSynthesisChunk, SsmlSynthesisResult, TtsConfig } from "./types.ts";
5
5
 
6
6
  function closeSpeechResources(speechConfig: SpeechSDK.SpeechConfig, synthesizer: SpeechSDK.SpeechSynthesizer): void {
7
7
  try {
@@ -80,12 +80,25 @@ export async function synthesizeSsml(ssml: string, config: TtsConfig): Promise<S
80
80
  ...(bookmarks ?? []).map((bookmark) => bookmark.audioOffsetMs),
81
81
  );
82
82
  const durationMs = result.audioDuration ? ticksToMilliseconds(result.audioDuration) : eventDurationMs;
83
+ const requestId = (result as SpeechSDK.SpeechSynthesisResult & { resultId?: string }).resultId;
84
+ const addSourceMetadata = <T extends { audioOffsetMs: number }>(event: T): T => ({
85
+ ...event,
86
+ ...(config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {}),
87
+ ...(requestId ? { requestId } : {}),
88
+ });
89
+ const sourceBoundaries = boundaries.map((boundary) => addSourceMetadata(boundary));
90
+ const sourceVisemes = visemes.map((viseme) => addSourceMetadata(viseme));
91
+ const sourceBookmarks = bookmarks.map((bookmark) => addSourceMetadata(bookmark));
83
92
  resolve({
84
93
  audioData: result.audioData,
85
94
  durationMs,
86
- ...(boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {}),
87
- ...(visemes.length > 0 ? { visemes } : {}),
88
- ...(bookmarks.length > 0 ? { bookmarks } : {}),
95
+ ...(config.sourceTextRange ? { textRange: { ...config.sourceTextRange } } : {}),
96
+ ...(requestId ? { requestId } : {}),
97
+ ...(sourceBoundaries.length > 0
98
+ ? { boundaries: sourceBoundaries, wordBoundary: sourceBoundaries, wordBoundaries: sourceBoundaries }
99
+ : {}),
100
+ ...(sourceVisemes.length > 0 ? { visemes: sourceVisemes } : {}),
101
+ ...(sourceBookmarks.length > 0 ? { bookmarks: sourceBookmarks } : {}),
89
102
  });
90
103
  };
91
104
 
@@ -107,6 +120,91 @@ export async function synthesizeSsml(ssml: string, config: TtsConfig): Promise<S
107
120
  });
108
121
  }
109
122
 
123
+ /** Synthesizes chunks sequentially, annotates synchronization events, and merges the results. */
124
+ export async function synthesizeSsmlChunks(
125
+ chunks: readonly (SsmlSynthesisChunk | string)[],
126
+ config: TtsConfig,
127
+ ): Promise<SsmlSynthesisResult> {
128
+ const results: SsmlSynthesisResult[] = [];
129
+ const totalChunks = chunks.length;
130
+ for (const [index, chunk] of chunks.entries()) {
131
+ const input = typeof chunk === "string" ? { ssml: chunk } : chunk;
132
+ const result = await synthesizeSsml(input.ssml, {
133
+ ...config,
134
+ ...(input.originalTextRange ? { sourceTextRange: input.originalTextRange } : {}),
135
+ onProgress: undefined,
136
+ });
137
+ results.push(result);
138
+ config.onProgress?.({
139
+ currentChunk: index + 1,
140
+ totalChunks,
141
+ percent: totalChunks === 0 ? 100 : Math.round(((index + 1) / totalChunks) * 100),
142
+ });
143
+ }
144
+ return mergeSynthesisResults(results);
145
+ }
146
+
147
+ /** Concatenates audio buffers and shifts all synchronization events by prior chunk durations. */
148
+ export function mergeSynthesisResults(results: readonly SsmlSynthesisResult[]): SsmlSynthesisResult {
149
+ const audioLength = results.reduce((total, result) => total + result.audioData.byteLength, 0);
150
+ const audioData = new Uint8Array(audioLength);
151
+ const boundaries: NonNullable<SsmlSynthesisResult["boundaries"]> = [];
152
+ const visemes: NonNullable<SsmlSynthesisResult["visemes"]> = [];
153
+ const bookmarks: NonNullable<SsmlSynthesisResult["bookmarks"]> = [];
154
+ let byteOffset = 0;
155
+ let durationOffset = 0;
156
+
157
+ for (const result of results) {
158
+ audioData.set(new Uint8Array(result.audioData), byteOffset);
159
+ byteOffset += result.audioData.byteLength;
160
+ const chunkBoundaries =
161
+ result.boundaries && result.boundaries.length > 0
162
+ ? result.boundaries
163
+ : (result.wordBoundary ?? result.wordBoundaries ?? []);
164
+ for (const boundary of chunkBoundaries) {
165
+ const textRange = boundary.textRange ?? result.textRange;
166
+ const requestId = boundary.requestId ?? result.requestId;
167
+ boundaries.push({
168
+ ...boundary,
169
+ audioOffsetMs: boundary.audioOffsetMs + durationOffset,
170
+ ...(textRange ? { textRange: { ...textRange } } : {}),
171
+ ...(requestId ? { requestId } : {}),
172
+ });
173
+ }
174
+ for (const viseme of result.visemes ?? []) {
175
+ const textRange = viseme.textRange ?? result.textRange;
176
+ const requestId = viseme.requestId ?? result.requestId;
177
+ visemes.push({
178
+ ...viseme,
179
+ audioOffsetMs: viseme.audioOffsetMs + durationOffset,
180
+ ...(textRange ? { textRange: { ...textRange } } : {}),
181
+ ...(requestId ? { requestId } : {}),
182
+ });
183
+ }
184
+ for (const bookmark of result.bookmarks ?? []) {
185
+ const textRange = bookmark.textRange ?? result.textRange;
186
+ const requestId = bookmark.requestId ?? result.requestId;
187
+ bookmarks.push({
188
+ ...bookmark,
189
+ audioOffsetMs: bookmark.audioOffsetMs + durationOffset,
190
+ ...(textRange ? { textRange: { ...textRange } } : {}),
191
+ ...(requestId ? { requestId } : {}),
192
+ });
193
+ }
194
+ durationOffset += Math.max(0, result.durationMs);
195
+ }
196
+
197
+ return {
198
+ audioData: audioData.buffer,
199
+ durationMs: durationOffset,
200
+ ...(boundaries.length > 0 ? { boundaries, wordBoundary: boundaries, wordBoundaries: boundaries } : {}),
201
+ ...(visemes.length > 0 ? { visemes } : {}),
202
+ ...(bookmarks.length > 0 ? { bookmarks } : {}),
203
+ ...(results.length === 1 && results[0]?.requestId ? { requestId: results[0].requestId } : {}),
204
+ ...(results.length === 1 && results[0]?.textRange ? { textRange: { ...results[0].textRange } } : {}),
205
+ };
206
+ }
207
+
110
208
  /** Backward-compatible audio-only synthesis helper. */
111
209
  export async function synthesizeSpeech(ssml: string, config: TtsConfig): Promise<ArrayBuffer> {
112
210
  return (await synthesizeSsml(ssml, config)).audioData;