File: wave-terrain.c

package info (click to toggle)
csound 1%3A6.18.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 63,220 kB
  • sloc: ansic: 192,643; cpp: 14,149; javascript: 9,654; objc: 9,181; python: 3,376; java: 3,337; sh: 1,840; yacc: 1,255; xml: 985; perl: 635; lisp: 411; tcl: 341; lex: 217; makefile: 128
file content (306 lines) | stat: -rw-r--r-- 9,126 bytes parent folder | download | duplicates (5)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
    wave-terrain.c:

    Copyright (C) 2002 Matt Gilliard, John ffitch

    This file is part of Csound.

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

    Csound 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
    02110-1301 USA
*/

#include "stdopcod.h"
#include "wave-terrain.h"
#include <math.h>

/*  Wave-terrain synthesis opcode
 *
 *  author: m gilliard
 *          en6mjg@bath.ac.uk
 */

static int32_t wtinit(CSOUND *csound, WAVETER *p)
{
    /* DECLARE */
    FUNC *ftpx = csound->FTnp2Find(csound, p->i_tabx);
    FUNC *ftpy = csound->FTnp2Find(csound, p->i_taby);

    /* CHECK */
    if (UNLIKELY((ftpx == NULL)||(ftpy == NULL))) {
      return csound->InitError(csound, "%s", Str("wterrain: ftable not found"));
    }

    /* POINT xarr AND yarr AT THE TABLES */
    p->xarr = ftpx->ftable;
    p->yarr = ftpy->ftable;

    /* PUT SIZES INTO STRUCT FOR REFERENCE AT PERF TIME */
    p->sizx = (MYFLT)ftpx->flen;
    p->sizy = (MYFLT)ftpy->flen;
    p->theta = 0.0;
    return OK;
}

static int32_t wtPerf(CSOUND *csound, WAVETER *p)
{
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t i, nsmps = CS_KSMPS;
    int32_t xloc, yloc;
    MYFLT xc, yc;
    MYFLT amp = *p->kamp;
    MYFLT pch = *p->kpch;
    MYFLT kcx = *(p->kcx), kcy = *(p->kcy);
    MYFLT krx = *(p->krx), kry = *(p->kry);
    MYFLT sizx = p->sizx, sizy = p->sizy;
    MYFLT theta = p->theta;
    MYFLT dtpidsr = csound->tpidsr;
    MYFLT *aout = p->aout;

    if (UNLIKELY(offset)) memset(aout, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&aout[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (i=offset; i<nsmps; i++) {

      /* COMPUTE LOCATION OF SCANNING POINT */
      xc = kcx + krx * SIN(theta);
      yc = kcy + kry * COS(theta);

      /* MAP SCANNING POINT TO BE IN UNIT SQUARE */
      xc = xc-FLOOR(xc);
      yc = yc-FLOOR(yc);

      /* SCALE TO TABLE-SIZE SQUARE */
      xloc = (int32_t)(xc * sizx);
      yloc = (int32_t)(yc * sizy);

      /* OUTPUT AM OF TABLE VALUES * KAMP */
      aout[i] = p->xarr[xloc] * p->yarr[yloc] * amp;

      /* MOVE SCANNING POINT ROUND THE ELLIPSE */
      theta += pch * dtpidsr;
    }
    p->theta = FMOD(theta,TWOPI_F);
    return OK;
}

/* ------------------------------------------------------------ */

/*  Simple scanned synthesis hammer hit opcode
 *
 *  Like tablecopy, except the src may be smaller, and hitting position
 *  and force given.
 *
 *  author: m gilliard
 *          en6mjg@bath.ac.uk
 */

static int32_t scanhinit(CSOUND *csound, SCANHAMMER *p)
{
  uint32_t srcpos = 0;
  uint32_t dstpos = (uint32_t)MYFLT2LONG(*p->ipos);

  FUNC *fsrc = csound->FTnp2Find(csound, p->isrc); /* Source table */
  FUNC *fdst = csound->FTnp2Find(csound, p->idst); /* Destination table */

  if (UNLIKELY(fsrc->flen > fdst->flen)) {
    return csound->InitError(csound,  "%s",  Str("Source table must be same size or "
                                         "smaller than dest table\n"));
  }

  for (srcpos=0; srcpos<fsrc->flen; srcpos++) {

    fdst->ftable[dstpos] = fsrc->ftable[srcpos] * *p->imode;

    if (++dstpos > fdst->flen) {
      dstpos = 0;
    }
  }
  return OK;
}

/* ------------------------------------------------------------ */
/*  Simple scanned synthesis opcode
 *
 *  makes a table update at k-rate for the duration of the note.
 *  also scans the system at a k-rate pitch, and k-rate amplitude.
 *
 *  author: m gilliard
 *          en6mjg@bath.ac.uk
 */

static int32_t scantinit(CSOUND *csound, SCANTABLE *p)
{
    /* DECLARE */
    FUNC *fpoint = csound->FTnp2Find(csound, p->i_point);
    FUNC *fmass  = csound->FTnp2Find(csound, p->i_mass);
    FUNC *fstiff = csound->FTnp2Find(csound, p->i_stiff);
    FUNC *fdamp  = csound->FTnp2Find(csound, p->i_damp);
    FUNC *fvel   = csound->FTnp2Find(csound, p->i_vel);

    /* CHECK */
    if (UNLIKELY(fpoint == NULL)) {
      return csound->InitError(csound, "%s",
                               Str("Scantable: point table not found"));
    }
    if (UNLIKELY(fmass == NULL)) {
      return csound->InitError(csound, "%s",
                               Str("Scantable: mass table not found"));
    }
    if (UNLIKELY(fstiff == NULL)) {
      return csound->InitError(csound, "%s",
                               Str("Scantable: stiffness table not found"));
    }
    if (UNLIKELY(fdamp == NULL)) {
      return csound->InitError(csound, "%s",
                               Str("Scantable: damping table not found"));
    }
    if (UNLIKELY(fvel == NULL)) {
      return csound->InitError(csound, "%s",
                               Str("Scantable: velocity table not found"));
    }

    /* CHECK ALL TABLES SAME LENGTH */
    if (UNLIKELY(!((fpoint->flen==fmass->flen) &&
          (fdamp->flen==fstiff->flen)  &&
          (fvel->flen==fstiff->flen)   &&
                   (fpoint->flen==fdamp->flen)))) {
      return csound->InitError(csound, "%s", Str("Table lengths do not agree!!"));
    }

    p->fpoint = fpoint;
    p->fmass  = fmass;
    p->fstiff = fstiff;
    p->fdamp  = fdamp;
    p->fvel   = fvel;

    p->size = (MYFLT)fpoint->flen;

    /* ALLOCATE SPACE FOR NEW POINTS AND VELOCITIES */
    csound->AuxAlloc(csound, fpoint->flen * sizeof(MYFLT), &p->newloca);
    csound->AuxAlloc(csound, fvel->flen * sizeof(MYFLT), &p->newvela);

    /* POINT newloc AND newvel AT THE ALLOCATED SPACE */
    p->newloc = (MYFLT*)p->newloca.auxp;
    p->newvel = (MYFLT*)p->newvela.auxp;

    /* SET SCANNING POSITION */
    p->pos = 0;

    return OK;
}

static int32_t scantPerf(CSOUND *csound, SCANTABLE *p)
{
    uint32_t offset = p->h.insdshead->ksmps_offset;
    uint32_t early  = p->h.insdshead->ksmps_no_end;
    uint32_t i, nsmps = CS_KSMPS;
    MYFLT force, fc1, fc2;
    int32_t next, last;

    /* DECLARE */
    FUNC *fpoint = p->fpoint;
    FUNC *fmass  = p->fmass;
    FUNC *fstiff = p->fstiff;
    FUNC *fdamp  = p->fdamp;
    FUNC *fvel   = p->fvel;
    MYFLT inc    = p->size * *(p->kpch) * csound->onedsr;
    MYFLT amp    = *(p->kamp);
    MYFLT pos    = p->pos;
    MYFLT *aout  = p->aout;

/* CALCULATE NEW POSITIONS
 *
 * fill in newloc and newvel arrays with calculated values
 * this is the string updating function
 *
 * a mass of zero means immovable, so as to allow fixed points
 */

    /* For all except end points  */
    for (i=0; i!=p->size; i++) {

      /* set up conditions for end-points */
      last = i - 1;
      next = i + 1;
      if (UNLIKELY(i == p->size - 1)) {
        next = 0;
      }
      else if (UNLIKELY(i == 0)) {
        last = (int32_t)p->size - 1;
      }

      if (UNLIKELY(fmass->ftable[i] == 0)) {
        /* if the mass is zero... */
        p->newloc[i] = fpoint->ftable[i];
        p->newvel[i] = 0;
      }
      else {
        /* update point according to scheme */
        fc1 = (fpoint->ftable[i] - fpoint->ftable[last]) * fstiff->ftable[last];
        fc2 = (fpoint->ftable[i] - fpoint->ftable[next]) * fstiff->ftable[i];
        force = fc1 + fc2;
        p->newvel[i] = (fvel->ftable[i]
                        - force / (fmass->ftable[i] * CS_EKR))
                       * fdamp->ftable[i];
        p->newloc[i] = fpoint->ftable[i] + p->newvel[i] * CS_ONEDKR;

      }
    }

    if (UNLIKELY(offset)) memset(aout, '\0', offset*sizeof(MYFLT));
    if (UNLIKELY(early)) {
      nsmps -= early;
      memset(&aout[nsmps], '\0', early*sizeof(MYFLT));
    }
    for (i=offset; i<nsmps; i++) {

      /* NO INTERPOLATION */
      aout[i] = fpoint->ftable[(int32_t)pos] * amp;

      pos += inc /* p->size * *(p->kpch) * csound->onedsr */;
      if (UNLIKELY(pos > p->size)) {
        pos -= p->size;
      }
    }
    p->pos = pos;

    /* COPY NEW VALUES TO FTABLE
     *
     * replace current values with new ones
     */
    memcpy(fpoint->ftable, p->newloc, p->size*sizeof(MYFLT));
    memcpy(fvel->ftable, p->newvel, p->size*sizeof(MYFLT));
    return OK;
}

#define S(x)    sizeof(x)

static OENTRY localops[] = {
  { "wterrain", S(WAVETER), TR, 3,  "a", "kkkkkkii",
    (SUBR)wtinit, (SUBR)wtPerf },
  { "scantable", S(SCANTABLE),TR, 3,"a", "kkiiiii",
    (SUBR)scantinit,(SUBR)scantPerf},
  { "scanhammer",S(SCANHAMMER),TB, 1,"", "iiii", (SUBR)scanhinit, NULL, NULL    }
};

int32_t wave_terrain_init_(CSOUND *csound)
{
    return csound->AppendOpcodes(csound, &(localops[0]),
                                 (int32_t
                                  ) (sizeof(localops) / sizeof(OENTRY)));
}