-
Notifications
You must be signed in to change notification settings - Fork 84
/
encode_linux.go
216 lines (202 loc) · 4.22 KB
/
encode_linux.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//+build linux
package klang
import (
"errors"
"strings"
"sync"
"github.com/oakmound/alsa"
)
type alsaAudio struct {
*Encoding
*alsa.Device
playAmount int
playProgress int
stopCh chan struct{}
playing bool
playCh chan error
period int
}
func (aa *alsaAudio) Play() <-chan error {
// If currently playing, restart
if aa.playing {
aa.playProgress = 0
return aa.playCh
}
aa.playing = true
aa.playCh = make(chan error)
go func() {
for {
var data []byte
if len(aa.Encoding.Data)-aa.playProgress <= aa.playAmount {
data = aa.Encoding.Data[aa.playProgress:]
if aa.Loop {
delta := aa.playAmount - (len(aa.Encoding.Data) - aa.playProgress)
data = append(data, aa.Encoding.Data[:delta]...)
}
} else {
data = aa.Encoding.Data[aa.playProgress : aa.playProgress+aa.playAmount]
}
if len(data) != 0 {
err := aa.Device.Write(data, aa.period)
if err != nil {
select {
case aa.playCh <- err:
default:
}
break
}
}
aa.playProgress += aa.playAmount
if aa.playProgress > len(aa.Encoding.Data) {
if aa.Loop {
aa.playProgress %= len(aa.Encoding.Data)
} else {
select {
case aa.playCh <- nil:
default:
}
break
}
}
select {
case <-aa.stopCh:
select {
case aa.playCh <- nil:
default:
}
break
default:
}
}
aa.playing = false
aa.playProgress = 0
}()
return aa.playCh
}
func (aa *alsaAudio) Stop() error {
if aa.playing {
go func() {
aa.stopCh <- struct{}{}
}()
} else {
return errors.New("Audio not playing, cannot stop")
}
return nil
}
func (aa *alsaAudio) SetVolume(int32) error {
return errors.New("SetVolume on Linux is not supported")
}
func (aa *alsaAudio) Filter(fs ...Filter) (Audio, error) {
var a Audio = aa
var err, consErr error
for _, f := range fs {
a, err = f.Apply(a)
if err != nil {
if consErr == nil {
consErr = err
} else {
consErr = errors.New(err.Error() + ":" + consErr.Error())
}
}
}
return aa, consErr
}
// MustFilter acts like Filter, but ignores errors (it does not panic,
// as filter errors are expected to be non-fatal)
func (aa *alsaAudio) MustFilter(fs ...Filter) Audio {
a, _ := aa.Filter(fs...)
return a
}
func EncodeBytes(enc Encoding) (Audio, error) {
handle, err := openDevice()
if err != nil {
return nil, err
}
// Todo: annotate these errors with more info
format, err := alsaFormat(enc.Bits)
if err != nil {
return nil, err
}
_, err = handle.NegotiateFormat(format)
if err != nil {
return nil, err
}
_, err = handle.NegotiateRate(int(enc.SampleRate))
if err != nil {
return nil, err
}
_, err = handle.NegotiateChannels(int(enc.Channels))
if err != nil {
return nil, err
}
// Default value at recommendation of library
period, err := handle.NegotiatePeriodSize(2048)
if err != nil {
return nil, err
}
_, err = handle.NegotiateBufferSize(4096)
if err != nil {
return nil, err
}
err = handle.Prepare()
if err != nil {
return nil, err
}
return &alsaAudio{
playAmount: period * int(enc.Bits) / 4,
period: period,
Encoding: &enc,
Device: handle,
stopCh: make(chan struct{}),
}, nil
}
var (
// Todo: support more customized audio device usage
openDeviceLock sync.Mutex
openedDevice *alsa.Device
)
func openDevice() (*alsa.Device, error) {
openDeviceLock.Lock()
defer openDeviceLock.Unlock()
if openedDevice != nil {
return openedDevice, nil
}
cards, err := alsa.OpenCards()
if err != nil {
return nil, err
}
defer alsa.CloseCards(cards)
for i, c := range cards {
dvcs, err := c.Devices()
if err != nil {
continue
}
for _, d := range dvcs {
if d.Type != alsa.PCM || !d.Play {
continue
}
if strings.Contains(d.Title, SkipDevicesContaining) {
continue
}
d.Close()
err := d.Open()
if err != nil {
continue
}
// We've a found a device we can hypothetically use
cards = append(cards[:i], cards[i+1:]...)
openedDevice = d
return d, nil
}
}
return nil, errors.New("No valid device found")
}
func alsaFormat(bits uint16) (alsa.FormatType, error) {
switch bits {
case 8:
return alsa.S8, nil
case 16:
return alsa.S16_LE, nil
}
return 0, errors.New("Undefined alsa format for encoding bits")
}