[2c3d4ca] | 1 | /* |
---|
| 2 | Copyright (C) 2012 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 | |
---|
| 35 | struct _aubio_sink_sndfile_t { |
---|
| 36 | uint_t samplerate; |
---|
| 37 | uint_t channels; |
---|
| 38 | char_t *path; |
---|
[11e92ea] | 39 | |
---|
| 40 | uint_t max_size; |
---|
| 41 | |
---|
[2c3d4ca] | 42 | SNDFILE *handle; |
---|
| 43 | uint_t scratch_size; |
---|
| 44 | smpl_t *scratch_data; |
---|
| 45 | }; |
---|
| 46 | |
---|
[8aed26d] | 47 | aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) { |
---|
[2c3d4ca] | 48 | aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t); |
---|
[c21acb9] | 49 | SF_INFO sfinfo; |
---|
[2c3d4ca] | 50 | |
---|
| 51 | if (path == NULL) { |
---|
[491e6ea] | 52 | AUBIO_ERR("sink_sndfile: Aborted opening null path\n"); |
---|
[2c3d4ca] | 53 | return NULL; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | s->samplerate = samplerate; |
---|
[8aed26d] | 57 | s->max_size = MAX_SIZE; |
---|
[2c3d4ca] | 58 | s->channels = 1; |
---|
| 59 | s->path = path; |
---|
| 60 | |
---|
| 61 | /* set output format */ |
---|
| 62 | AUBIO_MEMSET(&sfinfo, 0, sizeof (sfinfo)); |
---|
| 63 | sfinfo.samplerate = s->samplerate; |
---|
| 64 | sfinfo.channels = s->channels; |
---|
| 65 | sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; |
---|
| 66 | |
---|
| 67 | /* try creating the file */ |
---|
| 68 | s->handle = sf_open (s->path, SFM_WRITE, &sfinfo); |
---|
| 69 | |
---|
| 70 | if (s->handle == NULL) { |
---|
| 71 | /* show libsndfile err msg */ |
---|
[491e6ea] | 72 | AUBIO_ERR("sink_sndfile: Failed opening %s. %s\n", s->path, sf_strerror (NULL)); |
---|
[52b5f34] | 73 | AUBIO_FREE(s); |
---|
[2c3d4ca] | 74 | return NULL; |
---|
| 75 | } |
---|
| 76 | |
---|
[11e92ea] | 77 | s->scratch_size = s->max_size*s->channels; |
---|
[2c3d4ca] | 78 | /* allocate data for de/interleaving reallocated when needed. */ |
---|
| 79 | if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { |
---|
[491e6ea] | 80 | AUBIO_ERR("sink_sndfile: %d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n", |
---|
[8aed26d] | 81 | s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS); |
---|
[52b5f34] | 82 | AUBIO_FREE(s); |
---|
[2c3d4ca] | 83 | return NULL; |
---|
| 84 | } |
---|
| 85 | s->scratch_data = AUBIO_ARRAY(float,s->scratch_size); |
---|
| 86 | |
---|
| 87 | return s; |
---|
| 88 | } |
---|
| 89 | |
---|
[8aed26d] | 90 | void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){ |
---|
[776a5d3] | 91 | uint_t i, j, channels = s->channels; |
---|
[8aed26d] | 92 | int nsamples = channels*write; |
---|
[2c3d4ca] | 93 | smpl_t *pwrite; |
---|
[c21acb9] | 94 | sf_count_t written_frames; |
---|
[2c3d4ca] | 95 | |
---|
[8aed26d] | 96 | if (write > s->max_size) { |
---|
| 97 | AUBIO_WRN("trying to write %d frames, but only %d can be written at a time", |
---|
[11e92ea] | 98 | write, s->max_size); |
---|
| 99 | write = s->max_size; |
---|
[8aed26d] | 100 | } |
---|
| 101 | |
---|
[2c3d4ca] | 102 | /* interleaving data */ |
---|
| 103 | for ( i = 0; i < channels; i++) { |
---|
[8aed26d] | 104 | pwrite = (smpl_t *)write_data->data; |
---|
[776a5d3] | 105 | for (j = 0; j < write; j++) { |
---|
[2c3d4ca] | 106 | s->scratch_data[channels*j+i] = pwrite[j]; |
---|
| 107 | } |
---|
| 108 | } |
---|
[8aed26d] | 109 | |
---|
[c21acb9] | 110 | written_frames = sf_write_float (s->handle, s->scratch_data, nsamples); |
---|
[776a5d3] | 111 | if (written_frames/channels != write) { |
---|
[491e6ea] | 112 | AUBIO_WRN("sink_sndfile: trying to write %d frames to %s, but only %d could be written", |
---|
[776a5d3] | 113 | write, s->path, (uint_t)written_frames); |
---|
[8aed26d] | 114 | } |
---|
[2c3d4ca] | 115 | return; |
---|
| 116 | } |
---|
| 117 | |
---|
[a9fd272] | 118 | uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) { |
---|
| 119 | if (!s->handle) { |
---|
| 120 | return AUBIO_FAIL; |
---|
| 121 | } |
---|
[2c3d4ca] | 122 | if (sf_close(s->handle)) { |
---|
[491e6ea] | 123 | AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL)); |
---|
[a9fd272] | 124 | return AUBIO_FAIL; |
---|
[2c3d4ca] | 125 | } |
---|
[a9fd272] | 126 | s->handle = NULL; |
---|
| 127 | return AUBIO_OK; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){ |
---|
| 131 | if (!s) return; |
---|
| 132 | aubio_sink_sndfile_close(s); |
---|
[2c3d4ca] | 133 | AUBIO_FREE(s->scratch_data); |
---|
| 134 | AUBIO_FREE(s); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #endif /* HAVE_SNDFILE */ |
---|