66 "fmt"
77 "fyne.io/fyne/v2"
88 "fyne.io/fyne/v2/container"
9+ "fyne.io/fyne/v2/dialog"
910 "fyne.io/fyne/v2/layout"
1011 "fyne.io/fyne/v2/widget"
1112 "github.com/gen2brain/malgo"
@@ -26,10 +27,13 @@ type CurrentPlaybackDevice struct {
2627 OutputDeviceName string
2728 InputWaveWidget * widget.ProgressBar
2829 OutputWaveWidget * widget.ProgressBar
29- Context * malgo.Context
30+ Context * malgo.AllocatedContext
3031
32+ device * malgo.Device
3133 stopChannel chan bool
3234 playTestAudio bool
35+ testAudioChannels uint32
36+ testAudioSampleRate uint32
3337}
3438
3539func (c * CurrentPlaybackDevice ) Stop () {
@@ -51,22 +55,7 @@ func int32Map(x int32, in_min int32, in_max int32, out_min int32, out_max int32)
5155 return int32 (r )
5256}
5357
54- func (c * CurrentPlaybackDevice ) Init () {
55- if c .OutputWaveWidget == nil {
56- c .OutputWaveWidget = widget .NewProgressBar ()
57- c .OutputWaveWidget .Max = 100.0
58- c .OutputWaveWidget .TextFormatter = func () string {
59- return ""
60- }
61- }
62- if c .InputWaveWidget == nil {
63- c .InputWaveWidget = widget .NewProgressBar ()
64- c .InputWaveWidget .Max = 100.0
65- c .InputWaveWidget .TextFormatter = func () string {
66- return ""
67- }
68- }
69-
58+ func (c * CurrentPlaybackDevice ) InitTestAudio () (* bytes.Reader , * wav.Reader ) {
7059 byteReader := bytes .NewReader (resourceTestWav .Content ())
7160
7261 testAudioReader := wav .NewReader (byteReader )
@@ -77,24 +66,20 @@ func (c *CurrentPlaybackDevice) Init() {
7766 os .Exit (1 )
7867 }
7968
80- testAudioChannels : = uint32 (testAudioFormat .NumChannels )
81- testAudioSampleRate : = testAudioFormat .SampleRate
69+ c . testAudioChannels = uint32 (testAudioFormat .NumChannels )
70+ c . testAudioSampleRate = testAudioFormat .SampleRate
8271
83- //######################
72+ return byteReader , testAudioReader
73+ }
8474
85- ctx , err := malgo .InitContext ([]malgo.Backend {malgo .BackendWinmm }, malgo.ContextConfig {}, func (message string ) {
86- fmt .Printf ("LOG <%v>\n " , message )
87- })
88- if err != nil {
89- fmt .Println (err )
90- os .Exit (1 )
75+ func (c * CurrentPlaybackDevice ) InitDevices () {
76+ byteReader , testAudioReader := c .InitTestAudio ()
77+
78+ if c .device != nil && c .device .IsStarted () {
79+ c .device .Uninit ()
9180 }
92- defer func () {
93- _ = ctx .Uninit ()
94- ctx .Free ()
95- }()
9681
97- captureDevices , err := ctx .Devices (malgo .Capture )
82+ captureDevices , err := c . Context .Devices (malgo .Capture )
9883 if err != nil {
9984 fmt .Println (err )
10085 }
@@ -108,7 +93,7 @@ func (c *CurrentPlaybackDevice) Init() {
10893 }
10994 }
11095
111- playbackDevices , err := ctx .Devices (malgo .Playback )
96+ playbackDevices , err := c . Context .Devices (malgo .Playback )
11297 if err != nil {
11398 fmt .Println (err )
11499 }
@@ -131,9 +116,9 @@ func (c *CurrentPlaybackDevice) Init() {
131116 if selectedPlaybackDeviceIndex > - 1 {
132117 deviceConfig .Playback .DeviceID = playbackDevices [selectedPlaybackDeviceIndex ].ID .Pointer ()
133118 }
134- deviceConfig .Playback .Channels = testAudioChannels
119+ deviceConfig .Playback .Channels = c . testAudioChannels
135120 //deviceConfig.SampleRate = 44100
136- deviceConfig .SampleRate = testAudioSampleRate
121+ deviceConfig .SampleRate = c . testAudioSampleRate
137122 deviceConfig .Alsa .NoMMap = 1
138123
139124 sizeInBytesCapture := uint32 (malgo .SampleSizeInBytes (deviceConfig .Capture .Format ))
@@ -204,25 +189,60 @@ func (c *CurrentPlaybackDevice) Init() {
204189 captureCallbacks := malgo.DeviceCallbacks {
205190 Data : onRecvFrames ,
206191 }
207- device , err : = malgo .InitDevice (ctx .Context , deviceConfig , captureCallbacks )
192+ c . device , err = malgo .InitDevice (c . Context .Context , deviceConfig , captureCallbacks )
208193 if err != nil {
209194 fmt .Println (err )
210195 //os.Exit(1)
211196 }
212197
213- err = device .Start ()
198+ err = c . device .Start ()
214199 if err != nil {
215200 fmt .Println (err )
216201 //os.Exit(1)
217202 }
203+ }
204+
205+ func (c * CurrentPlaybackDevice ) Init () {
206+ if c .OutputWaveWidget == nil {
207+ c .OutputWaveWidget = widget .NewProgressBar ()
208+ c .OutputWaveWidget .Max = 100.0
209+ c .OutputWaveWidget .TextFormatter = func () string {
210+ return ""
211+ }
212+ }
213+ if c .InputWaveWidget == nil {
214+ c .InputWaveWidget = widget .NewProgressBar ()
215+ c .InputWaveWidget .Max = 100.0
216+ c .InputWaveWidget .TextFormatter = func () string {
217+ return ""
218+ }
219+ }
220+
221+
222+ //######################
223+ var err error
224+ c .Context , err = malgo .InitContext ([]malgo.Backend {malgo .BackendWinmm }, malgo.ContextConfig {}, func (message string ) {
225+ fmt .Printf ("LOG <%v>\n " , message )
226+ })
227+ if err != nil {
228+ fmt .Println (err )
229+ os .Exit (1 )
230+ }
231+ defer func () {
232+ _ = c .Context .Uninit ()
233+ c .Context .Free ()
234+ }()
235+
218236
219237 // run as long as no stop signal is received
220238 c .stopChannel = make (chan bool )
221239 for {
222240 select {
223241 case <- c .stopChannel :
224242 fmt .Println ("stopping..." )
225- device .Uninit ()
243+ if c .device != nil {
244+ c .device .Uninit ()
245+ }
226246 return
227247 }
228248 }
@@ -255,6 +275,8 @@ func GetAudioDevices(deviceType malgo.DeviceType) ([]CustomWidget.TextValueOptio
255275
256276func CreateProfileWindow (onClose func ()) fyne.CanvasObject {
257277 playBackDevice := CurrentPlaybackDevice {}
278+ //playBackDevice.Stop()
279+
258280 go playBackDevice .Init ()
259281
260282 audioInputDevices , _ := GetAudioDevices (malgo .Capture )
@@ -279,9 +301,10 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
279301 profileForm .Append ("Audio Input (mic)" , CustomWidget .NewTextValueSelect ("device_index" , audioInputDevices ,
280302 func (s CustomWidget.TextValueOption ) {
281303 println (s .Value )
282- playBackDevice .Stop ()
304+ // playBackDevice.Stop()
283305 playBackDevice .InputDeviceName = s .Text
284- go playBackDevice .Init ()
306+ playBackDevice .InitDevices ()
307+ //go playBackDevice.Init()
285308 },
286309 0 ),
287310 )
@@ -290,9 +313,10 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
290313 profileForm .Append ("Audio Output (speaker)" , CustomWidget .NewTextValueSelect ("device_out_index" , audioOutputDevices ,
291314 func (s CustomWidget.TextValueOption ) {
292315 println (s .Value )
293- playBackDevice .Stop ()
316+ // playBackDevice.Stop()
294317 playBackDevice .OutputDeviceName = s .Text
295- go playBackDevice .Init ()
318+ playBackDevice .InitDevices ()
319+ //go playBackDevice.Init()
296320 },
297321 0 ),
298322 )
@@ -338,7 +362,11 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
338362
339363 //profileOptions := []string{"device_index", "device_out_index", "ai_device", "model", "txt_translator_size", "websocket_ip", "websocket_port", "tts_enabled", "tts_ai_device"}
340364 //profileWindow := Settings.BuildSettingsForm(profileOptions, "").(*widget.Form)
365+
341366 profileListContent := container .NewVScroll (BuildProfileForm ())
367+ profileListContent .Hide ()
368+
369+ profileHelpTextContent := container .NewVScroll (widget .NewLabel ("Select an existing Profile or create a new one.\n \n Click Save and Load Profile." ))
342370
343371 // build profile list
344372 var settingsFiles []string
@@ -365,6 +393,9 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
365393 )
366394
367395 profileList .OnSelected = func (id widget.ListItemID ) {
396+ profileHelpTextContent .Hide ()
397+ profileListContent .Show ()
398+
368399 //profileSettings := Settings.Conf{
369400 // Websocket_ip: "127.0.0.1",
370401 // Websocket_port: 5000,
@@ -391,39 +422,43 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
391422 Osc_ip : "127.0.0.1" ,
392423 Osc_port : 9000 ,
393424 }
394- profileSettings .LoadYamlSettings (settingsFiles [id ])
425+ err = profileSettings .LoadYamlSettings (settingsFiles [id ])
426+ if err != nil {
427+ dialog .ShowError (err , fyne .CurrentApp ().Driver ().AllWindows ()[1 ])
428+ }
395429 profileForm := profileListContent .Content .(* widget.Form )
396430 profileForm .SubmitText = "Save and Load Profile"
397431 profileForm .Items [0 ].Widget .(* widget.Entry ).SetText (profileSettings .Websocket_ip )
398432 profileForm .Items [1 ].Widget .(* widget.Entry ).SetText (strconv .Itoa (profileSettings .Websocket_port ))
399433 // spacer
434+ deviceInValue := "-1"
435+ deviceInWidget := profileForm .Items [3 ].Widget .(* CustomWidget.TextValueSelect )
400436 if profileSettings .Device_index != nil {
401- deviceInValue := "-1"
402437 switch profileSettings .Device_index .(type ) {
403438 case int :
404439 deviceInValue = strconv .Itoa (profileSettings .Device_index .(int ))
405440 case string :
406441 deviceInValue = profileSettings .Device_index .(string )
407442 }
408- deviceInWidget := profileForm .Items [3 ].Widget .(* CustomWidget.TextValueSelect )
409- if profileSettings .Device_index != nil && deviceInWidget .GetSelected ().Value != deviceInValue {
410- deviceInWidget .SetSelected (deviceInValue )
411- }
443+ }
444+ if deviceInWidget .GetSelected ().Value != deviceInValue {
445+ deviceInWidget .SetSelected (deviceInValue )
412446 }
413447 // audio progressbar
448+ deviceOutValue := "-1"
449+ deviceOutWidget := profileForm .Items [5 ].Widget .(* CustomWidget.TextValueSelect )
414450 if profileSettings .Device_out_index != nil {
415- deviceOutValue := "-1"
416451 switch profileSettings .Device_out_index .(type ) {
417452 case int :
418453 deviceOutValue = strconv .Itoa (profileSettings .Device_out_index .(int ))
419454 case string :
420455 deviceOutValue = profileSettings .Device_out_index .(string )
421456 }
422- deviceOutWidget := profileForm .Items [5 ].Widget .(* CustomWidget.TextValueSelect )
423- if deviceOutWidget .GetSelected ().Value != deviceOutValue {
424- profileForm .Items [5 ].Widget .(* CustomWidget.TextValueSelect ).SetSelected (deviceOutValue )
425- }
426457 }
458+ if deviceOutWidget .GetSelected ().Value != deviceOutValue {
459+ deviceOutWidget .SetSelected (deviceOutValue )
460+ }
461+
427462 // audio progressbar
428463 // spacer
429464 if profileSettings .Ai_device != nil {
@@ -439,9 +474,9 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
439474 profileSettings .Websocket_ip = profileForm .Items [0 ].Widget .(* widget.Entry ).Text
440475 profileSettings .Websocket_port , _ = strconv .Atoi (profileForm .Items [1 ].Widget .(* widget.Entry ).Text )
441476
442- profileSettings .Device_index = profileForm .Items [3 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value
477+ profileSettings .Device_index , _ = strconv . Atoi ( profileForm .Items [3 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value )
443478
444- profileSettings .Device_out_index = profileForm .Items [5 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value
479+ profileSettings .Device_out_index , _ = strconv . Atoi ( profileForm .Items [5 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value )
445480
446481 profileSettings .Ai_device = profileForm .Items [8 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value
447482 profileSettings .Model = profileForm .Items [9 ].Widget .(* CustomWidget.TextValueSelect ).GetSelected ().Value
@@ -463,6 +498,8 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
463498 Txt_translator_size : profileSettings .Txt_translator_size ,
464499 Websocket_ip : profileSettings .Websocket_ip ,
465500 Websocket_port : profileSettings .Websocket_port ,
501+ Osc_ip : profileSettings .Osc_ip ,
502+ Osc_port : profileSettings .Osc_port ,
466503 Tts_enabled : profileSettings .Tts_enabled ,
467504 Tts_ai_device : profileSettings .Tts_ai_device ,
468505 }
@@ -478,6 +515,8 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
478515
479516 profileForm .Refresh ()
480517
518+ playBackDevice .InitDevices ()
519+
481520 //profileListContent.Content = profileForm
482521
483522 }
@@ -508,8 +547,9 @@ func CreateProfileWindow(onClose func()) fyne.CanvasObject {
508547 profileList .Refresh ()
509548 }), newProfileEntry )
510549
550+
511551 mainContent := container .NewHSplit (
512- profileListContent ,
552+ container . NewMax ( profileHelpTextContent , profileListContent ) ,
513553 container .NewBorder (newProfileRow , nil , nil , nil , profileList ),
514554 //container.NewMax(profileList),
515555 )
0 commit comments