Learn R Programming

openaiRtools (version 0.2.2)

create_speech: Convert Text to Speech (Convenience Function)

Description

Shortcut that creates an OpenAI client from the OPENAI_API_KEY environment variable and calls client$audio$speech$create(). Returns raw binary audio data that should be saved with writeBin().

Usage

create_speech(input, model = "tts-1", voice = "alloy", ...)

Value

A raw vector of binary audio data. Save using writeBin().

Arguments

input

Character. Required. The text to synthesize (max 4096 chars).

model

Character. TTS model: "tts-1" (fast) or "tts-1-hd" (higher quality). Default: "tts-1".

voice

Character. Voice style. One of: "alloy", "ash", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer". Default: "alloy".

...

Additional parameters passed to SpeechClient$create(), such as response_format ("mp3", "wav", "flac", etc.) and speed (0.25–4.0).

Examples

Run this code
if (FALSE) {
Sys.setenv(OPENAI_API_KEY = "sk-xxxxxx")

# Generate and save to MP3
audio <- create_speech(
  input = "The quick brown fox jumps over the lazy dog.",
  model = "tts-1",
  voice = "nova"
)
writeBin(audio, "output.mp3")

# High-quality WAV with slower speed
audio <- create_speech(
  input           = "Welcome to the lecture on macroeconomics.",
  model           = "tts-1-hd",
  voice           = "onyx",
  response_format = "wav",
  speed           = 0.9
)
writeBin(audio, "lecture_intro.wav")
}

Run the code above in your browser using DataLab