-
Notifications
You must be signed in to change notification settings - Fork 0
/
sndio.go
105 lines (90 loc) · 1.79 KB
/
sndio.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
package sndio
/*
#cgo LDFLAGS: -lsndio
#include <sndio.h>
*/
import "C"
import (
"errors"
"unsafe"
)
type Device struct {
hdl *C.struct_sio_hdl
}
const (
Play = C.SIO_PLAY
Record = C.SIO_REC
AnyDevice = C.SIO_DEVANY
)
func Open(name string, mode int, noblock bool) (*Device, error) {
Cnoblock := C.int(0)
if noblock {
Cnoblock = C.int(1)
}
hdl := C.sio_open(C.CString(name), C.uint(mode), Cnoblock)
if hdl == nil {
return nil, errors.New("sndio: unable to open unit")
}
device := &Device{
hdl: hdl,
}
return device, nil
}
func (d *Device) Close() error {
C.sio_close(d.hdl)
return nil
}
type Parameters struct {
PlayChans int
RecordChans int
Bits int
Signed bool
LittleEndian bool
}
func (d *Device) SetParameters(p *Parameters) (*Parameters, error) {
par := &C.struct_sio_par{}
C.sio_initpar(par)
par.pchan = C.uint(p.PlayChans)
par.rchan = C.uint(p.RecordChans)
par.bits = C.uint(p.Bits)
par.sig = C.uint(0)
if p.Signed {
par.sig = C.uint(1)
}
par.le = C.uint(0)
if p.LittleEndian {
par.le = C.uint(1)
}
e := C.sio_setpar(d.hdl, par)
if e == 0 {
return nil, errors.New("sio_setpar failed")
}
e = C.sio_getpar(d.hdl, par)
if e == 0 {
return nil, errors.New("sio_getpar failed")
}
// TODO: check parameters
return nil, nil
}
func (d *Device) Start() error {
e := C.sio_start(d.hdl)
if e == 0 {
return errors.New("start failed")
}
return nil
}
func (d *Device) Stop() error {
e := C.sio_stop(d.hdl)
if e == 0 {
return errors.New("stop failed")
}
return nil
}
func (d *Device) Read(p []byte) (int, error) {
n := C.sio_read(d.hdl, unsafe.Pointer(&p[0]), C.size_t(len(p)))
return int(n), nil
}
func (d *Device) Write(p []byte) (int, error) {
n := C.sio_write(d.hdl, unsafe.Pointer(&p[0]), C.size_t(len(p)))
return int(n), nil
}