2 Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
22 #include "aubio_priv.h"
25 #include "io/source.h"
26 #include "synth/sampler.h"
28 struct _aubio_sampler_t {
31 aubio_source_t *source;
32 fvec_t *source_output;
33 fmat_t *source_output_multi;
38 aubio_sampler_t *new_aubio_sampler(uint_t samplerate, uint_t blocksize)
40 aubio_sampler_t *s = AUBIO_NEW(aubio_sampler_t);
41 if ((sint_t)blocksize < 1) {
42 AUBIO_ERR("sampler: got blocksize %d, but can not be < 1\n", blocksize);
45 s->samplerate = samplerate;
46 s->blocksize = blocksize;
47 s->source_output = new_fvec(blocksize);
48 s->source_output_multi = new_fmat(4, blocksize);
57 uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri )
59 if (o->source) del_aubio_source(o->source);
61 if (o->uri) AUBIO_FREE(o->uri);
62 o->uri = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX));
63 strncpy(o->uri, uri, strnlen(uri, PATH_MAX));
65 o->source = new_aubio_source(uri, o->samplerate, o->blocksize);
66 if (o->source) return 0;
67 AUBIO_ERR("sampler: failed loading %s", uri);
71 void aubio_sampler_do ( aubio_sampler_t * o, const fvec_t * input, fvec_t * output)
75 aubio_source_do (o->source, o->source_output, &read);
76 for (i = 0; i < output->length; i++) {
77 output->data[i] += o->source_output->data[i];
79 if (read < o->blocksize) o->playing = 0;
81 if (input && input != output) {
82 for (i = 0; i < output->length; i++) {
83 output->data[i] += input->data[i];
88 void aubio_sampler_do_multi ( aubio_sampler_t * o, const fmat_t * input, fmat_t * output)
90 uint_t read = 0, i, j;
92 aubio_source_do_multi (o->source, o->source_output_multi, &read);
93 for (i = 0; i < output->height; i++) {
94 for (j = 0; j < output->length; j++) {
95 output->data[i][j] += o->source_output_multi->data[i][j];
98 if ( read < o->blocksize ) o->playing = 0;
100 if (input && input != output) {
101 for (i = 0; i < output->height; i++) {
102 for (j = 0; j < output->length; j++) {
103 output->data[i][j] += input->data[i][j];
109 uint_t aubio_sampler_get_playing ( const aubio_sampler_t * o )
114 uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing )
116 o->playing = (playing == 1) ? 1 : 0;
120 uint_t aubio_sampler_play ( aubio_sampler_t * o )
122 aubio_source_seek (o->source, 0);
123 return aubio_sampler_set_playing (o, 1);
126 uint_t aubio_sampler_stop ( aubio_sampler_t * o )
128 return aubio_sampler_set_playing (o, 0);
131 void del_aubio_sampler( aubio_sampler_t * o )
134 del_aubio_source(o->source);
136 if (o->uri) AUBIO_FREE(o->uri);
137 del_fvec(o->source_output);
138 del_fmat(o->source_output_multi);