Voice Translator

TsgcAIVoiceTranslator translates speech while the user talks. It streams the microphone to OpenAI gpt-realtime-translate over a WebSocket, plays the translated speech as it arrives and reports live transcripts of what was said and of the translation.

TsgcAIVoiceTranslator

A realtime speech to speech translator. Set the API key and the target language, call Start, speak. The model detects the source language, so there is nothing else to configure.

Component class

TsgcAIVoiceTranslator

Purpose

Live speech to speech translation

Family

Chat & agents

Platforms

Windows with microphone and speakers. Linux and macOS with your own audio.

Editions. TsgcAIVoiceTranslator ships in sgcAI and in sgcWebSockets Enterprise, the same editions as the realtime voice agent TsgcAIVoiceAgent. sgcAI is self-contained: it includes the sgcWebSockets Core runtime the component is built on. Available for Delphi 7 to Delphi 13 and C++ Builder.

Speak any language, hear Spanish

Set the API key and the target language, assign the transcript events, call Start.

uses
  sgcWebSocket_Types, sgcAI_VoiceTranslator;

procedure TForm1.StartTranslator;
begin
  oTranslator := TsgcAIVoiceTranslator.Create(Self);
  oTranslator.ApiKey := 'sk-...';
  oTranslator.Language := 'es';   // speak any language, hear Spanish
  oTranslator.NotifyEvents := neAsynchronous;
  oTranslator.OnInputTranscript := DoInputTranscript;
  oTranslator.OnOutputTranscript := DoOutputTranscript;
  oTranslator.OnError := DoError;
  oTranslator.Start;   // microphone in, translated speech out
end;

procedure TForm1.DoInputTranscript(Sender: TObject; const aDelta: string);
begin
  memoSource.Text := memoSource.Text + aDelta;   // what was said
end;

procedure TForm1.DoOutputTranscript(Sender: TObject; const aDelta: string);
begin
  memoTarget.Text := memoTarget.Text + aDelta;   // the translation
end;

procedure TForm1.DoError(Sender: TObject; const aError: string);
begin
  ShowMessage(aError);
end;

procedure TForm1.SwitchLanguage;
begin
  oTranslator.Language := 'fr';   // applied to the running session
end;
// includes: sgcWebSocket_Types.hpp, sgcAI_VoiceTranslator.hpp

void TForm1::StartVoiceTranslator()
{
  oTranslator = new TsgcAIVoiceTranslator(this);
  oTranslator->ApiKey = "sk-...";
  oTranslator->Language = "es";
  oTranslator->NotifyEvents = neAsynchronous;
  oTranslator->OnInputTranscript = DoInputTranscript;
  oTranslator->OnOutputTranscript = DoOutputTranscript;
  oTranslator->OnError = DoError;
  oTranslator->Start();
}

void __fastcall TForm1::DoInputTranscript(TObject *Sender, const String aDelta)
{
  memoSource->Text = memoSource->Text + aDelta;
}

void __fastcall TForm1::DoOutputTranscript(TObject *Sender, const String aDelta)
{
  memoTarget->Text = memoTarget->Text + aDelta;
}

void __fastcall TForm1::DoError(TObject *Sender, const String aError)
{
  ShowMessage(aError);
}

Bring your own audio source

Turn the devices off to translate audio from a file, a phone line or a network stream. Push PCM16 at 24 kHz mono with SendAudio and take the translated speech from OnOutputAudio.

procedure TForm1.StartCustomAudio(const aPCM16: TBytes);
begin
  oTranslator.AudioCapture := False;   // no microphone
  oTranslator.AudioPlayback := False;  // no speakers
  oTranslator.OnOutputAudio := DoOutputAudio;
  oTranslator.Start;
  oTranslator.SendAudio(aPCM16);   // PCM16, 24 kHz, mono
end;

procedure TForm1.DoOutputAudio(Sender: TObject; const aPCM16: TBytes);
begin
  // translated speech, PCM16 24 kHz mono, in order
end;

Key properties & methods

The members you reach for most often.

Session

Start connects, waits for the session, sends the target language and opens the devices. Stop flushes the pending audio and closes the session. Active does the same from the Object Inspector.

Language

The target language code, en by default. Assign a new value while the session runs and the change is sent to the model at once, no reconnect needed.

Connection

ApiKey, Model (gpt-realtime-translate) and BaseURL. SafetyIdentifier sends a hashed user id in the OpenAI-Safety-Identifier header.

Audio

AudioCapture and AudioPlayback use the microphone and the speakers. Set them False and feed SendAudio with PCM16 at 24 kHz mono instead.

Transcripts

OnInputTranscript streams the text of what was said, OnOutputTranscript the text of the translation, both as deltas.

Events

OnSessionCreated, OnOutputAudio with each translated audio chunk, OnSessionClosed and OnError. NotifyEvents selects asynchronous events for GUI applications or no synchronization for services.

Platforms. The microphone and speaker path uses the Windows audio API. On Linux and macOS set AudioCapture and AudioPlayback to False and handle the audio yourself with SendAudio and OnOutputAudio. With the devices left on, Start reports through OnError that no audio device is available on that platform.

Specifications & references

Authoritative sources for the service this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
OpenAI TranslatorRecord, translate and speak one segment at a time with TsgcAIOpenAITranslator.
All sgcAI ComponentsBrowse the full feature matrix of the sgcAI components.
Download Free TrialIncludes the Voice Translator demo. Bring your own OpenAI key.
PricingSingle, Team and Site licenses, full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and add live speech to speech translation to your application.