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 "utils/parameter.h"
27 #include "synth/wavetable.h"
29 #define WAVETABLE_LEN 4096
31 struct _aubio_wavetable_t {
34 uint_t wavetable_length;
39 aubio_parameter_t *freq;
40 aubio_parameter_t *amp;
43 aubio_wavetable_t *new_aubio_wavetable(uint_t samplerate, uint_t blocksize)
45 aubio_wavetable_t *s = AUBIO_NEW(aubio_wavetable_t);
47 s->samplerate = samplerate;
48 s->blocksize = blocksize;
49 s->wavetable_length = WAVETABLE_LEN;
50 s->wavetable = new_fvec(s->wavetable_length + 3);
51 for (i = 0; i < s->wavetable_length; i++) {
52 s->wavetable->data[i] = SIN(TWO_PI * i / (smpl_t) s->wavetable_length );
54 s->wavetable->data[s->wavetable_length] = s->wavetable->data[0];
55 s->wavetable->data[s->wavetable_length + 1] = s->wavetable->data[1];
56 s->wavetable->data[s->wavetable_length + 2] = s->wavetable->data[2];
59 s->freq = new_aubio_parameter( 0., s->samplerate / 2., 10 );
60 s->amp = new_aubio_parameter( 0., 1., 100 );
64 static smpl_t interp_2(fvec_t *input, smpl_t pos) {
65 uint_t idx = (uint_t)FLOOR(pos);
66 smpl_t frac = pos - (smpl_t)idx;
67 smpl_t a = input->data[idx];
68 smpl_t b = input->data[idx + 1];
69 return a + frac * ( b - a );
72 void aubio_wavetable_do ( aubio_wavetable_t * s, fvec_t * input, fvec_t * output)
76 smpl_t pos = s->last_pos;
77 for (i = 0; i < output->length; i++) {
78 smpl_t inc = aubio_parameter_get_next_value( s->freq );
79 inc *= (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
81 while (pos > s->wavetable_length) {
82 pos -= s->wavetable_length;
84 output->data[i] = aubio_parameter_get_next_value ( s->amp );
85 output->data[i] *= interp_2(s->wavetable, pos);
89 for (i = 0; i < output->length; i++) {
90 aubio_parameter_get_next_value ( s->freq );
91 aubio_parameter_get_next_value ( s->amp );
95 // add input to output if needed
96 if (input && input != output) {
97 for (i = 0; i < output->length; i++) {
98 output->data[i] += input->data[i];
103 void aubio_wavetable_do_multi ( aubio_wavetable_t * s, fmat_t * input, fmat_t * output)
107 smpl_t pos = s->last_pos;
108 for (j = 0; j < output->length; j++) {
109 smpl_t inc = aubio_parameter_get_next_value( s->freq );
110 inc *= (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
112 while (pos > s->wavetable_length) {
113 pos -= s->wavetable_length;
115 smpl_t amp = aubio_parameter_get_next_value ( s->amp );
116 for (i = 0; i < output->height; i++) {
117 output->data[i][j] = amp * interp_2(s->wavetable, pos);
122 for (j = 0; j < output->length; j++) {
123 aubio_parameter_get_next_value ( s->freq );
124 aubio_parameter_get_next_value ( s->amp );
126 fmat_set(output, 0.);
128 // add output to input if needed
129 if (input && input != output) {
130 for (i = 0; i < output->height; i++) {
131 for (j = 0; j < output->length; j++) {
132 output->data[i][j] += input->data[i][j];
138 uint_t aubio_wavetable_get_playing ( aubio_wavetable_t * s )
143 uint_t aubio_wavetable_set_playing ( aubio_wavetable_t * s, uint_t playing )
145 s->playing = (playing == 1) ? 1 : 0;
149 uint_t aubio_wavetable_play ( aubio_wavetable_t * s )
151 aubio_wavetable_set_amp (s, 0.7);
152 return aubio_wavetable_set_playing (s, 1);
155 uint_t aubio_wavetable_stop ( aubio_wavetable_t * s )
157 //aubio_wavetable_set_freq (s, 0.);
158 aubio_wavetable_set_amp (s, 0.);
160 return aubio_wavetable_set_playing (s, 1);
163 uint_t aubio_wavetable_set_freq ( aubio_wavetable_t * s, smpl_t freq )
165 return aubio_parameter_set_target_value ( s->freq, freq );
168 smpl_t aubio_wavetable_get_freq ( aubio_wavetable_t * s) {
169 return aubio_parameter_get_current_value ( s->freq);
172 uint_t aubio_wavetable_set_amp ( aubio_wavetable_t * s, smpl_t amp )
174 return aubio_parameter_set_target_value ( s->amp, amp );
177 smpl_t aubio_wavetable_get_amp ( aubio_wavetable_t * s) {
178 return aubio_parameter_get_current_value ( s->amp );
181 void del_aubio_wavetable( aubio_wavetable_t * s )
183 del_aubio_parameter(s->freq);
184 del_aubio_parameter(s->amp);
185 del_fvec(s->wavetable);