summaryrefslogtreecommitdiffhomepage
path: root/src/lingot-audio-jack.c
blob: 5135c38083b4c74bce372eee429a6de18f1016d4 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2011  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot 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 2 of the License, or
 * (at your option) any later version.
 *
 * lingot 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 lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>

#include "lingot-defs.h"
#include "lingot-audio-jack.h"
#include "lingot-i18n.h"
#include "lingot-msg.h"

#ifdef JACK
#include <jack/jack.h>

// persistent JACK client to obtain hardware parameters
jack_client_t* client = NULL;
pthread_mutex_t stop_mutex = PTHREAD_MUTEX_INITIALIZER;

// this array allows us to reconnect the client to the last ports it was
// connected in a previous session
#define MAX_LAST_PORTS 10
char last_ports[MAX_LAST_PORTS][80];

int lingot_audio_jack_process(jack_nframes_t nframes, void* param) {
	LingotAudioHandler* audio = param;
	audio->nframes = nframes;

	pthread_mutex_lock(&stop_mutex);
	if (audio->running) {
		lingot_audio_jack_read(audio);
		audio->process_callback(audio->flt_read_buffer,
				audio->read_buffer_size, audio->process_callback_arg);
	}
	pthread_mutex_unlock(&stop_mutex);

	return 0;
}

// JACK calls this shutdown_callback if the server ever shuts down or
// decides to disconnect the client.
void lingot_audio_jack_shutdown(void* param) {
	LingotAudioHandler* audio = param;
	lingot_msg_add_error(_("Missing connection with JACK audio server"));
	pthread_mutex_lock(&stop_mutex);
	audio->interrupted = 1;
	pthread_mutex_unlock(&stop_mutex);
}
#endif

LingotAudioHandler* lingot_audio_jack_new(char* device, int sample_rate) {

	LingotAudioHandler* audio = NULL;

#	ifdef JACK
	const char* exception;
	const char **ports = NULL;
	const char *client_name = "lingot";
	const char *server_name = NULL;

	jack_options_t options = JackNoStartServer;
	jack_status_t status;

	audio = malloc(sizeof(LingotAudioHandler));
	strcpy(audio->device, "");

	audio->audio_system = AUDIO_SYSTEM_JACK;
	audio->jack_client = jack_client_open(client_name, options, &status,
			server_name);

	try {
		if (audio->jack_client == NULL) {
			throw(_("Unable to connect to JACK server"));
		}

		if (status & JackServerStarted) {
			fprintf(stderr, "JACK server started\n");
		}
		if (status & JackNameNotUnique) {
			client_name = jack_get_client_name(audio->jack_client);
			fprintf(stderr, "unique name `%s' assigned\n", client_name);
		}

		jack_on_shutdown(audio->jack_client, lingot_audio_jack_shutdown, audio);

		audio->real_sample_rate = jack_get_sample_rate(audio->jack_client);
		audio->read_buffer_size = jack_get_buffer_size(audio->jack_client);

		//	printf("engine sample rate: %" PRIu32 "\n", jack_get_sample_rate(
		//			audio->jack_client));
		//	printf("buffer size: %" PRIu32 "\n", jack_get_buffer_size(
		//			audio->jack_client));

		audio->jack_input_port = jack_port_register(audio->jack_client,
				"input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);

		if ((audio->jack_input_port == NULL)) {
			throw(_("No more JACK ports available"));
		}

	} catch {
		free(audio);
		audio = NULL;
		lingot_msg_add_error(exception);
	}

	if (ports != NULL)
		free(ports);

	if (audio != NULL) {
		client = audio->jack_client;
	}

#	else
	lingot_msg_add_error(
			_("The application has not been built with JACK support"));
#	endif
	return audio;
}

void lingot_audio_jack_destroy(LingotAudioHandler* audio) {
#	ifdef JACK
	if (audio != NULL) {
		//jack_cycle_wait(audio->jack_client);
		//		jack_deactivate(audio->jack_client);
		jack_client_close(audio->jack_client);
		client = NULL;
	}
#	endif
}

int lingot_audio_jack_read(LingotAudioHandler* audio) {
#	ifdef JACK
	register int i;
	float* in = jack_port_get_buffer(audio->jack_input_port, audio->nframes);
	for (i = 0; i < audio->nframes; i++)
		audio->flt_read_buffer[i] = in[i] * 32768;
	return 0;
#	else
	return -1;
#	endif
}

LingotAudioSystemProperties* lingot_audio_jack_get_audio_system_properties(
		audio_system_t audio_system) {

	LingotAudioSystemProperties* properties = NULL;
#	ifdef JACK
	properties = (LingotAudioSystemProperties*) malloc(1
			* sizeof(LingotAudioSystemProperties));

	int sample_rate = -1;

	const char *client_name = "lingot-get-sample-rate";
	const char *server_name = NULL;

	jack_options_t options = JackNoStartServer;
	jack_status_t status;
	jack_client_t* jack_client = NULL;
	const char **ports = NULL;
	const char* exception;

	unsigned long int flags = JackPortIsOutput;

	try {
		if (client != NULL) {
			sample_rate = jack_get_sample_rate(client);
			ports = jack_get_ports(client, NULL, NULL, flags);
		} else {
			jack_client = jack_client_open(client_name, options, &status,
					server_name);
			if (jack_client == NULL) {
				throw(_("Unable to connect to JACK server"));
			}
			if (status & JackServerStarted) {
				fprintf(stderr, "JACK server started\n");
			}
			if (status & JackNameNotUnique) {
				client_name = jack_get_client_name(jack_client);
				fprintf(stderr, "unique name `%s' assigned\n", client_name);
			}

			sample_rate = jack_get_sample_rate(jack_client);

			ports = jack_get_ports(jack_client, NULL, NULL, flags);
		}
	} catch {
		// here I throw a warning message because we are only ontaining the
		// audio properties
		lingot_msg_add_warning(exception);
	}

	properties->forced_sample_rate = 1;
	properties->n_devices = 0;
	properties->devices = NULL;

	if (ports != NULL) {
		int i;
		for (i = 0; ports[i] != NULL; i++) {
		}
		properties->n_devices = i;

		if (properties->n_devices != 0) {
			properties->devices = malloc(properties->n_devices * sizeof(char*));
			for (i = 0; ports[i] != NULL; i++) {
				properties->devices[i] = strdup(ports[i]);
			}
		}

	}

	if (sample_rate == -1) {
		properties->n_sample_rates = 0;
		properties->sample_rates = NULL;
	} else {
		properties->n_sample_rates = 1;
		properties->sample_rates = malloc(properties->n_sample_rates
				* sizeof(int));
		properties->sample_rates[0] = sample_rate;
	}

	if (ports != NULL)
		free(ports);

	if (jack_client != NULL)
		jack_client_close(jack_client);

#	else
	lingot_msg_add_error(
			_("The application has not been built with JACK support"));
#	endif

	return properties;
}

int lingot_audio_jack_start(LingotAudioHandler* audio) {
	int result = 0;

#   ifdef JACK
	int index = 0;
	const char **ports = NULL;
	const char* exception;
	jack_set_process_callback(audio->jack_client, lingot_audio_jack_process,
			audio);

	try {
		if (jack_activate(audio->jack_client)) {
			throw(_("Cannot activate client"));
		}

		ports
				= jack_get_ports(audio->jack_client, NULL, NULL,
						JackPortIsOutput);
		if (ports == NULL) {
			throw(_("No active capture ports"));
		}

		// try to connect the client to the ports is was connected before
		int j = 0;
		int connections = 0;
		for (j = 0; j < MAX_LAST_PORTS; j++) {
			for (index = 0; ports[index]; index++) {
				if (!strcmp(last_ports[j], ports[index])) {
					if (jack_connect(audio->jack_client, ports[index],
							jack_port_name(audio->jack_input_port))) {
						throw(_("Cannot connect input ports"));
					} else {
						connections++;
					}
				}
			}
		}

		// if there wasn't connections before, we connect the client to the
		// first physical port
		if (!connections) {
			free(ports);
			ports = jack_get_ports(audio->jack_client, NULL, NULL,
					JackPortIsPhysical | JackPortIsOutput);
			if (ports == NULL) {
				throw(_("No physical capture ports"));
			}

			if (jack_connect(audio->jack_client, ports[0], jack_port_name(
					audio->jack_input_port))) {
				throw(_("Cannot connect input ports"));
			}
		}

	} catch {
		lingot_msg_add_error(exception);
		result = -1;
	}

	free(ports);
#	else
	lingot_msg_add_error(
			_("The application has not been built with JACK support"));
#	endif

	return result;
}

void lingot_audio_jack_stop(LingotAudioHandler* audio) {
#	ifdef JACK
	//jack_cycle_wait(audio->jack_client);
	const char** ports = jack_get_ports(audio->jack_client, NULL, NULL,
			JackPortIsOutput);

	if (ports != NULL) {
		int i, j = 0;

		for (i = 0; i < MAX_LAST_PORTS; i++) {
			strcpy(last_ports[i], "");
		}

		for (i = 0; ports[i]; i++) {
			if (jack_port_connected(jack_port_by_name(audio->jack_client,
					ports[i]))) {
				strcpy(last_ports[j++], ports[i]);
			}
		}
	}

	pthread_mutex_lock(&stop_mutex);
	jack_deactivate(audio->jack_client);
	pthread_mutex_unlock(&stop_mutex);
#	else
	lingot_msg_add_error(
			_("The application has not been built with JACK support"));
#	endif
}