audiospeech.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package openai
  4
  5import (
  6	"context"
  7	"net/http"
  8
  9	"github.com/openai/openai-go/internal/requestconfig"
 10	"github.com/openai/openai-go/option"
 11	"github.com/openai/openai-go/packages/param"
 12)
 13
 14// AudioSpeechService contains methods and other services that help with
 15// interacting with the openai API.
 16//
 17// Note, unlike clients, this service does not read variables from the environment
 18// automatically. You should not instantiate this service directly, and instead use
 19// the [NewAudioSpeechService] method instead.
 20type AudioSpeechService struct {
 21	Options []option.RequestOption
 22}
 23
 24// NewAudioSpeechService generates a new service that applies the given options to
 25// each request. These options are applied after the parent client's options (if
 26// there is one), and before any request-specific options.
 27func NewAudioSpeechService(opts ...option.RequestOption) (r AudioSpeechService) {
 28	r = AudioSpeechService{}
 29	r.Options = opts
 30	return
 31}
 32
 33// Generates audio from the input text.
 34func (r *AudioSpeechService) New(ctx context.Context, body AudioSpeechNewParams, opts ...option.RequestOption) (res *http.Response, err error) {
 35	opts = append(r.Options[:], opts...)
 36	opts = append([]option.RequestOption{option.WithHeader("Accept", "application/octet-stream")}, opts...)
 37	path := "audio/speech"
 38	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
 39	return
 40}
 41
 42type SpeechModel = string
 43
 44const (
 45	SpeechModelTTS1         SpeechModel = "tts-1"
 46	SpeechModelTTS1HD       SpeechModel = "tts-1-hd"
 47	SpeechModelGPT4oMiniTTS SpeechModel = "gpt-4o-mini-tts"
 48)
 49
 50type AudioSpeechNewParams struct {
 51	// The text to generate audio for. The maximum length is 4096 characters.
 52	Input string `json:"input,required"`
 53	// One of the available [TTS models](https://platform.openai.com/docs/models#tts):
 54	// `tts-1`, `tts-1-hd` or `gpt-4o-mini-tts`.
 55	Model SpeechModel `json:"model,omitzero,required"`
 56	// The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
 57	// `coral`, `echo`, `fable`, `onyx`, `nova`, `sage` and `shimmer`. Previews of the
 58	// voices are available in the
 59	// [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
 60	//
 61	// Any of "alloy", "ash", "coral", "echo", "fable", "onyx", "nova", "sage",
 62	// "shimmer".
 63	Voice AudioSpeechNewParamsVoice `json:"voice,omitzero,required"`
 64	// Control the voice of your generated audio with additional instructions. Does not
 65	// work with `tts-1` or `tts-1-hd`.
 66	Instructions param.Opt[string] `json:"instructions,omitzero"`
 67	// The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is
 68	// the default.
 69	Speed param.Opt[float64] `json:"speed,omitzero"`
 70	// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
 71	// `wav`, and `pcm`.
 72	//
 73	// Any of "mp3", "opus", "aac", "flac", "wav", "pcm".
 74	ResponseFormat AudioSpeechNewParamsResponseFormat `json:"response_format,omitzero"`
 75	paramObj
 76}
 77
 78// IsPresent returns true if the field's value is not omitted and not the JSON
 79// "null". To check if this field is omitted, use [param.IsOmitted].
 80func (f AudioSpeechNewParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
 81
 82func (r AudioSpeechNewParams) MarshalJSON() (data []byte, err error) {
 83	type shadow AudioSpeechNewParams
 84	return param.MarshalObject(r, (*shadow)(&r))
 85}
 86
 87// The voice to use when generating the audio. Supported voices are `alloy`, `ash`,
 88// `coral`, `echo`, `fable`, `onyx`, `nova`, `sage` and `shimmer`. Previews of the
 89// voices are available in the
 90// [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
 91type AudioSpeechNewParamsVoice string
 92
 93const (
 94	AudioSpeechNewParamsVoiceAlloy   AudioSpeechNewParamsVoice = "alloy"
 95	AudioSpeechNewParamsVoiceAsh     AudioSpeechNewParamsVoice = "ash"
 96	AudioSpeechNewParamsVoiceCoral   AudioSpeechNewParamsVoice = "coral"
 97	AudioSpeechNewParamsVoiceEcho    AudioSpeechNewParamsVoice = "echo"
 98	AudioSpeechNewParamsVoiceFable   AudioSpeechNewParamsVoice = "fable"
 99	AudioSpeechNewParamsVoiceOnyx    AudioSpeechNewParamsVoice = "onyx"
100	AudioSpeechNewParamsVoiceNova    AudioSpeechNewParamsVoice = "nova"
101	AudioSpeechNewParamsVoiceSage    AudioSpeechNewParamsVoice = "sage"
102	AudioSpeechNewParamsVoiceShimmer AudioSpeechNewParamsVoice = "shimmer"
103)
104
105// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`,
106// `wav`, and `pcm`.
107type AudioSpeechNewParamsResponseFormat string
108
109const (
110	AudioSpeechNewParamsResponseFormatMP3  AudioSpeechNewParamsResponseFormat = "mp3"
111	AudioSpeechNewParamsResponseFormatOpus AudioSpeechNewParamsResponseFormat = "opus"
112	AudioSpeechNewParamsResponseFormatAAC  AudioSpeechNewParamsResponseFormat = "aac"
113	AudioSpeechNewParamsResponseFormatFLAC AudioSpeechNewParamsResponseFormat = "flac"
114	AudioSpeechNewParamsResponseFormatWAV  AudioSpeechNewParamsResponseFormat = "wav"
115	AudioSpeechNewParamsResponseFormatPCM  AudioSpeechNewParamsResponseFormat = "pcm"
116)