xsAI v0.4 "AIAIAI" is now available! Read Announcement
xsAI0.5.0-beta.6
Streaming

Speech

Streams audio from the input text.

install sizeminified sizeminzipped size
npm i @xsai/stream-speech

Examples

Basic

import { streamSpeech } from '@xsai/stream-speech'
import { env } from 'node:process'
import PCMPlayer from 'pcm-player'

const { fullStream, usage } = await streamSpeech({
  apiKey: env.OPENAI_API_KEY!,
  baseURL: 'https://api.openai.com/v1/',
  input: 'The quick brown fox jumped over the lazy dog.',
  model: 'gpt-4o-mini-tts',
  voice: 'alloy',
  responseFormat: 'pcm',
})

const player = new PCMPlayer({
  inputCodec: 'Int16',
  channels: 1,
  sampleRate: 24000,
})

for (const bytes of fullStream)
  player.feed(toArrayBuffer(bytes.buffer))

console.log(await usage)

On this page