Ignore:
Timestamp:
Oct 9, 2016, 8:04:35 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
sampler
Children:
a006c5f
Parents:
128589e
Message:

src/synth/sampler.c: clean-up, move first read in reading thread

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/synth/sampler.c

    r128589e rf253b03  
    6565  aubio_ringbuffer_t *ring;
    6666  uint_t perfectloop;
     67  uint_t eof_remaining;
    6768  fvec_t *samples;
    6869  fmat_t *msamples;
     
    151152  //s->source_blocksize = 8192;
    152153
     154  if (s->threaded_read || s->perfectloop)
     155    s->source_output = new_fvec(s->source_blocksize);
    153156  if (s->perfectloop || s->source_blocksize != s->blocksize) {
    154     s->source_output = new_fvec(s->source_blocksize);
    155     s->ring = new_aubio_ringbuffer(s->source_blocksize, s->blocksize);
     157    s->ring = new_aubio_ringbuffer(s->source_blocksize * 2, s->blocksize);
    156158  }
    157159  //s->channels = 1;
     
    236238    o->finished = 0;
    237239    o->eof = 0;
     240    o->eof_remaining = 0;
    238241    o->opened = 1;
    239242    ret = AUBIO_OK;
     
    250253    o->finished = 1;
    251254    o->eof = 0;
     255    o->eof_remaining = 0;
    252256    o->opened = 0;
    253257    AUBIO_WRN("sampler: failed loading %s\n", uri);
    254258  }
    255259  if (o->ring) {
    256     AUBIO_WRN("sampler: resetting ringbuffer\n");
     260    //AUBIO_WRN("sampler: resetting ringbuffer\n");
    257261    aubio_ringbuffer_reset(o->ring);
    258262  }
     
    369373  pthread_exit(NULL);
    370374}
    371 
    372 void *aubio_sampler_readfn_ring(void *z) {
    373   aubio_sampler_t *p = z;
    374   while(1) {
    375     pthread_mutex_lock(&p->read_mutex);
    376     if (p->open_thread_running) {
    377       //AUBIO_WRN("sampler: readfn(): file is being opened\n");
    378       pthread_cond_signal(&p->read_avail);
    379       //pthread_cond_wait(&p->read_request, &p->read_mutex);
    380     } else if (p->opened && !p->started && !p->finished) {
    381       //AUBIO_WRN("sampler: readfn(): file started\n");
    382       p->available = aubio_sampler_pull_from_source(p);
    383       if (p->available < (sint_t)p->source_blocksize)
    384         aubio_sampler_do_eof(p);
    385       pthread_cond_signal(&p->read_avail);
    386       if (!p->finished) {
    387         pthread_cond_wait(&p->read_request, &p->read_mutex);
    388       }
    389     } else {
    390       //AUBIO_WRN("sampler: readfn(): idle?\n");
    391       //pthread_cond_signal(&p->read_avail);
    392       pthread_cond_wait(&p->read_request, &p->read_mutex);
    393       if (p->read_thread_finish) {
    394         goto done;
    395       }
    396     }
    397     pthread_mutex_unlock(&p->read_mutex);
    398   }
    399 done:
    400   //AUBIO_WRN("sampler: exiting reading thread\n");
    401   pthread_mutex_unlock(&p->read_mutex);
    402   pthread_exit(NULL);
    403 }
    404375#endif
    405376
     
    412383  }
    413384}
    414 
    415 static void aubio_sampler_do_perfectloop(aubio_sampler_t *s);
    416385
    417386static void
     
    438407  if (ring_avail < (sint_t)s->blocksize) {
    439408    available = aubio_sampler_pull_from_source(s);
    440 #if 0
    441     //AUBIO_MSG("sampler: got %d / %d fetching from source\n", s->available,
    442     //    s->source_blocksize);
    443     if (s->available == 0) {
    444       //aubio_sampler_do_eof(s);
    445       //aubio_sampler_do_perfectloop(s, output);
    446     }
    447     if (s->available < s->source_blocksize) {
    448       AUBIO_ERR("sampler: short read from source"
    449           " s->available %d, ring_avail %d \n", s->available, ring_avail);
    450       //s->eof = 0;
    451       //abort();
    452     }
    453 #endif
    454409    if (available > 0) {
    455410      aubio_ringbuffer_push(s->ring, s->source_output, available);
    456     } else {
    457       //if (s->playing && !s->finished)
    458       //AUBIO_WRN("sampler: not reading anymore should do eof now %d\n", check_wrote);
    459       // TODO: move eof fetch now
     411    }
     412    if (available < s->source_blocksize) {
     413      if (ring_avail + available <= s->blocksize) {
     414        s->eof_remaining = available + ring_avail;
     415        if (s->eof_remaining == 0) s->eof_remaining = s->blocksize;
     416        //AUBIO_ERR("sampler: marking special eof got: %d, in ring: %d, %d, eof remaining %d\n",
     417        //    available, ring_avail, s->blocksize, s->eof_remaining);
     418        if (s->loop) {
     419          aubio_sampler_seek(s,0);
     420          // read some frames from beginning of source for perfect looping
     421          if (s->perfectloop) {
     422            available = aubio_sampler_pull_from_source(s);
     423            if (available <= 0) {
     424              AUBIO_ERR("sampler: perfectloop but s->available = 0 !\n");
     425            } else {
     426              aubio_ringbuffer_push(s->ring, s->source_output, available);
     427            }
     428          }
     429        }
     430      }
    460431    }
    461432  }
    462433  return available;
    463434}
    464 
    465 uint_t
    466 aubio_sampler_write_remaining_ring(aubio_sampler_t *s, fvec_t *output, uint_t wrote);
    467435
    468436static void
     
    477445    check_wrote += s->blocksize;
    478446    *read = s->blocksize;
     447    if (s->eof_remaining > 0) {
     448      if (s->eof_remaining <= s->blocksize) {
     449        s->eof = 1;
     450        s->eof_remaining = 0;
     451      } else if (s->eof_remaining <= s->source_blocksize) {
     452        s->eof_remaining -= s->blocksize;
     453      }
     454    }
    479455  } else {
    480     //AUBIO_MSG("sampler: last frame, pulling remaining %d left\n", ring_avail);
     456    AUBIO_MSG("sampler: last frame, pulling remaining %d left\n", ring_avail);
    481457    *read = 0;
    482458    if (ring_avail > 0) {
     
    486462      *read += ring_avail;
    487463    }
    488     // signal eof, seek to 0 if looping
     464    // signal eof
    489465    aubio_sampler_do_eof(s);
    490     // read new frames from source early if needed
    491     aubio_sampler_do_perfectloop(s);
    492     // write the remaining frames
    493     *read += aubio_sampler_write_remaining_ring(s, output, ring_avail);
     466    // finished playing, reset ringbuffer for next read
     467    if (!s->playing)
     468      aubio_ringbuffer_reset(s->ring);
    494469  }
    495470}
     
    571546}
    572547
    573 uint_t
    574 aubio_sampler_write_remaining_ring(aubio_sampler_t *s, fvec_t *output, uint_t wrote){
    575   if (s->perfectloop && s->loop) {
    576     fvec_t tmpout;
    577     tmpout.data = output->data + wrote;
    578     tmpout.length = s->blocksize - wrote;
    579     aubio_ringbuffer_pull(s->ring, &tmpout, tmpout.length);
    580     check_wrote += tmpout.length;
    581     return tmpout.length;
    582   }
    583   return 0;
    584 }
    585 
    586 void
    587 aubio_sampler_do_perfectloop(aubio_sampler_t *s) {
    588   // perfectloop fetched new samples from start of source to ringbuffer
    589   if (s->perfectloop && s->loop) {
    590     s->available = aubio_sampler_pull_from_source(s);
    591     if (s->available <= 0) {
    592       AUBIO_ERR("sampler: perfectloop but s->available = 0 !\n");
    593     } else {
    594       aubio_ringbuffer_push(s->ring, s->source_output, s->available);
    595     }
    596   }
    597 }
    598 
    599548void
    600549aubio_sampler_read_from_table(aubio_sampler_t *o, fvec_t *output, uint_t *read) {
     
    778727#endif
    779728  //if (o->source_output) {
    780   if (o->perfectloop || o->ring) {
     729  if (o->source_output && (o->threaded_read || o->perfectloop)) {
    781730    del_fvec(o->source_output);
    782731  }
Note: See TracChangeset for help on using the changeset viewer.