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/wavetable.h"
29 #define WAVETABLE_LEN 4096
31 struct _aubio_wavetable_t {
34 uint_t wavetable_length;
48 aubio_wavetable_t *new_aubio_wavetable(uint_t samplerate, uint_t blocksize)
50 aubio_wavetable_t *s = AUBIO_NEW(aubio_wavetable_t);
52 s->samplerate = samplerate;
53 s->blocksize = blocksize;
54 s->wavetable_length = WAVETABLE_LEN;
55 s->wavetable = new_fvec(s->wavetable_length + 3);
56 for (i = 0; i < s->wavetable_length; i++) {
57 s->wavetable->data[i] = SIN(TWO_PI * i / (smpl_t) s->wavetable_length );
59 s->wavetable->data[s->wavetable_length] = s->wavetable->data[0];
60 s->wavetable->data[s->wavetable_length + 1] = s->wavetable->data[1];
61 s->wavetable->data[s->wavetable_length + 2] = s->wavetable->data[2];
74 static smpl_t interp_2(fvec_t *input, smpl_t pos) {
75 uint_t idx = (uint_t)FLOOR(pos);
76 smpl_t frac = pos - (smpl_t)idx;
77 smpl_t a = input->data[idx];
78 smpl_t b = input->data[idx + 1];
79 return a + frac * ( b - a );
82 void aubio_wavetable_do ( aubio_wavetable_t * s, fvec_t * input, fvec_t * output)
86 smpl_t pos = s->last_pos;
87 for (i = 0; i < output->length; i++) {
88 if ( ABS(s->freq - s->target_freq) > ABS(s->inc_freq) )
89 s->freq += s->inc_freq;
91 s->freq = s->target_freq;
92 smpl_t inc = s->freq * (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
94 while (pos > s->wavetable_length) {
95 pos -= s->wavetable_length;
97 if ( ABS(s->amp - s->target_amp) > ABS(s->inc_amp) )
100 s->amp = s->target_amp;
101 output->data[i] = s->amp * interp_2(s->wavetable, pos);
105 fvec_set(output, 0.);
107 // add input to output if needed
108 if (input && input != output) {
109 for (i = 0; i < output->length; i++) {
110 output->data[i] += input->data[i];
115 void aubio_wavetable_do_multi ( aubio_wavetable_t * s, fmat_t * input, fmat_t * output)
119 smpl_t pos = s->last_pos;
120 for (j = 0; j < output->length; j++) {
121 if ( ABS(s->freq - s->target_freq) > ABS(s->inc_freq) )
122 s->freq += s->inc_freq;
124 s->freq = s->target_freq;
125 smpl_t inc = s->freq * (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
127 while (pos > s->wavetable_length) {
128 pos -= s->wavetable_length;
130 if ( ABS(s->amp - s->target_amp) > ABS(s->inc_amp) )
131 s->amp += s->inc_amp;
133 s->amp = s->target_amp;
134 for (i = 0; i < output->height; i++) {
135 output->data[i][j] = s->amp * interp_2(s->wavetable, pos);
140 for (j = 0; j < output->length; j++) {
141 if (s->freq != s->target_freq)
142 s->freq += s->inc_freq;
144 fmat_set(output, 0.);
146 // add output to input if needed
147 if (input && input != output) {
148 for (i = 0; i < output->height; i++) {
149 for (j = 0; j < output->length; j++) {
150 output->data[i][j] += input->data[i][j];
156 uint_t aubio_wavetable_get_playing ( aubio_wavetable_t * s )
161 uint_t aubio_wavetable_set_playing ( aubio_wavetable_t * s, uint_t playing )
163 s->playing = (playing == 1) ? 1 : 0;
167 uint_t aubio_wavetable_play ( aubio_wavetable_t * s )
169 aubio_wavetable_set_amp (s, 0.7);
170 return aubio_wavetable_set_playing (s, 1);
173 uint_t aubio_wavetable_stop ( aubio_wavetable_t * s )
175 //aubio_wavetable_set_freq (s, 0.);
176 aubio_wavetable_set_amp (s, 0.);
178 return aubio_wavetable_set_playing (s, 1);
181 uint_t aubio_wavetable_set_freq ( aubio_wavetable_t * s, smpl_t freq )
183 if (freq >= 0 && freq < s->samplerate / 2.) {
185 s->inc_freq = (freq - s->freq) / steps;
186 s->target_freq = freq;
193 smpl_t aubio_wavetable_get_freq ( aubio_wavetable_t * s) {
197 uint_t aubio_wavetable_set_amp ( aubio_wavetable_t * s, smpl_t amp )
199 AUBIO_MSG("amp: %f, s->amp: %f, target_amp: %f, inc_amp: %f\n",
200 amp, s->amp, s->target_amp, s->inc_amp);
201 if (amp >= 0. && amp < 1.) {
203 s->inc_amp = (amp - s->amp) / steps;
205 AUBIO_ERR("amp: %f, s->amp: %f, target_amp: %f, inc_amp: %f\n",
206 amp, s->amp, s->target_amp, s->inc_amp);
213 smpl_t aubio_wavetable_get_amp ( aubio_wavetable_t * s) {
217 void del_aubio_wavetable( aubio_wavetable_t * s )
219 del_fvec(s->wavetable);