TsgcHTTP_API_OpenAIMethods › CreateSpeech

CreateSpeech Method

Generates audio speech from input text using an OpenAI text-to-speech model

Overloads

Overload 1

Syntax

function CreateSpeech(const aRequest: TsgcOpenAIClass_Request_Speech) : string;

Parameters

NameTypeDescription
aRequestconst TsgcOpenAIClass_Request_SpeechSpeech request specifying model, input text, voice and audio format

Return Value

Local file path of the generated audio file (string)

Remarks

Calls the POST /v1/audio/speech endpoint. This overload writes the synthesized audio to a temporary file on disk and returns its path. Supported models include tts-1 and tts-1-hd, with voices such as alloy, echo, fable, onyx, nova and shimmer. Useful when a file-based workflow is needed rather than in-memory streaming.

Example

oRequest := TsgcOpenAIClass_Request_Speech.Create;
oRequest.Model := 'tts-1';
oRequest.Input := 'Hello from sgcWebSockets.';
oRequest.Voice := 'alloy';
vFile := oAPI.CreateSpeech(oRequest);
ShowMessage('Audio saved to: ' + vFile);

Overload 2

Syntax

procedure CreateSpeech(const aRequest: TsgcOpenAIClass_Request_Speech; const aResponseStream: TStream);

Parameters

NameTypeDescription
aRequestconst TsgcOpenAIClass_Request_SpeechSpeech request specifying model, input text, voice and audio format
aResponseStreamconst TStreamDestination stream that receives the raw audio bytes returned by the API

Remarks

Calls the POST /v1/audio/speech endpoint and writes the binary audio payload directly into the provided stream. Use this overload to pipe generated audio to a memory stream, file stream or network stream without touching the file system. The caller owns the stream and is responsible for its lifetime.

Example

oRequest := TsgcOpenAIClass_Request_Speech.Create;
oRequest.Model := 'tts-1';
oRequest.Input := 'Streaming audio output.';
oRequest.Voice := 'nova';
oStream := TMemoryStream.Create;
oAPI.CreateSpeech(oRequest, oStream);
oStream.SaveToFile('speech.mp3');

Back to Methods