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); |
---|
42 | if ((sint_t)blocksize < 1) { |
---|
43 | AUBIO_ERR("sampler: got blocksize %d, but can not be < 1\n", blocksize); |
---|
44 | goto beach; |
---|
45 | } |
---|
46 | s->samplerate = samplerate; |
---|
47 | s->blocksize = blocksize; |
---|
48 | s->source_output = new_fvec(blocksize); |
---|
49 | s->source_output_multi = new_fmat(4, blocksize); |
---|
50 | s->source = NULL; |
---|
51 | s->playing = 0; |
---|
52 | return s; |
---|
53 | beach: |
---|
54 | AUBIO_FREE(s); |
---|
55 | return NULL; |
---|
56 | } |
---|
57 | |
---|
58 | uint_t aubio_sampler_load( aubio_sampler_t * o, char_t * uri ) |
---|
59 | { |
---|
60 | if (o->source) del_aubio_source(o->source); |
---|
61 | o->uri = uri; |
---|
62 | o->source = new_aubio_source(uri, o->samplerate, o->blocksize); |
---|
63 | if (o->source) return 0; |
---|
64 | AUBIO_ERR("sampler: failed loading %s", uri); |
---|
65 | return 1; |
---|
66 | } |
---|
67 | |
---|
68 | void aubio_sampler_do ( aubio_sampler_t * o, fvec_t * input, fvec_t * output) |
---|
69 | { |
---|
70 | uint_t read = 0, i; |
---|
71 | if (o->playing) { |
---|
72 | aubio_source_do (o->source, o->source_output, &read); |
---|
73 | for (i = 0; i < output->length; i++) { |
---|
74 | output->data[i] += o->source_output->data[i]; |
---|
75 | } |
---|
76 | if (read < o->blocksize) o->playing = 0; |
---|
77 | } |
---|
78 | if (input && input != output) { |
---|
79 | for (i = 0; i < output->length; i++) { |
---|
80 | output->data[i] += input->data[i]; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void aubio_sampler_do_multi ( aubio_sampler_t * o, fmat_t * input, fmat_t * output) |
---|
86 | { |
---|
87 | uint_t read = 0, i, j; |
---|
88 | if (o->playing) { |
---|
89 | aubio_source_do_multi (o->source, o->source_output_multi, &read); |
---|
90 | for (i = 0; i < output->height; i++) { |
---|
91 | for (j = 0; j < output->length; j++) { |
---|
92 | output->data[i][j] += o->source_output_multi->data[i][j]; |
---|
93 | } |
---|
94 | } |
---|
95 | if ( read < o->blocksize ) o->playing = 0; |
---|
96 | } |
---|
97 | if (input && input != output) { |
---|
98 | for (i = 0; i < output->height; i++) { |
---|
99 | for (j = 0; j < output->length; j++) { |
---|
100 | output->data[i][j] += input->data[i][j]; |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | uint_t aubio_sampler_get_playing ( aubio_sampler_t * o ) |
---|
107 | { |
---|
108 | return o->playing; |
---|
109 | } |
---|
110 | |
---|
111 | uint_t aubio_sampler_set_playing ( aubio_sampler_t * o, uint_t playing ) |
---|
112 | { |
---|
113 | o->playing = (playing == 1) ? 1 : 0; |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | uint_t aubio_sampler_play ( aubio_sampler_t * o ) |
---|
118 | { |
---|
119 | aubio_source_seek (o->source, 0); |
---|
120 | return aubio_sampler_set_playing (o, 1); |
---|
121 | } |
---|
122 | |
---|
123 | uint_t aubio_sampler_stop ( aubio_sampler_t * o ) |
---|
124 | { |
---|
125 | return aubio_sampler_set_playing (o, 0); |
---|
126 | } |
---|
127 | |
---|
128 | void del_aubio_sampler( aubio_sampler_t * o ) |
---|
129 | { |
---|
130 | if (o->source) { |
---|
131 | del_aubio_source(o->source); |
---|
132 | } |
---|
133 | del_fvec(o->source_output); |
---|
134 | del_fmat(o->source_output_multi); |
---|
135 | AUBIO_FREE(o); |
---|
136 | } |
---|