source: src/io/sink_sndfile.c @ ee092a8

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

src/io/: also copy null ending char

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