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/>.
23 #include "aubio_priv.h"
26 #include "io/source.h"
27 #include "synth/sampler.h"
29 struct _aubio_sampler_t {
32 aubio_source_t *source;
33 fvec_t *source_output;
34 fmat_t *source_output_multi;
39 aubio_sampler_t *new_aubio_sampler(uint_t samplerate, uint_t blocksize)
41 aubio_sampler_t *s = AUBIO_NEW(aubio_sampler_t);
42 s->samplerate = samplerate;
43 s->blocksize = blocksize;
44 s->source_output = new_fvec(blocksize);
45 s->source_output_multi = new_fmat(4, blocksize);
51 uint_t aubio_sampler_load( aubio_sampler_t * o, char_t * uri )
53 if (o->source) del_aubio_source(o->source);
55 o->source = new_aubio_source(uri, o->samplerate, o->blocksize);
56 if (o->source) return 0;
57 AUBIO_ERR("sampler: failed loading %s", uri);
61 void aubio_sampler_do ( aubio_sampler_t * o, fvec_t * input, fvec_t * output)
65 aubio_source_do (o->source, o->source_output, &read);
66 for (i = 0; i < output->length; i++) {
67 output->data[i] += o->source_output->data[i];
69 if (read < o->blocksize) o->playing = 0;
71 if (input && input != output) {
72 for (i = 0; i < output->length; i++) {
73 output->data[i] += input->data[i];
78 void aubio_sampler_do_multi ( aubio_sampler_t * o, fmat_t * input, fmat_t * output)
80 uint_t read = 0, i, j;
82 aubio_source_do_multi (o->source, o->source_output_multi, &read);
83 for (i = 0; i < output->height; i++) {
84 for (j = 0; j < output->length; j++) {
85 output->data[i][j] += o->source_output_multi->data[i][j];
88 if ( read < o->blocksize ) o->playing = 0;
90 if (input && input != output) {
91 for (i = 0; i < output->height; i++) {
92 for (j = 0; j < output->length; j++) {
93 output->data[i][j] += input->data[i][j];
99 uint_t aubio_sampler_get_playing ( aubio_sampler_t * o )
104 uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing )
106 o->playing = (playing == 1) ? 1 : 0;
110 uint_t aubio_sampler_play ( aubio_sampler_t * o )
112 aubio_source_seek (o->source, 0);
113 return aubio_sampler_set_playing (o, 1);
116 uint_t aubio_sampler_stop ( aubio_sampler_t * o )
118 return aubio_sampler_set_playing (o, 0);
121 void del_aubio_sampler( aubio_sampler_t * o )
124 del_aubio_source(o->source);
126 del_fvec(o->source_output);
127 del_fmat(o->source_output_multi);