[a682dbe] | 1 | /* |
---|
| 2 | Copyright (C) 2003-2013 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 | #include "aubio_priv.h" |
---|
| 24 | #include "fvec.h" |
---|
| 25 | #include "fmat.h" |
---|
| 26 | #include "io/source.h" |
---|
| 27 | #include "synth/sampler.h" |
---|
| 28 | |
---|
| 29 | struct _aubio_sampler_t { |
---|
| 30 | uint_t samplerate; |
---|
| 31 | uint_t blocksize; |
---|
| 32 | aubio_source_t *source; |
---|
| 33 | fvec_t *source_output; |
---|
| 34 | fmat_t *source_output_multi; |
---|
| 35 | char_t *uri; |
---|
| 36 | uint_t playing; |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | aubio_sampler_t *new_aubio_sampler(uint_t samplerate, uint_t blocksize) |
---|
| 40 | { |
---|
| 41 | aubio_sampler_t *s = AUBIO_NEW(aubio_sampler_t); |
---|
[79b54ea] | 42 | if ((sint_t)blocksize < 1) { |
---|
| 43 | AUBIO_ERR("sampler: got blocksize %d, but can not be < 1\n", blocksize); |
---|
| 44 | goto beach; |
---|
| 45 | } |
---|
[a682dbe] | 46 | s->samplerate = samplerate; |
---|
| 47 | s->blocksize = blocksize; |
---|
| 48 | s->source_output = new_fvec(blocksize); |
---|
[4ed0ed1] | 49 | s->source_output_multi = new_fmat(4, blocksize); |
---|
[a682dbe] | 50 | s->source = NULL; |
---|
| 51 | s->playing = 0; |
---|
| 52 | return s; |
---|
[79b54ea] | 53 | beach: |
---|
| 54 | AUBIO_FREE(s); |
---|
| 55 | return NULL; |
---|
[a682dbe] | 56 | } |
---|
| 57 | |
---|
[eaee767] | 58 | uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri ) |
---|
[a682dbe] | 59 | { |
---|
| 60 | if (o->source) del_aubio_source(o->source); |
---|
[eaee767] | 61 | |
---|
[e31aad20] | 62 | if (o->uri) AUBIO_FREE(o->uri); |
---|
| 63 | o->uri = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX)); |
---|
| 64 | strncpy(o->uri, uri, strnlen(uri, PATH_MAX)); |
---|
[eaee767] | 65 | |
---|
[a682dbe] | 66 | o->source = new_aubio_source(uri, o->samplerate, o->blocksize); |
---|
| 67 | if (o->source) return 0; |
---|
| 68 | AUBIO_ERR("sampler: failed loading %s", uri); |
---|
| 69 | return 1; |
---|
| 70 | } |
---|
| 71 | |
---|
[eaee767] | 72 | void aubio_sampler_do ( aubio_sampler_t * o, const fvec_t * input, fvec_t * output) |
---|
[a682dbe] | 73 | { |
---|
| 74 | uint_t read = 0, i; |
---|
| 75 | if (o->playing) { |
---|
| 76 | aubio_source_do (o->source, o->source_output, &read); |
---|
| 77 | for (i = 0; i < output->length; i++) { |
---|
| 78 | output->data[i] += o->source_output->data[i]; |
---|
| 79 | } |
---|
| 80 | if (read < o->blocksize) o->playing = 0; |
---|
| 81 | } |
---|
| 82 | if (input && input != output) { |
---|
| 83 | for (i = 0; i < output->length; i++) { |
---|
| 84 | output->data[i] += input->data[i]; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
[eaee767] | 89 | void aubio_sampler_do_multi ( aubio_sampler_t * o, const fmat_t * input, fmat_t * output) |
---|
[a682dbe] | 90 | { |
---|
| 91 | uint_t read = 0, i, j; |
---|
| 92 | if (o->playing) { |
---|
| 93 | aubio_source_do_multi (o->source, o->source_output_multi, &read); |
---|
| 94 | for (i = 0; i < output->height; i++) { |
---|
| 95 | for (j = 0; j < output->length; j++) { |
---|
[eaaba62] | 96 | output->data[i][j] += o->source_output_multi->data[i][j]; |
---|
[a682dbe] | 97 | } |
---|
| 98 | } |
---|
[eaaba62] | 99 | if ( read < o->blocksize ) o->playing = 0; |
---|
[a682dbe] | 100 | } |
---|
| 101 | if (input && input != output) { |
---|
| 102 | for (i = 0; i < output->height; i++) { |
---|
| 103 | for (j = 0; j < output->length; j++) { |
---|
| 104 | output->data[i][j] += input->data[i][j]; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[eaee767] | 110 | uint_t aubio_sampler_get_playing ( const aubio_sampler_t * o ) |
---|
[a682dbe] | 111 | { |
---|
| 112 | return o->playing; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing ) |
---|
| 116 | { |
---|
| 117 | o->playing = (playing == 1) ? 1 : 0; |
---|
| 118 | return 0; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | uint_t aubio_sampler_play ( aubio_sampler_t * o ) |
---|
| 122 | { |
---|
| 123 | aubio_source_seek (o->source, 0); |
---|
| 124 | return aubio_sampler_set_playing (o, 1); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | uint_t aubio_sampler_stop ( aubio_sampler_t * o ) |
---|
| 128 | { |
---|
| 129 | return aubio_sampler_set_playing (o, 0); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void del_aubio_sampler( aubio_sampler_t * o ) |
---|
| 133 | { |
---|
| 134 | if (o->source) { |
---|
| 135 | del_aubio_source(o->source); |
---|
| 136 | } |
---|
[e31aad20] | 137 | if (o->uri) AUBIO_FREE(o->uri); |
---|
[a682dbe] | 138 | del_fvec(o->source_output); |
---|
| 139 | del_fmat(o->source_output_multi); |
---|
| 140 | AUBIO_FREE(o); |
---|
| 141 | } |
---|