voicemeeter/publisher.go

85 lines
1.6 KiB
Go
Raw Permalink Normal View History

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