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
|
/*
sterrain.c:
Copyright (C) 2002 Matt Gilliard, John ffitch
for the original file wave-terrain.c from the csound distribution
Modifications and enhancements (C) 2020 Christian Bacher
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 "csoundCore.h"
#include "interlocks.h"
#include <math.h>
/* Wave-terrain synthesis opcode
*
* author: m gilliard
* en6mjg@bath.ac.uk
*
* Christian Bacher docb22@googlemail.com
* Changes to the original:
* - uses the superformula for generating the curves
*
* - tables are krate
* - added k parameter for rotating the curve arround the current x,y
*/
typedef struct {
OPDS h;
MYFLT *aout;
MYFLT *kamp;
MYFLT *kpch;
MYFLT *kx;
MYFLT *ky;
MYFLT *krx;
MYFLT *kry;
MYFLT *krot; // rotation of the curve
MYFLT *ktabx, *ktaby; /* Table numbers */
MYFLT *sy;
MYFLT *sz;
MYFLT *sn1;
MYFLT *sn2;
MYFLT *sn3;
MYFLT *sa;
MYFLT *sb;
MYFLT *speriod;
/* Internals */
MYFLT oldfnx; // storage of the current table for k-rate table change
MYFLT oldfny; // storage of the current table for k-rate table change
MYFLT *xarr, *yarr; /* Actual tables */
MYFLT sizx, sizy;
double theta;
} SUPERTER;
static void rotate_point(MYFLT cx, MYFLT cy, MYFLT angle, MYFLT *x, MYFLT *y)
{
if(angle == 0) return;
MYFLT s = SIN(angle);
MYFLT c = COS(angle);
*x -= cx;
*y -= cy;
float xnew = *x * c - *y * s;
float ynew = *x * s + *y * c;
*x = xnew + cx;
*y = ynew + cy;
}
typedef struct superparams {
double y;
double z;
double n1;
double n2;
double n3;
double a;
double b;
} SUPERPARAMS;
static void superformula(MYFLT t, MYFLT kx, MYFLT ky, MYFLT krx, MYFLT kry,
SUPERPARAMS *sp, MYFLT *outX, MYFLT *outY) {
if(sp->n1 == 0) return;
if(sp->a == 0) return;
if(sp->b == 0) return;
MYFLT y = sp->y;
MYFLT z = sp->z;
MYFLT n1 = sp->n1;
MYFLT n2 = sp->n2;
MYFLT n3 = sp->n3;
MYFLT a = sp->a;
MYFLT b = sp->b;
MYFLT t1 = COS(y*t/4);
MYFLT t2 = SIN(z*t/4);
MYFLT r0 = POWER(FABS(t1/a),n2) + POWER(FABS(t2/b),n3);
MYFLT r = POWER(r0, -1/n1);
*outX = kx + krx*COS(r);
*outY = ky + kry*SIN(r);
}
static int32_t wtinit(CSOUND *csound, SUPERTER *p)
{
p->xarr = NULL;
p->yarr = NULL;
p->oldfnx = -1;
p->oldfny = -1;
p->sizx = 0;
p->sizy = 0;
p->theta = 0.0;
return OK;
}
static int32_t wtPerf(CSOUND *csound, SUPERTER *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 = FL(0.0), yc = FL(0.0);
MYFLT amp = *p->kamp;
MYFLT pch = *p->kpch;
if (*(p->ktabx) != p->oldfnx || p->xarr == NULL) {
p->oldfnx = *(p->ktabx);
FUNC *ftp = csound->FTFindP(csound, p->ktabx); /* new table parameters */
if (UNLIKELY((ftp == NULL) || ((p->xarr = ftp->ftable) == NULL)))
return csound->PerfError(csound, &(p->h), Str("no table %g\n"), *p->ktabx);
p->sizx = (MYFLT)ftp->flen;
}
if (*(p->ktaby) != p->oldfny || p->yarr == NULL) {
p->oldfny = *(p->ktaby);
FUNC *ftp = csound->FTFindP(csound, p->ktaby); /* new table parameters */
if (UNLIKELY((ftp == NULL) || ((p->yarr = ftp->ftable) == NULL)))
return csound->PerfError(csound, &(p->h), Str("no table %g\n"), *p->ktaby);
p->sizy = (MYFLT)ftp->flen;
}
SUPERPARAMS s;
s.y = FLOOR(*p->sy);
s.z = FLOOR(*p->sz);
s.n1 = *p->sn1;
s.n2 = *p->sn2;
s.n3 = *p->sn3;
s.a = *p->sa;
s.b = *p->sb;
MYFLT period = 1;
if(*p->speriod != 0) period = 1/(*p->speriod);
MYFLT sizx = p->sizx, sizy = p->sizy;
MYFLT theta = p->theta;
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 */
superformula(theta,*p->kx,*p->ky,*p->krx,*p->kry,&s,&xc,&yc);
rotate_point(*p->kx,*p->ky,*p->krot,&xc,&yc);
/* 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*((period*TWOPI_F) / csound->GetSr(csound));
}
p->theta = theta;
return OK;
}
#define S(x) sizeof(x)
static OENTRY sterrain_localops[] = {
{ "sterrain", S(SUPERTER), TR, 3, "a", "kkkkkkkkkkkkkkkkk",
(SUBR)wtinit, (SUBR)wtPerf },
};
LINKAGE_BUILTIN(sterrain_localops)
|