[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] | 41 | struct _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] | 54 | uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s); |
---|
| 55 | |
---|
[0da5208] | 56 | uint_t aubio_str_extension_matches(const char_t *ext, |
---|
| 57 | const char_t *pattern); |
---|
| 58 | const char_t *aubio_str_get_extension(const char_t *filename); |
---|
| 59 | |
---|
[ae5d58a] | 60 | aubio_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 | |
---|
| 94 | beach: |
---|
| 95 | del_aubio_sink_sndfile(s); |
---|
| 96 | return NULL; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | uint_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 | |
---|
| 112 | uint_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] | 125 | uint_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 | s->format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; |
---|
[5fc6e81] | 140 | if (fmt && strnlen(fmt, PATH_MAX)) { |
---|
| 141 | AUBIO_WRN("sink_sndfile: could not guess format %s for %s," |
---|
| 142 | " using default (wav)\n", fmt, s->path); |
---|
| 143 | return AUBIO_FAIL; |
---|
| 144 | } |
---|
[0da5208] | 145 | } |
---|
| 146 | return AUBIO_OK; |
---|
| 147 | } |
---|
| 148 | |
---|
[ae5d58a] | 149 | uint_t aubio_sink_sndfile_get_samplerate(const aubio_sink_sndfile_t *s) |
---|
[14ac1db] | 150 | { |
---|
| 151 | return s->samplerate; |
---|
| 152 | } |
---|
| 153 | |
---|
[ae5d58a] | 154 | uint_t aubio_sink_sndfile_get_channels(const aubio_sink_sndfile_t *s) |
---|
[14ac1db] | 155 | { |
---|
| 156 | return s->channels; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | uint_t aubio_sink_sndfile_open(aubio_sink_sndfile_t *s) { |
---|
[2c3d4ca] | 160 | /* set output format */ |
---|
[14ac1db] | 161 | SF_INFO sfinfo; |
---|
[2c3d4ca] | 162 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 163 | sfinfo.samplerate = s->samplerate; |
---|
| 164 | sfinfo.channels = s->channels; |
---|
[0da5208] | 165 | sfinfo.format = s->format; |
---|
[2c3d4ca] | 166 | |
---|
| 167 | /* try creating the file */ |
---|
| 168 | s->handle = sf_open (s->path, SFM_WRITE, &sfinfo); |
---|
| 169 | |
---|
| 170 | if (s->handle == NULL) { |
---|
| 171 | /* show libsndfile err msg */ |
---|
[bb96d02] | 172 | AUBIO_ERR("sink_sndfile: Failed opening \"%s\" with %d channels, %dHz: %s\n", |
---|
| 173 | s->path, s->channels, s->samplerate, sf_strerror (NULL)); |
---|
[14ac1db] | 174 | return AUBIO_FAIL; |
---|
[a65d37a] | 175 | } |
---|
[2c3d4ca] | 176 | |
---|
[11e92ea] | 177 | s->scratch_size = s->max_size*s->channels; |
---|
[2c3d4ca] | 178 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
[a028a04] | 179 | if (s->scratch_size >= MAX_SIZE * AUBIO_MAX_CHANNELS) { |
---|
[e406835] | 180 | AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum buffer size %d\n", |
---|
[a028a04] | 181 | s->max_size, s->channels, MAX_SIZE * AUBIO_MAX_CHANNELS); |
---|
[14ac1db] | 182 | return AUBIO_FAIL; |
---|
[2c3d4ca] | 183 | } |
---|
[385a06e2] | 184 | s->scratch_data = AUBIO_ARRAY(smpl_t,s->scratch_size); |
---|
[2c3d4ca] | 185 | |
---|
[14ac1db] | 186 | return AUBIO_OK; |
---|
[2c3d4ca] | 187 | } |
---|
| 188 | |
---|
[8aed26d] | 189 | void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){ |
---|
[4f75d8a] | 190 | uint_t i, j; |
---|
[c21acb9] | 191 | sf_count_t written_frames; |
---|
[4f75d8a] | 192 | uint_t channels = s->channels; |
---|
| 193 | uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path, |
---|
| 194 | s->max_size, write_data->length, write); |
---|
| 195 | int nsamples = channels * length; |
---|
[ad1ba08] | 196 | |
---|
[2c3d4ca] | 197 | /* interleaving data */ |
---|
| 198 | for ( i = 0; i < channels; i++) { |
---|
[4f75d8a] | 199 | for (j = 0; j < length; j++) { |
---|
| 200 | s->scratch_data[channels*j+i] = write_data->data[j]; |
---|
[2c3d4ca] | 201 | } |
---|
| 202 | } |
---|
[8aed26d] | 203 | |
---|
[385a06e2] | 204 | written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples); |
---|
[776a5d3] | 205 | if (written_frames/channels != write) { |
---|
[ad1ba08] | 206 | AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n", |
---|
[776a5d3] | 207 | write, s->path, (uint_t)written_frames); |
---|
[8aed26d] | 208 | } |
---|
[2c3d4ca] | 209 | return; |
---|
| 210 | } |
---|
| 211 | |
---|
[2eccf22] | 212 | void aubio_sink_sndfile_do_multi(aubio_sink_sndfile_t *s, fmat_t * write_data, uint_t write){ |
---|
[4f75d8a] | 213 | uint_t i, j; |
---|
[2eccf22] | 214 | sf_count_t written_frames; |
---|
[4f75d8a] | 215 | uint_t channels = aubio_sink_validate_input_channels("sink_sndfile", s->path, |
---|
| 216 | s->channels, write_data->height); |
---|
| 217 | uint_t length = aubio_sink_validate_input_length("sink_sndfile", s->path, |
---|
| 218 | s->max_size, write_data->length, write); |
---|
| 219 | int nsamples = channels * length; |
---|
[ad1ba08] | 220 | |
---|
[2eccf22] | 221 | /* interleaving data */ |
---|
[4f75d8a] | 222 | for ( i = 0; i < channels; i++) { |
---|
| 223 | for (j = 0; j < length; j++) { |
---|
| 224 | s->scratch_data[channels*j+i] = write_data->data[i][j]; |
---|
[2eccf22] | 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
[385a06e2] | 228 | written_frames = aubio_sf_write_smpl (s->handle, s->scratch_data, nsamples); |
---|
[2eccf22] | 229 | if (written_frames/channels != write) { |
---|
[ad1ba08] | 230 | AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written\n", |
---|
[2eccf22] | 231 | write, s->path, (uint_t)written_frames); |
---|
| 232 | } |
---|
| 233 | return; |
---|
| 234 | } |
---|
| 235 | |
---|
[a9fd272] | 236 | uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) { |
---|
| 237 | if (!s->handle) { |
---|
| 238 | return AUBIO_FAIL; |
---|
| 239 | } |
---|
[2c3d4ca] | 240 | if (sf_close(s->handle)) { |
---|
[491e6ea] | 241 | AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL)); |
---|
[a9fd272] | 242 | return AUBIO_FAIL; |
---|
[2c3d4ca] | 243 | } |
---|
[a9fd272] | 244 | s->handle = NULL; |
---|
| 245 | return AUBIO_OK; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){ |
---|
[e406835] | 249 | AUBIO_ASSERT(s); |
---|
| 250 | if (s->handle) |
---|
| 251 | aubio_sink_sndfile_close(s); |
---|
| 252 | if (s->path) |
---|
| 253 | AUBIO_FREE(s->path); |
---|
| 254 | if (s->scratch_data) |
---|
| 255 | AUBIO_FREE(s->scratch_data); |
---|
[2c3d4ca] | 256 | AUBIO_FREE(s); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | #endif /* HAVE_SNDFILE */ |
---|