2 Copyright (C) 2003 Paul Brossier
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <jack/jack.h>
24 #include "aubio_priv.h"
27 typedef jack_default_audio_sample_t jack_sample_t;
29 #if !AUBIO_SINGLE_PRECISION
30 #define AUBIO_JACK_MAX_FRAMES 4096
31 #define AUBIO_JACK_NEEDS_CONVERSION
35 * jack device structure
37 struct _aubio_jack_t {
39 jack_client_t *client;
40 /** jack output ports */
42 /** jack input ports */
44 /** jack input buffer */
45 jack_sample_t **ibufs;
46 /** jack output buffer */
47 jack_sample_t **obufs;
48 #ifdef AUBIO_JACK_NEEDS_CONVERSION
49 /** converted jack input buffer */
51 /** converted jack output buffer */
54 /** jack input channels */
56 /** jack output channels */
58 /** jack samplerate (Hz) */
60 /** jack processing function */
61 aubio_process_func_t callback;
64 /* static memory management */
65 static aubio_jack_t * aubio_jack_alloc(uint_t ichan, uint_t ochan);
66 static uint_t aubio_jack_free(aubio_jack_t * jack_setup);
67 /* jack callback functions */
68 static int aubio_jack_process(jack_nframes_t nframes, void *arg);
69 static void aubio_jack_shutdown (void *arg);
71 aubio_jack_t * new_aubio_jack(uint_t ichan, uint_t ochan,
72 aubio_process_func_t callback) {
73 aubio_jack_t * jack_setup = aubio_jack_alloc (ichan, ochan);
75 char * client_name = "aubio";
77 /* initial jack client setup */
78 if ((jack_setup->client = jack_client_new (client_name)) == 0) {
79 AUBIO_ERR ("jack server not running?\n");
80 AUBIO_QUIT(AUBIO_FAIL);
84 jack_set_process_callback (jack_setup->client, aubio_jack_process,
86 jack_on_shutdown (jack_setup->client, aubio_jack_shutdown,
89 /* register jack output ports */
90 for (i = 0; i < ochan; i++)
92 AUBIO_SPRINTF(name, "out_%d", i+1);
93 AUBIO_MSG("%s\n", name);
94 if ((jack_setup->oports[i] =
95 jack_port_register (jack_setup->client, name,
96 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0)
98 AUBIO_ERR("failed registering output port \"%s\"!\n", name);
99 jack_client_close (jack_setup->client);
100 AUBIO_QUIT(AUBIO_FAIL);
104 /* register jack input ports */
105 for (i = 0; i < ichan; i++)
107 AUBIO_SPRINTF(name, "in_%d", i+1);
108 AUBIO_MSG("%s\n", name);
109 if ((jack_setup->iports[i] =
110 jack_port_register (jack_setup->client, name,
111 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0)
113 AUBIO_ERR("failed registering input port \"%s\"!\n", name);
114 jack_client_close (jack_setup->client);
115 AUBIO_QUIT(AUBIO_FAIL);
119 /* set processing callback */
120 jack_setup->callback = callback;
124 uint_t aubio_jack_activate(aubio_jack_t *jack_setup) {
125 /* get sample rate */
126 jack_setup->samplerate = jack_get_sample_rate (jack_setup->client);
127 /* actual jack process activation */
128 if (jack_activate (jack_setup->client))
130 AUBIO_ERR("jack client activation failed");
136 void aubio_jack_close(aubio_jack_t *jack_setup) {
137 /* bug : should disconnect all ports first */
138 jack_client_close(jack_setup->client);
139 aubio_jack_free(jack_setup);
142 /* memory management */
143 static aubio_jack_t * aubio_jack_alloc(uint_t ichan, uint_t ochan) {
144 aubio_jack_t *jack_setup = AUBIO_NEW(aubio_jack_t);
145 jack_setup->ichan = ichan;
146 jack_setup->ochan = ochan;
147 jack_setup->oports = AUBIO_ARRAY(jack_port_t*, ichan);
148 jack_setup->iports = AUBIO_ARRAY(jack_port_t*, ochan);
149 jack_setup->ibufs = AUBIO_ARRAY(jack_sample_t*, ichan);
150 jack_setup->obufs = AUBIO_ARRAY(jack_sample_t*, ochan);
151 #ifdef AUBIO_JACK_NEEDS_CONVERSION
152 jack_setup->sibufs = AUBIO_ARRAY(smpl_t*, ichan);
154 for (i = 0; i < ichan; i++) {
155 jack_setup->sibufs[i] = AUBIO_ARRAY(smpl_t, AUBIO_JACK_MAX_FRAMES);
157 jack_setup->sobufs = AUBIO_ARRAY(smpl_t*, ochan);
158 for (i = 0; i < ochan; i++) {
159 jack_setup->sobufs[i] = AUBIO_ARRAY(smpl_t, AUBIO_JACK_MAX_FRAMES);
165 static uint_t aubio_jack_free(aubio_jack_t * jack_setup) {
166 AUBIO_FREE(jack_setup->oports);
167 AUBIO_FREE(jack_setup->iports);
168 AUBIO_FREE(jack_setup->ibufs );
169 AUBIO_FREE(jack_setup->obufs );
170 AUBIO_FREE(jack_setup);
174 /* jack callback functions */
175 static void aubio_jack_shutdown (void *arg UNUSED){
176 AUBIO_ERR("jack shutdown\n");
177 AUBIO_QUIT(AUBIO_OK);
180 static int aubio_jack_process(jack_nframes_t nframes, void *arg) {
181 aubio_jack_t* dev = (aubio_jack_t *)arg;
183 for (i=0;i<dev->ichan;i++) {
184 /* get readable input */
186 (jack_sample_t *) jack_port_get_buffer (dev->iports[i], nframes);
187 /* get writable output */
189 (jack_sample_t *) jack_port_get_buffer (dev->oports[i], nframes);
191 #ifndef AUBIO_JACK_NEEDS_CONVERSION
192 dev->callback(dev->ibufs,dev->obufs,nframes);
195 for (j = 0; j < MIN(nframes, AUBIO_JACK_MAX_FRAMES); j++) {
196 for (i = 0; i < dev->ichan; i++) {
197 dev->sibufs[i][j] = (smpl_t)dev->ibufs[i][j];
200 dev->callback(dev->sibufs, dev->sobufs, nframes);
201 for (j = 0; j < MIN(nframes, AUBIO_JACK_MAX_FRAMES); j++) {
202 for (i = 0; i < dev->ichan; i++) {
203 dev->obufs[i][j] = (jack_sample_t)dev->sobufs[i][j];
211 #endif /* HAVE_JACK */