source: src/io/sink_sndfile.c @ 99365e9

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 99365e9 was 0da5208, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[io] sink_sndfile: try guessing format according to file extension

  • Property mode set to 100644
File size: 7.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;
[0da5208]51  int format;
[2c3d4ca]52};
53
[14ac1db]54uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
55
[0da5208]56uint_t aubio_str_extension_matches(const char_t *ext,
57    const char_t *pattern);
58const char_t *aubio_str_get_extension(const char_t *filename);
59
[ae5d58a]60aubio_sink_sndfile_t * new_aubio_sink_sndfile(const char_t * path, uint_t samplerate) {
[2c3d4ca]61  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
[14ac1db]62  s->max_size = MAX_SIZE;
[2c3d4ca]63
64  if (path == NULL) {
[491e6ea]65    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
[e406835]66    goto beach;
[2c3d4ca]67  }
68
[d2be104]69  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
70  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[b643a33]71
[14ac1db]72  s->samplerate = 0;
73  s->channels = 0;
74
[0da5208]75  aubio_sink_sndfile_preset_format(s, aubio_str_get_extension(path));
76
[14ac1db]77  // zero samplerate given. do not open yet
[cf19b8a]78  if ((sint_t)samplerate == 0) {
79    return s;
80  }
81  // invalid samplerate given, abort
82  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
83    goto beach;
84  }
[14ac1db]85
[2c3d4ca]86  s->samplerate = samplerate;
87  s->channels = 1;
88
[14ac1db]89  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
90    goto beach;
91  }
92  return s;
93
94beach:
95  del_aubio_sink_sndfile(s);
96  return NULL;
97}
98
99uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
100{
[cf19b8a]101  if (aubio_io_validate_samplerate("sink_sndfile", s->path, samplerate)) {
102    return AUBIO_FAIL;
103  }
[14ac1db]104  s->samplerate = samplerate;
105  // automatically open when both samplerate and channels have been set
[e406835]106  if (/* s->samplerate != 0 && */ s->channels != 0) {
[14ac1db]107    return aubio_sink_sndfile_open(s);
108  }
109  return AUBIO_OK;
110}
111
112uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
113{
[cf19b8a]114  if (aubio_io_validate_channels("sink_sndfile", s->path, channels)) {
115    return AUBIO_FAIL;
116  }
[14ac1db]117  s->channels = channels;
118  // automatically open when both samplerate and channels have been set
[e406835]119  if (s->samplerate != 0 /* && s->channels != 0 */) {
[14ac1db]120    return aubio_sink_sndfile_open(s);
121  }
122  return AUBIO_OK;
123}
124
[0da5208]125uint_t aubio_sink_sndfile_preset_format(aubio_sink_sndfile_t *s,
126    const char_t *fmt)
127{
128  if (aubio_str_extension_matches(fmt, "wav")) {
129    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
130  } else if (aubio_str_extension_matches(fmt, "aiff")) {
131    s->format = SF_FORMAT_AIFF | SF_FORMAT_PCM_16;
132  } else if (aubio_str_extension_matches(fmt, "flac")) {
133    s->format = SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
134  } else if (aubio_str_extension_matches(fmt, "ogg")) {
135    s->format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
136  } else if (atoi(fmt) > 0x010000) {
137    s->format = atoi(fmt);
138  } else {
139    AUBIO_WRN("sink_sndfile: could not guess format for %s,"
140       " using default (wav)\n", s->path);
141    s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
142    return AUBIO_FAIL;
143  }
144  return AUBIO_OK;
145}
146
[ae5d58a]147uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s)
[14ac1db]148{
149  return s->samplerate;
150}
151
[ae5d58a]152uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s)
[14ac1db]153{
154  return s->channels;
155}
156
157uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
[2c3d4ca]158  /* set output format */
[14ac1db]159  SF_INFO sfinfo;
[2c3d4ca]160  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
161  sfinfo.samplerate = s->samplerate;
162  sfinfo.channels   = s->channels;
[0da5208]163  sfinfo.format     = s->format;
[2c3d4ca]164
165  /* try creating the file */
166  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
167
168  if (s->handle == NULL) {
169    /* show libsndfile err msg */
[bb96d02]170    AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n",
171        s->path, s->channels, s->samplerate, sf_strerror (NULL));
[14ac1db]172    return AUBIO_FAIL;
[a65d37a]173  }
[2c3d4ca]174
[11e92ea]175  s->scratch_size = s->max_size*s->channels;
[2c3d4ca]176  /* allocate data for de/interleaving reallocated when needed. */
[a028a04]177  if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) {
[e406835]178    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n",
[a028a04]179        s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS);
[14ac1db]180    return AUBIO_FAIL;
[2c3d4ca]181  }
[385a06e2]182  s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size);
[2c3d4ca]183
[14ac1db]184  return AUBIO_OK;
[2c3d4ca]185}
186
[8aed26d]187void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
[4f75d8a]188  uint_t i, j;
[c21acb9]189  sf_count_t written_frames;
[4f75d8a]190  uint_t channels = s->channels;
191  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
192      s->max_size, write_data->length, write);
193  int nsamples = channels * length;
[ad1ba08]194
[2c3d4ca]195  /* interleaving data  */
196  for ( i = 0; i < channels; i++) {
[4f75d8a]197    for (j = 0; j < length; j++) {
198      s->scratch_data[channels*j+i] = write_data->data[j];
[2c3d4ca]199    }
200  }
[8aed26d]201
[385a06e2]202  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[776a5d3]203  if (written_frames/channels != write) {
[ad1ba08]204    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[776a5d3]205      write, s->path, (uint_t)written_frames);
[8aed26d]206  }
[2c3d4ca]207  return;
208}
209
[2eccf22]210void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){
[4f75d8a]211  uint_t i, j;
[2eccf22]212  sf_count_t written_frames;
[4f75d8a]213  uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path,
214      s->channels, write_data->height);
215  uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path,
216      s->max_size, write_data->length, write);
217  int nsamples = channels * length;
[ad1ba08]218
[2eccf22]219  /* interleaving data  */
[4f75d8a]220  for ( i = 0; i < channels; i++) {
221    for (j = 0; j < length; j++) {
222      s->scratch_data[channels*j+i] = write_data->data[i][j];
[2eccf22]223    }
224  }
225
[385a06e2]226  written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples);
[2eccf22]227  if (written_frames/channels != write) {
[ad1ba08]228    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n",
[2eccf22]229      write, s->path, (uint_t)written_frames);
230  }
231  return;
232}
233
[a9fd272]234uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
235  if (!s->handle) {
236    return AUBIO_FAIL;
237  }
[2c3d4ca]238  if (sf_close(s->handle)) {
[491e6ea]239    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
[a9fd272]240    return AUBIO_FAIL;
[2c3d4ca]241  }
[a9fd272]242  s->handle = NULL;
243  return AUBIO_OK;
244}
245
246void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
[e406835]247  AUBIO_ASSERT(s);
248  if (s->handle)
249    aubio_sink_sndfile_close(s);
250  if (s->path)
251    AUBIO_FREE(s->path);
252  if (s->scratch_data)
253    AUBIO_FREE(s->scratch_data);
[2c3d4ca]254  AUBIO_FREE(s);
255}
256
257#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.