TsgcHTTP_API_OpenAI › Methods › CreateSpeech
Generates audio speech from input text using an OpenAI text-to-speech model
function CreateSpeech(const aRequest: TsgcOpenAIClass_Request_Speech) : string;
| Name | Type | Description |
|---|---|---|
aRequest | const TsgcOpenAIClass_Request_Speech | Speech request specifying model, input text, voice and audio format |
Local file path of the generated audio file (string)
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.
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);
procedure CreateSpeech(const aRequest: TsgcOpenAIClass_Request_Speech; const aResponseStream: TStream);
| Name | Type | Description |
|---|---|---|
aRequest | const TsgcOpenAIClass_Request_Speech | Speech request specifying model, input text, voice and audio format |
aResponseStream | const TStream | Destination stream that receives the raw audio bytes returned by the API |
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.
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');