2022-06-25 20:11:15 +01:00
|
|
|
package voicemeeter
|
|
|
|
|
2022-06-30 23:20:15 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-06-26 01:36:32 +01:00
|
|
|
// observer defines the interface any registered observers must satisfy
|
2022-06-25 20:11:15 +01:00
|
|
|
type observer interface {
|
|
|
|
OnUpdate(subject string)
|
|
|
|
}
|
|
|
|
|
2022-06-30 23:08:35 +01:00
|
|
|
// publisher defines methods that support observers
|
2022-06-26 01:41:05 +01:00
|
|
|
type publisher struct {
|
2022-06-25 20:11:15 +01:00
|
|
|
observerList []observer
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:36:32 +01:00
|
|
|
// Register adds an observer to observerList
|
2022-06-26 01:41:05 +01:00
|
|
|
func (p *publisher) Register(o observer) {
|
2022-06-25 20:11:15 +01:00
|
|
|
p.observerList = append(p.observerList, o)
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:36:32 +01:00
|
|
|
// Deregister removes an observer from observerList
|
2022-06-26 01:41:05 +01:00
|
|
|
func (p *publisher) Deregister(o observer) {
|
2022-06-25 20:11:15 +01:00
|
|
|
var indexToRemove int
|
|
|
|
|
|
|
|
for i, observer := range p.observerList {
|
|
|
|
if observer == o {
|
|
|
|
indexToRemove = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.observerList = append(p.observerList[:indexToRemove], p.observerList[indexToRemove+1:]...)
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:36:32 +01:00
|
|
|
// notify updates observers of any changes
|
2022-06-26 01:41:05 +01:00
|
|
|
func (p *publisher) notify(subject string) {
|
2022-06-25 20:11:15 +01:00
|
|
|
for _, observer := range p.observerList {
|
|
|
|
observer.OnUpdate(subject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 23:08:35 +01:00
|
|
|
// pooler continuously polls the dirty paramters
|
2022-06-26 01:36:32 +01:00
|
|
|
// it is expected to be run in a goroutine
|
2022-06-26 01:41:05 +01:00
|
|
|
type pooler struct {
|
2022-07-09 19:01:58 +01:00
|
|
|
k *kind
|
2022-06-25 20:11:15 +01:00
|
|
|
run bool
|
2022-06-26 01:41:05 +01:00
|
|
|
publisher
|
2022-06-25 20:11:15 +01:00
|
|
|
}
|
|
|
|
|
2022-07-09 19:01:58 +01:00
|
|
|
func newPooler(k *kind) *pooler {
|
2022-06-26 01:41:05 +01:00
|
|
|
p := &pooler{
|
2022-07-09 19:01:58 +01:00
|
|
|
k: k,
|
2022-06-25 20:11:15 +01:00
|
|
|
run: true,
|
|
|
|
}
|
|
|
|
go p.runner()
|
2022-07-09 19:01:58 +01:00
|
|
|
go p.levels()
|
2022-06-25 20:11:15 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-26 01:41:05 +01:00
|
|
|
func (p *pooler) runner() {
|
2022-06-26 01:36:32 +01:00
|
|
|
for p.run {
|
2022-06-25 20:11:15 +01:00
|
|
|
if pdirty() {
|
2022-06-26 01:36:32 +01:00
|
|
|
p.notify("pdirty")
|
2022-06-25 20:11:15 +01:00
|
|
|
}
|
|
|
|
if mdirty() {
|
2022-06-26 01:36:32 +01:00
|
|
|
p.notify("mdirty")
|
2022-06-25 20:11:15 +01:00
|
|
|
}
|
2022-06-30 23:20:15 +01:00
|
|
|
time.Sleep(33 * time.Millisecond)
|
2022-06-25 20:11:15 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-09 19:01:58 +01:00
|
|
|
|
|
|
|
func (p *pooler) levels() {
|
|
|
|
_levelCache = newLevelCache(p.k)
|
|
|
|
|
|
|
|
for p.run {
|
|
|
|
if ldirty(p.k) {
|
2022-07-18 16:23:15 +01:00
|
|
|
update(_levelCache.stripLevels, _levelCache.stripLevelsBuff, (2*p.k.PhysIn)+(8*p.k.VirtIn))
|
|
|
|
update(_levelCache.busLevels, _levelCache.busLevelsBuff, 8*p.k.NumBus())
|
2022-07-09 19:01:58 +01:00
|
|
|
p.notify("ldirty")
|
|
|
|
}
|
|
|
|
time.Sleep(33 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|