summaryrefslogtreecommitdiff
path: root/src/synth/wavetable.c
blob: 830ffd751dc56f915d4b492cca80a6fcb40652e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
  Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org>

  This file is part of aubio.

  aubio is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  aubio is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with aubio.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "config.h"
#include "aubio_priv.h"
#include "fvec.h"
#include "fmat.h"
#include "utils/parameter.h"
#include "synth/wavetable.h"

#define WAVETABLE_LEN 4096

struct _aubio_wavetable_t {
  uint_t samplerate;
  uint_t blocksize;
  uint_t wavetable_length;
  fvec_t *wavetable;
  uint_t playing;
  smpl_t last_pos;

  aubio_parameter_t *freq;
  aubio_parameter_t *amp;
};

aubio_wavetable_t *new_aubio_wavetable(uint_t samplerate, uint_t blocksize)
{
  uint_t i = 0;
  aubio_wavetable_t *s = AUBIO_NEW(aubio_wavetable_t);
  if ((sint_t)samplerate <= 0) {
    AUBIO_ERR("Can not create wavetable with samplerate %d\n", samplerate);
    goto beach;
  }
  s->samplerate = samplerate;
  s->blocksize = blocksize;
  s->wavetable_length = WAVETABLE_LEN;
  s->wavetable = new_fvec(s->wavetable_length + 3);
  for (i = 0; i < s->wavetable_length; i++) {
    s->wavetable->data[i] = SIN(TWO_PI * i / (smpl_t) s->wavetable_length );
  }
  s->wavetable->data[s->wavetable_length] = s->wavetable->data[0];
  s->wavetable->data[s->wavetable_length + 1] = s->wavetable->data[1];
  s->wavetable->data[s->wavetable_length + 2] = s->wavetable->data[2];
  s->playing = 0;
  s->last_pos = 0.;
  s->freq = new_aubio_parameter( 0., s->samplerate / 2., 10 );
  s->amp = new_aubio_parameter( 0., 1., 100 );
  return s;
beach:
  AUBIO_FREE(s);
  return NULL;
}

static smpl_t interp_2(const fvec_t *input, smpl_t pos) {
  uint_t idx = (uint_t)FLOOR(pos);
  smpl_t frac = pos - (smpl_t)idx;
  smpl_t a = input->data[idx];
  smpl_t b = input->data[idx + 1];
  return a + frac * ( b - a );
}

void aubio_wavetable_do ( aubio_wavetable_t * s, const fvec_t * input, fvec_t * output)
{
  uint_t i;
  if (s->playing) {
    smpl_t pos = s->last_pos;
    for (i = 0; i < output->length; i++) {
      smpl_t inc = aubio_parameter_get_next_value( s->freq );
      inc *= (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
      pos += inc;
      while (pos > s->wavetable_length) {
        pos -= s->wavetable_length;
      }
      output->data[i] = aubio_parameter_get_next_value ( s->amp );
      output->data[i] *= interp_2(s->wavetable, pos);
    }
    s->last_pos = pos;
  } else {
    for (i = 0; i < output->length; i++) {
      aubio_parameter_get_next_value ( s->freq );
      aubio_parameter_get_next_value ( s->amp );
    }
    fvec_zeros (output);
  }
  // add input to output if needed
  if (input && input != output) {
    for (i = 0; i < output->length; i++) {
      output->data[i] += input->data[i];
    }
  }
}

void aubio_wavetable_do_multi ( aubio_wavetable_t * s, const fmat_t * input, fmat_t * output)
{
  uint_t i, j;
  if (s->playing) {
    smpl_t pos = s->last_pos;
    for (j = 0; j < output->length; j++) {
      smpl_t inc = aubio_parameter_get_next_value( s->freq );
      smpl_t amp = aubio_parameter_get_next_value ( s->amp );
      inc *= (smpl_t)(s->wavetable_length) / (smpl_t) (s->samplerate);
      pos += inc;
      while (pos > s->wavetable_length) {
        pos -= s->wavetable_length;
      }
      for (i = 0; i < output->height; i++) {
        output->data[i][j] = amp * interp_2(s->wavetable, pos);
      }
    }
    s->last_pos = pos;
  } else {
    for (j = 0; j < output->length; j++) {
      aubio_parameter_get_next_value ( s->freq );
      aubio_parameter_get_next_value ( s->amp );
    }
    fmat_zeros (output);
  }
  // add output to input if needed
  if (input && input != output) {
    for (i = 0; i < output->height; i++) {
      for (j = 0; j < output->length; j++) {
        output->data[i][j] += input->data[i][j];
      }
    }
  }
}

uint_t aubio_wavetable_get_playing ( const aubio_wavetable_t * s )
{
  return s->playing;
}

uint_t aubio_wavetable_set_playing ( aubio_wavetable_t * s, uint_t playing )
{
  s->playing = (playing == 1) ? 1 : 0;
  return 0;
}

uint_t aubio_wavetable_play ( aubio_wavetable_t * s )
{
  aubio_wavetable_set_amp (s, 0.7);
  return aubio_wavetable_set_playing (s, 1);
}

uint_t aubio_wavetable_stop ( aubio_wavetable_t * s )
{
  //aubio_wavetable_set_freq (s, 0.);
  aubio_wavetable_set_amp (s, 0.);
  //s->last_pos = 0;
  return aubio_wavetable_set_playing (s, 1);
}

uint_t aubio_wavetable_set_freq ( aubio_wavetable_t * s, smpl_t freq )
{
  return aubio_parameter_set_target_value ( s->freq, freq );
}

smpl_t aubio_wavetable_get_freq ( const aubio_wavetable_t * s) {
  return aubio_parameter_get_current_value ( s->freq);
}

uint_t aubio_wavetable_set_amp ( aubio_wavetable_t * s, smpl_t amp )
{
  return aubio_parameter_set_target_value ( s->amp, amp );
}

smpl_t aubio_wavetable_get_amp ( const aubio_wavetable_t * s) {
  return aubio_parameter_get_current_value ( s->amp );
}

void del_aubio_wavetable( aubio_wavetable_t * s )
{
  del_aubio_parameter(s->freq);
  del_aubio_parameter(s->amp);
  del_fvec(s->wavetable);
  AUBIO_FREE(s);
}