source: src/io/sink_sndfile.c @ e406835

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since e406835 was e406835, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[io] prevent potential memory leak, never call abort

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[2c3d4ca]1/*
[14ac1db]2  Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
[2c3d4ca]3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21
[33d0242]22#include "aubio_priv.h"
[2c3d4ca]23
24#ifdef HAVE_SNDFILE
25
26#include <sndfile.h>
27
28#include "fvec.h"
[2eccf22]29#include "fmat.h"
30#include "io/sink_sndfile.h"
[cf19b8a]31#include "io/ioutils.h"
[2c3d4ca]32
33#define MAX_SIZE 4096
34
[385a06e2]35#if !HAVE_AUBIO_DOUBLE
36#define aubio_sf_write_smpl sf_write_float
37#else /* HAVE_AUBIO_DOUBLE */
38#define aubio_sf_write_smpl sf_write_double
39#endif /* HAVE_AUBIO_DOUBLE */
40
[2c3d4ca]41struct _aubio_sink_sndfile_t {
42  uint_t samplerate;
43  uint_t channels;
44  char_t *path;
[11e92ea]45
46  uint_t max_size;
47
[2c3d4ca]48  SNDFILE *handle;
49  uint_t scratch_size;
50  smpl_t *scratch_data;
51};
52
[14ac1db]53uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
54
[ae5d58a]55aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
[2c3d4ca]56  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
[14ac1db]57  s->max_size = MAX_SIZE;
[2c3d4ca]58
59  if (path == NULL) {
[491e6ea]60    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
[e406835]61    goto beach;
[2c3d4ca]62  }
63
[d2be104]64  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
65  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[b643a33]66
[14ac1db]67  s->samplerate = 0;
68  s->channels = 0;
69
70  // zero samplerate given. do not open yet
[cf19b8a]71  if ((sint_t)samplerate == 0) {
72    return s;
73  }
74  // invalid samplerate given, abort
75  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
76    goto beach;
77  }
[14ac1db]78
[2c3d4ca]79  s->samplerate = samplerate;
80  s->channels = 1;
81
[14ac1db]82  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
83    goto beach;
84  }
85  return s;
86
87beach:
88  del_aubio_sink_sndfile(s);
89  return NULL;
90}
91
92uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
93{
[cf19b8a]94  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
95    return AUBIO_FAIL;
96  }
[14ac1db]97  s->samplerate = samplerate;
98  // automatically open when both samplerate and channels have been set
[e406835]99  if (/* s->samplerate != 0 && */ s->channels != 0) {
[14ac1db]100    return aubio_sink_sndfile_open(s);
101  }
102  return AUBIO_OK;
103}
104
105uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
106{
[cf19b8a]107  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
108    return AUBIO_FAIL;
109  }
[14ac1db]110  s->channels = channels;
111  // automatically open when both samplerate and channels have been set
[e406835]112  if (s->samplerate != 0 /* && s->channels != 0 */) {
[14ac1db]113    return aubio_sink_sndfile_open(s);
114  }
115  return AUBIO_OK;
116}
117
[ae5d58a]118uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
[14ac1db]119{
120  return s->samplerate;
121}
122
[ae5d58a]123uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
[14ac1db]124{
125  return s->channels;
126}
127
128uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
[2c3d4ca]129  /* set output format */
[14ac1db]130  SF_INFO sfinfo;
[2c3d4ca]131  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
132  sfinfo.samplerate = s->samplerate;
133  sfinfo.channels   = s->channels;
134  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
135
136  /* try creating the file */
137  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
138
139  if (s->handle == NULL) {
140    /* show libsndfile err msg */
[bb96d02]141    AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n",
142        s->path, s->channels, s->samplerate, sf_strerror (NULL));
[14ac1db]143    return AUBIO_FAIL;
[a65d37a]144  }
[2c3d4ca]145
[11e92ea]146  s->scratch_size = s->max_size*s->channels;
[2c3d4ca]147  /* allocate data for de/interleaving reallocated when needed. */
[a028a04]148  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
[e406835]149    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n",
[a028a04]150        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
[14ac1db]151    return AUBIO_FAIL;
[2c3d4ca]152  }
[385a06e2]153  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
[2c3d4ca]154
[14ac1db]155  return AUBIO_OK;
[2c3d4ca]156}
157
[8aed26d]158void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
[4f75d8a]159  uint_t i, j;
[c21acb9]160  sf_count_t written_frames;
[4f75d8a]161  uint_t channels = s->channels;
162  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
163      s->max_size, write_data->length, write);
164  int nsamples = channels * length;
[ad1ba08]165
[2c3d4ca]166  /* interleaving data  */
167  for ( i = 0; i < channels; i++) {
[4f75d8a]168    for (j = 0; j < length; j++) {
169      s->scratch_data[channels*j+i] = write_data->data[j];
[2c3d4ca]170    }
171  }
[8aed26d]172
[385a06e2]173  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[776a5d3]174  if (written_frames/channels != write) {
[ad1ba08]175    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[776a5d3]176      write, s->path, (uint_t)written_frames);
[8aed26d]177  }
[2c3d4ca]178  return;
179}
180
[2eccf22]181void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
[4f75d8a]182  uint_t i, j;
[2eccf22]183  sf_count_t written_frames;
[4f75d8a]184  uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path,
185      s->channels, write_data->height);
186  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
187      s->max_size, write_data->length, write);
188  int nsamples = channels * length;
[ad1ba08]189
[2eccf22]190  /* interleaving data  */
[4f75d8a]191  for ( i = 0; i < channels; i++) {
192    for (j = 0; j < length; j++) {
193      s->scratch_data[channels*j+i] = write_data->data[i][j];
[2eccf22]194    }
195  }
196
[385a06e2]197  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[2eccf22]198  if (written_frames/channels != write) {
[ad1ba08]199    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[2eccf22]200      write, s->path, (uint_t)written_frames);
201  }
202  return;
203}
204
[a9fd272]205uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
206  if (!s->handle) {
207    return AUBIO_FAIL;
208  }
[2c3d4ca]209  if (sf_close(s->handle)) {
[491e6ea]210    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
[a9fd272]211    return AUBIO_FAIL;
[2c3d4ca]212  }
[a9fd272]213  s->handle = NULL;
214  return AUBIO_OK;
215}
216
217void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
[e406835]218  AUBIO_ASSERT(s);
219  if (s->handle)
220    aubio_sink_sndfile_close(s);
221  if (s->path)
222    AUBIO_FREE(s->path);
223  if (s->scratch_data)
224    AUBIO_FREE(s->scratch_data);
[2c3d4ca]225  AUBIO_FREE(s);
226}
227
228#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.