-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
197 lines (170 loc) · 5.04 KB
/
writer.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
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2021
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
package pulseaudio
import (
"fmt"
"unsafe"
)
// #cgo pkg-config: libpulse libpulse-simple
//
// #include <stdlib.h>
// #include <pulse/simple.h>
import "C"
type writer struct {
simple *C.pa_simple
sampleSpec C.pa_sample_spec
config Config
}
func (s writer) UnsafeWrite(ptr unsafe.Pointer, size int) error {
var errCode C.int
if C.pa_simple_write(
s.simple,
ptr,
C.size_t(size),
&errCode,
) >= 0 {
return nil
}
return fmt.Errorf("pulseaudio: bad write, err code %d", int(errCode))
}
func (s writer) Close() {
C.pa_simple_free(s.simple)
}
func newWriter(cfg Config) (*writer, error) {
var (
paErr C.int
attr *C.pa_buffer_attr
)
switch cfg.Format {
case SampleFormatFloat32NE:
break
default:
return nil, fmt.Errorf("pulseaudio: unsupported stream format")
}
sampleSpec := C.pa_sample_spec{}
sampleSpec.format = C.pa_sample_format_t(cfg.Format)
sampleSpec.channels = C.uint8_t(cfg.Channels)
sampleSpec.rate = C.uint32_t(cfg.Rate)
// TODO(paultag): Add in support for frag, in particular.
// attr = &C.pa_buffer_attr{}
if C.pa_channels_valid(sampleSpec.channels) == 0 {
return nil, fmt.Errorf(
"pulseaudio: channels '%d' is invalid",
sampleSpec.channels,
)
}
if C.pa_sample_rate_valid(sampleSpec.rate) == 0 {
return nil, fmt.Errorf(
"pulseaudio: sample rate '%d' is invalid",
sampleSpec.rate,
)
}
var (
sinkName *C.char = nil
)
if cfg.SinkName != "" {
sinkName = C.CString(cfg.SinkName)
defer C.free(unsafe.Pointer(sinkName))
}
simple := C.pa_simple_new(
nil,
C.CString(cfg.AppName),
C.PA_STREAM_PLAYBACK,
sinkName,
C.CString(cfg.StreamName),
&sampleSpec,
nil,
attr,
&paErr,
)
if simple == nil {
return nil, rvToErr(paErr)
}
return &writer{
simple: simple,
sampleSpec: sampleSpec,
config: cfg,
}, nil
}
// NewWriter creates a new pulseaudio.Writer object, allowing audio to be
// written into Pulse.
func NewWriter(cfg Config) (*Writer, error) {
w, err := newWriter(cfg)
if err != nil {
return nil, err
}
return &Writer{writer: *w}, nil
}
// Writer is an encapsulation of the underlying pulseaudio stream, allowing
// for an idiomatic Go library interface to that stream.
type Writer struct {
writer writer
}
// Close will close the stream, and perform all cleanup required.
func (w Writer) Close() {
w.writer.Close()
}
// write2F32NE is called from `Write` when the provided input type is of
// type [][2]float32
func (w Writer) write2F32NE(samples [][2]float32) error {
if w.writer.config.Format != SampleFormatFloat32NE {
return fmt.Errorf("pulseaudio: stream format isn't float32 NE")
}
if w.writer.config.Channels != 2 {
return fmt.Errorf("pulseaudio: wrong number of channels provided")
}
return w.writer.UnsafeWrite(
unsafe.Pointer(&samples[0]),
len(samples)*int(unsafe.Sizeof(samples[0])),
)
}
// writeF32NE is called from `Write` when the provided input type is of
// type []float32
func (w Writer) writeF32NE(samples []float32) error {
if w.writer.config.Format != SampleFormatFloat32NE {
return fmt.Errorf("pulseaudio: stream format isn't float32 NE")
}
if w.writer.config.Channels != 1 {
return fmt.Errorf("pulseaudio: wrong number of channels provided")
}
return w.writer.UnsafeWrite(
unsafe.Pointer(&samples[0]),
len(samples)*int(unsafe.Sizeof(samples[0])),
)
}
// Write will write the provided data to the pulseaudio stream. This function
// accepts any type, but will return an error if th type is not something that
// this library understands, the number of channels and the stream format.
//
// Currently accepted types:
//
// - []float32 (Format: SampleFormatFloat32NE, Channels: 1)
// - [][2]float32 (Format: SampleFormatFloat32NE, Channels: 2)
func (w Writer) Write(samples interface{}) error {
switch s := samples.(type) {
case [][2]float32:
return w.write2F32NE(s)
case []float32:
return w.writeF32NE(s)
default:
return fmt.Errorf("pulseaudio: unknown sample type")
}
}
// vim: foldmethod=marker