Skip to content

Commit

Permalink
[BUGFIX] environment configuration on python startup
Browse files Browse the repository at this point in the history
[TASK] make energy, pause and phrase_time_limit configurable
  • Loading branch information
Sharrnah committed Dec 11, 2022
1 parent 97f32f3 commit af1e817
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 33 deletions.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/Sharrnah/whispering"
Name = "Whispering Tiger"
ID = "tiger.whispering"
Version = "1.0.0"
Build = 10
Build = 11
75 changes: 56 additions & 19 deletions Pages/Profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type CurrentPlaybackDevice struct {
OutputWaveWidget *widget.ProgressBar
Context *malgo.AllocatedContext

device *malgo.Device
stopChannel chan bool
playTestAudio bool
testAudioChannels uint32
device *malgo.Device
stopChannel chan bool
playTestAudio bool
testAudioChannels uint32
testAudioSampleRate uint32
}

Expand Down Expand Up @@ -218,7 +218,6 @@ func (c *CurrentPlaybackDevice) Init() {
}
}


//######################
var err error
c.Context, err = malgo.InitContext([]malgo.Backend{malgo.BackendWinmm}, malgo.ContextConfig{}, func(message string) {
Expand All @@ -233,7 +232,6 @@ func (c *CurrentPlaybackDevice) Init() {
c.Context.Free()
}()


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

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

energySliderState := widget.NewLabel("0.0")
energySliderWidget := widget.NewSlider(0, 1000)
energySliderWidget.OnChanged = func(value float64) {
energySliderState.SetText(fmt.Sprintf("%.0f", value))
}
profileForm.Append("Speech detection Level", container.NewBorder(nil, nil, nil, energySliderState, energySliderWidget))

pauseSliderState := widget.NewLabel("0.0")
pauseSliderWidget := widget.NewSlider(0, 5)
pauseSliderWidget.Step = 0.1
pauseSliderWidget.OnChanged = func(value float64) {
pauseSliderState.SetText(fmt.Sprintf("%.1f", value))
}
profileForm.Append("Speech pause detection", container.NewBorder(nil, nil, nil, pauseSliderState, pauseSliderWidget))

phraseLimitSliderState := widget.NewLabel("0.0")
phraseLimitSliderWidget := widget.NewSlider(0, 50)
phraseLimitSliderWidget.Step = 0.1
phraseLimitSliderWidget.OnChanged = func(value float64) {
phraseLimitSliderState.SetText(fmt.Sprintf("%.1f", value))
}
profileForm.Append("Phrase time limit", container.NewBorder(nil, nil, nil, phraseLimitSliderState, phraseLimitSliderWidget))

profileForm.Append("A.I. Device for Speech to Text", CustomWidget.NewTextValueSelect("ai_device", []CustomWidget.TextValueOption{
{Text: "CUDA", Value: "cuda"},
{Text: "CPU", Value: "cpu"},
Expand Down Expand Up @@ -421,6 +442,10 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
Current_language: "",
Osc_ip: "127.0.0.1",
Osc_port: 9000,

Phrase_time_limit: 0.0,
Pause: 0.8,
Energy: 300,
}
err = profileSettings.LoadYamlSettings(settingsFiles[id])
if err != nil {
Expand Down Expand Up @@ -461,14 +486,19 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {

// audio progressbar
// spacer
profileForm.Items[8].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Energy))
profileForm.Items[9].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Pause))
profileForm.Items[10].Widget.(*fyne.Container).Objects[0].(*widget.Slider).SetValue(float64(profileSettings.Phrase_time_limit))


if profileSettings.Ai_device != nil {
profileForm.Items[8].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Ai_device.(string))
profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Ai_device.(string))
}
profileForm.Items[9].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Model)
profileForm.Items[12].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Model)
// spacer
profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Txt_translator_size)
profileForm.Items[12].Widget.(*widget.Check).SetChecked(profileSettings.Tts_enabled)
profileForm.Items[13].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Tts_ai_device)
profileForm.Items[14].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Txt_translator_size)
profileForm.Items[15].Widget.(*widget.Check).SetChecked(profileSettings.Tts_enabled)
profileForm.Items[16].Widget.(*CustomWidget.TextValueSelect).SetSelected(profileSettings.Tts_ai_device)

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

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

profileSettings.Ai_device = profileForm.Items[8].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Model = profileForm.Items[9].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Energy = int(profileForm.Items[8].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value)
profileSettings.Pause = profileForm.Items[9].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value
profileSettings.Phrase_time_limit = profileForm.Items[10].Widget.(*fyne.Container).Objects[0].(*widget.Slider).Value

profileSettings.Txt_translator_size = profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Tts_enabled = profileForm.Items[12].Widget.(*widget.Check).Checked
profileSettings.Tts_ai_device = profileForm.Items[13].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Ai_device = profileForm.Items[11].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Model = profileForm.Items[12].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value

profileSettings.Txt_translator_size = profileForm.Items[14].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value
profileSettings.Tts_enabled = profileForm.Items[15].Widget.(*widget.Check).Checked
profileSettings.Tts_ai_device = profileForm.Items[16].Widget.(*CustomWidget.TextValueSelect).GetSelected().Value

// update existing settings or create new one if it does not exist yet
if Utilities.FileExists(settingsFiles[id]) {
Expand All @@ -498,10 +532,14 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
Txt_translator_size: profileSettings.Txt_translator_size,
Websocket_ip: profileSettings.Websocket_ip,
Websocket_port: profileSettings.Websocket_port,
Osc_ip: profileSettings.Osc_ip,
Osc_port: profileSettings.Osc_port,
Osc_ip: profileSettings.Osc_ip,
Osc_port: profileSettings.Osc_port,
Tts_enabled: profileSettings.Tts_enabled,
Tts_ai_device: profileSettings.Tts_ai_device,

Phrase_time_limit: profileSettings.Phrase_time_limit,
Pause: profileSettings.Pause,
Energy: profileSettings.Energy,
}
newProfileEntry.Save(settingsFiles[id])
}
Expand Down Expand Up @@ -547,7 +585,6 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
profileList.Refresh()
}), newProfileEntry)


mainContent := container.NewHSplit(
container.NewMax(profileHelpTextContent, profileListContent),
container.NewBorder(newProfileRow, nil, nil, nil, profileList),
Expand Down
5 changes: 5 additions & 0 deletions Profiles/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type Profile struct {
SettingsFilename string
Device_index interface{} `yaml:"device_index"`
Device_out_index interface{} `yaml:"device_out_index"`

Phrase_time_limit float64 `yaml:"phrase_time_limit,omitempty"`
Pause float64 `yaml:"pause,omitempty"`
Energy int `yaml:"energy,omitempty"`

// Whisper Settings
Ai_device interface{} `yaml:"ai_device"`
Model string `yaml:"model"`
Expand Down
40 changes: 34 additions & 6 deletions RuntimeBackend/Whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package RuntimeBackend
import (
"errors"
"io"
"os"
"os/exec"
"strings"
"syscall"
"whispering-tiger-ui/Utilities"
)
Expand All @@ -25,6 +27,10 @@ func (c *WhisperProcessConfig) RunWithStreams(name string, arguments []string, s
proc.SysProcAttr = &syscall.SysProcAttr{
HideWindow: true,
}
// attach environment variables
if c.environmentVars != nil {
proc.Env = c.environmentVars
}

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

type WhisperProcessConfig struct {
DeviceIndex string
DeviceOutIndex string
SettingsFile string
Program *exec.Cmd
ReaderBackend *io.PipeReader
WriterBackend *io.PipeWriter
DeviceIndex string
DeviceOutIndex string
SettingsFile string
Program *exec.Cmd
ReaderBackend *io.PipeReader
WriterBackend *io.PipeWriter
environmentVars []string
}

func NewWhisperProcess() WhisperProcessConfig {
Expand Down Expand Up @@ -78,6 +85,27 @@ func (c *WhisperProcessConfig) Stop() {
}
}

func (c *WhisperProcessConfig) AttachEnvironment(envName, envValue string) {
c.environmentVars = os.Environ()

envIndex := -1
for index, element := range c.environmentVars {
if strings.HasPrefix(element, envName + "=") {
envIndex = index
}
}

if value, ok := os.LookupEnv(envName); !ok {
c.environmentVars = append(c.environmentVars, envName + "=" + envValue)
} else {
if envIndex > -1 {
c.environmentVars[envIndex] = envName + "=" + envValue + ";" + value
} else {
c.environmentVars = append(c.environmentVars, envName + "=" + envValue + ";" + value)
}
}
}

func (c *WhisperProcessConfig) Start() {
go func(writer io.Writer, reader io.Reader) {
var tmpReader io.Reader
Expand Down
16 changes: 10 additions & 6 deletions Settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Conf struct {
Device_index interface{} `yaml:"device_index,omitempty"`
Device_out_index interface{} `yaml:"device_out_index,omitempty"`

Phrase_time_limit float64 `yaml:"phrase_time_limit,omitempty"`
Pause float64 `yaml:"pause,omitempty"`
Energy int `yaml:"energy,omitempty"`

// Whisper Settings
Ai_device interface{} `yaml:"ai_device"`
Whisper_task string `yaml:"whisper_task"`
Expand Down Expand Up @@ -185,14 +189,14 @@ func (c *Conf) SetOption(optionName string, value interface{}) {
}
case reflect.String:
switch value.(type) {
case int:
setValue = reflect.ValueOf(strconv.Itoa(value.(int)))
case int:
setValue = reflect.ValueOf(strconv.Itoa(value.(int)))
}
case reflect.Int:
switch value.(type) {
case string:
tmpValue, _ := strconv.Atoi(value.(string))
setValue = reflect.ValueOf(tmpValue)
case string:
tmpValue, _ := strconv.Atoi(value.(string))
setValue = reflect.ValueOf(tmpValue)
}

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

Config.WriteYamlSettings(settingsFile)

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

Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"whispering-tiger-ui/Pages"
"whispering-tiger-ui/RuntimeBackend"
"whispering-tiger-ui/Settings"
"whispering-tiger-ui/Utilities"
"whispering-tiger-ui/websocket"
)

Expand Down Expand Up @@ -83,6 +84,12 @@ func main() {
RuntimeBackend.BackendsList[0].DeviceIndex = strconv.Itoa(Settings.Config.Device_index.(int))
RuntimeBackend.BackendsList[0].DeviceOutIndex = strconv.Itoa(Settings.Config.Device_out_index.(int))
RuntimeBackend.BackendsList[0].SettingsFile = Settings.Config.SettingsFilename
if Utilities.FileExists("ffmpeg/bin/ffmpeg.exe") {
appExec, _ := os.Executable()
appPath := filepath.Dir(appExec)

RuntimeBackend.BackendsList[0].AttachEnvironment("Path", filepath.Join(appPath, "ffmpeg/bin"))
}
RuntimeBackend.BackendsList[0].Start()

// initialize main window
Expand Down Expand Up @@ -112,7 +119,7 @@ func main() {

profilePage := Pages.CreateProfileWindow(onProfileClose)
profileWindow.SetContent(profilePage)
profileWindow.Resize(fyne.NewSize(1200, 600))
profileWindow.Resize(fyne.NewSize(1400, 700))

profileWindow.CenterOnScreen()
profileWindow.Show()
Expand Down

0 comments on commit af1e817

Please sign in to comment.