Esempio n. 1
0
uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
  /* set output format */
  SF_INFO sfinfo;
  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
  sfinfo.samplerate = s->samplerate;
  sfinfo.channels   = s->channels;
  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;

  /* try creating the file */
  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);

  if (s->handle == NULL) {
    /* show libsndfile err msg */
    AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
    return AUBIO_FAIL;
  }

  s->scratch_size = s->max_size*s->channels;
  /* allocate data for de/interleaving reallocated when needed. */
  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
    return AUBIO_FAIL;
  }
  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);

  return AUBIO_OK;
}
Esempio n. 2
0
/** new_aubio_alsa_seq_driver */
aubio_midi_driver_t*  new_aubio_alsa_seq_driver(//aubio_settings_t* settings, 
        handle_midi_event_func_t handler, void* data)
{
    int i, err;                     
    aubio_alsa_seq_driver_t* dev;   /**< object to return */
    pthread_attr_t attr;            /**< sequencer thread */
    int sched = SCHED_FIFO;         /**< default scheduling policy */
    struct sched_param priority;    /**< scheduling priority settings */
    int count;                      /**< number of MIDI file descriptors */
    struct pollfd *pfd = NULL;      /**< poll file descriptor array (copied in dev->pfd) */
    char* device = NULL;            /**< the device name */
    char* id = NULL;
    char full_id[64];
    char full_name[64];

    /* not much use doing anything */
    if (handler == NULL) {
        AUBIO_ERR( "Invalid argument");
        return NULL;
    }

    /* allocate the device */
    dev = AUBIO_NEW(aubio_alsa_seq_driver_t);
    if (dev == NULL) {
        AUBIO_ERR( "Out of memory");
        return NULL;
    }
    AUBIO_MEMSET(dev, 0, sizeof(aubio_alsa_seq_driver_t));
    dev->seq_port = -1;
    dev->driver.data = data;
    dev->driver.handler = handler;

    /* get the device name. if none is specified, use the default device. */
    //aubio_settings_getstr(settings, "midi.alsa_seq.device", &device);
    if (device == NULL) {
        device = "default";
    }

    /* open the sequencer INPUT only, non-blocking */
    //if ((err = snd_seq_open(&dev->seq_handle, device, SND_SEQ_OPEN_INPUT,
    if ((err = snd_seq_open(&dev->seq_handle, device, SND_SEQ_OPEN_DUPLEX,
                    SND_SEQ_NONBLOCK)) < 0) {
        AUBIO_ERR( "Error opening ALSA sequencer");
        goto error_recovery;
    }

    /* get # of MIDI file descriptors */
    count = snd_seq_poll_descriptors_count(dev->seq_handle, POLLIN);
    if (count > 0) {        /* make sure there are some */
        pfd = AUBIO_MALLOC(sizeof (struct pollfd) * count);
        dev->pfd = AUBIO_MALLOC(sizeof (struct pollfd) * count);
        /* grab file descriptor POLL info structures */
        count = snd_seq_poll_descriptors(dev->seq_handle, pfd, count, POLLIN);
    }

    for (i = 0; i < count; i++) {        /* loop over file descriptors */
        /* copy the input FDs */
        if (pfd[i].events & POLLIN) { /* use only the input FDs */
            dev->pfd[dev->npfd].fd = pfd[i].fd;
            dev->pfd[dev->npfd].events = POLLIN; 
            dev->pfd[dev->npfd].revents = 0; 
            dev->npfd++;
        }
    }
    AUBIO_FREE(pfd);

    //aubio_settings_getstr(settings, "midi.alsa_seq.id", &id);

    if (id != NULL) {
        if (AUBIO_STRCMP(id, "pid") == 0) {
            snprintf(full_id, 64, "aubio (%d)", getpid());
            snprintf(full_name, 64, "aubio_port (%d)", getpid());
        } else {
            snprintf(full_id, 64, "aubio (%s)", id);
            snprintf(full_name, 64, "aubio_port (%s)", id);
        }
    } else {
        snprintf(full_id, 64, "aubio");
        snprintf(full_name, 64, "aubio_port");
    }

    /* set the client name */
    snd_seq_set_client_name (dev->seq_handle, full_id);

    if ((dev->seq_port = snd_seq_create_simple_port (dev->seq_handle,
                    full_name,
                    SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE |
                    SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ | 
                    SND_SEQ_PORT_CAP_DUPLEX,
                    SND_SEQ_PORT_TYPE_APPLICATION)) < 0)
    {
        AUBIO_ERR( "Error creating ALSA sequencer port");
        goto error_recovery;
    }

    dev->status = AUBIO_MIDI_READY;

    /* create the midi thread */
    if (pthread_attr_init(&attr)) {
        AUBIO_ERR( "Couldn't initialize midi thread attributes");
        goto error_recovery;
    }

    /* use fifo scheduling. if it fails, use default scheduling. */
    while (1) {
        err = pthread_attr_setschedpolicy(&attr, sched);
        if (err) {
            AUBIO_MSG( "Couldn't set high priority scheduling for the MIDI input");
            if (sched == SCHED_FIFO) {
                sched = SCHED_OTHER;
                continue;
            } else {
                AUBIO_ERR( "Couldn't set scheduling policy.");
                goto error_recovery;
            }
        }

        /* SCHED_FIFO will not be active without setting the priority */
        priority.sched_priority = (sched == SCHED_FIFO) ? ALSA_SEQ_SCHED_PRIORITY : 0;
        pthread_attr_setschedparam (&attr, &priority);

        err = pthread_create(&dev->thread, &attr, aubio_alsa_seq_run, (void*) dev);
        if (err) {
            AUBIO_ERR( "Couldn't set high priority scheduling for the MIDI input");
            if (sched == SCHED_FIFO) {
                sched = SCHED_OTHER;
                continue;
            } else {
                //AUBIO_LOG(AUBIO_PANIC, "Couldn't create the midi thread.");
                AUBIO_ERR( "Couldn't create the midi thread.");
                goto error_recovery;
            }
        }
        break;
    }
    return (aubio_midi_driver_t*) dev;


error_recovery:
    del_aubio_alsa_seq_driver((aubio_midi_driver_t*) dev);
    return NULL;
}