File size: 1,416 Bytes
811126d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import axios from "axios";
import fs from "fs";
import "dotenv/config";

async function textToSpeech(voiceId: string, text: string) {
  try {
    console.log("Début de la conversion texte vers voix...");

    const url = `https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`;
    const response = await axios.post(
      url,
      {
        text: text,
        model_id: "eleven_multilingual_v2",
        voice_settings: {
          stability: 0.3,
          similarity_boost: 0.85,
        },
      },
      {
        headers: {
          Accept: "audio/mpeg",
          "xi-api-key": process.env.ELEVENLABS_API_KEY,
          "Content-Type": "application/json",
        },
        responseType: "arraybuffer",
      }
    );

    console.log("Audio généré avec succès");

    // Sauvegarde l'audio dans un fichier
    fs.writeFileSync("output.mp3", response.data);
    console.log("Audio sauvegardé dans output.mp3");
  } catch (error) {
    console.error("Erreur:", error.response?.data || error);
  }
}

// Utilisation
const voiceId = "nGfHJWtJzDfxi8048hg3"; // Remplacez par votre voice ID
const textToConvert = `
<speak>
  <prosody rate="90%">Salut, je parle un peu plus lentement...</prosody>
  <prosody rate="110%">Et là je parle un peu plus vite!</prosody>
  <prosody rate="95%">Et maintenant je reviens à une vitesse presque normale.</prosody>
</speak>`;

textToSpeech(voiceId, textToConvert);