Skip to content

Commit af1e817

Browse files
committed
[BUGFIX] environment configuration on python startup
[TASK] make energy, pause and phrase_time_limit configurable
1 parent 97f32f3 commit af1e817

File tree

6 files changed

+114
-33
lines changed

6 files changed

+114
-33
lines changed

FyneApp.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Website = "https://github.com/Sharrnah/whispering"
55
Name = "Whispering Tiger"
66
ID = "tiger.whispering"
77
Version = "1.0.0"
8-
Build = 10
8+
Build = 11

Pages/Profiles.go

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ type CurrentPlaybackDevice struct {
2929
OutputWaveWidget *widget.ProgressBar
3030
Context *malgo.AllocatedContext
3131

32-
device *malgo.Device
33-
stopChannel chan bool
34-
playTestAudio bool
35-
testAudioChannels uint32
32+
device *malgo.Device
33+
stopChannel chan bool
34+
playTestAudio bool
35+
testAudioChannels uint32
3636
testAudioSampleRate uint32
3737
}
3838

@@ -218,7 +218,6 @@ func (c *CurrentPlaybackDevice) Init() {
218218
}
219219
}
220220

221-
222221
//######################
223222
var err error
224223
c.Context, err = malgo.InitContext([]malgo.Backend{malgo.BackendWinmm}, malgo.ContextConfig{}, func(message string) {
@@ -233,7 +232,6 @@ func (c *CurrentPlaybackDevice) Init() {
233232
c.Context.Free()
234233
}()
235234

236-
237235
// run as long as no stop signal is received
238236
c.stopChannel = make(chan bool)
239237
for {
@@ -324,6 +322,29 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
324322

325323
profileForm.Append("", layout.NewSpacer())
326324

325+
energySliderState := widget.NewLabel("0.0")
326+
energySliderWidget := widget.NewSlider(0, 1000)
327+
energySliderWidget.OnChanged = func(value float64) {
328+
energySliderState.SetText(fmt.Sprintf("%.0f", value))
329+
}
330+
profileForm.Append("Speech detection Level", container.NewBorder(nil, nil, nil, energySliderState, energySliderWidget))
331+
332+
pauseSliderState := widget.NewLabel("0.0")
333+
pauseSliderWidget := widget.NewSlider(0, 5)
334+
pauseSliderWidget.Step = 0.1
335+
pauseSliderWidget.OnChanged = func(value float64) {
336+
pauseSliderState.SetText(fmt.Sprintf("%.1f", value))
337+
}
338+
profileForm.Append("Speech pause detection", container.NewBorder(nil, nil, nil, pauseSliderState, pauseSliderWidget))
339+
340+
phraseLimitSliderState := widget.NewLabel("0.0")
341+
phraseLimitSliderWidget := widget.NewSlider(0, 50)
342+
phraseLimitSliderWidget.Step = 0.1
343+
phraseLimitSliderWidget.OnChanged = func(value float64) {
344+
phraseLimitSliderState.SetText(fmt.Sprintf("%.1f", value))
345+
}
346+
profileForm.Append("Phrase time limit", container.NewBorder(nil, nil, nil, phraseLimitSliderState, phraseLimitSliderWidget))
347+
327348
profileForm.Append("A.I. Device for Speech to Text", CustomWidget.NewTextValueSelect("ai_device", []CustomWidget.TextValueOption{
328349
{Text: "CUDA", Value: "cuda"},
329350
{Text: "CPU", Value: "cpu"},
@@ -421,6 +442,10 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
421442
Current_language: "",
422443
Osc_ip: "127.0.0.1",
423444
Osc_port: 9000,
445+
446+
Phrase_time_limit: 0.0,
447+
Pause: 0.8,
448+
Energy: 300,
424449
}
425450
err = profileSettings.LoadYamlSettings(settingsFiles[id])
426451
if err != nil {
@@ -461,14 +486,19 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
461486

462487
// audio progressbar
463488
// spacer
489+
profileForm.Items[8].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Energy))
490+
profileForm.Items[9].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Pause))
491+
profileForm.Items[10].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Phrase_time_limit))
492+
493+
464494
if profileSettings.Ai_device != nil {
465-
profileForm.Items[8].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Ai_device.(string))
495+
profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Ai_device.(string))
466496
}
467-
profileForm.Items[9].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Model)
497+
profileForm.Items[12].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Model)
468498
// spacer
469-
profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Txt_translator_size)
470-
profileForm.Items[12].Widget.(*widget.Check).SetChecked(profileSettings.Tts_enabled)
471-
profileForm.Items[13].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Tts_ai_device)
499+
profileForm.Items[14].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Txt_translator_size)
500+
profileForm.Items[15].Widget.(*widget.Check).SetChecked(profileSettings.Tts_enabled)
501+
profileForm.Items[16].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Tts_ai_device)
472502

473503
profileForm.OnSubmit = func() {
474504
profileSettings.Websocket_ip = profileForm.Items[0].Widget.(*widget.Entry).Text
@@ -478,12 +508,16 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
478508

479509
profileSettings.Device_out_index, _ = strconv.Atoi(profileForm.Items[5].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value)
480510

481-
profileSettings.Ai_device = profileForm.Items[8].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
482-
profileSettings.Model = profileForm.Items[9].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
511+
profileSettings.Energy = int(profileForm.Items[8].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value)
512+
profileSettings.Pause = profileForm.Items[9].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value
513+
profileSettings.Phrase_time_limit = profileForm.Items[10].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value
483514

484-
profileSettings.Txt_translator_size = profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
485-
profileSettings.Tts_enabled = profileForm.Items[12].Widget.(*widget.Check).Checked
486-
profileSettings.Tts_ai_device = profileForm.Items[13].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
515+
profileSettings.Ai_device = profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
516+
profileSettings.Model = profileForm.Items[12].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
517+
518+
profileSettings.Txt_translator_size = profileForm.Items[14].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
519+
profileSettings.Tts_enabled = profileForm.Items[15].Widget.(*widget.Check).Checked
520+
profileSettings.Tts_ai_device = profileForm.Items[16].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
487521

488522
// update existing settings or create new one if it does not exist yet
489523
if Utilities.FileExists(settingsFiles[id]) {
@@ -498,10 +532,14 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
498532
Txt_translator_size: profileSettings.Txt_translator_size,
499533
Websocket_ip: profileSettings.Websocket_ip,
500534
Websocket_port: profileSettings.Websocket_port,
501-
Osc_ip: profileSettings.Osc_ip,
502-
Osc_port: profileSettings.Osc_port,
535+
Osc_ip: profileSettings.Osc_ip,
536+
Osc_port: profileSettings.Osc_port,
503537
Tts_enabled: profileSettings.Tts_enabled,
504538
Tts_ai_device: profileSettings.Tts_ai_device,
539+
540+
Phrase_time_limit: profileSettings.Phrase_time_limit,
541+
Pause: profileSettings.Pause,
542+
Energy: profileSettings.Energy,
505543
}
506544
newProfileEntry.Save(settingsFiles[id])
507545
}
@@ -547,7 +585,6 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
547585
profileList.Refresh()
548586
}), newProfileEntry)
549587

550-
551588
mainContent := container.NewHSplit(
552589
container.NewMax(profileHelpTextContent, profileListContent),
553590
container.NewBorder(newProfileRow, nil, nil, nil, profileList),

Profiles/profiles.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ type Profile struct {
1111
SettingsFilename string
1212
Device_index interface{} `yaml:"device_index"`
1313
Device_out_index interface{} `yaml:"device_out_index"`
14+
15+
Phrase_time_limit float64 `yaml:"phrase_time_limit,omitempty"`
16+
Pause float64 `yaml:"pause,omitempty"`
17+
Energy int `yaml:"energy,omitempty"`
18+
1419
// Whisper Settings
1520
Ai_device interface{} `yaml:"ai_device"`
1621
Model string `yaml:"model"`

RuntimeBackend/Whisper.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package RuntimeBackend
33
import (
44
"errors"
55
"io"
6+
"os"
67
"os/exec"
8+
"strings"
79
"syscall"
810
"whispering-tiger-ui/Utilities"
911
)
@@ -25,6 +27,10 @@ func (c *WhisperProcessConfig) RunWithStreams(name string, arguments []string, s
2527
proc.SysProcAttr = &syscall.SysProcAttr{
2628
HideWindow: true,
2729
}
30+
// attach environment variables
31+
if c.environmentVars != nil {
32+
proc.Env = c.environmentVars
33+
}
2834

2935
proc.Stdout = stdOut
3036
proc.Stdin = stdIn
@@ -36,12 +42,13 @@ func (c *WhisperProcessConfig) RunWithStreams(name string, arguments []string, s
3642
}
3743

3844
type WhisperProcessConfig struct {
39-
DeviceIndex string
40-
DeviceOutIndex string
41-
SettingsFile string
42-
Program *exec.Cmd
43-
ReaderBackend *io.PipeReader
44-
WriterBackend *io.PipeWriter
45+
DeviceIndex string
46+
DeviceOutIndex string
47+
SettingsFile string
48+
Program *exec.Cmd
49+
ReaderBackend *io.PipeReader
50+
WriterBackend *io.PipeWriter
51+
environmentVars []string
4552
}
4653

4754
func NewWhisperProcess() WhisperProcessConfig {
@@ -78,6 +85,27 @@ func (c *WhisperProcessConfig) Stop() {
7885
}
7986
}
8087

88+
func (c *WhisperProcessConfig) AttachEnvironment(envName, envValue string) {
89+
c.environmentVars = os.Environ()
90+
91+
envIndex := -1
92+
for index, element := range c.environmentVars {
93+
if strings.HasPrefix(element, envName + "=") {
94+
envIndex = index
95+
}
96+
}
97+
98+
if value, ok := os.LookupEnv(envName); !ok {
99+
c.environmentVars = append(c.environmentVars, envName + "=" + envValue)
100+
} else {
101+
if envIndex > -1 {
102+
c.environmentVars[envIndex] = envName + "=" + envValue + ";" + value
103+
} else {
104+
c.environmentVars = append(c.environmentVars, envName + "=" + envValue + ";" + value)
105+
}
106+
}
107+
}
108+
81109
func (c *WhisperProcessConfig) Start() {
82110
go func(writer io.Writer, reader io.Reader) {
83111
var tmpReader io.Reader

Settings/settings.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type Conf struct {
2222
Device_index interface{} `yaml:"device_index,omitempty"`
2323
Device_out_index interface{} `yaml:"device_out_index,omitempty"`
2424

25+
Phrase_time_limit float64 `yaml:"phrase_time_limit,omitempty"`
26+
Pause float64 `yaml:"pause,omitempty"`
27+
Energy int `yaml:"energy,omitempty"`
28+
2529
// Whisper Settings
2630
Ai_device interface{} `yaml:"ai_device"`
2731
Whisper_task string `yaml:"whisper_task"`
@@ -185,14 +189,14 @@ func (c *Conf) SetOption(optionName string, value interface{}) {
185189
}
186190
case reflect.String:
187191
switch value.(type) {
188-
case int:
189-
setValue = reflect.ValueOf(strconv.Itoa(value.(int)))
192+
case int:
193+
setValue = reflect.ValueOf(strconv.Itoa(value.(int)))
190194
}
191195
case reflect.Int:
192196
switch value.(type) {
193-
case string:
194-
tmpValue, _ := strconv.Atoi(value.(string))
195-
setValue = reflect.ValueOf(tmpValue)
197+
case string:
198+
tmpValue, _ := strconv.Atoi(value.(string))
199+
setValue = reflect.ValueOf(tmpValue)
196200
}
197201

198202
}
@@ -360,7 +364,7 @@ func BuildSettingsForm(includeConfigFields []string, settingsFile string) fyne.C
360364

361365
Config.WriteYamlSettings(settingsFile)
362366

363-
dialog.ShowInformation("Settings Saved", "Settings have been saved to "+settingsFile + "\n This requires a restart of the application currently.", fyne.CurrentApp().Driver().AllWindows()[0])
367+
dialog.ShowInformation("Settings Saved", "Settings have been saved to "+settingsFile+"\n This requires a restart of the application currently.", fyne.CurrentApp().Driver().AllWindows()[0])
364368
}
365369
}
366370

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"whispering-tiger-ui/Pages"
1313
"whispering-tiger-ui/RuntimeBackend"
1414
"whispering-tiger-ui/Settings"
15+
"whispering-tiger-ui/Utilities"
1516
"whispering-tiger-ui/websocket"
1617
)
1718

@@ -83,6 +84,12 @@ func main() {
8384
RuntimeBackend.BackendsList[0].DeviceIndex = strconv.Itoa(Settings.Config.Device_index.(int))
8485
RuntimeBackend.BackendsList[0].DeviceOutIndex = strconv.Itoa(Settings.Config.Device_out_index.(int))
8586
RuntimeBackend.BackendsList[0].SettingsFile = Settings.Config.SettingsFilename
87+
if Utilities.FileExists("ffmpeg/bin/ffmpeg.exe") {
88+
appExec, _ := os.Executable()
89+
appPath := filepath.Dir(appExec)
90+
91+
RuntimeBackend.BackendsList[0].AttachEnvironment("Path", filepath.Join(appPath, "ffmpeg/bin"))
92+
}
8693
RuntimeBackend.BackendsList[0].Start()
8794

8895
// initialize main window
@@ -112,7 +119,7 @@ func main() {
112119

113120
profilePage := Pages.CreateProfileWindow(onProfileClose)
114121
profileWindow.SetContent(profilePage)
115-
profileWindow.Resize(fyne.NewSize(1200, 600))
122+
profileWindow.Resize(fyne.NewSize(1400, 700))
116123

117124
profileWindow.CenterOnScreen()
118125
profileWindow.Show()

0 commit comments

Comments
 (0)