File size: 2,845 Bytes
5008b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03fe1d8
5008b66
03fe1d8
5008b66
 
 
 
 
 
 
 
03fe1d8
5008b66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * API configuration and utilities
 * Handles all communication with the backend
 */

// Use environment variable or default to localhost:5000
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000';

export const api = {
  /**
   * Get the full API URL for an endpoint
   */
  getUrl: (endpoint: string) => {
    // Ensure endpoint starts with /
    const path = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
    return `${API_BASE_URL}/api${path}`;
  },

  /**
   * Fetch voices list
   */
  fetchVoices: async () => {
    const response = await fetch(api.getUrl('/voices'));
    if (!response.ok) throw new Error('Failed to fetch voices');
    return response.json();
  },

  /**
   * Enroll a voice with audio file
   */
  enrollVoice: async (formData: FormData) => {
    const response = await fetch(api.getUrl('/enroll'), {
      method: 'POST',
      body: formData,
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error || 'Failed to enroll voice');
    }
    return response.json();
  },

  /**
   * Synthesize speech from text (supports multilingual: english, hindi)
   */
  synthesize: async (voiceId: string, text: string, language: string = 'english') => {
    const response = await fetch(api.getUrl('/synthesize'), {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        voice_id: voiceId,
        text: text,
        language: language,
      }),
    });
    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.error || 'Failed to synthesize speech');
    }
    return response.json();
  },

  /**
   * Get spectrogram data for audio file
   */
  getSpectrogram: async (filename: string) => {
    const response = await fetch(api.getUrl(`/spectrogram/${filename}`));
    if (!response.ok) throw new Error('Failed to fetch spectrogram');
    return response.json();
  },

  /**
   * Get audio file
   */
  getAudio: async (filename: string) => {
    const response = await fetch(api.getUrl(`/audio/${filename}`));
    if (!response.ok) throw new Error('Failed to fetch audio');
    return response.arrayBuffer();
  },

  /**
   * Delete voice
   */
  deleteVoice: async (voiceId: string) => {
    const response = await fetch(api.getUrl(`/voices/${voiceId}`), {
      method: 'DELETE',
    });
    if (!response.ok) throw new Error('Failed to delete voice');
    return response.json();
  },

  /**
   * Get audio URL
   */
  getAudioUrl: (audioPath: string) => {
    if (audioPath.startsWith('http')) {
      return audioPath; // Already a full URL
    }
    if (audioPath.startsWith('/api')) {
      return `${API_BASE_URL}${audioPath}`;
    }
    return `${API_BASE_URL}/api/audio/${audioPath}`;
  },
};

export default api;