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 (s->freq != s->target_freq)
89 s->freq += s->inc_freq;
90 smpl_t inc = s->freq * (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
92 while (pos > s->wavetable_length) {
93 pos -= s->wavetable_length;
95 if ( ABS(s->amp - s->target_amp) > ABS(s->inc_amp) )
98 s->amp = s->target_amp;
99 output->data[i] = s->amp * interp_2(s->wavetable, pos);
103 fvec_set(output, 0.);
105 // add input to output if needed
106 if (input && input != output) {
107 for (i = 0; i < output->length; i++) {
108 output->data[i] += input->data[i];
113 void aubio_wavetable_do_multi ( aubio_wavetable_t * s, fmat_t * input, fmat_t * output)
117 smpl_t pos = s->last_pos;
118 for (j = 0; j < output->length; j++) {
119 if (s->freq != s->target_freq)
120 s->freq += s->inc_freq;
121 smpl_t inc = s->freq * (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
123 while (pos > s->wavetable_length) {
124 pos -= s->wavetable_length;
126 for (i = 0; i < output->height; i++) {
127 output->data[i][j] = interp_2(s->wavetable, pos);
132 for (j = 0; j < output->length; j++) {
133 if (s->freq != s->target_freq)
134 s->freq += s->inc_freq;
136 fmat_set(output, 0.);
138 // add output to input if needed
139 if (input && input != output) {
140 for (i = 0; i < output->height; i++) {
141 for (j = 0; j < output->length; j++) {
142 output->data[i][j] += input->data[i][j];
148 uint_t aubio_wavetable_get_playing ( aubio_wavetable_t * s )
153 uint_t aubio_wavetable_set_playing ( aubio_wavetable_t * s, uint_t playing )
155 s->playing = (playing == 1) ? 1 : 0;
159 uint_t aubio_wavetable_play ( aubio_wavetable_t * s )
161 aubio_wavetable_set_amp (s, 0.7);
162 return aubio_wavetable_set_playing (s, 1);
165 uint_t aubio_wavetable_stop ( aubio_wavetable_t * s )
167 //aubio_wavetable_set_freq (s, 0.);
168 aubio_wavetable_set_amp (s, 0.);
170 return aubio_wavetable_set_playing (s, 1);
173 uint_t aubio_wavetable_set_freq ( aubio_wavetable_t * s, smpl_t freq )
175 if (freq >= 0 && freq < s->samplerate / 2.) {
177 s->inc_freq = (freq - s->freq) / steps;
178 s->target_freq = freq;
185 smpl_t aubio_wavetable_get_freq ( aubio_wavetable_t * s) {
189 uint_t aubio_wavetable_set_amp ( aubio_wavetable_t * s, smpl_t amp )
191 AUBIO_MSG("amp: %f, s->amp: %f, target_amp: %f, inc_amp: %f\n",
192 amp, s->amp, s->target_amp, s->inc_amp);
193 if (amp >= 0. && amp < 1.) {
195 s->inc_amp = (amp - s->amp) / steps;
197 AUBIO_ERR("amp: %f, s->amp: %f, target_amp: %f, inc_amp: %f\n",
198 amp, s->amp, s->target_amp, s->inc_amp);
205 smpl_t aubio_wavetable_get_amp ( aubio_wavetable_t * s) {
209 void del_aubio_wavetable( aubio_wavetable_t * s )
211 del_fvec(s->wavetable);