aboutsummaryrefslogtreecommitdiff
path: root/src/iemlib1/sigiem_delay.c
blob: 9812a11392984fdbebd9f5f1ffc7c2a78d554b07 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iemlib1 written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2005 */

#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif


#include "m_pd.h"
#include "iemlib.h"
#include <math.h>
#include <stdio.h>
#include <string.h>

/* -------------------------- sigiem_delay~ ------------------------------ */

static t_class *sigiem_delay_class;

#define IEMDELAY_DEF_VEC_SIZE 64

typedef struct _sigiem_delay
{
  t_object  x_obj;
  int       x_mallocsize;
  t_float   x_max_delay_ms;
  t_float   x_current_delay_ms;
  t_float   *x_begmem1;
  t_float   *x_begmem2;
  int       x_writeindex;
  int       x_blocksize;
  int       x_delay_samples;
  t_float   x_sr;
  t_float   x_msi;
} t_sigiem_delay;

static void sigiem_delay_cur_del(t_sigiem_delay *x, t_floatarg f)
{
  if(f < 0.0f)
    f = 0.0f;
  else if(f > x->x_max_delay_ms)
    f = x->x_max_delay_ms;
  x->x_current_delay_ms = f;
  x->x_delay_samples = (int)(0.001f*x->x_sr * f + 0.5f);
}

static t_int *sigiem_delay_perform(t_int *w)
{
  t_float *in = (t_float *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  t_sigiem_delay *x = (t_sigiem_delay *)(w[3]);
  int n=(int)(w[4]);
  int writeindex = x->x_writeindex;
  t_float *vec1, *vec2, *vec3;
  
  vec1 = x->x_begmem1 + writeindex;
  vec2 = x->x_begmem2 + writeindex;
  vec3 = x->x_begmem2 + writeindex - x->x_delay_samples;
  writeindex += n;
  while(n--)
  {
    *vec1++ = *vec2++ = *in++;
    *out++ = *vec3++;
  }
  if(writeindex >= x->x_mallocsize)
  {
    writeindex -= x->x_mallocsize;
  }
  x->x_writeindex = writeindex;
  return(w+5);
}

static t_int *sigiem_delay_perf8(t_int *w)
{
  t_float *in = (t_float *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  t_sigiem_delay *x = (t_sigiem_delay *)(w[3]);
  int i, n=(int)(w[4]);
  int writeindex = x->x_writeindex;
  t_float *vec1, *vec2;
  
  vec1 = x->x_begmem1 + writeindex;
  vec2 = x->x_begmem2 + writeindex;
  for(i=0; i<n; i+=8)
  {
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
    *vec1++ = *vec2++ = *in++;
  }
  
  vec2 = x->x_begmem2 + writeindex - x->x_delay_samples;
  for(i=0; i<n; i+=8)
  {
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
    *out++ = *vec2++;
  }
  
  writeindex += n;
  if(writeindex >= x->x_mallocsize)
  {
    writeindex -= x->x_mallocsize;
  }
  x->x_writeindex = writeindex;
  return(w+5);
}

static void sigiem_delay_dsp(t_sigiem_delay *x, t_signal **sp)
{
  int blocksize = sp[0]->s_n, i;
  
  if(!x->x_blocksize)/*first time*/
  {
    int nsamps = x->x_max_delay_ms * (t_float)sp[0]->s_sr * 0.001f;
    
    if(nsamps < 1)
      nsamps = 1;
    nsamps += ((- nsamps) & (blocksize - 1));
    nsamps += blocksize;
    x->x_mallocsize = nsamps;
    x->x_begmem1 = (t_float *)getbytes(2 * x->x_mallocsize * sizeof(t_float));
    x->x_begmem2 = x->x_begmem1 + x->x_mallocsize;
    post("beginn = %x", (unsigned long)x->x_begmem1);
    x->x_writeindex = blocksize;
    x->x_sr = (t_float)sp[0]->s_sr;
    x->x_blocksize = blocksize;
    x->x_delay_samples = (int)(0.001f*x->x_sr * x->x_current_delay_ms + 0.5f);
  }
  else if((x->x_blocksize != blocksize) || ((t_float)sp[0]->s_sr != x->x_sr))
  {
    int nsamps = x->x_max_delay_ms * (t_float)sp[0]->s_sr * 0.001f;
    
    if(nsamps < 1)
      nsamps = 1;
    nsamps += ((- nsamps) & (blocksize - 1));
    nsamps += blocksize;
    
    x->x_begmem1 = (t_float *)resizebytes(x->x_begmem1, 2*x->x_mallocsize*sizeof(t_float), 2*nsamps*sizeof(t_float));
    x->x_mallocsize = nsamps;
    x->x_begmem2 = x->x_begmem1 + x->x_mallocsize;
    post("beginn = %x", (unsigned long)x->x_begmem1);
    if(x->x_writeindex >= nsamps)
      x->x_writeindex -= nsamps;
    x->x_sr = (t_float)sp[0]->s_sr;
    x->x_blocksize = blocksize;
    x->x_delay_samples = (int)(0.001f*x->x_sr * x->x_current_delay_ms + 0.5f);
  }
  
  if(blocksize&7)
    dsp_add(sigiem_delay_perform, 4, sp[0]->s_vec, sp[1]->s_vec, x, blocksize);
  else
    dsp_add(sigiem_delay_perf8, 4, sp[0]->s_vec, sp[1]->s_vec, x, blocksize);
}

static void *sigiem_delay_new(t_floatarg max_delay_ms, t_floatarg current_delay_ms)
{
  t_sigiem_delay *x = (t_sigiem_delay *)pd_new(sigiem_delay_class);
  int nsamps;
  
  if(max_delay_ms < 2.0f)
    max_delay_ms = 2.0f;
  x->x_max_delay_ms = max_delay_ms;
  if(current_delay_ms < 0.0f)
    current_delay_ms = 0.0f;
  else if(current_delay_ms > max_delay_ms)
    current_delay_ms = max_delay_ms;
  x->x_current_delay_ms = current_delay_ms;
  nsamps = max_delay_ms * sys_getsr() * 0.001f;
  if(nsamps < 1)
    nsamps = 1;
  nsamps += ((- nsamps) & (IEMDELAY_DEF_VEC_SIZE - 1));
  nsamps += IEMDELAY_DEF_VEC_SIZE;
  x->x_mallocsize = nsamps;
  x->x_begmem1 = (t_float *)getbytes(2 * x->x_mallocsize * sizeof(t_float));
  x->x_begmem2 = x->x_begmem1 + x->x_mallocsize;
  x->x_writeindex = IEMDELAY_DEF_VEC_SIZE;
  x->x_blocksize = 0;
  x->x_sr = 0.0f;
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("ft1"));
  outlet_new(&x->x_obj, &s_signal);
  x->x_msi = 0.0f;
  return (x);
}

static void sigiem_delay_free(t_sigiem_delay *x)
{
  freebytes(x->x_begmem1, 2 * x->x_mallocsize * sizeof(t_float));
}

void sigiem_delay_setup(void)
{
  sigiem_delay_class = class_new(gensym("iem_delay~"), (t_newmethod)sigiem_delay_new, (t_method)sigiem_delay_free,
    sizeof(t_sigiem_delay), 0, A_DEFFLOAT, A_DEFFLOAT, 0);
  CLASS_MAINSIGNALIN(sigiem_delay_class, t_sigiem_delay, x_msi);
  class_addmethod(sigiem_delay_class, (t_method)sigiem_delay_dsp, gensym("dsp"), 0);
  class_addmethod(sigiem_delay_class, (t_method)sigiem_delay_cur_del, gensym("ft1"), A_FLOAT, 0);
}