source: ext/jackio.c @ bccac6b

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since bccac6b was bccac6b, checked in by Paul Brossier <piem@piem.org>, 15 years ago

ext/jackio.{c,h}: add support for jack midi

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2  Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include <aubio.h>
22
23#if HAVE_JACK
24#include <jack/jack.h>
25#include "aubio_priv.h"
26#include "jackio.h"
27
28typedef jack_default_audio_sample_t jack_sample_t;
29
30#if !AUBIO_SINGLE_PRECISION
31#define AUBIO_JACK_MAX_FRAMES 4096
32#define AUBIO_JACK_NEEDS_CONVERSION
33#endif
34
35/**
36 * jack device structure
37 */
38struct _aubio_jack_t
39{
40  /** jack client */
41  jack_client_t *client;
42  /** jack output ports */
43  jack_port_t **oports;
44  /** jack input ports */
45  jack_port_t **iports;
46  /** jack input buffer */
47  jack_sample_t **ibufs;
48  /** jack output buffer */
49  jack_sample_t **obufs;
50#ifdef AUBIO_JACK_NEEDS_CONVERSION
51  /** converted jack input buffer */
52  smpl_t **sibufs;
53  /** converted jack output buffer */
54  smpl_t **sobufs;
55#endif
56  /** jack input audio channels */
57  uint_t ichan;
58  /** jack output audio channels */
59  uint_t ochan;
60  /** jack input midi channels */
61  uint_t imidichan;
62  /** jack output midi channels */
63  uint_t omidichan;
64  /** jack samplerate (Hz) */
65  uint_t samplerate;
66  /** jack processing function */
67  aubio_process_func_t callback;
68};
69
70/* static memory management */
71static aubio_jack_t *aubio_jack_alloc (uint_t ichan, uint_t ochan,
72    uint_t imidichan, uint_t omidichan);
73static uint_t aubio_jack_free (aubio_jack_t * jack_setup);
74/* jack callback functions */
75static int aubio_jack_process (jack_nframes_t nframes, void *arg);
76static void aubio_jack_shutdown (void *arg);
77
78aubio_jack_t *
79new_aubio_jack (uint_t ichan, uint_t ochan,
80    uint_t imidichan, uint_t omidichan, 
81    aubio_process_func_t callback)
82{
83  aubio_jack_t *jack_setup = aubio_jack_alloc (ichan, ochan,
84      imidichan, omidichan);
85  uint_t i;
86  char *client_name = "aubio";
87  char *jack_port_type;
88  char name[64];
89  /* initial jack client setup */
90  if ((jack_setup->client = jack_client_new (client_name)) == 0) {
91    AUBIO_ERR ("jack server not running?\n");
92    AUBIO_QUIT (AUBIO_FAIL);
93  }
94
95  /* set callbacks */
96  jack_set_process_callback (jack_setup->client, aubio_jack_process,
97      (void *) jack_setup);
98  jack_on_shutdown (jack_setup->client, aubio_jack_shutdown,
99      (void *) jack_setup);
100
101  /* register jack output audio and midi ports */
102  for (i = 0; i < ochan + omidichan; i++) {
103    if (i < ochan) {
104      jack_port_type = JACK_DEFAULT_AUDIO_TYPE;
105      AUBIO_SPRINTF (name, "out_%d", i + 1);
106    } else {
107      jack_port_type = JACK_DEFAULT_MIDI_TYPE;
108      AUBIO_SPRINTF (name, "midi_out_%d", i - ochan + 1);
109    }
110    if ((jack_setup->oports[i] =
111            jack_port_register (jack_setup->client, name,
112                jack_port_type, JackPortIsOutput, 0)) == 0) {
113      goto beach;
114    }
115    AUBIO_DBG ("%s:%s\n", client_name, name);
116  }
117
118  /* register jack input audio ports */
119  for (i = 0; i < ichan + imidichan; i++) {
120    if (i < ichan) {
121      jack_port_type = JACK_DEFAULT_AUDIO_TYPE;
122      AUBIO_SPRINTF (name, "in_%d", i + 1);
123    } else {
124      jack_port_type = JACK_DEFAULT_MIDI_TYPE;
125      AUBIO_SPRINTF (name, "midi_in_%d", i - ichan + 1);
126    }
127    if ((jack_setup->iports[i] =
128            jack_port_register (jack_setup->client, name,
129                jack_port_type, JackPortIsInput, 0)) == 0) {
130      goto beach;
131    }
132    AUBIO_DBG ("%s:%s\n", client_name, name);
133  }
134
135  /* set processing callback */
136  jack_setup->callback = callback;
137  return jack_setup;
138
139beach:
140  AUBIO_ERR ("failed registering port \"%s:%s\"!\n", 
141      client_name, name);
142  jack_client_close (jack_setup->client);
143  AUBIO_QUIT (AUBIO_FAIL);
144}
145
146uint_t
147aubio_jack_activate (aubio_jack_t * jack_setup)
148{
149  /* get sample rate */
150  jack_setup->samplerate = jack_get_sample_rate (jack_setup->client);
151  /* actual jack process activation */
152  if (jack_activate (jack_setup->client)) {
153    AUBIO_ERR ("jack client activation failed");
154    return 1;
155  }
156  return 0;
157}
158
159void
160aubio_jack_close (aubio_jack_t * jack_setup)
161{
162  /* bug : should disconnect all ports first */
163  jack_client_close (jack_setup->client);
164  aubio_jack_free (jack_setup);
165}
166
167/* memory management */
168static aubio_jack_t *
169aubio_jack_alloc (uint_t ichan, uint_t ochan,
170    uint_t imidichan, uint_t omidichan)
171{
172  aubio_jack_t *jack_setup = AUBIO_NEW (aubio_jack_t);
173  jack_setup->ichan = ichan;
174  jack_setup->ochan = ochan;
175  jack_setup->oports = AUBIO_ARRAY (jack_port_t *, ichan + imidichan);
176  jack_setup->iports = AUBIO_ARRAY (jack_port_t *, ochan + omidichan);
177  jack_setup->ibufs = AUBIO_ARRAY (jack_sample_t *, ichan);
178  jack_setup->obufs = AUBIO_ARRAY (jack_sample_t *, ochan);
179#ifdef AUBIO_JACK_NEEDS_CONVERSION
180  /* allocate arrays for data conversion */
181  jack_setup->sibufs = AUBIO_ARRAY (smpl_t *, ichan);
182  uint_t i;
183  for (i = 0; i < ichan; i++) {
184    jack_setup->sibufs[i] = AUBIO_ARRAY (smpl_t, AUBIO_JACK_MAX_FRAMES);
185  }
186  jack_setup->sobufs = AUBIO_ARRAY (smpl_t *, ochan);
187  for (i = 0; i < ochan; i++) {
188    jack_setup->sobufs[i] = AUBIO_ARRAY (smpl_t, AUBIO_JACK_MAX_FRAMES);
189  }
190#endif
191  return jack_setup;
192}
193
194static uint_t
195aubio_jack_free (aubio_jack_t * jack_setup)
196{
197  AUBIO_FREE (jack_setup->oports);
198  AUBIO_FREE (jack_setup->iports);
199  AUBIO_FREE (jack_setup->ibufs);
200  AUBIO_FREE (jack_setup->obufs);
201  AUBIO_FREE (jack_setup);
202  return AUBIO_OK;
203}
204
205/* jack callback functions */
206static void
207aubio_jack_shutdown (void *arg UNUSED)
208{
209  AUBIO_ERR ("jack shutdown\n");
210  AUBIO_QUIT (AUBIO_OK);
211}
212
213static int
214aubio_jack_process (jack_nframes_t nframes, void *arg)
215{
216  aubio_jack_t *dev = (aubio_jack_t *) arg;
217  uint_t i;
218  for (i = 0; i < dev->ichan; i++) {
219    /* get readable input */
220    dev->ibufs[i] =
221        (jack_sample_t *) jack_port_get_buffer (dev->iports[i], nframes);
222  }
223  for (i = 0; i < dev->ochan; i++) {
224    /* get writable output */
225    dev->obufs[i] =
226        (jack_sample_t *) jack_port_get_buffer (dev->oports[i], nframes);
227  }
228#ifndef AUBIO_JACK_NEEDS_CONVERSION
229  dev->callback (dev->ibufs, dev->obufs, nframes);
230#else
231  uint_t j;
232  for (j = 0; j < MIN (nframes, AUBIO_JACK_MAX_FRAMES); j++) {
233    for (i = 0; i < dev->ichan; i++) {
234      dev->sibufs[i][j] = (smpl_t) dev->ibufs[i][j];
235    }
236  }
237  dev->callback (dev->sibufs, dev->sobufs, nframes);
238  for (j = 0; j < MIN (nframes, AUBIO_JACK_MAX_FRAMES); j++) {
239    for (i = 0; i < dev->ochan; i++) {
240      dev->obufs[i][j] = (jack_sample_t) dev->sobufs[i][j];
241    }
242  }
243#endif
244  return 0;
245}
246
247
248#endif /* HAVE_JACK */
Note: See TracBrowser for help on using the repository browser.