-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_jack.c
315 lines (245 loc) · 8.03 KB
/
output_jack.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Squeezelite - lightweight headless squeezebox emulator
*
* (c) Adrian Smith 2012-2015, [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* JackDMP output for squeezelite
* (c) Arne Caspari 2015, [email protected]
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// JackDMP Output
#include "squeezelite.h"
#if JACK
#include <jack/jack.h>
jack_port_t *output_port1, *output_port2;
jack_client_t *client;
static log_level loglevel;
static bool running = true;
extern struct outputstate output;
extern struct buffer *outputbuf;
#define LOCK mutex_lock(outputbuf->mutex)
#define UNLOCK mutex_unlock(outputbuf->mutex)
#define CLIENT_NAME "squeezelite"
extern u8_t *silencebuf;
#if DSD
extern u8_t *silencebuf_dop;
#endif
void list_devices(void) {
}
void set_volume(unsigned left, unsigned right) {
LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
LOCK;
output.gainL = left;
output.gainR = right;
UNLOCK;
}
bool test_open(const char *device, unsigned rates[])
{
/* client = jack_client_open (CLIENT_NAME, JackNullOption, &status, NULL); */
/* if (client == NULL) { */
/* fprintf (stderr, "jack_client_open() failed, " */
/* "status = 0x%2.0x\n", status); */
/* if (status & JackServerFailed) { */
/* fprintf (stderr, "Unable to connect to JACK server\n"); */
/* } */
/* return false; */
/* } */
/* if (status & JackServerStarted){ */
/* fprintf (stderr, "JACK server started\n"); */
/* } */
/* if (status & JackNameNotUnique) { */
/* const char *client_name; */
/* client_name = jack_get_client_name(client); */
/* fprintf (stderr, "unique name `%s' assigned\n", client_name); */
/* } */
/* jack_client_close (client); */
return true;
}
static u8_t *optr;
static int _write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
s32_t cross_gain_in, s32_t cross_gain_out, s32_t **cross_ptr) {
if (!silence) {
if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
_apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
}
if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
_apply_gain(outputbuf, out_frames, gainL, gainR);
}
IF_DSD(
if (output.dop) {
update_dop((u32_t *) outputbuf->readp, out_frames, output.invert);
}
)
memcpy(optr, outputbuf->readp, out_frames * BYTES_PER_FRAME);
} else {
u8_t *buf = silencebuf;
IF_DSD(
if (output.dop) {
buf = silencebuf_dop;
update_dop((u32_t *) buf, out_frames, false); // don't invert silence
}
)
memcpy(optr, buf, out_frames * BYTES_PER_FRAME);
}
optr += out_frames * BYTES_PER_FRAME;
return (int)out_frames;
}
/**
* The process callback for this JACK application is called in a
* special realtime thread once for each audio cycle.
*
* This client follows a simple rule: when the JACK transport is
* running, copy the input port to the output. When it stops, exit.
*/
int
output_jack_process (jack_nframes_t nframes, void *arg)
{
jack_default_audio_sample_t *out1, *out2;
int i;
frames_t frames_remaining = nframes;
frames_t frames;
s32_t *ptr;
ptr = (s32_t *)malloc (nframes * BYTES_PER_FRAME);
optr = (u8_t*)ptr;
LOCK;
out1 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port1, nframes);
out2 = (jack_default_audio_sample_t*)jack_port_get_buffer (output_port2, nframes);
output.updated = gettime_ms();
output.frames_played_dmp = output.frames_played;
do {
frames = _output_frames(frames_remaining);
frames_remaining -= frames;
} while (frames_remaining > 0 && frames != 0);
if (frames_remaining > 0) {
LOG_DEBUG("pad with silence");
memset(optr, 0, frames_remaining * BYTES_PER_FRAME);
}
for( i=0; i<nframes; i++ )
{
out1[i] = (float)((float)ptr[i*2]/(float)INT_MAX); /* left */
out2[i] = (float)((float)ptr[i*2+1]/(float)INT_MAX); /* right */
}
UNLOCK;
free (ptr);
return 0;
}
void output_init_jack(log_level level,
const char *device, unsigned output_buf_size,
char *params, unsigned rates[], unsigned rate_delay,
unsigned idle)
{
const char **ports;
jack_status_t status;
int i;
char *portspec = next_param(params, ':');
char *p = next_param(NULL, ':');
LOG_INFO ("Enter init_jack");
loglevel = level;
LOG_INFO ("init output");
LOG_INFO ("params: %s, portspec: %s", params, portspec);
memset(&output, 0, sizeof(output));
/* output.latency = latency; */
output.format = 0;
output.start_frames = 0;
output.write_cb = &_write_frames;
output.rate_delay = rate_delay;
//LOG_INFO("requested latency: %u", output.latency);
client = jack_client_open (CLIENT_NAME, JackNullOption, &status, NULL);
if (client == NULL) {
fprintf (stderr, "jack_client_open() failed, "
"status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
return;
}
if (status & JackServerStarted){
fprintf (stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
const char *client_name;
client_name = jack_get_client_name(client);
fprintf (stderr, "unique name `%s' assigned\n", client_name);
}
for (i=0; i < MAX_SUPPORTED_SAMPLERATES; i++){
rates[i] = 0;
}
rates[0] = jack_get_sample_rate (client);
LOG_INFO ("sample rate: %d", rates[0]);
output_init_common(level, device, output_buf_size, rates, idle);
jack_set_process_callback (client, output_jack_process, NULL);
/* create two ports */
output_port1 = jack_port_register (client, "output_1",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
output_port2 = jack_port_register (client, "output_2",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
if ((output_port1 == NULL) || (output_port2 == NULL)) {
fprintf(stderr, "no more JACK ports available\n");
exit (1);
}
/* Tell the JACK server that we are ready to roll. Our
* process() callback will start running now. */
if (jack_activate (client)) {
fprintf (stderr, "cannot activate client");
exit (1);
}
/* Connect the ports. You can't do this before the client is
* activated, because we can't make connections to clients
* that aren't running. Note the confusing (but necessary)
* orientation of the driver backend ports: playback ports are
* "input" to the backend, and capture ports are "output" from
* it.
*/
ports = jack_get_ports (client, portspec, NULL,
JackPortIsInput);
if (ports == NULL) {
fprintf(stderr, "no playback ports match portspec \"%s\"\n",
portspec);
exit (1);
}
if (jack_connect (client, jack_port_name (output_port1), ports[0])) {
fprintf (stderr, "cannot connect output ports\n");
}
if (jack_connect (client, jack_port_name (output_port2), ports[1])) {
fprintf (stderr, "cannot connect output ports\n");
}
jack_free (ports);
}
void output_close_jack(void) {
LOG_INFO("close output");
LOCK;
running = false;
jack_client_close (client);
UNLOCK;
output_close_common();
}
#endif // JACK