5
Copyright (C) 2011 Gleb Rogozinsky
7
This file is part of Csound.
9
The Csound Library is free software; you can redistribute it
10
and/or modify it under the terms of the GNU Lesser General Public
11
License as published by the Free Software Foundation; either
12
version 2.1 of the License, or (at your option) any later version.
14
Csound is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU Lesser General Public License for more details.
19
You should have received a copy of the GNU Lesser General Public
20
License along with Csound; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27
// classical 1-D Cellular Automaton by Gleb Rogozinsky.
28
// It is the modified version of vcella opcode by Gabriel Maldonado
32
MYFLT *ktrig, *kreinit, *ioutFunc, *initStateFunc,
33
*iRuleFunc, *ielements;
34
MYFLT *currLine, *outVec, *initVec, *ruleVec;
39
static int cell_set(CSOUND *csound,CELL *p)
43
MYFLT *currLine, *initVec = NULL;
45
if (LIKELY((ftp = csound->FTnp2Find(csound,p->ioutFunc)) != NULL)) {
46
p->outVec = ftp->ftable;
47
elements = (p->elements = (int) *p->ielements);
48
if (UNLIKELY( elements > ftp->flen ))
49
return csound->InitError(csound, Str("cell: invalid num of elements"));
51
else return csound->InitError(csound, Str("cell: invalid output table"));
52
if (LIKELY((ftp = csound->FTnp2Find(csound,p->initStateFunc)) != NULL)) {
53
initVec = (p->initVec = ftp->ftable);
54
if (UNLIKELY(elements > ftp->flen ))
55
return csound->InitError(csound, Str("cell: invalid num of elements"));
58
return csound->InitError(csound, Str("cell: invalid initial state table"));
59
if (LIKELY((ftp = csound->FTnp2Find(csound,p->iRuleFunc)) != NULL)) {
60
p->ruleVec = ftp->ftable;
63
return csound->InitError(csound, Str("cell: invalid rule table"));
65
if (p->auxch.auxp == NULL)
66
csound->AuxAlloc(csound, elements * sizeof(MYFLT) * 2, &p->auxch);
67
currLine = (p->currLine = (MYFLT *) p->auxch.auxp);
69
memcpy(currLine, initVec, sizeof(MYFLT)*elements);
71
/* *currLine++ = *initVec++; */
72
/* } while (--elements); */
76
static int cell(CSOUND *csound,CELL *p)
80
memcpy(p->currLine, p->initVec, sizeof(MYFLT)*p->elements);
82
/* *currLine++ = *initVec++; */
83
/* } while (--elements); */
86
int j, elements = p->elements, jm1;
87
MYFLT *actual, *previous, *outVec = p->outVec , *ruleVec = p->ruleVec;
88
previous = &(p->currLine[elements * p->NewOld]);
91
actual = &(p->currLine[elements * p->NewOld]);
93
for (j=0; j < elements; j++) {
94
jm1 = (j < 1) ? elements-1 : j-1;
95
outVec[j] = previous[j];
96
actual[j] = ruleVec[(int)(previous[jm1]*4 + previous[j]*2 +
97
previous[(j+1) % elements])];
101
int elements = p->elements;
102
MYFLT *actual = &(p->currLine[elements * !(p->NewOld)]);
103
memcpy(p->outVec, actual, sizeof(MYFLT)*elements);
105
/* *outVec++ = *actual++ ; */
106
/* } while (--elements); */
114
static OENTRY localops[] = {
115
{"cell", S(CELL), 3|TB, "", "kkiiii",(SUBR)cell_set, (SUBR)cell }
120
// Author: Gleb Rogozinsky, October 2011