Changeset fefbbd8 for src/synth


Ignore:
Timestamp:
Oct 8, 2016, 8:59:30 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
sampler
Children:
128589e
Parents:
88a774c
Message:

src/synth/sampler.c: clean up and add threaded ring buffer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/synth/sampler.c

    r88a774c rfefbbd8  
    133133  s->available = 0;
    134134
     135  s->threaded_read = 0;
     136  s->perfectloop = 0;
    135137#if 0 // naive mode
    136138  s->source_blocksize = s->blocksize;
    137   s->perfectloop = 0;
    138 #elif 0 // threaded mode
     139#elif 0 // threaded mode, no ringbuffer
    139140  s->source_blocksize = s->blocksize;
    140   // FIXME: perfectloop fails if source_blocksize > 2048 ?!
    141   //s->source_blocksize = 4096; //32 * s->blocksize;
    142   s->perfectloop = 0;
    143   s->threaded_read = 0;
    144 #elif 1 // unthreaded ringbuffer mode
     141  s->threaded_read = 1;
     142#elif 0 // unthreaded, with ringbuffer
    145143  s->source_blocksize = 2048; //32 * s->blocksize;
    146   // FIXME: perfectloop fails if source_blocksize > 2048 ?!
    147   //s->source_blocksize = 4096; //32 * s->blocksize;
    148144  s->perfectloop = 1;
    149 #endif
     145#elif 1 // threaded with ringhbuffer
     146  s->source_blocksize = 2048; //32 * s->blocksize;
     147  s->perfectloop = 1;
     148  s->threaded_read = 1;
     149#endif
     150  // FIXME: perfectloop fails if source_blocksize > 2048 with source_avcodec
     151  //s->source_blocksize = 8192;
    150152
    151153  if (s->perfectloop || s->source_blocksize != s->blocksize) {
     
    153155    s->ring = new_aubio_ringbuffer(s->source_blocksize, s->blocksize);
    154156  }
     157  //s->channels = 1;
     158  //s->source_moutput = new_fmat(s->source_blocksize, s->channels);
    155159
    156160#ifdef HAVE_THREADS
     
    158162
    159163  if (s->threaded_read) {
    160     s->source_blocksize = s->blocksize;
    161     //s->source_output = new_fvec(s->source_blocksize);
    162     s->channels = 1;
    163     s->source_moutput = new_fmat(s->source_blocksize, s->channels);
     164    //AUBIO_WRN("sampler: starting reading thread\n");
    164165    aubio_sampler_open_reading_thread(s);
    165   } else if (0) {
    166     s->source_blocksize = s->blocksize;
    167   } else {
    168     //s->source_blocksize = 16*s->blocksize;
    169     //s->source_blocksize = 8192; //s->blocksize;
    170     //s->ring = new_aubio_ringbuffer(s->source_blocksize, s->source_blocksize);
    171166  }
    172167#endif
     
    337332
    338333uint_t aubio_sampler_reading_from_source_ring_fetch(aubio_sampler_t*s);
     334
    339335void *aubio_sampler_readfn(void *z) {
    340336  aubio_sampler_t *p = z;
     
    348344      //AUBIO_WRN("sampler: readfn(): file started\n");
    349345      if (p->ring) {
    350         aubio_sampler_reading_from_source_ring_fetch(p);
     346        p->available = aubio_sampler_reading_from_source_ring_fetch(p);
    351347      } else {
    352348        p->available = aubio_sampler_pull_from_source(p);
     
    485481    *read = 0;
    486482    if (ring_avail > 0) {
     483      // pull remaining frames in ring buffer
    487484      aubio_ringbuffer_pull(s->ring, output, ring_avail);
    488485      check_wrote += ring_avail;
    489486      *read += ring_avail;
    490487    }
     488    // signal eof, seek to 0 if looping
    491489    aubio_sampler_do_eof(s);
     490    // read new frames from source early if needed
    492491    aubio_sampler_do_perfectloop(s);
     492    // write the remaining frames
    493493    *read += aubio_sampler_write_remaining_ring(s, output, ring_avail);
    494494  }
     
    529529  //AUBIO_WRN("sampler: got %d available in _read_from_source\n", available);
    530530  // read -> number of samples read
    531   if (available >= (sint_t)s->blocksize) {
    532     fvec_copy(s->source_output, output);
    533     *read = s->blocksize;
    534   } else if (available > 0) {
    535     fvec_copy(s->source_output, output);
    536     *read = available;
     531  if (!s->perfectloop && s->source_blocksize == s->blocksize) {
     532    if (available >= (sint_t)s->blocksize) {
     533      fvec_copy(s->source_output, output);
     534      *read = s->blocksize;
     535    } else if (available > 0) {
     536      fvec_copy(s->source_output, output);
     537      *read = available;
     538    } else {
     539      fvec_zeros(output);
     540      *read = 0;
     541    }
    537542  } else {
    538     fvec_zeros(output);
    539     *read = 0;
     543    aubio_sampler_reading_from_source_ring_pull(s, output, read);
    540544  }
    541545  //*read = s->available;
     
    547551#ifdef HAVE_THREADS
    548552  if (s->threaded_read) { // if threaded
    549     if (s->source_blocksize == s->blocksize) {
    550       aubio_sampler_read_from_source_threaded(s, output, read);
    551     } else {
    552       //abort();
    553     }
     553    aubio_sampler_read_from_source_threaded(s, output, read);
    554554  } else
    555555#endif
     
    578578    tmpout.length = s->blocksize - wrote;
    579579    aubio_ringbuffer_pull(s->ring, &tmpout, tmpout.length);
    580     //*read += tmpout.length;
    581 #if 0
    582     int ring_avail = aubio_ringbuffer_get_available(s->ring);
    583     AUBIO_WRN("sampler: perfectloop, "
    584         "%d left in ring, looping pulled %d more, wrote %d + %d\n",
    585         ring_avail, s->available, wrote, tmpout.length);
    586 #endif
    587580    check_wrote += tmpout.length;
    588581    return tmpout.length;
     
    597590    s->available = aubio_sampler_pull_from_source(s);
    598591    if (s->available <= 0) {
    599       AUBIO_ERR("sampler: looping but s->available = 0 !\n");
     592      AUBIO_ERR("sampler: perfectloop but s->available = 0 !\n");
    600593    } else {
    601       //AUBIO_ERR("sampler: perfectloop fetch and push more\n");
    602594      aubio_ringbuffer_push(s->ring, s->source_output, s->available);
    603595    }
     
    621613    return -1;
    622614  }
    623   // do actual reading
    624615  aubio_source_do(s->source, s->source_output, &source_read);
    625 #if 0
    626   if (source_read < s->source_blocksize) {
    627     //AUBIO_ERR("sampler: _pull_from_source: short read %d < %d\n", source_read,
    628     //    s->source_blocksize);
    629     //aubio_sampler_do_eof(s);
    630     if (s->loop) {
    631       AUBIO_ERR("sampler: _pull_from_source: loop, should seeking to 0? %d / %d\n",
    632           source_read, s->source_blocksize);
    633       //aubio_sampler_seek(s, 0);
    634     }
    635   }
    636   //AUBIO_ERR("sampler: _pull_from_source: read %d / %d\n", source_read, length);
    637 #endif
    638616  return source_read;
    639617}
Note: See TracChangeset for help on using the changeset viewer.