/usr/include/csound/OpcodeBase.hpp is in libcsnd-dev 1:6.05~dfsg1-7build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef OPCODE_BASE_H
#define OPCODE_BASE_H
#include <interlocks.h>
#include <csdl.h>
#include <cstdarg>
/**
* Template base class, or pseudo-virtual base class,
* for writing Csound opcodes in C++.
* Derive opcode implementation classes like this:
*
* DerivedClass : public OpcodeBase<DerivedClass>
* {
* public:
* // All output fields must be declared first as MYFLT *:
* MYFLT *aret1;
* // All input fields must be declared next as MYFLT *:
* MYFLT *iarg1;
* MYFLT *karg2;
* MYFLT *aarg3;
* // All internal state variables must be declared after that:
* size_t state1;
* double state2;
* MYFLT state3;
* // Declare and implement only whichever of these are required:
* int init();
* int kontrol();
* int audio;
* int noteoff();
* void deinit();
* };
*/
template<typename T>
class OpcodeBase
{
public:
int init(CSOUND *csound)
{
return NOTOK;
}
static int init_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->init(csound);
}
int kontrol(CSOUND *csound)
{
return NOTOK;
}
static int kontrol_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->kontrol(csound);
}
int audio(CSOUND *csound)
{
return NOTOK;
}
static int audio_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->audio(csound);
}
/**
This is how to compute audio signals for normal opcodes:
(1) Zero all frames from 0 up to but not including Offset.
(2) Compute all frames from ksmps_offset up to but not including End.
(3) Zero all frames from End up to but not including ksmps.
Example from a C opcode:
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
if (UNLIKELY(offset)) memset(p->r, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&p->r[nsmps], '\0', early*sizeof(MYFLT));
}
for (n = offset; n < nsmps; n++) {
input1 = MYFLT2LRND(p->a[n]);
p->r[n] = (MYFLT) (input1 >> input2);
}
So in C++ it should look like this (which is much easier to understand):
int frameIndex = 0;
for( ; frameIndex < kperiodOffset(); ++frameIndex) {
asignal[frameIndex] = 0;
}
for( ; frameIndex < kperiodEnd(); ++frameIndex) {
asignal[frameIndex] = compute();
}
for( ; frameIndex < ksmps(); ++frameIndex) {
asignal[frameIndex] = 0;
}
*/
uint32_t kperiodOffset() const
{
return opds.insdshead->ksmps_offset;
}
uint32_t kperiodEnd() const
{
uint32_t end = opds.insdshead->ksmps_no_end;
if (end) {
return end;
} else {
return ksmps();
}
}
uint32_t ksmps() const
{
return opds.insdshead->ksmps;
}
void log(CSOUND *csound, const char *format,...)
{
va_list args;
va_start(args, format);
if(csound) {
csound->MessageV(csound, 0, format, args);
}
else {
vfprintf(stdout, format, args);
}
va_end(args);
}
void warn(CSOUND *csound, const char *format,...)
{
if(csound) {
if(csound->GetMessageLevel(csound) & WARNMSG) {
va_list args;
va_start(args, format);
csound->MessageV(csound, CSOUNDMSG_WARNING, format, args);
va_end(args);
}
}
else {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
}
OPDS opds;
};
template<typename T>
class OpcodeNoteoffBase
{
public:
int init(CSOUND *csound)
{
return NOTOK;
}
static int init_(CSOUND *csound, void *opcode)
{
if (!csound->GetReinitFlag(csound) && !csound->GetTieFlag(csound)) {
csound->RegisterDeinitCallback(csound, opcode,
&OpcodeNoteoffBase<T>::noteoff_);
}
return reinterpret_cast<T *>(opcode)->init(csound);
}
int kontrol(CSOUND *csound)
{
return NOTOK;
}
static int kontrol_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->kontrol(csound);
}
int audio(CSOUND *csound)
{
return NOTOK;
}
static int audio_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->audio(csound);
}
/**
This is how to compute audio signals for normal opcodes:
(1) Zero all frames from 0 up to but not including Offset.
(2) Compute all frames from ksmps_offset up to but not including End.
(3) Zero all frames from End up to but not including ksmps.
Example from a C opcode:
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
if (UNLIKELY(offset)) memset(p->r, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&p->r[nsmps], '\0', early*sizeof(MYFLT));
}
for (n = offset; n < nsmps; n++) {
input1 = MYFLT2LRND(p->a[n]);
p->r[n] = (MYFLT) (input1 >> input2);
}
So in C++ it should look like this (which is much easier to understand):
int frameIndex = 0;
for( ; frameIndex < kperiodOffset(); ++frameIndex) {
asignal[frameIndex] = 0;
}
for( ; frameIndex < kperiodEnd(); ++frameIndex) {
asignal[frameIndex] = compute();
}
for( ; frameIndex < ksmps(); ++frameIndex) {
asignal[frameIndex] = 0;
}
*/
uint32_t kperiodOffset() const
{
return opds.insdshead->ksmps_offset;
}
uint32_t kperiodEnd() const
{
uint32_t end = opds.insdshead->ksmps_no_end;
if (end) {
return end;
} else {
return ksmps();
}
}
uint32_t ksmps() const
{
return opds.insdshead->ksmps;
}
void log(CSOUND *csound, const char *format,...)
{
va_list args;
va_start(args, format);
if(csound) {
csound->MessageV(csound, 0, format, args);
}
else {
vfprintf(stdout, format, args);
}
va_end(args);
}
void warn(CSOUND *csound, const char *format,...)
{
if(csound) {
if(csound->GetMessageLevel(csound) & WARNMSG) {
va_list args;
va_start(args, format);
csound->MessageV(csound, CSOUNDMSG_WARNING, format, args);
va_end(args);
}
}
else {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
va_end(args);
}
}
int noteoff(CSOUND *csound)
{
return OK;
}
static int noteoff_(CSOUND *csound, void *opcode)
{
return reinterpret_cast<T *>(opcode)->noteoff(csound);
}
OPDS opds;
};
#endif
|