source: src/io/sink_sndfile.c @ 1120f86

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 1120f86 was 385a06e2, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/io/sink_sndfile.c: fix for double precision

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