Changeset c201306 for ext


Ignore:
Timestamp:
Sep 25, 2009, 9:53:40 PM (15 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
ebbf5a0
Parents:
04ceeab
Message:

ext/jackio.{c,h}: add midi_out_ring for midi output event processing

Location:
ext
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ext/jackio.c

    r04ceeab rc201306  
    2222
    2323#if HAVE_JACK
    24 #include <jack/jack.h>
    2524#include "aubio_priv.h"
    2625#include "jackio.h"
     
    3231#define AUBIO_JACK_NEEDS_CONVERSION
    3332#endif
     33
     34#define RINGBUFFER_SIZE 1024*sizeof(jack_midi_event_t)
    3435
    3536/**
     
    6263  /** jack output midi channels */
    6364  uint_t omidichan;
     65  /** midi output ringbuffer */
     66  jack_ringbuffer_t *midi_out_ring;
    6467  /** jack samplerate (Hz) */
    6568  uint_t samplerate;
     
    7881aubio_jack_t *
    7982new_aubio_jack (uint_t ichan, uint_t ochan,
    80     uint_t imidichan, uint_t omidichan,
    81     aubio_process_func_t callback)
     83    uint_t imidichan, uint_t omidichan, aubio_process_func_t callback)
    8284{
    8385  aubio_jack_t *jack_setup = aubio_jack_alloc (ichan, ochan,
     
    9395  }
    9496
     97  if (jack_setup->omidichan) {
     98    jack_setup->midi_out_ring = jack_ringbuffer_create (RINGBUFFER_SIZE);
     99
     100    if (jack_setup->midi_out_ring == NULL) {
     101      AUBIO_ERR ("Failed creating jack midi output ringbuffer.");
     102      AUBIO_QUIT (AUBIO_FAIL);
     103    }
     104
     105    jack_ringbuffer_mlock (jack_setup->midi_out_ring);
     106  }
     107
    95108  /* set callbacks */
    96109  jack_set_process_callback (jack_setup->client, aubio_jack_process,
     
    138151
    139152beach:
    140   AUBIO_ERR ("failed registering port \"%s:%s\"!\n",
    141       client_name, name);
     153  AUBIO_ERR ("failed registering port \"%s:%s\"!\n", client_name, name);
    142154  jack_client_close (jack_setup->client);
    143155  AUBIO_QUIT (AUBIO_FAIL);
     
    173185  jack_setup->ichan = ichan;
    174186  jack_setup->ochan = ochan;
     187  jack_setup->imidichan = imidichan;
     188  jack_setup->omidichan = omidichan;
    175189  jack_setup->oports = AUBIO_ARRAY (jack_port_t *, ichan + imidichan);
    176190  jack_setup->iports = AUBIO_ARRAY (jack_port_t *, ochan + omidichan);
     
    195209aubio_jack_free (aubio_jack_t * jack_setup)
    196210{
     211  if (jack_setup->omidichan && jack_setup->midi_out_ring) {
     212    jack_ringbuffer_free (jack_setup->midi_out_ring);
     213  }
    197214  AUBIO_FREE (jack_setup->oports);
    198215  AUBIO_FREE (jack_setup->iports);
     
    210227  AUBIO_QUIT (AUBIO_OK);
    211228}
     229
     230static void process_midi_output (aubio_jack_t * dev, jack_nframes_t nframes);
    212231
    213232static int
     
    242261  }
    243262#endif
     263
     264  /* now process midi stuff */
     265  if (dev->omidichan) {
     266    process_midi_output (dev, nframes);
     267  }
     268
    244269  return 0;
    245270}
    246271
     272void
     273aubio_jack_midi_event_write (aubio_jack_t * dev, jack_midi_event_t * event)
     274{
     275  int written;
     276
     277  if (jack_ringbuffer_write_space (dev->midi_out_ring) < sizeof (*event)) {
     278    AUBIO_ERR ("Not enough space to write midi output, midi event lost!\n");
     279    return;
     280  }
     281
     282  written = jack_ringbuffer_write (dev->midi_out_ring,
     283      (char *) event, sizeof (*event));
     284
     285  if (written != sizeof (*event)) {
     286    AUBIO_WRN ("Call to jack_ringbuffer_write failed, midi event lost! \n");
     287  }
     288}
     289
     290static void
     291process_midi_output (aubio_jack_t * dev, jack_nframes_t nframes)
     292{
     293  int read, sendtime;
     294  jack_midi_event_t ev;
     295  unsigned char *buffer;
     296  jack_nframes_t last_frame_time = jack_last_frame_time (dev->client);
     297  // TODO for each omidichan
     298  void *port_buffer = jack_port_get_buffer (dev->oports[dev->ochan], nframes);
     299
     300  if (port_buffer == NULL) {
     301    AUBIO_WRN ("Failed to get jack midi output port, will not send anything\n");
     302    return;
     303  }
     304
     305  jack_midi_clear_buffer (port_buffer);
     306
     307  // TODO add rate_limit
     308
     309  while (jack_ringbuffer_read_space (dev->midi_out_ring)) {
     310    read = jack_ringbuffer_peek (dev->midi_out_ring, (char *) &ev, sizeof (ev));
     311
     312    if (read != sizeof (ev)) {
     313      AUBIO_WRN ("Short read from the ringbuffer, possible note loss.\n");
     314      jack_ringbuffer_read_advance (dev->midi_out_ring, read);
     315      continue;
     316    }
     317
     318    sendtime = ev.time + nframes - last_frame_time;
     319
     320    /* send time is after current period, will do this one later */
     321    if (sendtime >= (int) nframes) {
     322      break;
     323    }
     324
     325    if (sendtime < 0) {
     326      sendtime = 0;
     327    }
     328
     329    jack_ringbuffer_read_advance (dev->midi_out_ring, sizeof (ev));
     330
     331    buffer = jack_midi_event_reserve (port_buffer, sendtime, ev.size);
     332
     333    if (buffer == NULL) {
     334      AUBIO_WRN ("Call to jack_midi_event_reserve failed, note lost.\n");
     335      break;
     336    }
     337
     338    AUBIO_MEMCPY (buffer, ev.buffer, ev.size);
     339  }
     340}
    247341
    248342#endif /* HAVE_JACK */
  • ext/jackio.h

    r04ceeab rc201306  
    3434#endif
    3535
     36#include <jack/jack.h>
     37#include <jack/midiport.h>
     38#include <jack/ringbuffer.h>
     39
    3640/** jack object */
    3741typedef struct _aubio_jack_t aubio_jack_t;
     
    4953void aubio_jack_close (aubio_jack_t * jack_setup);
    5054
     55/** write a jack_midi_event_t to the midi output ringbuffer */
     56void aubio_jack_midi_event_write (aubio_jack_t * jack_setup,
     57    jack_midi_event_t * event);
     58
    5159#ifdef __cplusplus
    5260}
Note: See TracChangeset for help on using the changeset viewer.