source: src/io/sink_sndfile.c @ 14ac1db

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

src/io/sink_sndfile.h: add do_multi, preset_samplerate, preset_channels, get_samplerate, and get_channels

  • Property mode set to 100644
File size: 4.9 KB
Line 
1/*
2  Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org>
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 "sink_sndfile.h"
30#include "fvec.h"
31
32#define MAX_CHANNELS 6
33#define MAX_SIZE 4096
34
35struct _aubio_sink_sndfile_t {
36  uint_t samplerate;
37  uint_t channels;
38  char_t *path;
39
40  uint_t max_size;
41
42  SNDFILE *handle;
43  uint_t scratch_size;
44  smpl_t *scratch_data;
45};
46
47uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s);
48
49aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) {
50  aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t);
51  s->max_size = MAX_SIZE;
52  s->path = path;
53
54  if (path == NULL) {
55    AUBIO_ERR("sink_sndfile: Aborted opening null path\n");
56    return NULL;
57  }
58
59  s->samplerate = 0;
60  s->channels = 0;
61
62  // negative samplerate given, abort
63  if ((sint_t)samplerate < 0) goto beach;
64  // zero samplerate given. do not open yet
65  if ((sint_t)samplerate == 0) return s;
66
67  s->samplerate = samplerate;
68  s->channels = 1;
69
70  if (aubio_sink_sndfile_open(s) != AUBIO_OK) {;
71    goto beach;
72  }
73  return s;
74
75beach:
76  del_aubio_sink_sndfile(s);
77  return NULL;
78}
79
80uint_t aubio_sink_sndfile_preset_samplerate(aubio_sink_sndfile_t *s, uint_t samplerate)
81{
82  if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
83  s->samplerate = samplerate;
84  // automatically open when both samplerate and channels have been set
85  if (s->samplerate != 0 && s->channels != 0) {
86    return aubio_sink_sndfile_open(s);
87  }
88  return AUBIO_OK;
89}
90
91uint_t aubio_sink_sndfile_preset_channels(aubio_sink_sndfile_t *s, uint_t channels)
92{
93  if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
94  s->channels = channels;
95  // automatically open when both samplerate and channels have been set
96  if (s->samplerate != 0 && s->channels != 0) {
97    return aubio_sink_sndfile_open(s);
98  }
99  return AUBIO_OK;
100}
101
102uint_t aubio_sink_sndfile_get_samplerate(aubio_sink_sndfile_t *s)
103{
104  return s->samplerate;
105}
106
107uint_t aubio_sink_sndfile_get_channels(aubio_sink_sndfile_t *s)
108{
109  return s->channels;
110}
111
112uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) {
113  /* set output format */
114  SF_INFO sfinfo;
115  AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo));
116  sfinfo.samplerate = s->samplerate;
117  sfinfo.channels   = s->channels;
118  sfinfo.format     = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
119
120  /* try creating the file */
121  s->handle = sf_open (s->path, SFM_WRITE, &sfinfo);
122
123  if (s->handle == NULL) {
124    /* show libsndfile err msg */
125    AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL));
126    return AUBIO_FAIL;
127  }     
128
129  s->scratch_size = s->max_size*s->channels;
130  /* allocate data for de/interleaving reallocated when needed. */
131  if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) {
132    AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n",
133        s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS);
134    return AUBIO_FAIL;
135  }
136  s->scratch_data = AUBIO_ARRAY(float,s->scratch_size);
137
138  return AUBIO_OK;
139}
140
141void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){
142  uint_t i, j,  channels = s->channels;
143  int nsamples = channels*write;
144  smpl_t *pwrite;
145  sf_count_t written_frames;
146
147  if (write > s->max_size) {
148    AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
149      write, s->max_size);
150    write = s->max_size;
151  }
152
153  /* interleaving data  */
154  for ( i = 0; i < channels; i++) {
155    pwrite = (smpl_t *)write_data->data;
156    for (j = 0; j < write; j++) {
157      s->scratch_data[channels*j+i] = pwrite[j];
158    }
159  }
160
161  written_frames = sf_write_float (s->handle, s->scratch_data, nsamples);
162  if (written_frames/channels != write) {
163    AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written",
164      write, s->path, (uint_t)written_frames);
165  }
166  return;
167}
168
169uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
170  if (!s->handle) {
171    return AUBIO_FAIL;
172  }
173  if (sf_close(s->handle)) {
174    AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL));
175    return AUBIO_FAIL;
176  }
177  s->handle = NULL;
178  return AUBIO_OK;
179}
180
181void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
182  if (!s) return;
183  aubio_sink_sndfile_close(s);
184  AUBIO_FREE(s->scratch_data);
185  AUBIO_FREE(s);
186}
187
188#endif /* HAVE_SNDFILE */
Note: See TracBrowser for help on using the repository browser.