-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
playback_control.go
77 lines (59 loc) · 1.2 KB
/
playback_control.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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
package diago
import (
"io"
"sync/atomic"
)
type AudioPlaybackControl struct {
AudioPlayback
control *audioControl
}
func (p *AudioPlaybackControl) Mute(mute bool) {
p.control.Mute(mute)
}
func (p *AudioPlaybackControl) Stop() {
p.control.Stop()
}
/*
Playback control should provide functionality like Mute Unmute over audio.
*/
type audioControl struct {
Reader io.Reader // MUST be set if usede as reader
Writer io.Writer // Must be set if used as writer
muted atomic.Bool
stop atomic.Bool
}
func (c *audioControl) Read(b []byte) (n int, err error) {
if c.stop.Load() {
return 0, io.EOF
}
n, err = c.Reader.Read(b)
if err != nil {
return n, err
}
if c.muted.Load() {
for i := range b[:n] {
b[i] = 0
}
}
return n, err
}
func (c *audioControl) Write(b []byte) (n int, err error) {
if c.stop.Load() {
return 0, io.EOF
}
if c.muted.Load() {
for i := range b {
b[i] = 0
}
}
return c.Writer.Write(b)
}
func (c *audioControl) Mute(mute bool) {
c.muted.Store(mute)
}
// Stop will stop reader/writer and return io.Eof
func (c *audioControl) Stop() {
c.stop.Store(true)
}