Découvrez comment lire un texte à voix haute avec une voix de synthèse naturelle.
Les modèles tts-1, tts-1-hd et gpt-4o-mini-tts sont obsolètes et seront retirés le 6 janvier 2027. Migrez vers gpt-realtime-2.1-mini en utilisant la Realtime API. Cette migration change la façon d’appeler l’API, car le modèle gpt-realtime-2.1-mini ne peut pas être utilisé avec /v1/audio/speech. Les intégrations existantes peuvent continuer à utiliser l’API Speech comme indiqué ci-dessous jusqu’au retrait des modèles. Consultez l’avis de dépréciation.
Nos politiques d’utilisation vous imposent d’indiquer clairement aux utilisateurs finaux que la voix de synthèse qu’ils entendent est générée par une IA et qu’il ne s’agit pas d’une voix humaine.
Démarrage rapide
Le point de terminaison speech accepte trois paramètres principaux :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import fs from "fs";import path from "path";import OpenAI from "openai";const openai = new OpenAI();const speechFile = path.resolve("./speech.mp3");const mp3 = await openai.audio.speech.create({ model: "gpt-4o-mini-tts", voice: "coral", input: "Today is a wonderful day to build something people love!", instructions: "Speak in a cheerful and positive tone.",});const buffer = Buffer.from(await mp3.arrayBuffer());await fs.promises.writeFile(speechFile, buffer);
1
2
3
4
5
6
7
8
9
10
11
12
13from pathlib import Pathfrom openai import OpenAIclient = OpenAI()speech_file_path = Path(__file__).parent /"speech.mp3"with client.audio.speech.with_streaming_response.create(model="gpt-4o-mini-tts",voice="coral",input="Today is a wonderful day to build something people love!",instructions="Speak in a cheerful and positive tone.",) as response: response.stream_to_file(speech_file_path)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34package mainimport ( "context" "io" "os" "github.com/openai/openai-go/v3")func main() { client := openai.NewClient() response, err := client.Audio.Speech.New(context.Background(), openai.AudioSpeechNewParams{ Model: openai.SpeechModelGPT4oMiniTTS, Voice: openai.AudioSpeechNewParamsVoiceUnion{OfAudioSpeechNewsVoiceString2: openai.String("coral")}, Input: "Today is a wonderful day to build something people love!", Instructions: openai.String("Speak in a cheerful and positive tone."), }) if err != nil { panic(err) } defer response.Body.Close() file, err := os.Create("speech.mp3") if err != nil { panic(err) } if _, err := io.Copy(file, response.Body); err != nil { panic(err) } if err := file.Close(); err != nil { panic(err) }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import com.openai.client.OpenAIClient;import com.openai.client.okhttp.OpenAIOkHttpClient;import com.openai.core.http.HttpResponse;import com.openai.models.audio.speech.SpeechCreateParams;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.StandardCopyOption;try (HttpResponse audio = client .audio() .speech() .create( SpeechCreateParams.builder() .model("gpt-4o-mini-tts") .voice("coral") .input("Today is a wonderful day to build something people love!") .instructions("Speak in a cheerful and positive tone.") .build())) { Files.copy(audio.body(), Path.of("speech.mp3"), StandardCopyOption.REPLACE_EXISTING);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17using OpenAI.Audio;#pragma warning disable OPENAI001string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;string model = "gpt-4o-mini-tts";AudioClient client = new(model, key);BinaryData audio = await client.GenerateSpeechAsync( "Today is a wonderful day to build something people love!", GeneratedSpeechVoice.Coral, new SpeechGenerationOptions { Instructions = "Speak in a cheerful and positive tone.", });await File.WriteAllBytesAsync("speech.mp3", audio.ToArray());
1
2
3
4
5
6
7
8
9
10require "openai"client = OpenAI::Client.newaudio = client.audio.speech.create( model: "gpt-4o-mini-tts", voice: "coral", input: "Today is a wonderful day to build something people love!", instructions: "Speak in a cheerful and positive tone.")File.binwrite("speech.mp3", audio.read)
1
2
3
4
5
6
7
8
9
10curl https://api.openai.com/v1/audio/speech \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini-tts", "input": "Today is a wonderful day to build something people love!", "voice": "coral", "instructions": "Speak in a cheerful and positive tone." }' \ --output speech.mp3
1
2
3
4
5
6openai audio:speech create \ --model gpt-4o-mini-tts \ --voice coral \ --instructions "Speak in a cheerful and positive tone." \ --input "Today is a wonderful day to build something people love!" \ --output speech.mp3
Par défaut, le point de terminaison renvoie un fichier MP3 contenant le texte lu à voix haute. Vous pouvez le configurer pour obtenir la réponse dans n’importe quel format pris en charge.
Modèles de synthèse vocale
Si vous utilisez déjà l’API Speech, le modèle gpt-4o-mini-tts vous permet de contrôler les caractéristiques suivantes de la voix à l’aide de prompts :
Accent
Palette d’émotions
Intonation
Imitations vocales
Débit de parole
Ton
Chuchotement
Nos autres modèles de synthèse vocale sont tts-1 et tts-1-hd. Le modèle tts-1 offre une latence plus faible, mais la qualité audio est inférieure à celle du modèle tts-1-hd.
Choix de la voix
Le point de terminaison de synthèse vocale propose 13 voix intégrées pour choisir comment le texte sera lu. Écoutez et essayez ces voix sur OpenAI.fm, notre démo interactive du dernier modèle de synthèse vocale de l’API OpenAI. Les voix sont actuellement optimisées pour l’anglais.
alloy
ash
ballad
coral
echo
fable
nova
onyx
sage
shimmer
verse
marin
cedar
Pour une qualité optimale, nous vous recommandons d’utiliser marin ou cedar.
Les voix disponibles varient selon le modèle. Les modèles tts-1 et tts-1-hd prennent en charge un choix plus restreint de voix : alloy, ash, coral, echo, fable, onyx, nova, sage et shimmer.
L’API Speech permet de diffuser l’audio en temps réel grâce au codage de transfert par blocs. La lecture peut ainsi commencer avant que le fichier complet soit généré et disponible.
Diffusez la lecture d’un texte en continu directement sur vos haut-parleurs
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14import OpenAI from "openai";import { playAudio } from "openai/helpers/audio";const openai = new OpenAI();const response = await openai.audio.speech.create({ model: "gpt-4o-mini-tts", voice: "coral", input: "Today is a wonderful day to build something people love!", instructions: "Speak in a cheerful and positive tone.", response_format: "wav",});await playAudio(response);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21import asynciofrom openai import AsyncOpenAIfrom openai.helpers import LocalAudioPlayeropenai = AsyncOpenAI()asyncdefmain() -> None:asyncwith openai.audio.speech.with_streaming_response.create(model="gpt-4o-mini-tts",voice="coral",input="Today is a wonderful day to build something people love!",instructions="Speak in a cheerful and positive tone.",response_format="pcm", ) as response:await LocalAudioPlayer().play(response)if__name__=="__main__": asyncio.run(main())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27package mainimport ( "context" "io" "os" "github.com/openai/openai-go/v3")func main() { client := openai.NewClient() response, err := client.Audio.Speech.New(context.Background(), openai.AudioSpeechNewParams{ Model: openai.SpeechModelGPT4oMiniTTS, Voice: openai.AudioSpeechNewParamsVoiceUnion{OfAudioSpeechNewsVoiceString2: openai.String("coral")}, Input: "Today is a wonderful day to build something people love!", Instructions: openai.String("Speak in a cheerful and positive tone."), ResponseFormat: openai.AudioSpeechNewParamsResponseFormatWAV, }) if err != nil { panic(err) } defer response.Body.Close() if _, err := io.Copy(os.Stdout, response.Body); err != nil { panic(err) }}
1
2
3
4
5
6
7
8
9
10curl https://api.openai.com/v1/audio/speech \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o-mini-tts", "input": "Today is a wonderful day to build something people love!", "voice": "coral", "instructions": "Speak in a cheerful and positive tone.", "response_format": "wav" }' | ffplay -i -
Pour obtenir les temps de réponse les plus courts, nous vous recommandons d’utiliser wav ou pcm comme format de réponse.
Formats de sortie pris en charge
Le format de réponse par défaut est mp3, mais d’autres formats comme opus et wav sont disponibles.
MP3 : format de réponse par défaut pour les usages courants.
Opus : format à faible latence adapté à la diffusion en continu et aux communications sur Internet.
AAC : format de compression audio numérique privilégié par YouTube, Android et iOS.
FLAC : format de compression audio sans perte que les amateurs de haute fidélité privilégient pour l’archivage.
WAV : format audio WAV non compressé, adapté aux applications à faible latence puisqu’il évite le coût du décodage.
PCM : format similaire au WAV, mais sans en-tête. Il contient les échantillons bruts à 24 kHz, codés sur 16 bits signés avec l’octet de poids faible en premier.
Langues prises en charge
Le modèle de synthèse vocale prend généralement en charge les mêmes langues que le modèle Whisper. Whisper prend en charge les langues suivantes et donne de bons résultats, même si les voix sont optimisées pour l’anglais :
Pour générer de la parole dans l’une de ces langues, fournissez un texte dans la langue de votre choix.
Voix personnalisées
Pour créer une voix personnalisée approuvée, fournissez un enregistrement dans lequel la personne donne son consentement ainsi qu’un échantillon audio de sa voix. Consultez le guide Voix personnalisées pour connaître les critères d’éligibilité, les exigences d’enregistrement, les phrases de consentement et les requêtes API à utiliser.