File size: 3,678 Bytes
7afe4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d105b9
 
7afe4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d105b9
 
7afe4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d105b9
 
7afe4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { NextResponse } from 'next/server';

const VOICES = {
  fr: {
    LAWYER_VOICE: {
      id: "ThT5KcBeYPX3keUQqHPh",
      volume: 0
    },
    GLITCH_VOICE: {
      id: "XrExE9yKIg1WjnnlVkGX",
      volume: -10
    }
  },
  en: {
    LAWYER_VOICE: {
      id: "ThT5KcBeYPX3keUQqHPh",
      volume: 0
    },
    GLITCH_VOICE: {
      id: "XrExE9yKIg1WjnnlVkGX",
      volume: -10
    }
  },
  es: {
    LAWYER_VOICE: {
      id: "ThT5KcBeYPX3keUQqHPh",
      volume: 0
    },
    GLITCH_VOICE: {
      id: "XrExE9yKIg1WjnnlVkGX",
      volume: -10
    }
  }
};


export async function POST(request: Request) {
  try {
    const { text, language = 'en' } = await request.json();
    console.log('language:', language)
    console.log('text:', text)

    if (!VOICES[language as keyof typeof VOICES]) {
      return NextResponse.json(
        { error: 'Language not supported' },
        { status: 400 }
      );
    }

    const segments = text.split('*');
    let finalText = '';

    for (let i = 0; i < segments.length; i++) {
      const segment = segments[i].trim();
      if (segment !== "") {
        if (i % 2 === 1) {
          // Pour les segments glitch, on utilise une voix différente
          const voiceConfig = VOICES[language as keyof typeof VOICES].GLITCH_VOICE;
          const response = await fetch(
            `https://api.elevenlabs.io/v1/text-to-speech/${voiceConfig.id}`,
            {
              method: 'POST',
              headers: {
                'Accept': 'audio/mpeg',
                'Content-Type': 'application/json',
                'xi-api-key': process.env.ELEVEN_LABS_API_KEY!
              },
              body: JSON.stringify({
                text: segment,
                model_id: "eleven_monolingual_v1",
                voice_settings: {
                  stability: 0.5,
                  similarity_boost: 0.75
                }
              })
            }
          );

          if (!response.ok) {
            throw new Error('Failed to generate glitch voice');
          }

          const audioBuffer = await response.arrayBuffer();
          return new NextResponse(audioBuffer, {
            headers: {
              'Content-Type': 'audio/mpeg'
            }
          });
        } else {
          // Pour les segments normaux
          // const voiceConfig = VOICES[language as keyof typeof VOICES].LAWYER_VOICE;
          finalText += "..." + segment + "...";
        }
      }
    }

    console.log('process.env.ELEVEN_LABS_API_KEY:', process.env.ELEVEN_LABS_API_KEY)

    // Si aucun segment glitch n'a été trouvé, on génère la voix normale
    const response = await fetch(
      `https://api.elevenlabs.io/v1/text-to-speech/${VOICES[language as keyof typeof VOICES].LAWYER_VOICE.id}`,
      {
        method: 'POST',
        headers: {
          'Accept': 'audio/mpeg',
          'Content-Type': 'application/json',
          'xi-api-key': process.env.ELEVEN_LABS_API_KEY!
        },
        body: JSON.stringify({
          text: finalText,
          model_id: "eleven_monolingual_v1",
          voice_settings: {
            stability: 0.5,
            similarity_boost: 0.75
          }
        })
      }
    );

    console.log('response:', response);

    if (!response.ok) {
      throw new Error('Failed to generate voice');
    }

    const audioBuffer = await response.arrayBuffer();
    return new NextResponse(audioBuffer, {
      headers: {
        'Content-Type': 'audio/mpeg'
      }
    });

  } catch (error) {
    console.error('Voice generation error:', error);
    return NextResponse.json(
      { error: 'Failed to generate voice' },
      { status: 500 }
    );
  }
}