-
Notifications
You must be signed in to change notification settings - Fork 7
/
beep_linux.go
227 lines (204 loc) · 4.59 KB
/
beep_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
217
218
219
220
221
222
223
224
225
226
227
//go:build linux
// +build linux
package beep
/*
#cgo LDFLAGS: -lasound
#include <alsa/asoundlib.h>
void *cbuf[2];
*/
import "C"
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/user"
"strconv"
"strings"
"unsafe"
)
var (
pcmHandle *C.snd_pcm_t
pcmHwParams *C.snd_pcm_hw_params_t
)
// OpenSoundDevice opens hardware sound device
func OpenSoundDevice(device string) error {
code := C.snd_pcm_open(
&pcmHandle,
C.CString(device),
C.SND_PCM_STREAM_PLAYBACK,
0)
if code < 0 {
err := fmt.Errorf("snd_pcm_open: %v\n", strerror(code))
fmt.Fprintln(os.Stderr, err)
return err
}
C.snd_pcm_drop(pcmHandle)
return nil
}
// InitSoundDevice initialize sound device
func InitSoundDevice() error {
var sampleFormat C.snd_pcm_format_t = C.SND_PCM_FORMAT_S8
if sample16bit {
sampleFormat = C.SND_PCM_FORMAT_S16
}
if code := C.snd_pcm_hw_params_malloc(&pcmHwParams); code < 0 {
err := fmt.Errorf("snd_pcm_hw_params_malloc: %v\n", strerror(code))
fmt.Fprintln(os.Stderr, err)
return err
}
if code := C.snd_pcm_hw_params_any(pcmHandle, pcmHwParams); code < 0 {
err := fmt.Errorf("snd_pcm_hw_params_any: %v\n", strerror(code))
fmt.Fprintln(os.Stderr, err)
return err
}
// C.SND_PCM_ACCESS_RW_NONINTERLEAVED - is not working with PulseAudio
code := C.snd_pcm_set_params(
pcmHandle,
sampleFormat,
C.SND_PCM_ACCESS_RW_INTERLEAVED,
1,
44100,
1,
500000)
if code < 0 {
err := fmt.Errorf("snd_pcm_set_params: %v\n", strerror(code))
fmt.Fprintln(os.Stderr, err)
return err
}
code = C.snd_pcm_prepare(pcmHandle)
if code < 0 {
err := fmt.Errorf("snd_pcm_prepare: %v\n", strerror(code))
fmt.Fprintln(os.Stderr, err)
return err
}
return nil
}
// Playback sends stereo wave buffer to sound device
func (m *Music) Playback(buf1, buf2 []int16) {
bufsize := len(buf1)
if bufsize < SampleRate {
// prevent buffer underrun
rest := make([]int16, SampleRate)
buf1 = append(buf1, rest...)
//buf2 = append(buf2, rest...)
}
// Changing to single channel interleaved buffer format for PulseAudio
buf := unsafe.Pointer(&buf1[0])
for {
n := C.snd_pcm_writei(pcmHandle, buf, C.snd_pcm_uframes_t(bufsize))
written := int(n)
if written < 0 {
if m.stopping {
break
}
// error
code := C.int(written)
written = 0
_ = written
fmt.Fprintln(os.Stderr, "snd_pcm_writei:", code, strerror(code))
code = C.snd_pcm_recover(pcmHandle, code, 0)
if code < 0 {
fmt.Fprintln(os.Stderr, "snd_pcm_recover:", strerror(code))
break
}
}
break // don't retry, breaks timing
/*
if written == bufsize {
break
}
if written == 0 {
C.snd_pcm_wait(pcmHandle, 1000)
continue
}
fmt.Fprintf(os.Stderr, "snd_pcm_writei: wrote: %d/%d\n", written, bufsize)
buffers = []unsafe.Pointer{
unsafe.Pointer(&buf1[written]),
unsafe.Pointer(&buf2[written]),
}
pos = &buffers[0]
bufsize -= written
*/
}
m.linePlayed <- true // notify that playback is done
}
// FlushSoundBuffer flushes sound buffer
func FlushSoundBuffer() {
if pcmHandle != nil {
C.snd_pcm_drain(pcmHandle)
}
}
// StopPlayBack stops play back
func StopPlayBack() {
if pcmHandle != nil {
C.snd_pcm_drop(pcmHandle)
}
}
func strerror(code C.int) string {
return C.GoString(C.snd_strerror(code))
}
// CloseSoundDevice closes sound device
func CloseSoundDevice() {
if pcmHandle != nil {
C.snd_pcm_close(pcmHandle)
C.snd_pcm_hw_free(pcmHandle)
}
}
// SendBell send bell sound to console
func SendBell() {
bell := []byte{7}
os.Stdout.Write(bell)
console, err := os.OpenFile("/dev/console", os.O_WRONLY, 0644)
if err != nil {
console, err = os.OpenFile("/dev/tty0", os.O_WRONLY, 0644)
if err != nil {
return
}
}
defer console.Close()
console.Write(bell)
}
// HomeDir returns user's home directory
func HomeDir() string {
var home string
usr, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, "Unable to get current user directory.")
home = "/home"
} else {
home = usr.HomeDir
}
return home + "/.beep"
}
// BatteryLevel return battery charge level.
func BatteryLevel() (int, error) {
const prefix = "POWER_SUPPLY_CAPACITY="
var uevent *os.File
for i := 0; i <= 3; i++ {
fname := fmt.Sprintf("/sys/class/power_supply/BAT%d/uevent", i)
file, err := os.Open(fname)
if err != nil {
log.Println(err)
continue
}
defer file.Close()
uevent = file
break
}
if uevent == nil {
return 0, io.EOF
}
scanner := bufio.NewScanner(uevent)
var level int
err := io.EOF
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, prefix) {
level, err = strconv.Atoi(strings.Split(line, prefix)[1])
break
}
}
return level, err
}