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