| 1 |
/* |
| 2 |
OpcodeBase.hpp: |
| 3 |
|
| 4 |
Copyright (C) 2005, 2009, 2017 by Istva Varga, Victor Lazzarini and |
| 5 |
Michael Gogins |
| 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., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 |
02110-1301 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef OPCODE_BASE_H |
| 26 |
#define OPCODE_BASE_H |
| 27 |
|
| 28 |
#include <interlocks.h> |
| 29 |
#include <csdl.h> |
| 30 |
#include <cstdarg> |
| 31 |
|
| 32 |
/** |
| 33 |
* Template base class, or pseudo-virtual base class, |
| 34 |
* for writing Csound opcodes in C++. |
| 35 |
* Derive opcode implementation classes like this: |
| 36 |
* |
| 37 |
* DerivedClass : public OpcodeBase<DerivedClass> |
| 38 |
* { |
| 39 |
* public: |
| 40 |
* // All output fields must be declared first as MYFLT *: |
| 41 |
* MYFLT *aret1; |
| 42 |
* // All input fields must be declared next as MYFLT *: |
| 43 |
* MYFLT *iarg1; |
| 44 |
* MYFLT *karg2; |
| 45 |
* MYFLT *aarg3; |
| 46 |
* // All internal state variables must be declared after that: |
| 47 |
* size_t state1; |
| 48 |
* double state2; |
| 49 |
* MYFLT state3; |
| 50 |
* // If the opcode shares data protect it by creating one or more void |
| 51 |
* // *mutex member pointers: |
| 52 |
* void *mutex1; |
| 53 |
* void *mutex2; |
| 54 |
* // and create them usind csound->Create_Mutex() in csoundModuleCreate |
| 55 |
* // and destroy them using csound->DeleteMutex(mutex) |
| 56 |
* // csoundModuleDestroy, and lock them using LockGuard guard(mutex). |
| 57 |
* int init(); |
| 58 |
* int kontrol(); |
| 59 |
* int audio; |
| 60 |
* int noteoff(); |
| 61 |
* void deinit(); |
| 62 |
* }; |
| 63 |
*/ |
| 64 |
|
| 65 |
namespace csound |
| 66 |
{ |
| 67 |
|
| 68 |
/** |
| 69 |
* Use this to guard against data races in opcode functions. The mutex should |
| 70 |
* be created by csound->Create_Mutex() in csoundModuleCreate(), and should |
| 71 |
* be destroyed by csound->DeleteMutex() in csoundModuleDestroy(). |
| 72 |
* |
| 73 |
* If data is shared between opcode instances, the mutex should be global to |
| 74 |
* the opcode library; if data is shared between threads for a single instance, |
| 75 |
* the mutex should belong to the opcode instance. |
| 76 |
*/ |
| 77 |
struct LockGuard { |
| 78 |
LockGuard(CSOUND *csound_, void *mutex_) : csound(csound_), mutex(mutex_) |
| 79 |
{ |
| 80 |
csound->LockMutex(mutex); |
| 81 |
} |
| 82 |
~LockGuard() |
| 83 |
{ |
| 84 |
csound->UnlockMutex(mutex); |
| 85 |
} |
| 86 |
CSOUND *csound; |
| 87 |
void *mutex; |
| 88 |
}; |
| 89 |
|
| 90 |
/** |
| 91 |
* Use this to store a pointer to a global heap-allocated object, e.g. one |
| 92 |
* used to manage state between opcode instances. |
| 93 |
*/ |
| 94 |
|
| 95 |
template<typename T> int CreateGlobalPointer(CSOUND *csound, const char *name, T *pointer) |
| 96 |
{ |
| 97 |
T **pointer_to_pointer = 0; |
| 98 |
int result = csound->CreateGlobalVariable(csound, name, sizeof(pointer_to_pointer)); |
| 99 |
pointer_to_pointer = static_cast<T **>(csound->QueryGlobalVariable(csound, name)); |
| 100 |
*pointer_to_pointer = pointer; |
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Retrieve a pointer to a global heap-allocated object, e.g. one |
| 106 |
* used to manage state between opcode instances. |
| 107 |
*/ |
| 108 |
template<typename T> T *QueryGlobalPointer(CSOUND *csound, const char *name, T*& pointer) |
| 109 |
{ |
| 110 |
T **pointer_to_pointer = static_cast<T **>(csound->QueryGlobalVariableNoCheck(csound, name)); |
| 111 |
if (pointer_to_pointer != 0) { |
| 112 |
pointer = *pointer_to_pointer; |
| 113 |
} else { |
| 114 |
pointer = 0; |
| 115 |
} |
| 116 |
return pointer; |
| 117 |
} |
| 118 |
|
| 119 |
template<typename T> |
| 120 |
class OpcodeBase |
| 121 |
{ |
| 122 |
public: |
| 123 |
int init(CSOUND *csound) |
| 124 |
{ |
| 125 |
return NOTOK; |
| 126 |
} |
| 127 |
static int init_(CSOUND *csound, void *opcode) |
| 128 |
{ |
| 129 |
return reinterpret_cast<T *>(opcode)->init(csound); |
| 130 |
} |
| 131 |
int kontrol(CSOUND *csound) |
| 132 |
{ |
| 133 |
return NOTOK; |
| 134 |
} |
| 135 |
static int kontrol_(CSOUND *csound, void *opcode) |
| 136 |
{ |
| 137 |
return reinterpret_cast<T *>(opcode)->kontrol(csound); |
| 138 |
} |
| 139 |
int audio(CSOUND *csound) |
| 140 |
{ |
| 141 |
return NOTOK; |
| 142 |
} |
| 143 |
static int audio_(CSOUND *csound, void *opcode) |
| 144 |
{ |
| 145 |
return reinterpret_cast<T *>(opcode)->audio(csound); |
| 146 |
} |
| 147 |
/** |
| 148 |
This is how to compute audio signals for normal opcodes: |
| 149 |
(1) Zero all frames from 0 up to but not including Offset. |
| 150 |
(2) Compute all frames from ksmps_offset up to but not including End. |
| 151 |
(3) Zero all frames from End up to but not including ksmps. |
| 152 |
Example from a C opcode: |
| 153 |
uint32_t offset = p->h.insdshead->ksmps_offset; |
| 154 |
uint32_t early = p->h.insdshead->ksmps_no_end; |
| 155 |
uint32_t n, nsmps = CS_KSMPS; |
| 156 |
if (UNLIKELY(offset)) memset(p->r, '\0', offset*sizeof(MYFLT)); |
| 157 |
if (UNLIKELY(early)) { |
| 158 |
nsmps -= early; |
| 159 |
memset(&p->r[nsmps], '\0', early*sizeof(MYFLT)); |
| 160 |
} |
| 161 |
for (n = offset; n < nsmps; n++) { |
| 162 |
input1 = MYFLT2LRND(p->a[n]); |
| 163 |
p->r[n] = (MYFLT) (input1 >> input2); |
| 164 |
} |
| 165 |
So in C++ it should look like this (which is much easier to understand): |
| 166 |
int frameIndex = 0; |
| 167 |
for( ; frameIndex < kperiodOffset(); ++frameIndex) { |
| 168 |
asignal[frameIndex] = 0; |
| 169 |
} |
| 170 |
for( ; frameIndex < kperiodEnd(); ++frameIndex) { |
| 171 |
asignal[frameIndex] = compute(); |
| 172 |
} |
| 173 |
for( ; frameIndex < ksmps(); ++frameIndex) { |
| 174 |
asignal[frameIndex] = 0; |
| 175 |
} |
| 176 |
*/ |
| 177 |
uint32_t kperiodOffset() const |
| 178 |
{ |
| 179 |
return opds.insdshead->ksmps_offset; |
| 180 |
} |
| 181 |
uint32_t kperiodEnd() const |
| 182 |
{ |
| 183 |
uint32_t end = opds.insdshead->ksmps_no_end; |
| 184 |
if (end) { |
| 185 |
return end; |
| 186 |
} else { |
| 187 |
return ksmps(); |
| 188 |
} |
| 189 |
} |
| 190 |
uint32_t ksmps() const |
| 191 |
{ |
| 192 |
return opds.insdshead->ksmps; |
| 193 |
} |
| 194 |
void log(CSOUND *csound, const char *format,...) |
| 195 |
{ |
| 196 |
va_list args; |
| 197 |
va_start(args, format); |
| 198 |
if(csound) { |
| 199 |
csound->MessageV(csound, 0, format, args); |
| 200 |
} else { |
| 201 |
vfprintf(stdout, format, args); |
| 202 |
} |
| 203 |
va_end(args); |
| 204 |
} |
| 205 |
void warn(CSOUND *csound, const char *format,...) |
| 206 |
{ |
| 207 |
if(csound) { |
| 208 |
if(csound->GetMessageLevel(csound) & WARNMSG) { |
| 209 |
va_list args; |
| 210 |
va_start(args, format); |
| 211 |
csound->MessageV(csound, CSOUNDMSG_WARNING, format, args); |
| 212 |
va_end(args); |
| 213 |
} |
| 214 |
} else { |
| 215 |
va_list args; |
| 216 |
va_start(args, format); |
| 217 |
vfprintf(stdout, format, args); |
| 218 |
va_end(args); |
| 219 |
} |
| 220 |
} |
| 221 |
OPDS opds; |
| 222 |
}; |
| 223 |
|
| 224 |
template<typename T> |
| 225 |
class OpcodeNoteoffBase |
| 226 |
{ |
| 227 |
public: |
| 228 |
int init(CSOUND *csound) |
| 229 |
{ |
| 230 |
IGN(csound); |
| 231 |
return NOTOK; |
| 232 |
} |
| 233 |
static int init_(CSOUND *csound, void *opcode) |
| 234 |
{ |
| 235 |
if (!csound->GetReinitFlag(csound) && !csound->GetTieFlag(csound)) { |
| 236 |
csound->RegisterDeinitCallback(csound, opcode, |
| 237 |
&OpcodeNoteoffBase<T>::noteoff_); |
| 238 |
} |
| 239 |
return reinterpret_cast<T *>(opcode)->init(csound); |
| 240 |
} |
| 241 |
int kontrol(CSOUND *csound) |
| 242 |
{ |
| 243 |
IGN(csound); |
| 244 |
return NOTOK; |
| 245 |
} |
| 246 |
static int kontrol_(CSOUND *csound, void *opcode) |
| 247 |
{ |
| 248 |
return reinterpret_cast<T *>(opcode)->kontrol(csound); |
| 249 |
} |
| 250 |
int audio(CSOUND *csound) |
| 251 |
{ |
| 252 |
IGN(csound); |
| 253 |
return NOTOK; |
| 254 |
} |
| 255 |
static int audio_(CSOUND *csound, void *opcode) |
| 256 |
{ |
| 257 |
return reinterpret_cast<T *>(opcode)->audio(csound); |
| 258 |
} |
| 259 |
/** |
| 260 |
This is how to compute audio signals for normal opcodes: |
| 261 |
(1) Zero all frames from 0 up to but not including Offset. |
| 262 |
(2) Compute all frames from ksmps_offset up to but not including End. |
| 263 |
(3) Zero all frames from End up to but not including ksmps. |
| 264 |
Example from a C opcode: |
| 265 |
uint32_t offset = p->h.insdshead->ksmps_offset; |
| 266 |
uint32_t early = p->h.insdshead->ksmps_no_end; |
| 267 |
uint32_t n, nsmps = CS_KSMPS; |
| 268 |
if (UNLIKELY(offset)) memset(p->r, '\0', offset*sizeof(MYFLT)); |
| 269 |
if (UNLIKELY(early)) { |
| 270 |
nsmps -= early; |
| 271 |
memset(&p->r[nsmps], '\0', early*sizeof(MYFLT)); |
| 272 |
} |
| 273 |
for (n = offset; n < nsmps; n++) { |
| 274 |
input1 = MYFLT2LRND(p->a[n]); |
| 275 |
p->r[n] = (MYFLT) (input1 >> input2); |
| 276 |
} |
| 277 |
So in C++ it should look like this (which is much easier to understand): |
| 278 |
int frameIndex = 0; |
| 279 |
for( ; frameIndex < kperiodOffset(); ++frameIndex) { |
| 280 |
asignal[frameIndex] = 0; |
| 281 |
} |
| 282 |
for( ; frameIndex < kperiodEnd(); ++frameIndex) { |
| 283 |
asignal[frameIndex] = compute(); |
| 284 |
} |
| 285 |
for( ; frameIndex < ksmps(); ++frameIndex) { |
| 286 |
asignal[frameIndex] = 0; |
| 287 |
} |
| 288 |
*/ |
| 289 |
uint32_t kperiodOffset() const |
| 290 |
{ |
| 291 |
return opds.insdshead->ksmps_offset; |
| 292 |
} |
| 293 |
uint32_t kperiodEnd() const |
| 294 |
{ |
| 295 |
uint32_t end = opds.insdshead->ksmps_no_end; |
| 296 |
if (end) { |
| 297 |
return end; |
| 298 |
} else { |
| 299 |
return ksmps(); |
| 300 |
} |
| 301 |
} |
| 302 |
uint32_t ksmps() const |
| 303 |
{ |
| 304 |
return opds.insdshead->ksmps; |
| 305 |
} |
| 306 |
void log(CSOUND *csound, const char *format,...) |
| 307 |
{ |
| 308 |
va_list args; |
| 309 |
va_start(args, format); |
| 310 |
if(csound) { |
| 311 |
csound->MessageV(csound, 0, format, args); |
| 312 |
} else { |
| 313 |
vfprintf(stdout, format, args); |
| 314 |
} |
| 315 |
va_end(args); |
| 316 |
} |
| 317 |
void warn(CSOUND *csound, const char *format,...) |
| 318 |
{ |
| 319 |
if(csound) { |
| 320 |
if(csound->GetMessageLevel(csound) & WARNMSG) { |
| 321 |
va_list args; |
| 322 |
va_start(args, format); |
| 323 |
csound->MessageV(csound, CSOUNDMSG_WARNING, format, args); |
| 324 |
va_end(args); |
| 325 |
} |
| 326 |
} else { |
| 327 |
va_list args; |
| 328 |
va_start(args, format); |
| 329 |
vfprintf(stdout, format, args); |
| 330 |
va_end(args); |
| 331 |
} |
| 332 |
} |
| 333 |
int noteoff(CSOUND *csound) |
| 334 |
{ |
| 335 |
return OK; |
| 336 |
} |
| 337 |
static int noteoff_(CSOUND *csound, void *opcode) |
| 338 |
{ |
| 339 |
return reinterpret_cast<T *>(opcode)->noteoff(csound); |
| 340 |
} |
| 341 |
OPDS opds; |
| 342 |
}; |
| 343 |
|
| 344 |
}; |
| 345 |
|
| 346 |
#endif |