## 2 The Framework
### Registering Opcodes with Csound
They are more difficult to integrate and use properly in this environment, especially given the fact that we are operating under in a hybrid space that includes a C platform supporting the C++ code. The main mechanism for memory allocation in opcodes is provided by the AuxAlloc() function in the Csound API. This is encapsulated by the helper class AuxMem in CPOF, which allows an object-oriented approach to memory manipulation. This class has the following methods:

* allocate(): allocates new memory whenever required. * operator[]: array-subscript access to the allocated memory. * data(): returns a pointer to the data. * len(): returns the length of the vector. * begin(),cbegin(): begin iterators to the data memory. * end(),cend(): end iterators. * iterator and const_iterator: iterator types for this class. In Listing 6, the DelayLine class implements a simple comb filter [24] with three parameters (audio input, i-time delay time, and k-rate feedback amount. It demonstrates the use of the AuxMem template class, which holds the delay memory for the opcode. ```
structDelayLine:csnd::Plugin<1,3>{ staticconstexprcharconst*otypes=\"a\"; staticconstexprcharconst*itypes=\"aik\"; csnd::AuxMem<MYFLT>delay; csnd::AuxMem<MYFLT>::iteratoriter; intinit(){ delay.allocate(csound,csound->sr()*inargs[1]); iter=delay.begin(); returnOK; } intaper(){ csnd::AudioSign(this,inargs(0)); csnd::AudioSignout(this,outargs(0)); MYFLTg=inargs[2]; std::transform(in.begin(),in.end(),out.begin(),[this](MYFLTs){ MYFLTo=*iter; *iter=s+o*g; if(++iter==delay.end())iter=delay.begin(); returno; } ); returnOK; } };
```
In this example, we use an AuxMem iterator to access the delay vector, in a typical C++ idiom. The delay line access is implemented via a lambda that captures he opcode dataspace and processes every sample of the input producing the output vector. While this example uses an iterator for convenience, it is equally possible to access each element with an array-style subscript. The memory allocated by this class is managed by Csound, so we do not need to be concerned about disposing of it. To register this opcode, we can use csnd::plugin<DelayLine>(csound, \"delayline\", csnd::thread::ia); because the output and input types have already been declared in the class as the compile-time constants itypes and otypes. In addition to the automatic AuxMen mechanism, Csound also offers the more conventional malloc-style allocation. This is not generally used in opcode development, but it is accessible via methods of the Csound engine object. It requires explicit de-allocation of resources on clean-up. ### Function Table Access

Function tables are used by Csound for a variety of applications. They hold an array of floating-point numbers that is normally created by one of the GEN routines offered by the system [4]. These can generate tables based on trigonometric functions, polynomials, envelopes, windows, and other various mathematical formulae. Function tables are essential for many opcodes, such as oscillators, granular generators, waveshapers, and various different types of processors. The Csound C API offers access to function tables via a FUNC structure. In CPOF, this access is facilitated by a thin wrapper class that allows us to treat it as a vector object. This is provided by the Table class:

* init(): initialises a table object from an opcode argument pointer. * operator[]: array-subscript access to the function table. * data(): returns a pointer to the function table data. * len(): returns the length of the table (excluding guard point). * begin(),cbegin(): iterators to the beginning of the function table. * end(),cend(): end iterators. * iterator and const_iterator: iterator types for this class. A typical usage example is given by the table-lookup oscillator algorithm [25]. In listing 7, the Oscillator class implements truncating lookup using a C++11 range-for facility.