source: src/io/sink_sndfile.c @ cf19b8a

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampleryinfft+
Last change on this file since cf19b8a was cf19b8a, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/io/ioutils.h: add functions to check samplerate and channels, use in sink_*.c

  • Property mode set to 100644
File size: 6.3 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
22#include "config.h"
23
24#ifdef HAVE_SNDFILE
25
26#include <sndfile.h>
27
28#include "aubio_priv.h"
29#include "fvec.h"
[2eccf22]30#include "fmat.h"
31#include "io/sink_sndfile.h"
[cf19b8a]32#include "io/ioutils.h"
[2c3d4ca]33
34#define MAX_CHANNELS 6
35#define MAX_SIZE 4096
36
[385a06e2]37#if !HAVE_AUBIO_DOUBLE
38#define aubio_sf_write_smpl sf_write_float
39#else /* HAVE_AUBIO_DOUBLE */
40#define aubio_sf_write_smpl sf_write_double
41#endif /* HAVE_AUBIO_DOUBLE */
42
[2c3d4ca]43struct _aubio_sink_sndfile_t {
44  uint_t samplerate;
45  uint_t channels;
46  char_t *path;
[11e92ea]47
48  uint_t max_size;
49
[2c3d4ca]50  SNDFILE *handle;
51  uint_t scratch_size;
52  smpl_t *scratch_data;
53};
54
[14ac1db]55uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
56
[ae5d58a]57aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
[2c3d4ca]58  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
[14ac1db]59  s->max_size = MAX_SIZE;
[2c3d4ca]60
61  if (path == NULL) {
[491e6ea]62    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
[2c3d4ca]63    return NULL;
64  }
65
[b643a33]66  if (s->path) AUBIO_FREE(s->path);
[d2be104]67  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
68  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[b643a33]69
[14ac1db]70  s->samplerate = 0;
71  s->channels = 0;
72
73  // zero samplerate given. do not open yet
[cf19b8a]74  if ((sint_t)samplerate == 0) {
75    return s;
76  }
77  // invalid samplerate given, abort
78  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
79    goto beach;
80  }
[14ac1db]81
[2c3d4ca]82  s->samplerate = samplerate;
83  s->channels = 1;
84
[14ac1db]85  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
86    goto beach;
87  }
88  return s;
89
90beach:
91  del_aubio_sink_sndfile(s);
92  return NULL;
93}
94
95uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
96{
[cf19b8a]97  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
98    return AUBIO_FAIL;
99  }
[14ac1db]100  s->samplerate = samplerate;
101  // automatically open when both samplerate and channels have been set
102  if (s->samplerate != 0 && s->channels != 0) {
103    return aubio_sink_sndfile_open(s);
104  }
105  return AUBIO_OK;
106}
107
108uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
109{
[cf19b8a]110  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
111    return AUBIO_FAIL;
112  }
[14ac1db]113  s->channels = channels;
114  // automatically open when both samplerate and channels have been set
115  if (s->samplerate != 0 && s->channels != 0) {
116    return aubio_sink_sndfile_open(s);
117  }
118  return AUBIO_OK;
119}
120
[ae5d58a]121uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
[14ac1db]122{
123  return s->samplerate;
124}
125
[ae5d58a]126uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
[14ac1db]127{
128  return s->channels;
129}
130
131uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
[2c3d4ca]132  /* set output format */
[14ac1db]133  SF_INFO sfinfo;
[2c3d4ca]134  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
135  sfinfo.samplerate = s->samplerate;
136  sfinfo.channels   = s->channels;
137  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
138
139  /* try creating the file */
140  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
141
142  if (s->handle == NULL) {
143    /* show libsndfile err msg */
[491e6ea]144    AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
[14ac1db]145    return AUBIO_FAIL;
[a65d37a]146  }
[2c3d4ca]147
[11e92ea]148  s->scratch_size = s->max_size*s->channels;
[2c3d4ca]149  /* allocate data for de/interleaving reallocated when needed. */
150  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
[491e6ea]151    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
[8aed26d]152        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
[14ac1db]153    return AUBIO_FAIL;
[2c3d4ca]154  }
[385a06e2]155  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
[2c3d4ca]156
[14ac1db]157  return AUBIO_OK;
[2c3d4ca]158}
159
[8aed26d]160void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
[a65d37a]161  uint_t i, j, channels = s->channels;
[ad1ba08]162  int nsamples = 0;
[2c3d4ca]163  smpl_t *pwrite;
[c21acb9]164  sf_count_t written_frames;
[2c3d4ca]165
[8aed26d]166  if (write > s->max_size) {
[ad1ba08]167    AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
[11e92ea]168      write, s->max_size);
169    write = s->max_size;
[8aed26d]170  }
171
[ad1ba08]172  nsamples = channels * write;
173
[2c3d4ca]174  /* interleaving data  */
175  for ( i = 0; i < channels; i++) {
[8aed26d]176    pwrite = (smpl_t *)write_data->data;
[776a5d3]177    for (j = 0; j < write; j++) {
[2c3d4ca]178      s->scratch_data[channels*j+i] = pwrite[j];
179    }
180  }
[8aed26d]181
[385a06e2]182  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[776a5d3]183  if (written_frames/channels != write) {
[ad1ba08]184    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[776a5d3]185      write, s->path, (uint_t)written_frames);
[8aed26d]186  }
[2c3d4ca]187  return;
188}
189
[2eccf22]190void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
[a65d37a]191  uint_t i, j, channels = s->channels;
[ad1ba08]192  int nsamples = 0;
[2eccf22]193  smpl_t *pwrite;
194  sf_count_t written_frames;
195
196  if (write > s->max_size) {
[ad1ba08]197    AUBIO_WRN("sink_sndfile: trying to write %d frames, but only %d can be written at a time\n",
[2eccf22]198      write, s->max_size);
199    write = s->max_size;
200  }
201
[ad1ba08]202  nsamples = channels * write;
203
[2eccf22]204  /* interleaving data  */
205  for ( i = 0; i < write_data->height; i++) {
206    pwrite = (smpl_t *)write_data->data[i];
207    for (j = 0; j < write; j++) {
208      s->scratch_data[channels*j+i] = pwrite[j];
209    }
210  }
211
[385a06e2]212  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[2eccf22]213  if (written_frames/channels != write) {
[ad1ba08]214    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[2eccf22]215      write, s->path, (uint_t)written_frames);
216  }
217  return;
218}
219
[a9fd272]220uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
221  if (!s->handle) {
222    return AUBIO_FAIL;
223  }
[2c3d4ca]224  if (sf_close(s->handle)) {
[491e6ea]225    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
[a9fd272]226    return AUBIO_FAIL;
[2c3d4ca]227  }
[a9fd272]228  s->handle = NULL;
229  return AUBIO_OK;
230}
231
232void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
233  if (!s) return;
[b643a33]234  if (s->path) AUBIO_FREE(s->path);
[a9fd272]235  aubio_sink_sndfile_close(s);
[2c3d4ca]236  AUBIO_FREE(s->scratch_data);
237  AUBIO_FREE(s);
238}
239
240#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.