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 "aubio_priv.h"
23 #include "fvec.h"
24 #include "fmat.h"
25 #include "io/source.h"
26 #include "synth/sampler.h"
27
28 struct _aubio_sampler_t {
29 uint_t samplerate;
30 uint_t blocksize;
31 aubio_source_t *source;
32 fvec_t *source_output;
33 fmat_t *source_output_multi;
34 char_t *uri;
35 uint_t playing;
36 };
37
new_aubio_sampler(uint_t samplerate,uint_t blocksize)38 aubio_sampler_t *new_aubio_sampler(uint_t samplerate, uint_t blocksize)
39 {
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);
43 goto beach;
44 }
45 s->samplerate = samplerate;
46 s->blocksize = blocksize;
47 s->source_output = new_fvec(blocksize);
48 s->source_output_multi = new_fmat(4, blocksize);
49 s->source = NULL;
50 s->playing = 0;
51 return s;
52 beach:
53 AUBIO_FREE(s);
54 return NULL;
55 }
56
aubio_sampler_load(aubio_sampler_t * o,const char_t * uri)57 uint_t aubio_sampler_load( aubio_sampler_t * o, const char_t * uri )
58 {
59 if (o->source) del_aubio_source(o->source);
60
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));
64
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);
68 return 1;
69 }
70
aubio_sampler_do(aubio_sampler_t * o,const fvec_t * input,fvec_t * output)71 void aubio_sampler_do ( aubio_sampler_t * o, const fvec_t * input, fvec_t * output)
72 {
73 uint_t read = 0, i;
74 if (o->playing) {
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];
78 }
79 if (read < o->blocksize) o->playing = 0;
80 }
81 if (input && input != output) {
82 for (i = 0; i < output->length; i++) {
83 output->data[i] += input->data[i];
84 }
85 }
86 }
87
aubio_sampler_do_multi(aubio_sampler_t * o,const fmat_t * input,fmat_t * output)88 void aubio_sampler_do_multi ( aubio_sampler_t * o, const fmat_t * input, fmat_t * output)
89 {
90 uint_t read = 0, i, j;
91 if (o->playing) {
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];
96 }
97 }
98 if ( read < o->blocksize ) o->playing = 0;
99 }
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];
104 }
105 }
106 }
107 }
108
aubio_sampler_get_playing(const aubio_sampler_t * o)109 uint_t aubio_sampler_get_playing ( const aubio_sampler_t * o )
110 {
111 return o->playing;
112 }
113
aubio_sampler_set_playing(aubio_sampler_t * o,uint_t playing)114 uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing )
115 {
116 o->playing = (playing == 1) ? 1 : 0;
117 return 0;
118 }
119
aubio_sampler_play(aubio_sampler_t * o)120 uint_t aubio_sampler_play ( aubio_sampler_t * o )
121 {
122 aubio_source_seek (o->source, 0);
123 return aubio_sampler_set_playing (o, 1);
124 }
125
aubio_sampler_stop(aubio_sampler_t * o)126 uint_t aubio_sampler_stop ( aubio_sampler_t * o )
127 {
128 return aubio_sampler_set_playing (o, 0);
129 }
130
del_aubio_sampler(aubio_sampler_t * o)131 void del_aubio_sampler( aubio_sampler_t * o )
132 {
133 if (o->source) {
134 del_aubio_source(o->source);
135 }
136 if (o->uri) AUBIO_FREE(o->uri);
137 del_fvec(o->source_output);
138 del_fmat(o->source_output_multi);
139 AUBIO_FREE(o);
140 }
141