@palabra-ai/translator
v0.0.11
Published
๐ A TypeScript library for Palabra AI's real-time speech-to-speech translation API. ๐ Break down language barriers and enable seamless communication across 25+ languages.
Readme
Palabra AI TypeScript Library
๐ A TypeScript library for Palabra AI's real-time speech-to-speech translation API. ๐ Break down language barriers and enable seamless communication across 25+ languages.
Overview ๐
๐ฏ The @palabra-ai/translator TypeScript library enables you to integrate real-time speech translation into your Web applications.
Whether you're building a new application, enhancing an existing product, or streamlining business processes, this library has the tools you need.
With Palabra AI, you can:
- โก Translate live speech in real time, making conversations smooth and natural
- ๐๏ธ Preserve the original speaker's voice and tone in translated speech
- ๐ Convert spoken language instantly into accurate, readable text โ great for captions, accessibility, and analysis
Installation
npm install @palabra-ai/translator
# or
pnpm add @palabra-ai/translator
# or
yarn add @palabra-ai/translatorPrerequisites
- A modern web browser (uses WebRTC and Web Audio APIs)
- Palabra API credentials
Quick Start
Follow the steps below to run your first translation using Palabra AI's TypeScript library.
1. Get a local audio track
Use a function to return a MediaStreamTrack from the user's microphone:
import { getLocalAudioTrack } from '@palabra-ai/translator';2. Initialize the client
import { PalabraClient } from '@palabra-ai/translator';
const client = new PalabraClient({
auth: {
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
},
translateFrom: 'en', // Source language code
translateTo: 'es', // Target language code
handleOriginalTrack: getLocalAudioTrack, // Function returning a MediaStreamTrack
});3. Start translation
await client.startTranslation();4. Play translated audio
await client.startPlayback();5. Stop translation and playback
await client.stopPlayback();
await client.stopTranslation();6. Output device changing
[!NOTE] Audio output device switching is supported only in browsers that implement setSinkId(). In unsupported browsers like Safari, this method will have no effect.
await client.changeAudioOutputDevice('deviceId')7. Volume changing for audio track by language
[!NOTE] Volume should be a value between 0.0 and 1.0, where 0.0 is muted and 1.0 is maximum volume.
client.setVolume('es', .7)[!NOTE] Browsers may restrict audio playback initiated without user interaction. Each browser may also define user interaction differently. (For example, Safari on iOS is restrictive.)
API Reference
See TypeScript types for full API documentation.
PalabraClient
The PalabraClient class is the main entry point for integration with the Palabra API.
It manages connection setup, session lifecycle, audio handling, transcription and translation events, and playback of translated speech.
Features
Key features of PalabraClient:
- Connects to the Palabra API
- Manages translation sessions
- Manages language settings
- Emits events for transcription and translation results
- Plays translated audio in the browser
- Manages target languages and session configuration
Constructor
new PalabraClient(options: PalabraClientData)Parameters
auth: Authentication data (eitherclientId/clientSecretoruserToken)translateFrom: Source language code (e.g., 'en')translateTo: Target language code (e.g., 'es')handleOriginalTrack: Function returning the original audio track (MediaStreamTrack)apiBaseUrl(optional): API URL (defaults to Palabra cloud)
Public Methods
startTranslation(): Promise<boolean>
Starts a translation session and connects the audio stream. Returnstrueon success.stopTranslation(): Promise<void>Stops a translation session and disconnects the transport.startPlayback(): Promise<void>
Enables playback of translated audio in the browser.stopPlayback(): Promise<void>
Stops playback of translated audio.setTranslateFrom(langCode: SourceLangCode): Promise<void>
Changes the source language for translation on the fly.setTranslateTo(langCode: TargetLangCode): Promise<void>
Changes the target language for translation on the fly.addTranslationTarget(langCode: TargetLangCode): Promise<void>
Adds a target language for translation.removeTranslationTarget(langCode: TargetLangCode | TargetLangCode[]): Promise<void>
Removes one or more target languages from translation.muteOriginalTrack(): void
Mutes the original audio track (microphone).unmuteOriginalTrack(): void
Unmutes the original audio track (microphone).setVolume(language: string, volume: number): void
Set volume for audio track by given language. Volume should be between 0.0 (muted) and 1.0 (maximum)changeAudioOutputDevice(deviceId: string): Promise<void>Change output deviceNote: Audio output device switching is supported only in browsers that implement setSinkId(). In unsupported browsers like Safari, this method will have no effect.
cleanup(): Promise<void>
Stops translation and playback, releases resources, and resets the client to its initial state.
Events
The PalabraClient class provides events that let you track connection status, receive audio tracks, and handle transcription and translation results.
You can use these events to update your UI, handle errors, and get real-time updates during the speech processing flow โ from connecting to receiving translated audio and text.
const client = new PalabraClient({
auth: {
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
},
translateFrom: 'en', // Source language code
translateTo: 'es', // Target language code
handleOriginalTrack: getLocalAudioTrack, // Function returning a MediaStreamTrack
});
client.on(EVENT_REMOTE_TRACKS_UPDATE, (tracksData) => {
// Process tracks
});EVENT_REMOTE_TRACKS_UPDATE - An update has occurred to the set of remote audio tracks. (Use this event to access new audio streams.)EVENT_ROOM_CONNECTED - The WebRTC room connection was established.EVENT_ROOM_DISCONNECTED - The WebRTC room connection closed or lost.EVENT_CONNECTION_STATE_CHANGED - The connection state has changed (e.g., connecting, connected, disconnected).EVENT_DATA_RECEIVED - Custom data or messages have been received from the server via the WebRTC data channel.EVENT_START_TRANSLATION - The translation process has started.EVENT_STOP_TRANSLATION โ The translation process has stopped.EVENT_TRANSCRIPTION_RECEIVED - The full transcription (recognized text) of the source audio has been received.EVENT_TRANSLATION_RECEIVED - The full, written translation of the source audio has been received.EVENT_PARTIAL_TRANSLATED_TRANSCRIPTION_RECEIVED - A partial translation of the transcription has been received.EVENT_PARTIAL_TRANSCRIPTION_RECEIVED - A partial transcription has been received. (Useful for real-time updates.)EVENT_PIPELINE_TIMINGS_RECEIVED - Timing or performance data about the translation pipeline has been received. (Useful for diagnostics and/or analytics.)EVENT_ERROR_RECEIVED - An error in the translation or streaming process has occurred.
Usage Examples
Basic: Start translation and playback
import { PalabraClient, getLocalAudioTrack } from '@palabra-ai/translator';
// 1. Create the client
const client = new PalabraClient({
auth: {
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
},
translateFrom: 'en',
translateTo: 'es',
handleOriginalTrack: getLocalAudioTrack,
});
// 2. Start translation session
await client.startTranslation();
// 3. Start playback of translated audio
await client.startPlayback();
// 4. Stop translation and playback when done
await client.stopPlayback();
await client.stopTranslation();Advanced: Output translated audio to a custom <audio> element
Listen for the EVENT_REMOTE_TRACKS_UPDATE event to get the translated audio tracks and play them in your own <audio> element:
import { PalabraClient, getLocalAudioTrack } from '@palabra-ai/translator';
import { EVENT_REMOTE_TRACKS_UPDATE } from '@palabra-ai/translator';
// Create an <audio> element in your code
const audioElement = new Audio();
const client = new PalabraClient({
auth: {
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
},
translateFrom: 'en',
translateTo: 'fr',
handleOriginalTrack: getLocalAudioTrack,
});
client.on(EVENT_REMOTE_TRACKS_UPDATE, (tracks) => {
// tracks - RemoteTrackInfo[]
// tracks is an array of { track: MediaStreamTrack, ... }
audioElement.srcObject = new MediaStream(tracks.map(t => t.track));
audioElement.play();
});
// Start translation as usual
await client.startTranslation();
// Handle playback
const stopPlayback = () => {
audioElement.value.pause();
};
const startPlayback = () => {
audioElement.value.play();
};The examples below show how to integrate Palabra's real-time translation into any web application and control audio output as needed.
PalabraAsrClient
The PalabraAsrClient class is the entry point for the realtime STT API.
It captures an audio track, streams it to the API as raw chunks and emits partial, final and translated transcriptions.
Features
- Opens an STT session over a websocket, the whole configuration goes into the query
- Captures the track into
pcm_s16lechunks of 320 ms with anAudioWorklet - Emits partial results while speaking and final ones on every end of sentence
- Optionally emits translations of the final transcriptions
- Mutes the source track without dropping the session
Constructor
new PalabraAsrClient(options: PalabraAsrClientData)Parameters
auth:{ apiKey }โ API key from platform.palabra.ai/api-keyscreateSession(optional): function returning{ streamUrl, token }, use it to keep the API key on your backendhandleOriginalTrack: function returning the track to transcribe,getLocalAudioTrackcovers the microphonelanguage(optional): spoken language,auto(default) lets the API detect ittranslateLanguages(optional): target languages of thetranslated_transcriptionmessagesenableFillerFilter(optional): filler filter, enabled by the API for every language except JapanesewsBaseUrl(optional):ASR_WS_BASE_URL_EU(default) orASR_WS_BASE_URL_USaudioContext(optional): existing audio context to capture inchunkMs(optional): size of the audio chunks, defaults to the recommended 320 ms
Public Methods
startTranscription(): Promise<boolean>โ take the track, connect and start streamingstopTranscription(): Promise<void>โ stop the capture, close the socket and release the trackmuteOriginalTrack()/unmuteOriginalTrack()/isOriginalTrackMuted()setLanguage(language),setTranslateLanguages(languages)โ restart an ongoing session, the config lives in the querygetConfig(),getSessionStatus(),getConnectionStatus(),getOriginalTrack()cleanup(): Promise<void>โ stop the session and close the audio context
Events
EVENT_ASR_SESSION_STARTED / EVENT_ASR_SESSION_STOPPED - The STT session has been opened or closed.
EVENT_ASR_CONNECTED / EVENT_ASR_DISCONNECTED - The websocket has been opened or closed (the close code and reason are passed).
EVENT_ASR_CONNECTION_STATE_CHANGED - The connection state has changed (connecting, connected, disconnected).
EVENT_ASR_PARTIAL_TRANSCRIPTION_RECEIVED - A partial transcription, updated while the phrase is still being spoken.
EVENT_ASR_TRANSCRIPTION_RECEIVED - A final transcription (is_eos: true).
EVENT_ASR_TRANSLATED_TRANSCRIPTION_RECEIVED - A translation of a final transcription, only with translateLanguages set.
EVENT_ASR_ERROR_RECEIVED - A websocket level error.
EVENT_ASR_MESSAGE_RECEIVED - A raw message from the API.
Usage Example
import {
PalabraAsrClient,
getLocalAudioTrack,
EVENT_ASR_PARTIAL_TRANSCRIPTION_RECEIVED,
EVENT_ASR_TRANSCRIPTION_RECEIVED,
} from '@palabra-ai/translator';
const asrClient = new PalabraAsrClient({
auth: { apiKey: 'YOUR_API_KEY' },
language: 'en',
translateLanguages: ['es'],
handleOriginalTrack: getLocalAudioTrack,
});
asrClient.on(EVENT_ASR_PARTIAL_TRANSCRIPTION_RECEIVED, (data) => {
console.log('partial', data?.segment.text);
});
asrClient.on(EVENT_ASR_TRANSCRIPTION_RECEIVED, (data) => {
console.log('final', data?.segment.text);
});
await asrClient.startTranscription();
// ...
await asrClient.stopTranscription();
await asrClient.cleanup();Notes
- The API keeps one active session per key: a second connection is rejected with
409during the upgrade, so close the previous session before opening a new one. - The configuration is passed in the query, so changing the language or the translation targets reconnects.
- Audio is sent as raw binary frames in the sample rate of the audio context, which is declared in the query.
- After a successful upgrade the API reports problems by closing the socket, there are no error messages on the wire.
PalabraTtsClient
The PalabraTtsClient class is the entry point for the realtime TTS API.
It keeps a websocket session, streams text to synthesize and plays the received audio chunks back gapless.
๐ TTS.md โ use cases, custom playback (own
<audio>element, audio graph, WebRTC, raw chunks, Node.js), limits and gotchas.
Features
- Opens a TTS session over a websocket and sends the
initmessage - Splits text into chunks accepted by the API and respects its rate limits
- Plays
pcmchunks back to back through anAudioContext - Exposes the synthesized speech as a
MediaStreamTrack - Emits events for audio chunks, finished generations and API errors
Constructor
new PalabraTtsClient(options: PalabraTtsClientData)Parameters
auth:{ apiKey }โ API key from platform.palabra.ai/api-keyscreateSession(optional): function returning{ streamUrl, token }, use it to keep the API key on your backendlanguage: language of the synthesized speech (e.g., 'en')model(optional): TTS model id (defaults toauto)voiceOptions(optional):voice_id,speed(0โ2),deaccent_strength(0โ1)output(optional):format(pcm|mp3|wav) andsample_rate(8000โ48000), onlypcmcan be played chunk by chunkwsBaseUrl(optional):TTS_WS_BASE_URL_EU(default) orTTS_WS_BASE_URL_USaudioContext(optional): existing audio context to play the speech in โ its own rate then wins overoutput.sample_rateignoreAudioContext(optional): skip the playback chain and only emit audio chunks
Public Methods
startSession(): Promise<boolean>โ connect the websocket and send theinitmessagestopSession(): Promise<void>โ close the session and release the playback chainspeak(text: string, options?: TtsSpeakOptions): Promise<string>โ stream text, returns thegenerationIdcancel(): Promise<void>โ drop everything that is still being synthesizedstartPlayback(): Promise<void>/stopPlayback(): Promise<void>setVolume(volume: number): void/getVolume(): numbergetSpeechTrack(): MediaStreamTrack | nullsetLanguage(language),setVoiceOptions(options),setOutput(output)โ theinitmessage is immutable within a session, so an ongoing session is restartedgetConfig(),getSessionStatus(),getConnectionStatus()cleanup(): Promise<void>โ stop the session and close the audio context
Events
EVENT_TTS_SESSION_STARTED / EVENT_TTS_SESSION_STOPPED - The TTS session has been opened or closed.
EVENT_TTS_CONNECTED / EVENT_TTS_DISCONNECTED - The websocket has been opened or closed (the close code and reason are passed).
EVENT_TTS_CONNECTION_STATE_CHANGED - The connection state has changed (connecting, connected, disconnected).
EVENT_TTS_AUDIO_CHUNK_RECEIVED - An audio chunk has been received (base64 audio, generation_id, last_chunk).
EVENT_TTS_GENERATION_COMPLETED - The last chunk of a generation has been received.
EVENT_TTS_PLAYBACK_STARTED / EVENT_TTS_PLAYBACK_ENDED - The playback of the scheduled chunks has started or drained.
EVENT_TTS_ERROR_RECEIVED - The API reported an error (see TTS_RETRYABLE_ERROR_CODES).
EVENT_TTS_MESSAGE_RECEIVED - A raw message from the API.
EVENT_TTS_VOLUME_CHANGED - The playback volume has changed.
Usage Example
import {
PalabraTtsClient,
EVENT_TTS_GENERATION_COMPLETED,
EVENT_TTS_ERROR_RECEIVED,
} from '@palabra-ai/translator';
const ttsClient = new PalabraTtsClient({
auth: { apiKey: 'YOUR_API_KEY' },
language: 'en',
voiceOptions: { voice_id: 'default_low', speed: 1.0 },
});
ttsClient.on(EVENT_TTS_GENERATION_COMPLETED, ({ generationId }) => {
console.log('finished', generationId);
});
ttsClient.on(EVENT_TTS_ERROR_RECEIVED, (error) => {
console.error(error);
});
await ttsClient.startSession();
await ttsClient.startPlayback();
// a long text is split into chunks automatically
await ttsClient.speak('Hello, how can I help you today?');
// stream a sentence in parts and finalize it with the last call
const generationId = await ttsClient.speak('One moment', { isEos: false });
await ttsClient.speak('please', { generationId });
await ttsClient.stopSession();
await ttsClient.cleanup();Attach the speech to your own element instead of the default output:
await ttsClient.startSession();
const audioElement = new Audio();
audioElement.srcObject = new MediaStream([ttsClient.getSpeechTrack()!]);
await audioElement.play();Monorepo Structure
Development Setup
This project contains two main packages:
@palabra-ai/translator: The main library packagepackages/libdev-app: A Vue.js development application for testing the librarypackages/dev-app
Prerequisites
Installation
# Install dependencies for all packages
pnpm installRunning in Development Mode
Library Development
Run the library in watch mode (auto-rebuild on changes):
cd packages/lib
pnpm devDevelopment Application
Run the dev app with hot-reload:
cd packages/dev-app
pnpm devOpen http://localhost:5173 in your browser to view the dev app.
More Commands
Library Package packages/lib
pnpm build- Build the librarypnpm test- Run testspnpm lint- Run linting
Development App packages/dev-app
pnpm build- Build for productionpnpm dev- Run dev app
Supported Languages
Speech Recognition Languages
๐ธ๐ฆ Arabic (AR), ๐จ๐ณ Chinese (ZH), ๐จ๐ฟ Czech (CS), ๐ฉ๐ฐ Danish (DA), ๐ณ๐ฑ Dutch (NL), ๐ฌ๐ง English (EN), ๐ซ๐ฎ Finnish (FI), ๐ซ๐ท French (FR), ๐ฉ๐ช German (DE), ๐ฌ๐ท Greek (EL), ๐ฎ๐ฑ Hebrew (HE), ๐ญ๐บ Hungarian (HU), ๐ฎ๐น Italian (IT), ๐ฏ๐ต Japanese (JA), ๐ฐ๐ท Korean (KO), ๐ต๐ฑ Polish (PL), ๐ต๐น Portuguese (PT), ๐ท๐บ Russian (RU), ๐ช๐ธ Spanish (ES), ๐น๐ท Turkish (TR), ๐บ๐ฆ Ukrainian (UK)
Translation Languages
๐ธ๐ฆ Arabic (AR), ๐ง๐ฌ Bulgarian (BG), ๐จ๐ณ Chinese Mandarin (ZH), ๐จ๐ฟ Czech (CS), ๐ฉ๐ฐ Danish (DA), ๐ณ๐ฑ Dutch (NL), ๐ฌ๐ง English UK (EN_GB), ๐บ๐ธ English US (EN_US), ๐ซ๐ฎ Finnish (FI), ๐ซ๐ท French (FR), ๐ฉ๐ช German (DE), ๐ฌ๐ท Greek (EL), ๐ฎ๐ฑ Hebrew (HE), ๐ญ๐บ Hungarian (HU), ๐ฎ๐ฉ Indonesian (ID), ๐ฎ๐น Italian (IT), ๐ฏ๐ต Japanese (JA), ๐ฐ๐ท Korean (KO), ๐ต๐ฑ Polish (PL), ๐ต๐น Portuguese (PT), ๐ง๐ท Portuguese Brazilian (PT_BR), ๐ท๐ด Romanian (RO), ๐ท๐บ Russian (RU), ๐ธ๐ฐ Slovak (SK), ๐ช๐ธ Spanish (ES), ๐ฒ๐ฝ Spanish Mexican (ES_MX), ๐ธ๐ช Swedish (SV), ๐น๐ท Turkish (TR), ๐บ๐ฆ Ukrainian (UK), ๐ป๐ณ Vietnamese (VN)
License
MIT
