This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeech.go
More file actions
499 lines (399 loc) · 10.9 KB
/
Copy pathspeech.go
File metadata and controls
499 lines (399 loc) · 10.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package sdk
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
errors "github.com/xraph/go-utils/errs"
logger "github.com/xraph/go-utils/log"
"github.com/xraph/go-utils/metrics"
)
// SpeechSynthesizer generates speech from text.
type SpeechSynthesizer interface {
Synthesize(ctx context.Context, request SpeechRequest) (*SpeechResponse, error)
}
// SpeechVoice represents available voices.
type SpeechVoice string
const (
VoiceAlloy SpeechVoice = "alloy"
VoiceEcho SpeechVoice = "echo"
VoiceFable SpeechVoice = "fable"
VoiceOnyx SpeechVoice = "onyx"
VoiceNova SpeechVoice = "nova"
VoiceShimmer SpeechVoice = "shimmer"
)
// SpeechFormat represents audio output format.
type SpeechFormat string
const (
SpeechFormatMP3 SpeechFormat = "mp3"
SpeechFormatOpus SpeechFormat = "opus"
SpeechFormatAAC SpeechFormat = "aac"
SpeechFormatFLAC SpeechFormat = "flac"
SpeechFormatWAV SpeechFormat = "wav"
SpeechFormatPCM SpeechFormat = "pcm"
)
// SpeechRequest represents a speech synthesis request.
type SpeechRequest struct {
Model string `json:"model"`
Input string `json:"input"`
Voice SpeechVoice `json:"voice"`
ResponseFormat SpeechFormat `json:"response_format,omitempty"`
Speed float64 `json:"speed,omitempty"`
}
// SpeechResponse represents a speech synthesis response.
type SpeechResponse struct {
Audio io.ReadCloser `json:"-"`
Format SpeechFormat `json:"format"`
SizeBytes int64 `json:"size_bytes,omitempty"`
}
// GetBytes reads all audio data.
func (r *SpeechResponse) GetBytes() ([]byte, error) {
if r.Audio == nil {
return nil, errors.New("no audio data")
}
defer func() { _ = r.Audio.Close() }()
return io.ReadAll(r.Audio)
}
// SaveTo saves the audio to a file.
func (r *SpeechResponse) SaveTo(path string) error {
if r.Audio == nil {
return errors.New("no audio data")
}
defer func() { _ = r.Audio.Close() }()
// Clean path to prevent directory traversal
cleanPath := filepath.Clean(path)
file, err := os.Create(cleanPath)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
_, err = io.Copy(file, r.Audio)
return err
}
// SpeechBuilder provides a fluent API for speech synthesis.
type SpeechBuilder struct {
ctx context.Context
synthesizer SpeechSynthesizer
logger logger.Logger
metrics metrics.Metrics
// Request configuration
model string
input string
voice SpeechVoice
responseFormat SpeechFormat
speed float64
timeout time.Duration
// Output configuration
outputPath string
// Callbacks
onStart func()
onComplete func(*SpeechResponse)
onError func(error)
}
// NewSpeechBuilder creates a new speech builder.
func NewSpeechBuilder(ctx context.Context, synthesizer SpeechSynthesizer, logger logger.Logger, metrics metrics.Metrics) *SpeechBuilder {
return &SpeechBuilder{
ctx: ctx,
synthesizer: synthesizer,
logger: logger,
metrics: metrics,
model: "tts-1",
voice: VoiceAlloy,
responseFormat: SpeechFormatMP3,
speed: 1.0,
timeout: 120 * time.Second,
}
}
// WithModel sets the model.
func (b *SpeechBuilder) WithModel(model string) *SpeechBuilder {
b.model = model
return b
}
// WithHDModel uses the HD model.
func (b *SpeechBuilder) WithHDModel() *SpeechBuilder {
b.model = "tts-1-hd"
return b
}
// WithInput sets the text input.
func (b *SpeechBuilder) WithInput(input string) *SpeechBuilder {
b.input = input
return b
}
// WithVoice sets the voice.
func (b *SpeechBuilder) WithVoice(voice SpeechVoice) *SpeechBuilder {
b.voice = voice
return b
}
// WithFormat sets the output format.
func (b *SpeechBuilder) WithFormat(format SpeechFormat) *SpeechBuilder {
b.responseFormat = format
return b
}
// WithSpeed sets the speech speed (0.25 to 4.0).
func (b *SpeechBuilder) WithSpeed(speed float64) *SpeechBuilder {
if speed < 0.25 {
speed = 0.25
}
if speed > 4.0 {
speed = 4.0
}
b.speed = speed
return b
}
// SaveTo sets the output file path.
func (b *SpeechBuilder) SaveTo(path string) *SpeechBuilder {
b.outputPath = path
return b
}
// WithTimeout sets the timeout.
func (b *SpeechBuilder) WithTimeout(timeout time.Duration) *SpeechBuilder {
b.timeout = timeout
return b
}
// OnStart registers a callback for start.
func (b *SpeechBuilder) OnStart(fn func()) *SpeechBuilder {
b.onStart = fn
return b
}
// OnComplete registers a callback for completion.
func (b *SpeechBuilder) OnComplete(fn func(*SpeechResponse)) *SpeechBuilder {
b.onComplete = fn
return b
}
// OnError registers a callback for errors.
func (b *SpeechBuilder) OnError(fn func(error)) *SpeechBuilder {
b.onError = fn
return b
}
// Generate generates speech.
func (b *SpeechBuilder) Generate() (*SpeechResponse, error) {
if b.input == "" {
return nil, errors.New("input text is required")
}
if b.onStart != nil {
b.onStart()
}
ctx, cancel := context.WithTimeout(b.ctx, b.timeout)
defer cancel()
if b.logger != nil {
b.logger.Debug("Generating speech",
F("model", b.model),
F("voice", b.voice),
F("format", b.responseFormat),
F("input_length", len(b.input)),
)
}
startTime := time.Now()
response, err := b.synthesizer.Synthesize(ctx, SpeechRequest{
Model: b.model,
Input: b.input,
Voice: b.voice,
ResponseFormat: b.responseFormat,
Speed: b.speed,
})
duration := time.Since(startTime)
if err != nil {
if b.onError != nil {
b.onError(err)
}
if b.metrics != nil {
b.metrics.Counter("forge.ai.sdk.speech.errors", metrics.WithLabel("model", b.model), metrics.WithLabel("voice", string(b.voice))).Inc()
}
return nil, err
}
// Save to file if path specified
if b.outputPath != "" {
if err := response.SaveTo(b.outputPath); err != nil {
if b.onError != nil {
b.onError(err)
}
return nil, fmt.Errorf("failed to save audio: %w", err)
}
}
if b.logger != nil {
b.logger.Info("Speech generation completed",
F("model", b.model),
F("voice", b.voice),
F("duration", duration),
)
}
if b.metrics != nil {
b.metrics.Counter("forge.ai.sdk.speech.success", metrics.WithLabel("model", b.model), metrics.WithLabel("voice", string(b.voice))).Inc()
b.metrics.Histogram("forge.ai.sdk.speech.duration", metrics.WithLabel("model", b.model)).Observe(duration.Seconds())
}
if b.onComplete != nil {
b.onComplete(response)
}
return response, nil
}
// ExecuteAndSave generates speech and saves to the configured path.
func (b *SpeechBuilder) ExecuteAndSave() error {
if b.outputPath == "" {
return errors.New("output path not configured")
}
_, err := b.Generate()
return err
}
// SpeechChunker breaks text into speakable chunks.
type SpeechChunker struct {
maxChunkSize int
breakPoints []string
}
// NewSpeechChunker creates a new speech chunker.
func NewSpeechChunker(maxChunkSize int) *SpeechChunker {
if maxChunkSize <= 0 {
maxChunkSize = 4096 // Default OpenAI limit
}
return &SpeechChunker{
maxChunkSize: maxChunkSize,
breakPoints: []string{"\n\n", "\n", ". ", "! ", "? ", "; ", ", ", " "},
}
}
// Chunk splits text into speakable chunks.
func (c *SpeechChunker) Chunk(text string) []string {
if len(text) <= c.maxChunkSize {
return []string{text}
}
var chunks []string
remaining := text
for len(remaining) > 0 {
if len(remaining) <= c.maxChunkSize {
chunks = append(chunks, remaining)
break
}
// Find best break point
chunk := remaining[:c.maxChunkSize]
breakIdx := -1
for _, bp := range c.breakPoints {
idx := lastIndex(chunk, bp)
if idx > 0 {
breakIdx = idx + len(bp)
break
}
}
if breakIdx > 0 {
chunks = append(chunks, remaining[:breakIdx])
remaining = remaining[breakIdx:]
} else {
// Force break at max size
chunks = append(chunks, chunk)
remaining = remaining[c.maxChunkSize:]
}
}
return chunks
}
func lastIndex(s, substr string) int {
for i := len(s) - len(substr); i >= 0; i-- {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}
// BatchSpeechBuilder generates speech for multiple chunks.
type BatchSpeechBuilder struct {
builder *SpeechBuilder
chunks []string
onChunk func(int, *SpeechResponse)
}
// NewBatchSpeechBuilder creates a batch speech builder.
func NewBatchSpeechBuilder(builder *SpeechBuilder) *BatchSpeechBuilder {
return &BatchSpeechBuilder{
builder: builder,
chunks: make([]string, 0),
}
}
// WithText sets the full text to be chunked.
func (b *BatchSpeechBuilder) WithText(text string, maxChunkSize int) *BatchSpeechBuilder {
chunker := NewSpeechChunker(maxChunkSize)
b.chunks = chunker.Chunk(text)
return b
}
// WithChunks sets pre-chunked text.
func (b *BatchSpeechBuilder) WithChunks(chunks []string) *BatchSpeechBuilder {
b.chunks = chunks
return b
}
// OnChunk registers a callback for each chunk.
func (b *BatchSpeechBuilder) OnChunk(fn func(int, *SpeechResponse)) *BatchSpeechBuilder {
b.onChunk = fn
return b
}
// Generate generates speech for all chunks.
func (b *BatchSpeechBuilder) Generate() ([]*SpeechResponse, error) {
responses := make([]*SpeechResponse, 0, len(b.chunks))
for i, chunk := range b.chunks {
b.builder.input = chunk
response, err := b.builder.Generate()
if err != nil {
return responses, fmt.Errorf("chunk %d failed: %w", i, err)
}
if b.onChunk != nil {
b.onChunk(i, response)
}
responses = append(responses, response)
}
return responses, nil
}
// VoicePreset represents a voice configuration preset.
type VoicePreset struct {
Name string
Voice SpeechVoice
Speed float64
Model string
Format SpeechFormat
Description string
}
// Common voice presets.
var (
PresetNarrator = VoicePreset{
Name: "narrator",
Voice: VoiceFable,
Speed: 0.9,
Model: "tts-1-hd",
Format: SpeechFormatMP3,
Description: "Clear, dramatic narration voice",
}
PresetConversational = VoicePreset{
Name: "conversational",
Voice: VoiceAlloy,
Speed: 1.1,
Model: "tts-1",
Format: SpeechFormatMP3,
Description: "Natural, conversational voice",
}
PresetNews = VoicePreset{
Name: "news",
Voice: VoiceOnyx,
Speed: 1.0,
Model: "tts-1-hd",
Format: SpeechFormatMP3,
Description: "Professional news reader voice",
}
PresetAssistant = VoicePreset{
Name: "assistant",
Voice: VoiceNova,
Speed: 1.0,
Model: "tts-1",
Format: SpeechFormatMP3,
Description: "Friendly assistant voice",
}
PresetAudiobook = VoicePreset{
Name: "audiobook",
Voice: VoiceShimmer,
Speed: 0.85,
Model: "tts-1-hd",
Format: SpeechFormatFLAC,
Description: "High-quality audiobook voice",
}
)
// ApplyPreset applies a voice preset to the builder.
func (b *SpeechBuilder) ApplyPreset(preset VoicePreset) *SpeechBuilder {
b.voice = preset.Voice
b.speed = preset.Speed
b.model = preset.Model
b.responseFormat = preset.Format
return b
}