This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys_pulseaudio.c
199 lines (169 loc) · 5.43 KB
/
sys_pulseaudio.c
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
/*
Faun PulseAudio backend
*/
#include <pulse/pulseaudio.h>
typedef struct {
pa_mainloop* loop;
pa_context* context;
pa_stream* stream;
}
PulseSession;
static PulseSession paSession;
static void sysaudio_close()
{
if (paSession.context) {
pa_context_disconnect(paSession.context);
pa_context_unref(paSession.context);
paSession.context = NULL;
}
if (paSession.loop) {
pa_mainloop_free(paSession.loop);
paSession.loop = NULL;
}
}
static const char* sysaudio_open(const char* appName)
{
int error;
paSession.stream = NULL;
paSession.loop = pa_mainloop_new();
paSession.context = pa_context_new(pa_mainloop_get_api(paSession.loop),
appName);
if (! paSession.context) {
sysaudio_close();
return "pa_context_new failed";
}
error = pa_context_connect(paSession.context, NULL, 0, NULL);
if (error) {
sysaudio_close();
return "pa_context_connect failed";
}
return NULL;
}
static const char* sysaudio_allocVoice(FaunVoice* voice, int updateHz,
const char* appName)
{
// This table is synced with FaunSampleFormat.
static const uint8_t _paFormat[FAUN_FORMAT_COUNT] = {
PA_SAMPLE_U8,
PA_SAMPLE_S16NE,
PA_SAMPLE_S24NE,
PA_SAMPLE_FLOAT32NE
};
pa_sample_spec ss;
pa_buffer_attr ba;
int error;
pa_usec_t dur = 2500000 / updateHz;
//= 50 * 1000; // 50 ms latency
(void) appName;
ss.channels = faun_channelCount(voice->mix.chanLayout);
ss.rate = voice->mix.rate;
ss.format = _paFormat[voice->mix.format];
// Use default attributes except for a lower latency.
// This can be overridden by the PULSE_LATENCY_MSEC environment variable.
ba.maxlength = -1;
ba.tlength = pa_usec_to_bytes(dur, &ss);
ba.prebuf = -1;
ba.minreq = -1;
ba.fragsize = -1;
while (! paSession.stream) {
error = pa_mainloop_iterate(paSession.loop, 1, NULL);
if (error < 0)
return pa_strerror(error);
switch (pa_context_get_state(paSession.context)) {
case PA_CONTEXT_READY:
paSession.stream = pa_stream_new(paSession.context,
"Faun Voice", &ss, NULL);
if (! paSession.stream)
return "pa_stream_new failed";
break;
case PA_CONTEXT_FAILED:
return "PA_CONTEXT_FAILED";
case PA_CONTEXT_TERMINATED:
return "PA_CONTEXT_TERMINATED";
default:
break;
}
}
// Use the default device & volume. Flags are the same as pa_simple_new.
error = pa_stream_connect_playback(paSession.stream, NULL, &ba,
PA_STREAM_INTERPOLATE_TIMING |
PA_STREAM_ADJUST_LATENCY |
PA_STREAM_AUTO_TIMING_UPDATE,
NULL, NULL);
if (error)
return pa_strerror(error);
for (;;) {
pa_stream_state_t state = pa_stream_get_state(paSession.stream);
if (state == PA_STREAM_READY) {
#if 0
const pa_buffer_attr* sa =
pa_stream_get_buffer_attr(paSession.stream);
printf("KR buffer_attr %d %d %d %d\n",
sa->maxlength, sa->tlength, sa->prebuf, sa->minreq);
#endif
break;
}
if (state != PA_STREAM_CREATING)
return "pa_stream_connect_playback failed";
error = pa_mainloop_iterate(paSession.loop, 1, NULL);
if (error < 0)
return pa_strerror(error);
}
voice->backend = &paSession;
return NULL;
}
#define PS ((PulseSession*) voice->backend)
static void sysaudio_freeVoice(FaunVoice *voice)
{
PulseSession* ps = PS;
if (ps) {
pa_stream_disconnect(ps->stream);
pa_stream_unref(ps->stream);
voice->backend = NULL;
}
}
static const char* sysaudio_write(FaunVoice* voice, const void* data,
uint32_t len)
{
PulseSession* ps = PS;
size_t nr;
int error;
// Use pa_stream_writable_size to throttle the output, but feed all data
// with a single write so that we can return ASAP. The actual write
// limit is buffer_attr.maxlength.
while (! (nr = pa_stream_writable_size(ps->stream))) {
//printf("KR nr .\n");
error = pa_mainloop_iterate(ps->loop, 1, NULL);
if (error < 0)
return pa_strerror(error);
}
//printf("KR nr %ld\n", nr);
pa_stream_write(ps->stream, data, len, NULL, 0, PA_SEEK_RELATIVE);
pa_mainloop_iterate(ps->loop, 0, NULL);
return NULL;
}
static void _corkComplete(pa_stream *s, int success, void *userdata)
{
(void) s;
(void) userdata;
// NOTE: success is zero when un-corking a pause of more than 30 sec.
#ifdef DEBUG
if (! success)
fprintf(stderr /*_errStream*/, "pa_stream_cork failed\n");
#else
(void) success;
#endif
}
static int sysaudio_startVoice(FaunVoice *voice)
{
pa_stream_cork(PS->stream, 0, _corkComplete, NULL);
return 1;
}
static int sysaudio_stopVoice(FaunVoice *voice)
{
PulseSession* ps = PS;
pa_stream_cork(ps->stream, 1, _corkComplete, NULL);
// Must run loop for cork to take effect.
pa_mainloop_iterate(ps->loop, 0, NULL);
return 1;
}