source: src/io/sink_sndfile.c @ 5e56bbd

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

src/aubio_priv.h: move include config.h here

  • Property mode set to 100644
File size: 6.4 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");
[2c3d4ca]61    return NULL;
62  }
63
[b643a33]64  if (s->path) AUBIO_FREE(s->path);
[d2be104]65  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
66  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[b643a33]67
[14ac1db]68  s->samplerate = 0;
69  s->channels = 0;
70
71  // zero samplerate given. do not open yet
[cf19b8a]72  if ((sint_t)samplerate == 0) {
73    return s;
74  }
75  // invalid samplerate given, abort
76  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
77    goto beach;
78  }
[14ac1db]79
[2c3d4ca]80  s->samplerate = samplerate;
81  s->channels = 1;
82
[14ac1db]83  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
84    goto beach;
85  }
86  return s;
87
88beach:
89  del_aubio_sink_sndfile(s);
90  return NULL;
91}
92
93uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
94{
[cf19b8a]95  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
96    return AUBIO_FAIL;
97  }
[14ac1db]98  s->samplerate = samplerate;
99  // automatically open when both samplerate and channels have been set
100  if (s->samplerate != 0 && s->channels != 0) {
101    return aubio_sink_sndfile_open(s);
102  }
103  return AUBIO_OK;
104}
105
106uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
107{
[cf19b8a]108  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
109    return AUBIO_FAIL;
110  }
[14ac1db]111  s->channels = channels;
112  // automatically open when both samplerate and channels have been set
113  if (s->samplerate != 0 && s->channels != 0) {
114    return aubio_sink_sndfile_open(s);
115  }
116  return AUBIO_OK;
117}
118
[ae5d58a]119uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
[14ac1db]120{
121  return s->samplerate;
122}
123
[ae5d58a]124uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
[14ac1db]125{
126  return s->channels;
127}
128
129uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
[2c3d4ca]130  /* set output format */
[14ac1db]131  SF_INFO sfinfo;
[2c3d4ca]132  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
133  sfinfo.samplerate = s->samplerate;
134  sfinfo.channels   = s->channels;
135  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
136
137  /* try creating the file */
138  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
139
140  if (s->handle == NULL) {
141    /* show libsndfile err msg */
[bb96d02]142    AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n",
143        s->path, s->channels, s->samplerate, sf_strerror (NULL));
[14ac1db]144    return AUBIO_FAIL;
[a65d37a]145  }
[2c3d4ca]146
[11e92ea]147  s->scratch_size = s->max_size*s->channels;
[2c3d4ca]148  /* allocate data for de/interleaving reallocated when needed. */
[a028a04]149  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
150    abort();
[491e6ea]151    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
[a028a04]152        s->max_size, s->channels, MAX_SIZE * AUBIO_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.