source: src/synth/wavetable.c @ c2e0aef

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since c2e0aef was c2e0aef, checked in by Paul Brossier <piem@piem.org>, 6 years ago

src/synth/wavetable.c: use parameters for frequency and amplitude

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 "utils/parameter.h"
27#include "synth/wavetable.h"
28
29#define WAVETABLE_LEN 4096
30
31struct _aubio_wavetable_t {
32  uint_t samplerate;
33  uint_t blocksize;
34  uint_t wavetable_length;
35  fvec_t *wavetable;
36  uint_t playing;
37  smpl_t last_pos;
38
39  aubio_parameter_t *freq;
40  aubio_parameter_t *amp;
41};
42
43aubio_wavetable_t *new_aubio_wavetable(uint_t samplerate, uint_t blocksize)
44{
45  aubio_wavetable_t *s = AUBIO_NEW(aubio_wavetable_t);
46  uint_t i = 0;
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 );
53  }
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];
57  s->playing = 0;
58  s->last_pos = 0.;
59  s->freq = new_aubio_parameter( 0., s->samplerate / 2., 10 );
60  s->amp = new_aubio_parameter( 0., 1., 100 );
61  return s;
62}
63
64static 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 );
70}
71
72void aubio_wavetable_do ( aubio_wavetable_t * s, fvec_t * input, fvec_t * output)
73{
74  uint_t i;
75  if (s->playing) {
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);
80      pos += inc;
81      while (pos > s->wavetable_length) {
82        pos -= s->wavetable_length;
83      }
84      output->data[i] = aubio_parameter_get_next_value ( s->amp );
85      output->data[i] *= interp_2(s->wavetable, pos);
86    }
87    s->last_pos = pos;
88  } else {
89    for (i = 0; i < output->length; i++) {
90      aubio_parameter_get_next_value ( s->freq );
91      aubio_parameter_get_next_value ( s->amp );
92    }
93    fvec_set(output, 0.);
94  }
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];
99    }
100  }
101}
102
103void aubio_wavetable_do_multi ( aubio_wavetable_t * s, fmat_t * input, fmat_t * output)
104{
105  uint_t i, j;
106  if (s->playing) {
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);
111      pos += inc;
112      while (pos > s->wavetable_length) {
113        pos -= s->wavetable_length;
114      }
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);
118      }
119    }
120    s->last_pos = pos;
121  } else {
122    for (j = 0; j < output->length; j++) {
123      aubio_parameter_get_next_value ( s->freq );
124      aubio_parameter_get_next_value ( s->amp );
125    }
126    fmat_set(output, 0.);
127  }
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];
133      }
134    }
135  }
136}
137
138uint_t aubio_wavetable_get_playing ( aubio_wavetable_t * s )
139{
140  return s->playing;
141}
142
143uint_t aubio_wavetable_set_playing ( aubio_wavetable_t * s, uint_t playing )
144{
145  s->playing = (playing == 1) ? 1 : 0;
146  return 0;
147}
148
149uint_t aubio_wavetable_play ( aubio_wavetable_t * s )
150{
151  aubio_wavetable_set_amp (s, 0.7);
152  return aubio_wavetable_set_playing (s, 1);
153}
154
155uint_t aubio_wavetable_stop ( aubio_wavetable_t * s )
156{
157  //aubio_wavetable_set_freq (s, 0.);
158  aubio_wavetable_set_amp (s, 0.);
159  //s->last_pos = 0;
160  return aubio_wavetable_set_playing (s, 1);
161}
162
163uint_t aubio_wavetable_set_freq ( aubio_wavetable_t * s, smpl_t freq )
164{
165  return aubio_parameter_set_target_value ( s->freq, freq );
166}
167
168smpl_t aubio_wavetable_get_freq ( aubio_wavetable_t * s) {
169  return aubio_parameter_get_current_value ( s->freq);
170}
171
172uint_t aubio_wavetable_set_amp ( aubio_wavetable_t * s, smpl_t amp )
173{
174  return aubio_parameter_set_target_value ( s->amp, amp );
175}
176
177smpl_t aubio_wavetable_get_amp ( aubio_wavetable_t * s) {
178  return aubio_parameter_get_current_value ( s->amp );
179}
180
181void del_aubio_wavetable( aubio_wavetable_t * s )
182{
183  del_fvec(s->wavetable);
184  AUBIO_FREE(s);
185}
Note: See TracBrowser for help on using the repository browser.