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
|
package gui
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
ttwidget "github.com/dweymouth/fyne-tooltip/widget"
)
// AudioPlayer is a custom widget for playing audio files
type AudioPlayer struct {
widget.BaseWidget
container *fyne.Container
playButton *ttwidget.Button
stopButton *ttwidget.Button
statusLabel *widget.Label
phoneticLabel *widget.Label
audioFile string
isPlaying bool
playCmd *exec.Cmd
voiceInfo string // Stores voice and speed info
autoPlayEnabled *bool // Pointer to parent's auto-play state
}
// NewAudioPlayer creates a new audio player widget
func NewAudioPlayer() *AudioPlayer {
p := &AudioPlayer{}
// Create controls (tooltips will be set later after tooltip layer is created)
p.playButton = ttwidget.NewButton("", p.onPlay)
p.playButton.Icon = theme.MediaPlayIcon()
p.stopButton = ttwidget.NewButton("", p.onStop)
p.stopButton.Icon = theme.MediaStopIcon()
p.statusLabel = widget.NewLabel("No audio loaded")
// Create phonetic label
p.phoneticLabel = widget.NewLabel("")
p.phoneticLabel.TextStyle = fyne.TextStyle{
Bold: true,
Italic: true,
}
// Initially disable controls
p.playButton.Disable()
p.stopButton.Disable()
// Create main container with phonetic display
p.container = container.NewHBox(
p.playButton,
p.stopButton,
p.phoneticLabel,
layout.NewSpacer(),
p.statusLabel,
)
p.ExtendBaseWidget(p)
return p
}
// CreateRenderer implements fyne.Widget
func (p *AudioPlayer) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(p.container)
}
// SetAudioFile sets the audio file to play
func (p *AudioPlayer) SetAudioFile(audioFile string) {
p.audioFile = audioFile
p.isPlaying = false
if audioFile != "" {
p.playButton.Enable()
// Try to load voice metadata
wordDir := filepath.Dir(audioFile)
metadataFile := filepath.Join(wordDir, "audio_metadata.txt")
voice := ""
speed := ""
if data, err := os.ReadFile(metadataFile); err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "voice=") {
voice = strings.TrimPrefix(line, "voice=")
} else if strings.HasPrefix(line, "speed=") {
speed = strings.TrimPrefix(line, "speed=")
}
}
}
// Store voice info
if voice != "" && speed != "" {
p.voiceInfo = fmt.Sprintf(" (voice: %s, speed: %s)", voice, speed)
} else {
p.voiceInfo = ""
}
// Format status text with voice and speed info
statusText := fmt.Sprintf("Audio: %s%s", filepath.Base(audioFile), p.voiceInfo)
p.statusLabel.SetText(statusText)
// Auto-play if enabled
if p.autoPlayEnabled != nil && *p.autoPlayEnabled {
// Small delay to ensure UI is ready
go func() {
// Wait a tiny bit for UI to be ready
time.Sleep(100 * time.Millisecond)
fyne.Do(func() {
p.onPlay()
})
}()
}
} else {
p.Clear()
}
}
// Clear clears the audio player
func (p *AudioPlayer) Clear() {
p.onStop() // Stop any playing audio
p.audioFile = ""
p.isPlaying = false
p.voiceInfo = ""
p.playButton.Disable()
p.stopButton.Disable()
p.statusLabel.SetText("No audio loaded")
p.phoneticLabel.SetText("")
}
// SetPhonetic sets the phonetic transcription text
func (p *AudioPlayer) SetPhonetic(phonetic string) {
p.phoneticLabel.SetText(phonetic)
}
// SetAutoPlayEnabled sets the reference to the auto-play state
func (p *AudioPlayer) SetAutoPlayEnabled(autoPlayEnabled *bool) {
p.autoPlayEnabled = autoPlayEnabled
}
// onPlay handles play button click
func (p *AudioPlayer) onPlay() {
if p.audioFile == "" {
return
}
if p.isPlaying {
// Pause functionality - just stop for now
p.onStop()
return
}
// Start playing
if err := p.startPlayback(); err != nil {
p.statusLabel.SetText(fmt.Sprintf("Error: %v", err))
return
}
p.isPlaying = true
p.playButton.SetIcon(theme.MediaPauseIcon())
p.stopButton.Enable()
p.statusLabel.SetText(fmt.Sprintf("Playing: %s%s", filepath.Base(p.audioFile), p.voiceInfo))
}
// onStop handles stop button click
func (p *AudioPlayer) onStop() {
if p.playCmd != nil && p.playCmd.Process != nil {
p.playCmd.Process.Kill()
p.playCmd = nil
}
p.isPlaying = false
p.playButton.SetIcon(theme.MediaPlayIcon())
p.stopButton.Disable()
p.statusLabel.SetText(fmt.Sprintf("Stopped: %s%s", filepath.Base(p.audioFile), p.voiceInfo))
}
// Play triggers audio playback
func (p *AudioPlayer) Play() {
if !p.playButton.Disabled() {
fyne.Do(func() {
p.onPlay()
})
}
}
// startPlayback starts audio playback using platform-specific commands
func (p *AudioPlayer) startPlayback() error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin": // macOS
cmd = exec.Command("afplay", p.audioFile)
case "linux":
// Try multiple commands in order of preference
// mpg123 first since it handles MP3 files best
if _, err := exec.LookPath("mpg123"); err == nil {
cmd = exec.Command("mpg123", "-q", p.audioFile) // -q for quiet mode
} else if _, err := exec.LookPath("ffplay"); err == nil {
cmd = exec.Command("ffplay", "-nodisp", "-autoexit", "-loglevel", "quiet", p.audioFile)
} else if _, err := exec.LookPath("play"); err == nil {
// SoX play command
cmd = exec.Command("play", "-q", p.audioFile)
} else if _, err := exec.LookPath("paplay"); err == nil {
cmd = exec.Command("paplay", p.audioFile)
} else if _, err := exec.LookPath("aplay"); err == nil {
cmd = exec.Command("aplay", "-q", p.audioFile)
} else {
return fmt.Errorf("no audio player found. Install mpg123, ffplay, sox, paplay, or aplay")
}
case "windows":
// Use Windows Media Player
cmd = exec.Command("cmd", "/c", "start", "/min", p.audioFile)
default:
return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
}
// Store the command so we can stop it later
p.playCmd = cmd
// Start playback in background
go func() {
err := cmd.Run()
if err == nil {
// Playback finished normally
fyne.Do(func() {
p.isPlaying = false
p.playButton.SetIcon(theme.MediaPlayIcon())
p.stopButton.Disable()
p.statusLabel.SetText(fmt.Sprintf("Finished: %s%s", filepath.Base(p.audioFile), p.voiceInfo))
})
}
}()
return nil
}
|