aboutsummaryrefslogtreecommitdiff
path: root/src/delay.cpp
blob: 6addd26d1ef2541510754c587447dec9d121d1e9 (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
/*
    cw_binaural~: a binaural synthesis external for pure data
    by David Doukhan - david.doukhan@gmail.com - http://perso.limsi.fr/doukhan
    and Anne Sedes - sedes.anne@gmail.com
    Copyright (C) 2009-2011  David Doukhan and Anne Sedes

    For more details, see CW_binaural~, a binaural synthesis external for Pure Data
    David Doukhan and Anne Sedes, PDCON09


    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <math.h>
#include "delay.hpp"
#include "logstring.hpp"

Delay*	Delay::create(string delay_name, unsigned size)
{
  //slog << "creating delay " << delay_name << endl;
  // kind of factory design pattern
  if (!delay_name.compare("Hermite4"))
    return new SCDelay(size);
  else if (!delay_name.compare("nodelay"))
    return new NoDelay(size);
  else if (!delay_name.compare("linear"))
    return new BasicInterpolDelay(size);
  else if (!delay_name.compare("nofractional"))
    return new DummyDelay(size);
  else if (!delay_name.compare("6points"))
    return new CZDelay(size);
  else
    {
      slog << delay_name << " delay method is not supported" << endl;
      throw 0;
    }
}

Delay::Delay(unsigned size): _size(size)
{
  // FIXME
  // ASUMPTION: ITD WILL NEVER BE > SIZE/2
  // IT SHOULD BE CHECKED DURING ITD COMPUTATION
  _fifo = new float[size];
  _fifo_id = 0;
  for (unsigned i = 0; i < size; ++i)
    _fifo[i] = 0;
  _cur_delay = 0;
}

Delay::~Delay()
{
  delete [] _fifo;
}

inline void Delay::push_value(float f)
{
  _fifo_id = (_fifo_id ? _fifo_id - 1 : _size - 1);
  _fifo[_fifo_id] = f;
}

// That ugly macro has been done because of the inneficiency
// of virtual interpolation functions for demanding RT purposes
#define DELAY_PROCESS(code)					\
  unsigned i;							\
  _old_delay = _cur_delay; _cur_delay = new_delay;		\
  float delay = _old_delay;					\
  const float inc_delay = (_cur_delay - _old_delay) / n;	\
								\
  for (i = 0; i < n; ++i, delay += inc_delay)			\
    {								\
      push_value(input[i]);					\
      /* compute the id of the current output element		\
	 delayed by and integer value */			\
      float floor_delay = floorf(delay);			\
								\
      /* get the current element id in the fifo */		\
      unsigned id = _fifo_id + (unsigned) floor_delay;			\
      if (id >= _size)						\
	id -= _size;						\
								\
      /* compute the fractional part of the delay */		\
      float frac_del = delay - floor_delay;			\
								\
      code							\
								\
	output[i] = res;					\
    }


void DummyDelay::process(const float* input, float* output, float new_delay, unsigned n)
{
  DELAY_PROCESS(
    float res = _fifo[id];
    )
}

void BasicInterpolDelay::process(const float* input, float* output, float new_delay, unsigned n)
{
  DELAY_PROCESS(
    unsigned older = (id + 1 == _size ? 0: id + 1);
    float res = _fifo[id] * frac_del + (1 - frac_del) * _fifo[older];
    )
}

void SCDelay::process(const float* input, float* output, float new_delay, unsigned n)
{
  DELAY_PROCESS(
    float y0 = _fifo[id];
    float y1 = _fifo[(id + 1) % _size];
    float y2 = _fifo[(id + 2) % _size];
    float y3 = _fifo[(id + 3) % _size];
    
    // 4-point, 3rd-order Hermite (x-form)
    float c0 = y1;
    float c1 = 0.5 * (y2 - y0);
    float c2 = y0 - 2.5 * y1 + 2. * y2 - 0.5 * y3;
    float c3 = 0.5 * (y3 - y0) + 1.5 * (y1 - y2);
    
    float res = ((c3 * frac_del + c2) * frac_del + c1) * frac_del + c0;
    )
}

void CZDelay::process(const float* input, float* output, float new_delay, unsigned n)
{
  // 6 points interpolation
  DELAY_PROCESS(
    float ym2 = _fifo[id];
    float ym1 = _fifo[(id + 1) % _size];
    float y0 = _fifo[(id + 2) % _size];
    float y1 = _fifo[(id + 3) % _size];
    float y2 = _fifo[(id + 4) % _size];
    float y3 = _fifo[(id + 5) % _size];
    
    float a0= y0;
    float a1= (1./12.)*ym2 - (2./3.)*ym1 + (2./3.)*y1 - (1./12.)*y2;
    float a2= (-1./24.)*ym2 + (2./3.)*ym1 - (5./4.)*y0 + (2./3.)*y1 - (1./24.)*y2;
    float a3= (-3./8.)*ym2 + (13./8.)*ym1 - (35./12.)*y0 + (11./4.)*y1 - (11./8.)*y2 + (7./24.)*y3;
    float a4= (13./24.)*ym2 - (8./3.)*ym1 + (21./4.)*y0 - (31./6.)*y1 + (61./24.)*y2 - (1./2.)*y3;
    float a5= (-5./24.)*ym2 + (25./24.)*ym1 - (25./12.)*y0 + (25./12.)*y1 - (25./24.)*y2 + (5./24.)*y3;
    
    float res = a0 + frac_del * (a1 + frac_del * (a2 + frac_del * (a3 + frac_del * (a4 + frac_del * a5))));
    )
}


void NoDelay::process(const float *input, float *output, float new_delay, unsigned n)
{
  for (unsigned i = 0; i < n; ++i)
    output[i] = input[i];
}