Changeset 7a02ce9 for src


Ignore:
Timestamp:
Apr 1, 2019, 1:30:22 AM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/cnn, feature/crepe, feature/timestretch, fix/ffmpeg5, master
Children:
65f7886
Parents:
1301f66 (diff), 439ba7b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'feature/pitchshift' into feature/timestretch

Location:
src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • src/aubio_priv.h

    r1301f66 r7a02ce9  
    6363#endif
    6464
     65#ifdef HAVE_ERRNO_H
     66#include <errno.h>
     67#endif
     68
    6569#ifdef HAVE_LIMITS_H
    6670#include <limits.h> // for CHAR_BIT, in C99 standard
     
    7175#endif
    7276
    73 #if defined(HAVE_ACCELERATE)
    74 #define HAVE_ATLAS 1
    75 #define HAVE_BLAS 1
    76 #include <Accelerate/Accelerate.h>
    77 #elif defined(HAVE_ATLAS_CBLAS_H)
    78 #elif defined(HAVE_BLAS)
     77#if defined(HAVE_BLAS) // --enable-blas=true
     78// check which cblas header we found
    7979#if defined(HAVE_ATLAS_CBLAS_H)
    8080#define HAVE_ATLAS 1
     
    8484#elif defined(HAVE_CBLAS_H)
    8585#include <cblas.h>
    86 #endif
    87 #endif
    88 
    89 #ifdef HAVE_ACCELERATE
     86#elif !defined(HAVE_ACCELERATE)
     87#error "HAVE_BLAS was defined, but no blas header was found"
     88#endif /* end of cblas includes */
     89#endif
     90
     91#if defined(HAVE_ACCELERATE)
     92// include accelerate framework after blas
     93#define HAVE_ATLAS 1
     94#define HAVE_BLAS 1
    9095#include <Accelerate/Accelerate.h>
     96
    9197#ifndef HAVE_AUBIO_DOUBLE
    9298#define aubio_vDSP_mmov       vDSP_mmov
     
    325331#endif
    326332
     333#if !defined(_MSC_VER)
     334#define AUBIO_STRERROR(errno,buf,len) strerror_r(errno, buf, len)
     335#else
     336#define AUBIO_STRERROR(errno,buf,len) strerror_s(buf, len, errno)
     337#endif
     338
     339#ifdef HAVE_C99_VARARGS_MACROS
     340#define AUBIO_STRERR(...)            \
     341    char errorstr[256]; \
     342    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); \
     343    AUBIO_ERR(__VA_ARGS__)
     344#else
     345#define AUBIO_STRERR(format, args...)   \
     346    char errorstr[256]; \
     347    AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); \
     348    AUBIO_ERR(format, ##args)
     349#endif
     350
    327351/* handy shortcuts */
    328352#define DB2LIN(g) (POW(10.0,(g)*0.05f))
  • src/io/ioutils.c

    r1301f66 r7a02ce9  
    2020
    2121#include "aubio_priv.h"
     22#include "fmat.h"
    2223
    2324uint_t
     
    5152  }
    5253  return AUBIO_OK;
     54}
     55
     56uint_t
     57aubio_source_validate_input_length(const char_t *kind, const char_t *path,
     58    uint_t hop_size, uint_t read_data_length)
     59{
     60  uint_t length = hop_size;
     61  if (hop_size < read_data_length) {
     62    AUBIO_WRN("%s: partial read from %s, trying to read %d frames, but"
     63        " hop_size is %d\n", kind, path, read_data_length, hop_size);
     64  } else if (hop_size > read_data_length) {
     65    AUBIO_WRN("%s: partial read from %s, trying to read %d frames into"
     66        " a buffer of length %d\n", kind, path, hop_size, read_data_length);
     67    length = read_data_length;
     68  }
     69  return length;
     70}
     71
     72uint_t
     73aubio_source_validate_input_channels(const char_t *kind, const char_t *path,
     74    uint_t source_channels, uint_t read_data_height)
     75{
     76  uint_t channels = source_channels;
     77  if (read_data_height < source_channels) {
     78    AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
     79        " but found output of height %d\n", kind, path, source_channels,
     80        read_data_height);
     81    channels = read_data_height;
     82  } else if (read_data_height > source_channels) {
     83    // do not show a warning when trying to read into more channels than
     84    // the input source.
     85#if 0
     86    AUBIO_WRN("%s: partial read from %s, trying to read %d channels,"
     87        " but found output of height %d\n", kind, path, source_channels,
     88        read_data_height);
     89#endif
     90    channels = source_channels;
     91  }
     92  return channels;
     93}
     94
     95void
     96aubio_source_pad_output (fvec_t *read_data, uint_t source_read)
     97{
     98  if (source_read < read_data->length) {
     99    AUBIO_MEMSET(read_data->data + source_read, 0,
     100        (read_data->length - source_read) * sizeof(smpl_t));
     101  }
     102}
     103
     104void
     105aubio_source_pad_multi_output (fmat_t *read_data,
     106    uint_t source_channels, uint_t source_read) {
     107  uint_t i;
     108  if (source_read < read_data->length) {
     109    for (i = 0; i < read_data->height; i++) {
     110      AUBIO_MEMSET(read_data->data[i] + source_read, 0,
     111          (read_data->length - source_read) * sizeof(smpl_t));
     112    }
     113  }
     114
     115  // destination matrix has more channels than the file
     116  // copy channels from the source to extra output channels
     117  if (read_data->height > source_channels) {
     118    for (i = source_channels; i < read_data->height; i++) {
     119      AUBIO_MEMCPY(read_data->data[i], read_data->data[i % source_channels],
     120          sizeof(smpl_t) * read_data->length);
     121    }
     122  }
    53123}
    54124
  • src/io/ioutils.h

    r1301f66 r7a02ce9  
    5454    uint_t channels);
    5555
    56 /** validate length of input
     56/** validate length of source output
     57
     58  \param kind       the object kind to report on
     59  \param path       the path to report on
     60  \param hop_size   number of frames to be read
     61  \param read_data_length actual length of input
     62
     63  \return hop_size or the maximum number of frames that can be written
     64*/
     65uint_t
     66aubio_source_validate_input_length(const char_t *kind, const char_t *path,
     67    uint_t hop_size, uint_t read_data_length);
     68
     69/** validate height of source output
     70
     71  \param kind       the object kind to report on
     72  \param path       the path to report on
     73  \param source_channels maximum number of channels that can be written
     74  \param read_data_height actual height of input
     75
     76  \return write_data_height or the maximum number of channels
     77*/
     78uint_t
     79aubio_source_validate_input_channels(const char_t *kind, const char_t *path,
     80    uint_t source_channels, uint_t read_data_height);
     81
     82/** pad end of source output vector with zeroes
     83
     84  \param read_data   output vector to pad
     85  \param source_read number of frames read
     86
     87*/
     88void
     89aubio_source_pad_output (fvec_t *read_data, uint_t source_read);
     90
     91/** pad end of source output matrix with zeroes
     92
     93  \param read_data   output matrix to pad
     94  \param source_channels number of channels in the source
     95  \param source_read number of frames read
     96
     97*/
     98void
     99aubio_source_pad_multi_output (fmat_t *read_data, uint_t source_channels,
     100        uint_t source_read);
     101
     102/** validate length of sink input
    57103
    58104  \param kind       the object kind to report on
    59105  \param path       the path to report on
    60106  \param max_size   maximum number of frames that can be written
    61   \param write_data_length actual length of input vector/matrix
     107  \param write_data_length actual length of input
    62108  \param write number of samples asked
    63109
     
    68114    uint_t max_size, uint_t write_data_length, uint_t write);
    69115
    70 /** validate height of input
     116/** validate height of sink input
    71117
    72118  \param kind       the object kind to report on
    73119  \param path       the path to report on
    74   \param max_size  maximum number of channels that can be written
     120  \param sink_channels maximum number of channels that can be written
    75121  \param write_data_height actual height of input matrix
    76122
  • src/io/sink.c

    r1301f66 r7a02ce9  
    136136
    137137void del_aubio_sink(aubio_sink_t * s) {
    138   AUBIO_ASSERT(s);
    139   if (s->s_del && s->sink)
     138  //AUBIO_ASSERT(s);
     139  if (s && s->s_del && s->sink)
    140140    s->s_del((void *)s->sink);
    141141  AUBIO_FREE(s);
  • src/io/sink_wavwrite.c

    r1301f66 r7a02ce9  
    2828#include "io/sink_wavwrite.h"
    2929#include "io/ioutils.h"
    30 
    31 #include <errno.h>
    3230
    3331#define MAX_SIZE 4096
     
    163161  unsigned char buf[5];
    164162  uint_t byterate, blockalign;
     163  size_t written = 0;
    165164
    166165  /* open output file */
    167166  s->fid = fopen((const char *)s->path, "wb");
    168167  if (!s->fid) {
    169     AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));
     168    AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr);
    170169    goto beach;
    171170  }
    172171
    173172  // ChunkID
    174   fwrite("RIFF", 4, 1, s->fid);
     173  written += fwrite("RIFF", 4, 1, s->fid);
    175174
    176175  // ChunkSize (0 for now, actual size will be written in _close)
    177   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     176  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
    178177
    179178  // Format
    180   fwrite("WAVE", 4, 1, s->fid);
     179  written += fwrite("WAVE", 4, 1, s->fid);
    181180
    182181  // Subchunk1ID
    183   fwrite("fmt ", 4, 1, s->fid);
     182  written += fwrite("fmt ", 4, 1, s->fid);
    184183
    185184  // Subchunk1Size
    186   fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
     185  written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);
    187186
    188187  // AudioFormat
    189   fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
     188  written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);
    190189
    191190  // NumChannels
    192   fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
     191  written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);
    193192
    194193  // SampleRate
    195   fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
     194  written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);
    196195
    197196  // ByteRate
    198197  byterate = s->samplerate * s->channels * s->bitspersample / 8;
    199   fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
     198  written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);
    200199
    201200  // BlockAlign
    202201  blockalign = s->channels * s->bitspersample / 8;
    203   fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
     202  written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);
    204203
    205204  // BitsPerSample
    206   fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
     205  written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);
    207206
    208207  // Subchunk2ID
    209   fwrite("data", 4, 1, s->fid);
     208  written += fwrite("data", 4, 1, s->fid);
    210209
    211210  // Subchunk1Size (0 for now, actual size will be written in _close)
    212   fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     211  written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);
     212
     213  // fwrite(*, *, 1, s->fid) was called 13 times, check success
     214  if (written != 13 || fflush(s->fid)) {
     215    AUBIO_STRERR("sink_wavwrite: writing header to %s failed"
     216        " (wrote %d/%d, %s)\n", s->path, written, 13, errorstr);
     217    fclose(s->fid);
     218    s->fid = NULL;
     219    return AUBIO_FAIL;
     220  }
    213221
    214222  s->scratch_size = s->max_size * s->channels;
     
    227235}
    228236
     237static
     238void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write)
     239{
     240  uint_t written_frames = 0;
     241
     242  written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid);
     243
     244  if (written_frames != write) {
     245    AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d"
     246        " could be written (%s)\n", write, s->path, written_frames, errorstr);
     247  }
     248  s->total_frames_written += written_frames;
     249}
    229250
    230251void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){
    231   uint_t c = 0, i = 0, written_frames = 0;
     252  uint_t c = 0, i = 0;
    232253  uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path,
    233254      s->max_size, write_data->length, write);
     
    238259    }
    239260  }
    240   written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    241 
    242   if (written_frames != write) {
    243     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    244         "but only %d could be written\n", write, s->path, written_frames);
    245   }
    246   s->total_frames_written += written_frames;
    247   return;
     261
     262  aubio_sink_wavwrite_write_frames(s, length);
    248263}
    249264
    250265void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){
    251   uint_t c = 0, i = 0, written_frames = 0;
     266  uint_t c = 0, i = 0;
    252267
    253268  uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path,
     
    261276    }
    262277  }
    263   written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid);
    264 
    265   if (written_frames != write * s->channels) {
    266     AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, "
    267         "but only %d could be written\n", write, s->path, written_frames / s->channels);
    268   }
    269   s->total_frames_written += written_frames;
    270   return;
     278
     279  aubio_sink_wavwrite_write_frames(s, length);
    271280}
    272281
     
    274283  uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
    275284  unsigned char buf[5];
     285  size_t written = 0, err = 0;
    276286  if (!s->fid) return AUBIO_FAIL;
    277287  // ChunkSize
    278   fseek(s->fid, 4, SEEK_SET);
    279   fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
     288  err += fseek(s->fid, 4, SEEK_SET);
     289  written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
    280290  // Subchunk2Size
    281   fseek(s->fid, 40, SEEK_SET);
    282   fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     291  err += fseek(s->fid, 40, SEEK_SET);
     292  written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid);
     293  if (written != 2 || err != 0) {
     294    AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d"
     295        " write but got only %d (%s)\n", s->path, 2, written, errorstr);
     296  }
    283297  // close file
    284298  if (fclose(s->fid)) {
    285     AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
     299    AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr);
    286300  }
    287301  s->fid = NULL;
  • src/io/source.c

    r1301f66 r7a02ce9  
    139139
    140140void del_aubio_source(aubio_source_t * s) {
    141   AUBIO_ASSERT(s);
    142   if (s->s_del && s->source)
     141  //AUBIO_ASSERT(s);
     142  if (s && s->s_del && s->source)
    143143    s->s_del((void *)s->source);
    144144  AUBIO_FREE(s);
  • src/io/source_apple_audio.c

    r1301f66 r7a02ce9  
    2525#include "fvec.h"
    2626#include "fmat.h"
     27#include "ioutils.h"
    2728#include "io/source_apple_audio.h"
    2829
     
    210211  uint_t c, v;
    211212  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
     213  uint_t length = aubio_source_validate_input_length("source_apple_audio",
     214      s->path, s->block_size, read_to->length);
    212215  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    213216
    214   for (v = 0; v < loadedPackets; v++) {
     217  length = MIN(loadedPackets, length);
     218
     219  for (v = 0; v < length; v++) {
    215220    read_to->data[v] = 0.;
    216221    for (c = 0; c < s->channels; c++) {
     
    220225  }
    221226  // short read, fill with zeros
    222   if (loadedPackets < s->block_size) {
    223     for (v = loadedPackets; v < s->block_size; v++) {
    224       read_to->data[v] = 0.;
    225     }
    226   }
    227 
    228   *read = (uint_t)loadedPackets;
    229   return;
     227  aubio_source_pad_output(read_to, length);
     228
     229  *read = (uint_t)length;
    230230}
    231231
    232232void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
    233233  uint_t c, v;
     234  uint_t length = aubio_source_validate_input_length("source_apple_audio",
     235      s->path, s->block_size, read_to->length);
     236  uint_t channels = aubio_source_validate_input_channels("source_apple_audio",
     237      s->path, s->channels, read_to->height);
    234238  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
    235239  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    236240
    237   for (v = 0; v < loadedPackets; v++) {
    238     for (c = 0; c < read_to->height; c++) {
     241  length = MIN(loadedPackets, length);
     242
     243  for (v = 0; v < length; v++) {
     244    for (c = 0; c < channels; c++) {
    239245      read_to->data[c][v] = data[ v * s->channels + c];
    240246    }
    241247  }
    242   // if read_data has more channels than the file
    243   if (read_to->height > s->channels) {
    244     // copy last channel to all additional channels
    245     for (v = 0; v < loadedPackets; v++) {
    246       for (c = s->channels; c < read_to->height; c++) {
    247         read_to->data[c][v] = data[ v * s->channels + (s->channels - 1)];
    248       }
    249     }
    250   }
    251   // short read, fill with zeros
    252   if (loadedPackets < s->block_size) {
    253     for (v = loadedPackets; v < s->block_size; v++) {
    254       for (c = 0; c < read_to->height; c++) {
    255         read_to->data[c][v] = 0.;
    256       }
    257     }
    258   }
    259 
    260   *read = (uint_t)loadedPackets;
    261   return;
     248
     249  aubio_source_pad_multi_output(read_to, s->channels, (uint_t)length);
     250
     251  *read = (uint_t)length;
    262252}
    263253
     
    355345        "error in ExtAudioFileGetProperty (%s)\n", s->path,
    356346        getPrintableOSStatusError(errorstr, err));
    357     return err;
     347    return 0;
    358348  }
    359349  return (uint_t)fileLengthFrames;
  • src/io/source_avcodec.c

    r1301f66 r7a02ce9  
    3131#endif
    3232#include <libavutil/opt.h>
    33 #include <stdlib.h>
    3433
    3534// determine whether we use libavformat from ffmpeg or from libav
     
    6160#include "fvec.h"
    6261#include "fmat.h"
     62#include "ioutils.h"
    6363#include "source_avcodec.h"
    6464
     
    120120    uint_t samplerate, uint_t hop_size) {
    121121  aubio_source_avcodec_t * s = AUBIO_NEW(aubio_source_avcodec_t);
    122   AVFormatContext *avFormatCtx = s->avFormatCtx;
    123   AVCodecContext *avCodecCtx = s->avCodecCtx;
    124   AVFrame *avFrame = s->avFrame;
     122  AVFormatContext *avFormatCtx = NULL;
     123  AVCodecContext *avCodecCtx = NULL;
     124  AVFrame *avFrame = NULL;
    125125  sint_t selected_stream = -1;
    126126#if FF_API_LAVF_AVCTX
     
    464464      (const uint8_t **)avFrame->data, in_samples);
    465465#endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    466   if (out_samples <= 0) {
    467     AUBIO_WRN("source_avcodec: no sample found while converting frame (%s)\n",
    468         s->path);
     466  if (out_samples < 0) {
     467    AUBIO_WRN("source_avcodec: error while resampling %s (%d)\n",
     468        s->path, out_samples);
    469469    goto beach;
    470470  }
     
    473473
    474474beach:
    475   s->avFormatCtx = avFormatCtx;
    476   s->avCodecCtx = avCodecCtx;
    477   s->avFrame = avFrame;
    478 #if defined(HAVE_AVRESAMPLE) || defined(HAVE_SWRESAMPLE)
    479   s->avr = avr;
    480 #endif /* HAVE_AVRESAMPLE || HAVE_SWRESAMPLE */
    481   s->output = output;
    482 
    483475  av_packet_unref(&avPacket);
    484476}
     
    489481  uint_t end = 0;
    490482  uint_t total_wrote = 0;
    491   while (total_wrote < s->hop_size) {
    492     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
     483  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
     484      s->hop_size, read_data->length);
     485  if (!s->avr || !s->avFormatCtx || !s->avCodecCtx) {
     486    AUBIO_ERR("source_avcodec: could not read from %s (file was closed)\n",
     487        s->path);
     488    *read= 0;
     489    return;
     490  }
     491  while (total_wrote < length) {
     492    end = MIN(s->read_samples - s->read_index, length - total_wrote);
    493493    for (i = 0; i < end; i++) {
    494494      read_data->data[i + total_wrote] = 0.;
     
    500500    }
    501501    total_wrote += end;
    502     if (total_wrote < s->hop_size) {
     502    if (total_wrote < length) {
    503503      uint_t avcodec_read = 0;
    504504      aubio_source_avcodec_readframe(s, &avcodec_read);
     
    512512    }
    513513  }
    514   if (total_wrote < s->hop_size) {
    515     for (i = total_wrote; i < s->hop_size; i++) {
    516       read_data->data[i] = 0.;
    517     }
    518   }
     514
     515  aubio_source_pad_output(read_data, total_wrote);
     516
    519517  *read = total_wrote;
    520518}
     
    525523  uint_t end = 0;
    526524  uint_t total_wrote = 0;
    527   while (total_wrote < s->hop_size) {
    528     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
    529     for (j = 0; j < read_data->height; j++) {
     525  uint_t length = aubio_source_validate_input_length("source_avcodec", s->path,
     526      s->hop_size, read_data->length);
     527  uint_t channels = aubio_source_validate_input_channels("source_avcodec",
     528      s->path, s->input_channels, read_data->height);
     529  if (!s->avr || !s->avFormatCtx || !s->avCodecCtx) {
     530    AUBIO_ERR("source_avcodec: could not read from %s (file was closed)\n",
     531        s->path);
     532    *read= 0;
     533    return;
     534  }
     535  while (total_wrote < length) {
     536    end = MIN(s->read_samples - s->read_index, length - total_wrote);
     537    for (j = 0; j < channels; j++) {
    530538      for (i = 0; i < end; i++) {
    531539        read_data->data[j][i + total_wrote] =
     
    534542    }
    535543    total_wrote += end;
    536     if (total_wrote < s->hop_size) {
     544    if (total_wrote < length) {
    537545      uint_t avcodec_read = 0;
    538546      aubio_source_avcodec_readframe(s, &avcodec_read);
     
    546554    }
    547555  }
    548   if (total_wrote < s->hop_size) {
    549     for (j = 0; j < read_data->height; j++) {
    550       for (i = total_wrote; i < s->hop_size; i++) {
    551         read_data->data[j][i] = 0.;
    552       }
    553     }
    554   }
     556
     557  aubio_source_pad_multi_output(read_data, s->input_channels, total_wrote);
     558
    555559  *read = total_wrote;
    556560}
     
    616620#ifdef HAVE_AVRESAMPLE
    617621    avresample_close( s->avr );
     622    av_free ( s->avr );
    618623#elif defined(HAVE_SWRESAMPLE)
    619624    swr_close ( s->avr );
    620 #endif
    621     av_free ( s->avr );
     625    swr_free ( &s->avr );
     626#endif
    622627  }
    623628  s->avr = NULL;
  • src/io/source_sndfile.c

    r1301f66 r7a02ce9  
    2727#include "fvec.h"
    2828#include "fmat.h"
     29#include "ioutils.h"
    2930#include "source_sndfile.h"
    3031
     
    170171  uint_t i,j, input_channels = s->input_channels;
    171172  /* read from file into scratch_data */
    172   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
     173  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
     174      s->hop_size, read_data->length);
     175  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
     176      s->scratch_size);
     177  uint_t read_length = read_samples / s->input_channels;
    173178
    174179  /* where to store de-interleaved data */
    175180  smpl_t *ptr_data;
     181
     182  if (!s->handle) {
     183    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
     184        s->path);
     185    *read = 0;
     186    return;
     187  }
     188
    176189#ifdef HAVE_SAMPLERATE
    177190  if (s->ratio != 1) {
     
    180193#endif /* HAVE_SAMPLERATE */
    181194  {
     195    read_length = MIN(length, read_length);
    182196    ptr_data = read_data->data;
    183197  }
    184198
    185199  /* de-interleaving and down-mixing data  */
    186   for (j = 0; j < read_samples / input_channels; j++) {
     200  for (j = 0; j < read_length; j++) {
    187201    ptr_data[j] = 0;
    188202    for (i = 0; i < input_channels; i++) {
     
    198212#endif /* HAVE_SAMPLERATE */
    199213
    200   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
    201 
    202   if (*read < s->hop_size) {
    203     for (j = *read; j < s->hop_size; j++) {
    204       read_data->data[j] = 0;
    205     }
    206   }
     214  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
     215
     216  aubio_source_pad_output (read_data, *read);
    207217
    208218}
     
    211221  uint_t i,j, input_channels = s->input_channels;
    212222  /* do actual reading */
    213   sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
     223  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
     224      s->hop_size, read_data->length);
     225  uint_t channels = aubio_source_validate_input_channels("source_sndfile",
     226      s->path, s->input_channels, read_data->height);
     227  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
     228      s->scratch_size);
     229  uint_t read_length = read_samples / s->input_channels;
    214230
    215231  /* where to store de-interleaved data */
    216232  smpl_t **ptr_data;
     233
     234  if (!s->handle) {
     235    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
     236        s->path);
     237    *read = 0;
     238    return;
     239  }
     240
    217241#ifdef HAVE_SAMPLERATE
    218242  if (s->ratio != 1) {
     
    221245#endif /* HAVE_SAMPLERATE */
    222246  {
     247    read_length = MIN(read_length, length);
    223248    ptr_data = read_data->data;
    224249  }
    225250
    226   if (read_data->height < input_channels) {
    227     // destination matrix has less channels than the file; copy only first
    228     // channels of the file, de-interleaving data
    229     for (j = 0; j < read_samples / input_channels; j++) {
    230       for (i = 0; i < read_data->height; i++) {
    231         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    232       }
    233     }
    234   } else {
    235     // destination matrix has as many or more channels than the file; copy each
    236     // channel from the file to the destination matrix, de-interleaving data
    237     for (j = 0; j < read_samples / input_channels; j++) {
    238       for (i = 0; i < input_channels; i++) {
    239         ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    240       }
    241     }
    242   }
    243 
    244   if (read_data->height > input_channels) {
    245     // destination matrix has more channels than the file; copy last channel
    246     // of the file to each additional channels, de-interleaving data
    247     for (j = 0; j < read_samples / input_channels; j++) {
    248       for (i = input_channels; i < read_data->height; i++) {
    249         ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
    250       }
     251  for (j = 0; j < read_length; j++) {
     252    for (i = 0; i < channels; i++) {
     253      ptr_data[i][j] = s->scratch_data[j * input_channels + i];
    251254    }
    252255  }
     
    265268#endif /* HAVE_SAMPLERATE */
    266269
    267   *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
    268 
    269   if (*read < s->hop_size) {
    270     for (i = 0; i < read_data->height; i++) {
    271       for (j = *read; j < s->hop_size; j++) {
    272         read_data->data[i][j] = 0.;
    273       }
    274     }
    275   }
    276 
     270  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
     271
     272  aubio_source_pad_multi_output(read_data, input_channels, *read);
    277273}
    278274
  • src/io/source_wavread.c

    r1301f66 r7a02ce9  
    2525#include "fvec.h"
    2626#include "fmat.h"
     27#include "ioutils.h"
    2728#include "source_wavread.h"
    2829
    29 #include <errno.h>
    30 
    3130#define AUBIO_WAVREAD_BUFSIZE 1024
    3231
    33 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
     32//#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
    3433
    3534struct _aubio_source_wavread_t {
     
    10099  s->fid = fopen((const char *)path, "rb");
    101100  if (!s->fid) {
    102     AUBIO_ERR("source_wavread: Failed opening %s (System error: %s)\n", s->path, strerror(errno));
     101    AUBIO_STRERR("source_wavread: Failed opening %s (%s)\n", s->path, errorstr);
    103102    goto beach;
    104103  }
     
    133132    bytes_junk += read_little_endian(buf, 4);
    134133    if (fseek(s->fid, bytes_read + bytes_junk, SEEK_SET) != 0) {
    135       AUBIO_ERR("source_wavread: Failed opening %s (could not seek past JUNK Chunk: %s)\n",
    136           s->path, strerror(errno));
     134      AUBIO_STRERR("source_wavread: Failed opening %s (could not seek past JUNK Chunk: %s)\n",
     135          s->path, errorstr);
    137136      goto beach;
    138137    }
     
    261260    bytes_junk += read_little_endian(buf, 4);
    262261    if (fseek(s->fid, bytes_read + bytes_junk, SEEK_SET) != 0) {
    263       AUBIO_ERR("source_wavread: could not seek past unknown chunk in %s (%s)\n",
    264           s->path, strerror(errno));
     262      AUBIO_STRERR("source_wavread: could not seek past unknown chunk in %s (%s)\n",
     263          s->path, errorstr);
    265264      goto beach;
    266265    }
     
    348347  uint_t end = 0;
    349348  uint_t total_wrote = 0;
     349  uint_t length = aubio_source_validate_input_length("source_wavread", s->path,
     350      s->hop_size, read_data->length);
    350351  if (s->fid == NULL) {
    351352    AUBIO_ERR("source_wavread: could not read from %s (file not opened)\n",
     
    353354    return;
    354355  }
    355   while (total_wrote < s->hop_size) {
    356     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
     356  while (total_wrote < length) {
     357    end = MIN(s->read_samples - s->read_index, length - total_wrote);
    357358    for (i = 0; i < end; i++) {
    358359      read_data->data[i + total_wrote] = 0;
     
    363364    }
    364365    total_wrote += end;
    365     if (total_wrote < s->hop_size) {
     366    if (total_wrote < length) {
    366367      uint_t wavread_read = 0;
    367368      aubio_source_wavread_readframe(s, &wavread_read);
     
    375376    }
    376377  }
    377   if (total_wrote < s->hop_size) {
    378     for (i = end; i < s->hop_size; i++) {
    379       read_data->data[i] = 0.;
    380     }
    381   }
     378
     379  aubio_source_pad_output (read_data, total_wrote);
     380
    382381  *read = total_wrote;
    383382}
     
    387386  uint_t end = 0;
    388387  uint_t total_wrote = 0;
     388  uint_t length = aubio_source_validate_input_length("source_wavread", s->path,
     389      s->hop_size, read_data->length);
     390  uint_t channels = aubio_source_validate_input_channels("source_wavread",
     391      s->path, s->input_channels, read_data->height);
    389392  if (s->fid == NULL) {
    390393    AUBIO_ERR("source_wavread: could not read from %s (file not opened)\n",
     
    392395    return;
    393396  }
    394   while (total_wrote < s->hop_size) {
    395     end = MIN(s->read_samples - s->read_index, s->hop_size - total_wrote);
    396     for (j = 0; j < read_data->height; j++) {
     397  while (total_wrote < length) {
     398    end = MIN(s->read_samples - s->read_index, length - total_wrote);
     399    for (j = 0; j < channels; j++) {
    397400      for (i = 0; i < end; i++) {
    398401        read_data->data[j][i + total_wrote] = s->output->data[j][i];
     
    400403    }
    401404    total_wrote += end;
    402     if (total_wrote < s->hop_size) {
     405    if (total_wrote < length) {
    403406      uint_t wavread_read = 0;
    404407      aubio_source_wavread_readframe(s, &wavread_read);
     
    412415    }
    413416  }
    414   if (total_wrote < s->hop_size) {
    415     for (j = 0; j < read_data->height; j++) {
    416       for (i = end; i < s->hop_size; i++) {
    417         read_data->data[j][i] = 0.;
    418       }
    419     }
    420   }
     417
     418  aubio_source_pad_multi_output(read_data, s->input_channels, total_wrote);
     419
    421420  *read = total_wrote;
    422421}
     
    442441  ret = fseek(s->fid, s->seek_start + pos * s->blockalign, SEEK_SET);
    443442  if (ret != 0) {
    444     AUBIO_ERR("source_wavread: could not seek %s at %d (%s)\n", s->path, pos, strerror(errno));
     443    AUBIO_STRERR("source_wavread: could not seek %s at %d (%s)\n", s->path, pos, errorstr);
    445444    return AUBIO_FAIL;
    446445  }
     
    463462  }
    464463  if (fclose(s->fid)) {
    465     AUBIO_ERR("source_wavread: could not close %s (%s)\n", s->path, strerror(errno));
     464    AUBIO_STRERR("source_wavread: could not close %s (%s)\n", s->path, errorstr);
    466465    return AUBIO_FAIL;
    467466  }
Note: See TracChangeset for help on using the changeset viewer.