import {z} from 'zod' export type ConvertTextToSpeechParameters = {pitch_shift?: number | undefined, pitch_variance?: number | undefined, speed?: number | undefined, duration?: number | undefined, similarity?: number | undefined, text_guidance?: number | undefined, subharmonic_amplitude_control?: number | undefined}; export type PredictTTSDurationRequest = {text: string, language: 'en' | 'ko' | 'ja' | 'bg' | 'cs' | 'da' | 'el' | 'es' | 'et' | 'fi' | 'hu' | 'it' | 'nl' | 'pl' | 'pt' | 'ro' | 'ar' | 'de' | 'fr' | 'hi' | 'id' | 'ru' | 'vi' | 'hr' | 'lt' | 'lv' | 'sk' | 'sl' | 'sv' | 'tr' | 'uk', style?: string | undefined, model?: ('sona_speech_1' | 'sona_speech_2' | 'sona_speech_2_flash' | 'supertonic_api_1' | 'supertonic_api_3') | undefined, output_format?: ('wav' | 'mp3') | undefined, voice_settings?: ConvertTextToSpeechParameters | undefined}; export type CreateApiV1PredictDurationVoiceIdArgs = {voice_id: string, body: PredictTTSDurationRequest}; export type CreateApiV1PredictDurationVoiceIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1PredictDurationVoiceIdResponse = z.object({duration: z.number().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Predict text-to-speech duration * * POST /v1/predict-duration/{voice_id} */ export const createApiV1PredictDurationVoiceId = async (args: CreateApiV1PredictDurationVoiceIdArgs, options?: CreateApiV1PredictDurationVoiceIdOptions) => { const {baseUrl = "https://supertoneapi.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-sup-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/predict-duration/${encodeURIComponent(String(args.voice_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1PredictDurationVoiceIdResponse, await res.json()) };