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.
22 #include <jack/jack.h>
23 #include "aubio_priv.h"
26 /*typedef jack_default_audio_sample_t jack_sample_t; */
27 /* work with float, never tried in double */
28 typedef smpl_t jack_sample_t;
31 * jack device structure
33 struct _aubio_jack_t {
35 jack_client_t *client;
36 /** jack output ports */
38 /** jack input ports */
40 /** jack input buffer */
41 jack_sample_t **ibufs;
42 /** jack output buffer */
43 jack_sample_t **obufs;
44 /** jack input channels */
46 /** jack output channels */
48 /** jack samplerate (Hz) */
50 /** jack processing function */
51 aubio_process_func_t callback;
54 /* static memory management */
55 static aubio_jack_t * aubio_jack_alloc(uint_t ichan, uint_t ochan);
56 static uint_t aubio_jack_free(aubio_jack_t * jack_setup);
57 /* jack callback functions */
58 static int aubio_jack_process(jack_nframes_t nframes, void *arg);
59 static void aubio_jack_shutdown (void *arg);
61 aubio_jack_t * new_aubio_jack(uint_t ichan, uint_t ochan,
62 aubio_process_func_t callback) {
63 aubio_jack_t * jack_setup = aubio_jack_alloc (ichan, ochan);
65 char * client_name = "aubio";
67 /* initial jack client setup */
68 if ((jack_setup->client = jack_client_new (client_name)) == 0) {
69 AUBIO_ERR ("jack server not running?\n");
70 AUBIO_QUIT(AUBIO_FAIL);
74 jack_set_process_callback (jack_setup->client, aubio_jack_process,
76 jack_on_shutdown (jack_setup->client, aubio_jack_shutdown,
79 /* register jack output ports */
80 for (i = 0; i < ochan; i++)
82 AUBIO_SPRINTF(name, "out_%d", i+1);
83 AUBIO_MSG("%s\n", name);
84 if ((jack_setup->oports[i] =
85 jack_port_register (jack_setup->client, name,
86 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0)
88 AUBIO_ERR("failed registering output port \"%s\"!\n", name);
89 jack_client_close (jack_setup->client);
90 AUBIO_QUIT(AUBIO_FAIL);
94 /* register jack input ports */
95 for (i = 0; i < ichan; i++)
97 AUBIO_SPRINTF(name, "in_%d", i+1);
98 AUBIO_MSG("%s\n", name);
99 if ((jack_setup->iports[i] =
100 jack_port_register (jack_setup->client, name,
101 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0)
103 AUBIO_ERR("failed registering input port \"%s\"!\n", name);
104 jack_client_close (jack_setup->client);
105 AUBIO_QUIT(AUBIO_FAIL);
109 /* set processing callback */
110 jack_setup->callback = callback;
114 uint_t aubio_jack_activate(aubio_jack_t *jack_setup) {
115 /* get sample rate */
116 jack_setup->samplerate = jack_get_sample_rate (jack_setup->client);
117 /* actual jack process activation */
118 if (jack_activate (jack_setup->client))
120 AUBIO_ERR("jack client activation failed");
126 void aubio_jack_close(aubio_jack_t *jack_setup) {
127 /* bug : should disconnect all ports first */
128 jack_client_close(jack_setup->client);
129 aubio_jack_free(jack_setup);
132 /* memory management */
133 static aubio_jack_t * aubio_jack_alloc(uint_t ichan, uint_t ochan) {
134 aubio_jack_t *jack_setup = AUBIO_NEW(aubio_jack_t);
135 jack_setup->ichan = ichan;
136 jack_setup->ochan = ochan;
137 jack_setup->oports = AUBIO_ARRAY(jack_port_t*, ichan);
138 jack_setup->iports = AUBIO_ARRAY(jack_port_t*, ochan);
139 jack_setup->ibufs = AUBIO_ARRAY(jack_sample_t*, ichan);
140 jack_setup->obufs = AUBIO_ARRAY(jack_sample_t*, ochan);
144 static uint_t aubio_jack_free(aubio_jack_t * jack_setup) {
145 AUBIO_FREE(jack_setup->oports);
146 AUBIO_FREE(jack_setup->iports);
147 AUBIO_FREE(jack_setup->ibufs );
148 AUBIO_FREE(jack_setup->obufs );
149 AUBIO_FREE(jack_setup);
153 /* jack callback functions */
154 static void aubio_jack_shutdown (void *arg){
155 AUBIO_ERR("jack shutdown\n");
156 AUBIO_QUIT(AUBIO_OK);
159 static int aubio_jack_process(jack_nframes_t nframes, void *arg) {
160 aubio_jack_t* dev = (aubio_jack_t *)arg;
162 for (i=0;i<dev->ichan;i++) {
163 /* get readable input */
165 (jack_sample_t *) jack_port_get_buffer (dev->iports[i], nframes);
166 /* get writable output */
168 (jack_sample_t *) jack_port_get_buffer (dev->oports[i], nframes);
170 dev->callback(dev->ibufs,dev->obufs,nframes);
175 #endif /* JACK_SUPPORT */