-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoices.go
More file actions
177 lines (156 loc) · 4.28 KB
/
voices.go
File metadata and controls
177 lines (156 loc) · 4.28 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package elevenlabs
import (
"context"
"github.com/plexusone/elevenlabs-go/internal/api"
)
// VoicesService handles voice operations.
type VoicesService struct {
client *Client
}
// Voice represents an ElevenLabs voice.
type Voice struct {
// VoiceID is the unique identifier for the voice.
VoiceID string
// Name is the display name of the voice.
Name string
// Category is the category of the voice (e.g., "premade", "cloned").
Category string
// Description is the description of the voice.
Description string
// PreviewURL is the URL to preview the voice.
PreviewURL string
// Labels contains additional metadata about the voice.
Labels map[string]string
}
// List returns all available voices.
func (s *VoicesService) List(ctx context.Context) ([]*Voice, error) {
resp, err := s.client.apiClient.GetVoices(ctx, api.GetVoicesParams{})
if err != nil {
return nil, err
}
// Handle response type
switch r := resp.(type) {
case *api.GetVoicesResponseModel:
voices := make([]*Voice, 0, len(r.Voices))
for _, v := range r.Voices {
voice := &Voice{
VoiceID: v.VoiceID,
Name: v.Name,
Category: string(v.Category),
Labels: make(map[string]string),
}
if v.Description.Set && !v.Description.Null {
voice.Description = v.Description.Value
}
if v.PreviewURL.Set && !v.PreviewURL.Null {
voice.PreviewURL = v.PreviewURL.Value
}
// Convert labels
for k, val := range v.Labels {
voice.Labels[k] = val
}
voices = append(voices, voice)
}
return voices, nil
default:
return nil, &APIError{Message: "unexpected response type"}
}
}
// Get returns a voice by ID.
func (s *VoicesService) Get(ctx context.Context, voiceID string) (*Voice, error) {
if voiceID == "" {
return nil, ErrEmptyVoiceID
}
resp, err := s.client.apiClient.GetVoiceByID(ctx, api.GetVoiceByIDParams{
VoiceID: voiceID,
})
if err != nil {
return nil, err
}
// Handle response type
switch r := resp.(type) {
case *api.VoiceResponseModel:
voice := &Voice{
VoiceID: r.VoiceID,
Name: r.Name,
Category: string(r.Category),
Labels: make(map[string]string),
}
if r.Description.Set && !r.Description.Null {
voice.Description = r.Description.Value
}
if r.PreviewURL.Set && !r.PreviewURL.Null {
voice.PreviewURL = r.PreviewURL.Value
}
// Convert labels
for k, val := range r.Labels {
voice.Labels[k] = val
}
return voice, nil
default:
return nil, &APIError{Message: "unexpected response type"}
}
}
// GetSettings returns the settings for a voice.
func (s *VoicesService) GetSettings(ctx context.Context, voiceID string) (*VoiceSettings, error) {
if voiceID == "" {
return nil, ErrEmptyVoiceID
}
resp, err := s.client.apiClient.GetVoiceSettings(ctx, api.GetVoiceSettingsParams{
VoiceID: voiceID,
})
if err != nil {
return nil, err
}
// Handle response type
switch r := resp.(type) {
case *api.VoiceSettingsResponseModel:
settings := &VoiceSettings{}
if r.Stability.Set && !r.Stability.Null {
settings.Stability = r.Stability.Value
}
if r.SimilarityBoost.Set && !r.SimilarityBoost.Null {
settings.SimilarityBoost = r.SimilarityBoost.Value
}
if r.Style.Set && !r.Style.Null {
settings.Style = r.Style.Value
}
if r.Speed.Set && !r.Speed.Null {
settings.Speed = r.Speed.Value
}
return settings, nil
default:
return nil, &APIError{Message: "unexpected response type"}
}
}
// GetDefaultSettings returns the default voice settings.
func (s *VoicesService) GetDefaultSettings(ctx context.Context) (*VoiceSettings, error) {
resp, err := s.client.apiClient.GetVoiceSettingsDefault(ctx)
if err != nil {
return nil, err
}
settings := &VoiceSettings{}
if resp.Stability.Set && !resp.Stability.Null {
settings.Stability = resp.Stability.Value
}
if resp.SimilarityBoost.Set && !resp.SimilarityBoost.Null {
settings.SimilarityBoost = resp.SimilarityBoost.Value
}
if resp.Style.Set && !resp.Style.Null {
settings.Style = resp.Style.Value
}
if resp.Speed.Set && !resp.Speed.Null {
settings.Speed = resp.Speed.Value
}
return settings, nil
}
// Delete deletes a voice by ID.
func (s *VoicesService) Delete(ctx context.Context, voiceID string) error {
if voiceID == "" {
return ErrEmptyVoiceID
}
_, err := s.client.apiClient.DeleteVoice(ctx, api.DeleteVoiceParams{
VoiceID: voiceID,
})
return err
}