~ubuntu-branches/ubuntu/precise/csound/precise

« back to all changes in this revision

Viewing changes to Opcodes/cellular.c

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2012-04-19 09:26:46 UTC
  • mfrom: (3.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20120419092646-96xbj1n6atuqosk2
Tags: 1:5.17.6~dfsg-1
* New upstream release
 - Do not build the wiimote opcodes (we need wiiuse).
* Add new API function to symbols file
* Disable lua opcodes, they were broken. Requires OpenMP to be enabled.
* Backport fixes from upstream:
  - Link dssi4cs with dl. Backport
  - Fix building of CsoundAC

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
    pitch.c:
 
4
 
 
5
    Copyright (C) 2011 Gleb Rogozinsky
 
6
 
 
7
    This file is part of Csound.
 
8
 
 
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.
 
13
 
 
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.
 
18
 
 
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
 
22
    02111-1307 USA
 
23
*/
 
24
 
 
25
#include "csdl.h"
 
26
 
 
27
// classical 1-D Cellular Automaton by Gleb Rogozinsky.
 
28
// It is the modified version of vcella opcode by Gabriel Maldonado
 
29
 
 
30
typedef struct {
 
31
    OPDS    h;
 
32
    MYFLT   *ktrig, *kreinit, *ioutFunc, *initStateFunc,
 
33
            *iRuleFunc, *ielements;
 
34
    MYFLT   *currLine, *outVec, *initVec, *ruleVec;
 
35
    int     elements, NewOld;
 
36
    AUXCH   auxch;
 
37
} CELL;
 
38
 
 
39
static int cell_set(CSOUND *csound,CELL *p)
 
40
{
 
41
    FUNC        *ftp;
 
42
    int elements;
 
43
    MYFLT *currLine, *initVec = NULL;
 
44
 
 
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"));
 
50
    }
 
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"));
 
56
    }
 
57
    else
 
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;
 
61
    }
 
62
    else
 
63
      return csound->InitError(csound, Str("cell: invalid rule table"));
 
64
 
 
65
    if (p->auxch.auxp == NULL)
 
66
      csound->AuxAlloc(csound, elements * sizeof(MYFLT) * 2, &p->auxch);
 
67
    currLine = (p->currLine = (MYFLT *) p->auxch.auxp);
 
68
    p->NewOld = 0;
 
69
    memcpy(currLine, initVec, sizeof(MYFLT)*elements);
 
70
    /* do { */
 
71
    /*   *currLine++ = *initVec++; */
 
72
    /* } while (--elements); */
 
73
    return OK;
 
74
}
 
75
 
 
76
static int cell(CSOUND *csound,CELL *p)
 
77
{
 
78
    if (*p->kreinit) {
 
79
      p->NewOld = 0;
 
80
      memcpy(p->currLine, p->initVec, sizeof(MYFLT)*p->elements);
 
81
      /* do { */
 
82
      /*   *currLine++ = *initVec++; */
 
83
      /* } while (--elements); */
 
84
    }
 
85
    if (*p->ktrig) {
 
86
      int j, elements = p->elements, jm1;
 
87
      MYFLT *actual, *previous, *outVec = p->outVec , *ruleVec = p->ruleVec;
 
88
      previous = &(p->currLine[elements * p->NewOld]);
 
89
      p->NewOld += 1;
 
90
      p->NewOld %= 2;
 
91
      actual   = &(p->currLine[elements * p->NewOld]);
 
92
// Cellular Engine
 
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])];
 
98
      }
 
99
 
 
100
    } else {
 
101
      int elements =  p->elements;
 
102
      MYFLT *actual = &(p->currLine[elements * !(p->NewOld)]);
 
103
      memcpy(p->outVec, actual, sizeof(MYFLT)*elements);
 
104
      /* do { */
 
105
      /*   *outVec++ = *actual++ ; */
 
106
      /* } while (--elements); */
 
107
    }
 
108
    return OK;
 
109
}
 
110
 
 
111
 
 
112
#define S sizeof
 
113
 
 
114
static OENTRY localops[] = {
 
115
  {"cell",  S(CELL),  3|TB, "",  "kkiiii",(SUBR)cell_set, (SUBR)cell        }
 
116
};
 
117
 
 
118
LINKAGE
 
119
 
 
120
// Author: Gleb Rogozinsky, October 2011