This repository was archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.go
More file actions
76 lines (61 loc) · 1.67 KB
/
device.go
File metadata and controls
76 lines (61 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gosound
import (
"context"
"github.com/pkg/errors"
)
var (
// ErrDeviceNotSupported is returned when the requested device is not supported
ErrDeviceNotSupported = errors.New("device not supported")
)
// DisplayFunc defines the callback for when a premix buffer is mixed/rendered and output on the device
type DisplayFunc func(deviceKind Kind, premix *PremixData)
// Device is an interface to output device operations
type Device interface {
Name() string
Play(in <-chan *PremixData) error
PlayWithCtx(ctx context.Context, in <-chan *PremixData) error
Close()
}
type kindGetter interface {
GetKind() Kind
}
type createOutputDeviceFunc func(settings Settings) (Device, error)
type deviceDetails struct {
create createOutputDeviceFunc
Kind Kind
}
// GetKind returns the kind for the passed in device
func GetKind(d Device) Kind {
if dev, ok := d.(kindGetter); ok {
return dev.GetKind()
}
return KindNone
}
var (
// Map is the mapping of device name to device details
Map = make(map[string]deviceDetails)
)
// CreateOutputDevice creates an output device based on the provided settings
func CreateOutputDevice(settings Settings) (Device, error) {
if details, ok := Map[settings.Name]; ok && details.create != nil {
dev, err := details.create(settings)
if err != nil {
return nil, err
}
return dev, nil
}
return nil, errors.Wrap(ErrDeviceNotSupported, settings.Name)
}
type device struct {
Device
onRowOutput DisplayFunc
}
// Settings is the settings for configuring an output device
type Settings struct {
Name string
Channels int
SamplesPerSecond int
BitsPerSample int
Filepath string
OnRowOutput DisplayFunc
}