Files
beatglow/audio/registry.go
T
2026-10-29 17:29:41 +01:00

42 lines
863 B
Go

package audio
import (
"fmt"
"sync"
"github.com/sirupsen/logrus"
"go.maze.io/harmon/config"
)
// NotFound signals the player type can not be found.
type NotFound struct {
Type string
}
func (err NotFound) Error() string {
return fmt.Sprintf("audio driver %q can not be found", err.Type)
}
type Opener func(config config.Audio) (Stream, error)
var (
protocols sync.Map
)
func Register(name string, opener Opener) {
logrus.WithField("type", name).Debug("register audio driver")
if _, loaded := protocols.LoadOrStore(name, opener); loaded {
panic(fmt.Sprintf("audio type %q already registered", name))
}
}
func New(config config.Audio) (Stream, error) {
logrus.WithField("type", config.Type).Debug("load audio driver")
opener, ok := protocols.Load(config.Type)
if ok {
return opener.(Opener)(config)
}
return nil, NotFound{config.Type}
}