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
|
package audio
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/sashabaranov/go-openai"
)
// Compile-time check that OpenAIProvider implements the Provider interface.
var _ Provider = (*OpenAIProvider)(nil)
// OpenAIProvider implements Provider interface for OpenAI TTS.
// It stores only the OpenAI-specific sub-config so it never sees Gemini fields.
type OpenAIProvider struct {
client *openai.Client
config OpenAIAudioConfig
outputFormat string
}
// NewOpenAIProvider creates a new OpenAI TTS provider from the OpenAI-specific
// sub-config. Callers that have a flat Config should use NewProvider instead.
func NewOpenAIProvider(config OpenAIAudioConfig, outputFormat string) (Provider, error) {
if config.Key == "" {
return nil, errors.New("OpenAI API key is required")
}
return &OpenAIProvider{
client: openai.NewClient(config.Key),
config: config,
outputFormat: outputFormat,
}, nil
}
// GenerateAudio generates audio using OpenAI TTS
func (p *OpenAIProvider) GenerateAudio(ctx context.Context, text string, outputFile string) error {
// Validate Bulgarian text
if err := ValidateBulgarianText(text); err != nil {
return err
}
// Preprocess text for clearer Bulgarian pronunciation
processedText := p.preprocessBulgarianText(text)
// Prepare the TTS request
// OpenAI TTS will automatically detect and pronounce Bulgarian text
fmt.Printf("OpenAI TTS: Using model '%s' with voice '%s' at speed %.2f\n", p.config.Model, p.config.Voice, p.config.Speed)
if p.config.Instruction != "" && (p.config.Model == "gpt-4o-mini-tts" || p.config.Model == "gpt-4o-mini-audio-preview") {
fmt.Printf("OpenAI TTS Instruction: '%s'\n", p.config.Instruction)
}
fmt.Printf("OpenAI TTS Input: '%s'\n", processedText)
req := openai.CreateSpeechRequest{
Model: openai.SpeechModel(p.config.Model),
Input: processedText,
Voice: openai.SpeechVoice(p.config.Voice),
Speed: p.config.Speed,
}
// Add instructions for gpt-4o-mini-tts model
if p.config.Instruction != "" && (p.config.Model == "gpt-4o-mini-tts" || p.config.Model == "gpt-4o-mini-audio-preview") {
req.Instructions = p.config.Instruction
}
// Determine response format based on output file extension
ext := strings.ToLower(filepath.Ext(outputFile))
switch ext {
case ".mp3":
req.ResponseFormat = openai.SpeechResponseFormatMp3
case ".wav":
req.ResponseFormat = openai.SpeechResponseFormatWav
case ".opus":
req.ResponseFormat = openai.SpeechResponseFormatOpus
case ".aac":
req.ResponseFormat = openai.SpeechResponseFormatAac
case ".flac":
req.ResponseFormat = openai.SpeechResponseFormatFlac
default:
req.ResponseFormat = openai.SpeechResponseFormatMp3
if !strings.HasSuffix(outputFile, ".mp3") {
outputFile += ".mp3"
}
}
// Make the API call
response, err := p.client.CreateSpeech(ctx, req)
if err != nil {
// Check if it's a model access error
errStr := err.Error()
if strings.Contains(errStr, "does not have access to model") && (p.config.Model == "gpt-4o-mini-tts" || p.config.Model == "gpt-4o-mini-audio-preview") {
return fmt.Errorf("OpenAI TTS API error: %w\nNote: The %s model requires access. Try using --openai-model tts-1-hd instead", err, p.config.Model)
}
return err
}
defer func() {
_ = response.Close()
}()
// Ensure output directory exists
dir := filepath.Dir(outputFile)
if dir != "" && dir != "." {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
}
// Create output file
out, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer func() {
_ = out.Close()
}()
// Copy the audio data
written, err := io.Copy(out, response)
if err != nil {
return fmt.Errorf("failed to write audio file: %w", err)
}
if written == 0 {
return errors.New("no audio data received from OpenAI")
}
return nil
}
// Name returns the provider name
func (p *OpenAIProvider) Name() string {
return "openai"
}
// IsAvailable checks if the OpenAI API is accessible
func (p *OpenAIProvider) IsAvailable() error {
if p.config.Key == "" {
return errors.New("OpenAI API key not configured")
}
// We could make a test API call here, but that would use credits
// For now, just check that we have a key
return nil
}
// Voices returns the list of OpenAI voices supported by the app.
func (p *OpenAIProvider) Voices() []string {
return OpenAIVoices
}
// BuildAttribution returns the OpenAI attribution text for a generated audio file.
func (p *OpenAIProvider) BuildAttribution(params AttributionParams) string {
return BuildOpenAIAttribution(params)
}
// preprocessBulgarianText prepares Bulgarian text for clearer TTS pronunciation
func (p *OpenAIProvider) preprocessBulgarianText(text string) string {
return openAIProcessedText(text)
}
|