19 |
#include <vector> |
#include <vector> |
20 |
#include <map> |
#include <map> |
21 |
#include <stddef.h> // offsetof() |
#include <stddef.h> // offsetof() |
22 |
|
#include <functional> // std::function<> |
23 |
|
|
24 |
namespace LinuxSampler { |
namespace LinuxSampler { |
25 |
|
|
38 |
typedef uint64_t vmuint; |
typedef uint64_t vmuint; |
39 |
|
|
40 |
/** |
/** |
41 |
* Native data type used internally by the script engine for floating point |
* Native data type used by the script engine both internally for floating |
42 |
* data types. This type is currently not exposed to scripts. |
* point data, as well as for all @c real data types used by scripts (i.e. |
43 |
|
* for all ~foo variables in NKSP scripts). |
44 |
*/ |
*/ |
45 |
typedef float vmfloat; |
typedef float vmfloat; |
46 |
|
|
66 |
INT_ARR_EXPR, ///< integer array expression |
INT_ARR_EXPR, ///< integer array expression |
67 |
STRING_EXPR, ///< string expression |
STRING_EXPR, ///< string expression |
68 |
STRING_ARR_EXPR, ///< string array expression |
STRING_ARR_EXPR, ///< string array expression |
69 |
|
REAL_EXPR, ///< floating point (scalar) expression |
70 |
|
REAL_ARR_EXPR, ///< floating point array expression |
71 |
}; |
}; |
72 |
|
|
73 |
/** @brief Result flags of a script statement or script function call. |
/** @brief Result flags of a script statement or script function call. |
138 |
}; |
}; |
139 |
|
|
140 |
/** |
/** |
141 |
|
* This constant is used for comparison with Unit::unitFactor() to check |
142 |
|
* whether a number does have any metric unit prefix at all. |
143 |
|
* |
144 |
|
* @see Unit::unitFactor() |
145 |
|
*/ |
146 |
|
static const vmfloat VM_NO_FACTOR = vmfloat(1); |
147 |
|
|
148 |
|
/** |
149 |
* All measurement unit types supported by this script engine. |
* All measurement unit types supported by this script engine. |
150 |
* |
* |
151 |
* @e Note: there is no standard unit "cents" here (for pitch/tuning), use |
* @e Note: there is no standard unit "cents" here (for pitch/tuning), use |
162 |
VM_BEL, ///< Measuring relation between two energy levels (in logarithmic scale). Since we are using it for accoustics, we are always referring to A-weighted Bels (i.e. dBA). |
VM_BEL, ///< Measuring relation between two energy levels (in logarithmic scale). Since we are using it for accoustics, we are always referring to A-weighted Bels (i.e. dBA). |
163 |
}; |
}; |
164 |
|
|
165 |
|
//TODO: see Unit::hasUnitFactorEver() |
166 |
|
enum EverTriState_t { |
167 |
|
VM_NEVER = 0, |
168 |
|
VM_MAYBE, |
169 |
|
VM_ALWAYS, |
170 |
|
}; |
171 |
|
|
172 |
// just symbol prototyping |
// just symbol prototyping |
173 |
class VMIntExpr; |
class VMIntExpr; |
174 |
|
class VMRealExpr; |
175 |
class VMStringExpr; |
class VMStringExpr; |
176 |
|
class VMNumberExpr; |
177 |
|
class VMArrayExpr; |
178 |
class VMIntArrayExpr; |
class VMIntArrayExpr; |
179 |
|
class VMRealArrayExpr; |
180 |
class VMStringArrayExpr; |
class VMStringArrayExpr; |
181 |
class VMParserContext; |
class VMParserContext; |
182 |
|
|
183 |
/** @brief Virtual machine measuring unit. |
/** @brief Virtual machine standard measuring unit. |
184 |
* |
* |
185 |
* Abstract base class representing standard measurement units throughout |
* Abstract base class representing standard measurement units throughout |
186 |
* the script engine. These might be i.e. "dB" (deci Bel) for loudness, |
* the script engine. These might be e.g. "dB" (deci Bel) for loudness, |
187 |
* "Hz" (Hertz) for frequencies or "s" for "seconds". |
* "Hz" (Hertz) for frequencies or "s" for "seconds". These unit types can |
188 |
|
* combined with metric prefixes, for instance "kHz" (kilo Hertz), |
189 |
|
* "us" (micro second), etc. |
190 |
* |
* |
191 |
* Originally the script engine only supported abstract integer values for |
* Originally the script engine only supported abstract integer values for |
192 |
* controlling any synthesis parameter or built-in function argument or |
* controlling any synthesis parameter or built-in function argument or |
193 |
* variable. Under certain situations it makes sense though for an |
* variable. Under certain situations it makes sense though for an |
194 |
* instrument script author to provide values in real, standard measurement |
* instrument script author to provide values in real, standard measurement |
195 |
* units, for example setting the frequency of some LFO directly to "20Hz". |
* units to provide a more natural and intuitive approach for writing |
196 |
* Hence support for standard units in scripts was added as an extension to |
* instrument scripts, for example by setting the frequency of some LFO |
197 |
* the NKSP script engine. |
* directly to "20Hz" or reducing loudness by "-4.2dB". Hence support for |
198 |
|
* standard units in scripts was added as an extension to the NKSP script |
199 |
|
* engine. |
200 |
|
* |
201 |
|
* So a unit consists of 1) a sequence of metric prefixes as scale factor |
202 |
|
* (e.g. "k" for kilo) and 2) the actual unit type (e.g. "Hz" for Hertz). |
203 |
|
* The unit type is a constant feature of number literals and variables, so |
204 |
|
* once a variable was declared with a unit type (or no unit type at all) |
205 |
|
* then that unit type of that variable cannot be changed for the entire |
206 |
|
* life time of the script. This is different from the unit's metric |
207 |
|
* prefix(es) of variables which may freely be changed at runtime. |
208 |
*/ |
*/ |
209 |
class VMUnit { |
class VMUnit { |
210 |
public: |
public: |
211 |
/** |
/** |
212 |
* Returns the metric prefix of this unit. A metric prefix essentially |
* Returns the metric prefix(es) of this unit as unit factor. A metric |
213 |
* is just a mathematical scale factor that should be applied to the |
* prefix essentially is just a mathematical scale factor that should be |
214 |
* number associated with the measurement unit. Usually a unit either |
* applied to the number associated with the measurement unit. Consider |
215 |
* has exactly none or one prefix, but note that there might also be |
* a string literal in an NKSP script like '3kHz' where 'k' (kilo) is |
216 |
* units with more than one prefix, for example mdB (mili deci bel) |
* the metric prefix, which essentically is a scale factor of 1000. |
|
* is used sometimes which has two prefixes. This is an exception though |
|
|
* and more than two prefixes is currently not supported by the script |
|
|
* engine. |
|
217 |
* |
* |
218 |
* Start iterating over the prefixes of this unit by passing @c 0 as |
* Usually a unit either has exactly none or one metric prefix, but note |
219 |
* argument to this method. The prefixes are terminated with return |
* that there might also be units with more than one prefix, for example |
220 |
* value VM_NO_PREFIX being always the last element. |
* @c mdB (milli deci Bel) is used sometimes which has two prefixes. The |
221 |
|
* latter is an exception though and more than two prefixes is currently |
222 |
|
* not supported by the script engine. |
223 |
* |
* |
224 |
* @param i - index of prefix |
* The factor returned by this method is the final mathematical factor |
225 |
* @returns prefix of requested index or VM_NO_PREFIX otherwise |
* that should be multiplied against the number associated with this |
226 |
* @see unitFactor() |
* unit. This factor results from the sequence of metric prefixes of |
227 |
|
* this unit. |
228 |
|
* |
229 |
|
* @see MetricPrefix_t, hasUnitFactorNow(), hasUnitFactorEver(), |
230 |
|
* VM_NO_FACTOR |
231 |
|
* @returns current metric unit factor |
232 |
*/ |
*/ |
233 |
virtual MetricPrefix_t unitPrefix(vmuint i) const = 0; |
virtual vmfloat unitFactor() const = 0; |
234 |
|
|
235 |
|
//TODO: this still needs to be implemented in tree.h/.pp, built-in functions and as 2nd pass of parser appropriately |
236 |
|
/*virtual*/ EverTriState_t hasUnitFactorEver() const { return VM_NEVER; } |
237 |
|
|
238 |
/** |
/** |
239 |
* Conveniently returns the final mathematical factor that should be |
* Whether this unit currently does have any metric unit prefix. |
240 |
* multiplied against the number associated with this unit. This factor |
* |
241 |
* results from the sequence of metric prefixes of this unit. |
* This is actually just a convenience method which returns @c true if |
242 |
|
* unitFactor() is not @c 1.0. |
243 |
* |
* |
244 |
* @see unitPrefix() |
* @see MetricPrefix_t, unitFactor(), hasUnitFactorEver(), VM_NO_FACTOR |
245 |
|
* @returns @c true if this unit currently has any metric prefix |
246 |
*/ |
*/ |
247 |
vmfloat unitFactor() const; |
bool hasUnitFactorNow() const; |
248 |
|
|
249 |
/** |
/** |
250 |
* This is the actual fundamental measuring unit base type of this unit, |
* This is the actual fundamental measuring unit base type of this unit, |
251 |
* which might be either Hertz, second or Bel. |
* which might be either Hertz, second or Bel. |
252 |
* |
* |
253 |
* @returns standard unit type identifier or VM_NO_UNIT if no unit used |
* Note that a number without a unit type may still have metric |
254 |
|
* prefixes. |
255 |
|
* |
256 |
|
* @returns standard unit type identifier or VM_NO_UNIT if no unit type |
257 |
|
* is used for this object |
258 |
*/ |
*/ |
259 |
virtual StdUnit_t unitType() const = 0; |
virtual StdUnit_t unitType() const = 0; |
260 |
|
|
267 |
/** |
/** |
268 |
* Returns the actual mathematical factor represented by the passed |
* Returns the actual mathematical factor represented by the passed |
269 |
* two @a prefix1 and @a prefix2 arguments. |
* two @a prefix1 and @a prefix2 arguments. |
270 |
|
* |
271 |
|
* @returns scale factor of given metric unit prefixes |
272 |
*/ |
*/ |
273 |
static vmfloat unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2); |
static vmfloat unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2); |
274 |
|
|
275 |
|
/** |
276 |
|
* Returns the actual mathematical factor represented by the passed |
277 |
|
* @a prefixes array. The passed array should always be terminated by a |
278 |
|
* VM_NO_PREFIX value as last element. |
279 |
|
* |
280 |
|
* @param prefixes - sequence of metric prefixes |
281 |
|
* @param size - max. amount of elements of array @a prefixes |
282 |
|
* @returns scale factor of given metric unit prefixes |
283 |
|
*/ |
284 |
|
static vmfloat unitFactor(const MetricPrefix_t* prefixes, vmuint size = 2); |
285 |
}; |
}; |
286 |
|
|
287 |
/** @brief Virtual machine expression |
/** @brief Virtual machine expression |
315 |
* if this expression is i.e. actually a string expression like "12", |
* if this expression is i.e. actually a string expression like "12", |
316 |
* calling asInt() will @b not cast that numerical string expression to |
* calling asInt() will @b not cast that numerical string expression to |
317 |
* an integer expression 12 for you, instead this method will simply |
* an integer expression 12 for you, instead this method will simply |
318 |
* return NULL! |
* return NULL! Same applies if this expression is actually a real |
319 |
|
* number expression: asInt() would return NULL in that case as well. |
320 |
* |
* |
321 |
* @see exprType() |
* @see exprType(), asReal(), asNumber() |
322 |
*/ |
*/ |
323 |
VMIntExpr* asInt() const; |
VMIntExpr* asInt() const; |
324 |
|
|
325 |
/** |
/** |
326 |
|
* In case this expression is a real number (floating point) expression, |
327 |
|
* then this method returns a casted pointer to that VMRealExpr object. |
328 |
|
* It returns NULL if this expression is not a real number expression. |
329 |
|
* |
330 |
|
* @b Note: type casting performed by this method is strict! That means |
331 |
|
* if this expression is i.e. actually a string expression like "12", |
332 |
|
* calling asReal() will @b not cast that numerical string expression to |
333 |
|
* a real number expression 12.0 for you, instead this method will |
334 |
|
* simply return NULL! Same applies if this expression is actually an |
335 |
|
* integer expression: asReal() would return NULL in that case as well. |
336 |
|
* |
337 |
|
* @see exprType(), asInt(), asNumber() |
338 |
|
*/ |
339 |
|
VMRealExpr* asReal() const; |
340 |
|
|
341 |
|
/** |
342 |
|
* In case this expression is a scalar number expression, that is either |
343 |
|
* an integer (scalar) expression or a real number (floating point |
344 |
|
* scalar) expression, then this method returns a casted pointer to that |
345 |
|
* VMNumberExpr base class object. It returns NULL if this |
346 |
|
* expression is neither an integer (scalar), nor a real number (scalar) |
347 |
|
* expression. |
348 |
|
* |
349 |
|
* Since the methods asInt() and asReal() are very strict, this method |
350 |
|
* is provided as convenience access in case only very general |
351 |
|
* information (e.g. which standard measurement unit is being used or |
352 |
|
* whether final operator being effective to this expression) is |
353 |
|
* intended to be retrieved of this scalar number expression independent |
354 |
|
* from whether this expression is actually an integer or a real number |
355 |
|
* expression. |
356 |
|
* |
357 |
|
* @see exprType(), asInt(), asReal() |
358 |
|
*/ |
359 |
|
VMNumberExpr* asNumber() const; |
360 |
|
|
361 |
|
/** |
362 |
* In case this expression is a string expression, then this method |
* In case this expression is a string expression, then this method |
363 |
* returns a casted pointer to that VMStringExpr object. It returns NULL |
* returns a casted pointer to that VMStringExpr object. It returns NULL |
364 |
* if this expression is not a string expression. |
* if this expression is not a string expression. |
379 |
* returns NULL if this expression is not an integer array expression. |
* returns NULL if this expression is not an integer array expression. |
380 |
* |
* |
381 |
* @b Note: type casting performed by this method is strict! That means |
* @b Note: type casting performed by this method is strict! That means |
382 |
* if this expression is i.e. an integer expression or a string |
* if this expression is i.e. an integer scalar expression, a real |
383 |
* expression, calling asIntArray() will @b not cast those scalar |
* number expression or a string expression, calling asIntArray() will |
384 |
* expressions to an array expression for you, instead this method will |
* @b not cast those expressions to an integer array expression for you, |
385 |
* simply return NULL! |
* instead this method will simply return NULL! |
386 |
* |
* |
387 |
* @b Note: this method is currently, and in contrast to its other |
* @b Note: this method is currently, and in contrast to its other |
388 |
* counter parts, declared as virtual method. Some deriving classes are |
* counter parts, declared as virtual method. Some deriving classes are |
397 |
virtual VMIntArrayExpr* asIntArray() const; |
virtual VMIntArrayExpr* asIntArray() const; |
398 |
|
|
399 |
/** |
/** |
400 |
|
* In case this expression is a real number (floating point) array |
401 |
|
* expression, then this method returns a casted pointer to that |
402 |
|
* VMRealArrayExpr object. It returns NULL if this expression is not a |
403 |
|
* real number array expression. |
404 |
|
* |
405 |
|
* @b Note: type casting performed by this method is strict! That means |
406 |
|
* if this expression is i.e. a real number scalar expression, an |
407 |
|
* integer expression or a string expression, calling asRealArray() will |
408 |
|
* @b not cast those scalar expressions to a real number array |
409 |
|
* expression for you, instead this method will simply return NULL! |
410 |
|
* |
411 |
|
* @b Note: this method is currently, and in contrast to its other |
412 |
|
* counter parts, declared as virtual method. Some deriving classes are |
413 |
|
* currently using this to override this default implementation in order |
414 |
|
* to implement an "evaluate now as real number array" behavior. This |
415 |
|
* has efficiency reasons, however this also currently makes this part |
416 |
|
* of the API less clean and should thus be addressed in future with |
417 |
|
* appropriate changes to the API. |
418 |
|
* |
419 |
|
* @see exprType() |
420 |
|
*/ |
421 |
|
virtual VMRealArrayExpr* asRealArray() const; |
422 |
|
|
423 |
|
/** |
424 |
|
* This is an alternative to calling either asIntArray() or |
425 |
|
* asRealArray(). This method here might be used if the fundamental |
426 |
|
* scalar data type (real or integer) of the array is not relevant, |
427 |
|
* i.e. for just getting the size of the array. Since all as*() methods |
428 |
|
* here are very strict regarding type casting, this asArray() method |
429 |
|
* sometimes can reduce code complexity. |
430 |
|
* |
431 |
|
* Likewise calling this method only returns a valid pointer if the |
432 |
|
* expression is some array type (currently either integer array or real |
433 |
|
* number array). For any other expression type this method will return |
434 |
|
* NULL instead. |
435 |
|
* |
436 |
|
* @see exprType() |
437 |
|
*/ |
438 |
|
VMArrayExpr* asArray() const; |
439 |
|
|
440 |
|
/** |
441 |
* Returns true in case this expression can be considered to be a |
* Returns true in case this expression can be considered to be a |
442 |
* constant expression. A constant expression will retain the same |
* constant expression. A constant expression will retain the same |
443 |
* value throughout the entire life time of a script and the |
* value throughout the entire life time of a script and the |
468 |
bool isModifyable() const; |
bool isModifyable() const; |
469 |
}; |
}; |
470 |
|
|
471 |
|
/** @brief Virtual machine scalar number expression |
472 |
|
* |
473 |
|
* This is the abstract base class for integer (scalar) expressions and |
474 |
|
* real number (floating point scalar) expressions of scripts. |
475 |
|
*/ |
476 |
|
class VMNumberExpr : virtual public VMExpr, virtual public VMUnit { |
477 |
|
public: |
478 |
|
/** |
479 |
|
* Returns @c true if the value of this expression should be applied |
480 |
|
* as final value to the respective destination synthesis chain |
481 |
|
* parameter. |
482 |
|
* |
483 |
|
* This property is somewhat special and dedicated for the purpose of |
484 |
|
* this expression's (integer or real number) value to be applied as |
485 |
|
* parameter to the synthesis chain of the sampler (i.e. for altering a |
486 |
|
* filter cutoff frequency). Now historically and by default all values |
487 |
|
* of scripts are applied relatively to the sampler's synthesis chain, |
488 |
|
* that is the synthesis parameter value of a script is multiplied |
489 |
|
* against other sources for the same synthesis parameter (i.e. an LFO |
490 |
|
* or a dedicated MIDI controller either hard wired in the engine or |
491 |
|
* defined by the instrument patch). So by default the resulting actual |
492 |
|
* final synthesis parameter is a combination of all these sources. This |
493 |
|
* has the advantage that it creates a very living and dynamic overall |
494 |
|
* sound. |
495 |
|
* |
496 |
|
* However sometimes there are requirements by script authors where this |
497 |
|
* is not what you want. Therefore the NKSP script engine added a |
498 |
|
* language extension by prefixing a value in scripts with a @c ! |
499 |
|
* character the value will be defined as being the "final" value of the |
500 |
|
* destination synthesis parameter, so that causes this value to be |
501 |
|
* applied exclusively, and the values of all other sources are thus |
502 |
|
* entirely ignored by the sampler's synthesis core as long as this |
503 |
|
* value is assigned by the script engine as "final" value for the |
504 |
|
* requested synthesis parameter. |
505 |
|
*/ |
506 |
|
virtual bool isFinal() const = 0; |
507 |
|
|
508 |
|
/** |
509 |
|
* Calling this method evaluates the expression and returns the value |
510 |
|
* of the expression as integer. If this scalar number expression is a |
511 |
|
* real number expression then this method automatically casts the value |
512 |
|
* from real number to integer. |
513 |
|
*/ |
514 |
|
vmint evalCastInt(); |
515 |
|
|
516 |
|
/** |
517 |
|
* Calling this method evaluates the expression and returns the value |
518 |
|
* of the expression as integer and thus behaves similar to the previous |
519 |
|
* method, however this overridden method automatically takes unit |
520 |
|
* prefixes into account and returns a converted value corresponding to |
521 |
|
* the given unit @a prefix expected by the caller. |
522 |
|
* |
523 |
|
* Example: Assume this expression was an integer expression '12kHz' |
524 |
|
* then calling this method as @c evalCastInt(VM_MILLI) would return |
525 |
|
* the value @c 12000000. |
526 |
|
* |
527 |
|
* @param prefix - measuring unit prefix expected for result by caller |
528 |
|
*/ |
529 |
|
vmint evalCastInt(MetricPrefix_t prefix); |
530 |
|
|
531 |
|
/** |
532 |
|
* This method behaves like the previous method, just that it takes a |
533 |
|
* measuring unit prefix with two elements (e.g. "milli cents" for |
534 |
|
* tuning). |
535 |
|
* |
536 |
|
* @param prefix1 - 1st measuring unit prefix element expected by caller |
537 |
|
* @param prefix2 - 2nd measuring unit prefix element expected by caller |
538 |
|
*/ |
539 |
|
vmint evalCastInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2); |
540 |
|
|
541 |
|
/** |
542 |
|
* Calling this method evaluates the expression and returns the value |
543 |
|
* of the expression as real number. If this scalar number expression is |
544 |
|
* an integer expression then this method automatically casts the value |
545 |
|
* from integer to real number. |
546 |
|
*/ |
547 |
|
vmfloat evalCastReal(); |
548 |
|
|
549 |
|
/** |
550 |
|
* Calling this method evaluates the expression and returns the value |
551 |
|
* of the expression as real number and thus behaves similar to the |
552 |
|
* previous method, however this overridden method automatically takes |
553 |
|
* unit prefixes into account and returns a converted value |
554 |
|
* corresponding to the given unit @a prefix expected by the caller. |
555 |
|
* |
556 |
|
* Example: Assume this expression was an integer expression '8ms' then |
557 |
|
* calling this method as @c evalCastReal(VM_NO_PREFIX) would return the |
558 |
|
* value @c 0.008. |
559 |
|
* |
560 |
|
* @param prefix - measuring unit prefix expected for result by caller |
561 |
|
*/ |
562 |
|
vmfloat evalCastReal(MetricPrefix_t prefix); |
563 |
|
|
564 |
|
/** |
565 |
|
* This method behaves like the previous method, just that it takes a |
566 |
|
* measuring unit prefix with two elements (e.g. "milli cents" for |
567 |
|
* tuning). |
568 |
|
* |
569 |
|
* @param prefix1 - 1st measuring unit prefix element expected by caller |
570 |
|
* @param prefix2 - 2nd measuring unit prefix element expected by caller |
571 |
|
*/ |
572 |
|
vmfloat evalCastReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2); |
573 |
|
}; |
574 |
|
|
575 |
/** @brief Virtual machine integer expression |
/** @brief Virtual machine integer expression |
576 |
* |
* |
577 |
* This is the abstract base class for all expressions inside scripts which |
* This is the abstract base class for all expressions inside scripts which |
579 |
* abstract method evalInt() to return the actual integer result value of |
* abstract method evalInt() to return the actual integer result value of |
580 |
* the expression. |
* the expression. |
581 |
*/ |
*/ |
582 |
class VMIntExpr : virtual public VMExpr, virtual public VMUnit { |
class VMIntExpr : virtual public VMNumberExpr { |
583 |
public: |
public: |
584 |
/** |
/** |
585 |
* Returns the result of this expression as integer (scalar) value. |
* Returns the result of this expression as integer (scalar) value. |
608 |
* Returns always INT_EXPR for instances of this class. |
* Returns always INT_EXPR for instances of this class. |
609 |
*/ |
*/ |
610 |
ExprType_t exprType() const OVERRIDE { return INT_EXPR; } |
ExprType_t exprType() const OVERRIDE { return INT_EXPR; } |
611 |
|
}; |
612 |
|
|
613 |
|
/** @brief Virtual machine real number (floating point scalar) expression |
614 |
|
* |
615 |
|
* This is the abstract base class for all expressions inside scripts which |
616 |
|
* evaluate to a real number (floating point scalar) value. Deriving classes |
617 |
|
* implement the abstract method evalReal() to return the actual floating |
618 |
|
* point result value of the expression. |
619 |
|
*/ |
620 |
|
class VMRealExpr : virtual public VMNumberExpr { |
621 |
|
public: |
622 |
/** |
/** |
623 |
* Returns @c true if the value of this expression should be applied |
* Returns the result of this expression as real number (floating point |
624 |
* as final value to the respective destination synthesis chain |
* scalar) value. This abstract method must be implemented by deriving |
625 |
* parameter. |
* classes. |
626 |
* |
*/ |
627 |
* This property is somewhat special and dedicated for the purpose of |
virtual vmfloat evalReal() = 0; |
628 |
* this expression's integer value to be applied as parameter to the |
|
629 |
* synthesis chain of the sampler (i.e. for altering a filter cutoff |
/** |
630 |
* frequency). Now historically and by default all values of scripts are |
* Returns the result of this expression as real number (floating point |
631 |
* applied relatively to the sampler's synthesis chain, that is the |
* scalar) value and thus behaves similar to the previous method, |
632 |
* synthesis parameter value of a script is multiplied against other |
* however this overridden method automatically takes unit prefixes into |
633 |
* sources for the same synthesis parameter (i.e. an LFO or a dedicated |
* account and returns a value corresponding to the expected given unit |
634 |
* MIDI controller either hard wired in the engine or defined by the |
* @a prefix. |
|
* instrument patch). So by default the resulting actual final synthesis |
|
|
* parameter is a combination of all these sources. This has the |
|
|
* advantage that it creates a very living and dynamic overall sound. |
|
635 |
* |
* |
636 |
* However sometimes there are requirements by script authors where this |
* @param prefix - default measurement unit prefix expected by caller |
|
* is not what you want. Therefore the NKSP script engine added a |
|
|
* language extension by prefixing a value in scripts with a @c ! |
|
|
* character the value will be defined as being the "final" value of the |
|
|
* destination synthesis parameter, so that causes this value to be |
|
|
* applied exclusively, and the values of all other sources are thus |
|
|
* entirely ignored by the sampler's synthesis core as long as this |
|
|
* value is assigned by the script engine as "final" value for the |
|
|
* requested synthesis parameter. |
|
637 |
*/ |
*/ |
638 |
virtual bool isFinal() const = 0; |
vmfloat evalReal(MetricPrefix_t prefix); |
639 |
|
|
640 |
|
/** |
641 |
|
* This method behaves like the previous method, just that it takes |
642 |
|
* a default measurement prefix with two elements (i.e. "milli cents" |
643 |
|
* for tuning). |
644 |
|
*/ |
645 |
|
vmfloat evalReal(MetricPrefix_t prefix1, MetricPrefix_t prefix2); |
646 |
|
|
647 |
|
/** |
648 |
|
* Returns always REAL_EXPR for instances of this class. |
649 |
|
*/ |
650 |
|
ExprType_t exprType() const OVERRIDE { return REAL_EXPR; } |
651 |
}; |
}; |
652 |
|
|
653 |
/** @brief Virtual machine string expression |
/** @brief Virtual machine string expression |
687 |
virtual vmint arraySize() const = 0; |
virtual vmint arraySize() const = 0; |
688 |
}; |
}; |
689 |
|
|
690 |
|
/** @brief Virtual Machine Number Array Expression |
691 |
|
* |
692 |
|
* This is the abstract base class for all expressions which either evaluate |
693 |
|
* to an integer array or real number array. |
694 |
|
*/ |
695 |
|
class VMNumberArrayExpr : virtual public VMArrayExpr { |
696 |
|
public: |
697 |
|
/** |
698 |
|
* Returns the metric unit factor of the requested array element. |
699 |
|
* |
700 |
|
* @param i - array element index (must be between 0 .. arraySize() - 1) |
701 |
|
* @see VMUnit::unitFactor() for details about metric unit factors |
702 |
|
*/ |
703 |
|
virtual vmfloat unitFactorOfElement(vmuint i) const = 0; |
704 |
|
|
705 |
|
/** |
706 |
|
* Changes the current unit factor of the array element given by element |
707 |
|
* index @a i. |
708 |
|
* |
709 |
|
* @param i - array element index (must be between 0 .. arraySize() - 1) |
710 |
|
* @param factor - new unit factor to be assigned |
711 |
|
* @see VMUnit::unitFactor() for details about metric unit factors |
712 |
|
*/ |
713 |
|
virtual void assignElementUnitFactor(vmuint i, vmfloat factor) = 0; |
714 |
|
}; |
715 |
|
|
716 |
/** @brief Virtual Machine Integer Array Expression |
/** @brief Virtual Machine Integer Array Expression |
717 |
* |
* |
718 |
* This is the abstract base class for all expressions inside scripts which |
* This is the abstract base class for all expressions inside scripts which |
720 |
* abstract methods arraySize(), evalIntElement() and assignIntElement() to |
* abstract methods arraySize(), evalIntElement() and assignIntElement() to |
721 |
* access the individual integer array values. |
* access the individual integer array values. |
722 |
*/ |
*/ |
723 |
class VMIntArrayExpr : virtual public VMArrayExpr { |
class VMIntArrayExpr : virtual public VMNumberArrayExpr { |
724 |
public: |
public: |
725 |
/** |
/** |
726 |
* Returns the (scalar) integer value of the array element given by |
* Returns the (scalar) integer value of the array element given by |
745 |
ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; } |
ExprType_t exprType() const OVERRIDE { return INT_ARR_EXPR; } |
746 |
}; |
}; |
747 |
|
|
748 |
|
/** @brief Virtual Machine Real Number Array Expression |
749 |
|
* |
750 |
|
* This is the abstract base class for all expressions inside scripts which |
751 |
|
* evaluate to an array of real numbers (floating point values). Deriving |
752 |
|
* classes implement the abstract methods arraySize(), evalRealElement() and |
753 |
|
* assignRealElement() to access the array's individual real numbers. |
754 |
|
*/ |
755 |
|
class VMRealArrayExpr : virtual public VMNumberArrayExpr { |
756 |
|
public: |
757 |
|
/** |
758 |
|
* Returns the (scalar) real mumber (floating point value) of the array |
759 |
|
* element given by element index @a i. |
760 |
|
* |
761 |
|
* @param i - array element index (must be between 0 .. arraySize() - 1) |
762 |
|
*/ |
763 |
|
virtual vmfloat evalRealElement(vmuint i) = 0; |
764 |
|
|
765 |
|
/** |
766 |
|
* Changes the current value of an element (given by array element |
767 |
|
* index @a i) of this real number array. |
768 |
|
* |
769 |
|
* @param i - array element index (must be between 0 .. arraySize() - 1) |
770 |
|
* @param value - new real number value to be assigned to that array element |
771 |
|
*/ |
772 |
|
virtual void assignRealElement(vmuint i, vmfloat value) = 0; |
773 |
|
|
774 |
|
/** |
775 |
|
* Returns always REAL_ARR_EXPR for instances of this class. |
776 |
|
*/ |
777 |
|
ExprType_t exprType() const OVERRIDE { return REAL_ARR_EXPR; } |
778 |
|
}; |
779 |
|
|
780 |
/** @brief Arguments (parameters) for being passed to a built-in script function. |
/** @brief Arguments (parameters) for being passed to a built-in script function. |
781 |
* |
* |
782 |
* An argument or a set of arguments passed to a script function are |
* An argument or a set of arguments passed to a script function are |
857 |
/** |
/** |
858 |
* Script data type of the function's return value. If the function does |
* Script data type of the function's return value. If the function does |
859 |
* not return any value (void), then it returns EMPTY_EXPR here. |
* not return any value (void), then it returns EMPTY_EXPR here. |
860 |
|
* |
861 |
|
* Some functions may have a different return type depending on the |
862 |
|
* arguments to be passed to this function. That's what the @a args |
863 |
|
* parameter is for, so that the method implementation can look ahead |
864 |
|
* of what kind of parameters are going to be passed to the built-in |
865 |
|
* function later on in order to decide which return value type would |
866 |
|
* be used and returned by the function accordingly in that case. |
867 |
|
* |
868 |
|
* @param args - function arguments going to be passed for executing |
869 |
|
* this built-in function later on |
870 |
|
*/ |
871 |
|
virtual ExprType_t returnType(VMFnArgs* args) = 0; |
872 |
|
|
873 |
|
/** |
874 |
|
* Standard measuring unit type of the function's result value |
875 |
|
* (e.g. second, Hertz). |
876 |
|
* |
877 |
|
* Some functions may have a different standard measuring unit type for |
878 |
|
* their return value depending on the arguments to be passed to this |
879 |
|
* function. That's what the @a args parameter is for, so that the |
880 |
|
* method implementation can look ahead of what kind of parameters are |
881 |
|
* going to be passed to the built-in function later on in order to |
882 |
|
* decide which return value type would be used and returned by the |
883 |
|
* function accordingly in that case. |
884 |
|
* |
885 |
|
* @param args - function arguments going to be passed for executing |
886 |
|
* this built-in function later on |
887 |
|
* @see Unit for details about standard measuring units |
888 |
|
*/ |
889 |
|
virtual StdUnit_t returnUnitType(VMFnArgs* args) = 0; |
890 |
|
|
891 |
|
/** |
892 |
|
* Whether the result value returned by this built-in function is |
893 |
|
* considered to be a 'final' value. |
894 |
|
* |
895 |
|
* Some functions may have a different 'final' feature for their return |
896 |
|
* value depending on the arguments to be passed to this function. |
897 |
|
* That's what the @a args parameter is for, so that the method |
898 |
|
* implementation can look ahead of what kind of parameters are going to |
899 |
|
* be passed to the built-in function later on in order to decide which |
900 |
|
* return value type would be used and returned by the function |
901 |
|
* accordingly in that case. |
902 |
|
* |
903 |
|
* @param args - function arguments going to be passed for executing |
904 |
|
* this built-in function later on |
905 |
|
* @see VMNumberExpr::isFinal() for details about 'final' values |
906 |
*/ |
*/ |
907 |
virtual ExprType_t returnType() = 0; |
virtual bool returnsFinal(VMFnArgs* args) = 0; |
908 |
|
|
909 |
/** |
/** |
910 |
* Minimum amount of function arguments this function accepts. If a |
* Minimum amount of function arguments this function accepts. If a |
1014 |
* @return true if a "final" value would be accepted for the respective |
* @return true if a "final" value would be accepted for the respective |
1015 |
* function argument by the function |
* function argument by the function |
1016 |
* |
* |
1017 |
* @see VMIntExpr::isFinal() |
* @see VMNumberExpr::isFinal(), returnsFinal() |
1018 |
*/ |
*/ |
1019 |
virtual bool acceptsArgFinal(vmint iArg) const; |
virtual bool acceptsArgFinal(vmint iArg) const; |
1020 |
|
|
1034 |
virtual bool modifiesArg(vmint iArg) const = 0; |
virtual bool modifiesArg(vmint iArg) const = 0; |
1035 |
|
|
1036 |
/** |
/** |
1037 |
|
* This method is called by the parser to let the built-in function |
1038 |
|
* perform its own, individual parse time checks on the arguments to be |
1039 |
|
* passed to the built-in function. So this method is the place for |
1040 |
|
* implementing custom checks which are very specific to the individual |
1041 |
|
* built-in function's purpose and its individual requirements. |
1042 |
|
* |
1043 |
|
* For instance the built-in 'in_range()' function uses this method to |
1044 |
|
* check whether the last 2 of their 3 arguments are of same data type |
1045 |
|
* and if not it triggers a parser error. 'in_range()' also checks |
1046 |
|
* whether all of its 3 arguments do have the same standard measuring |
1047 |
|
* unit type and likewise raises a parser error if not. |
1048 |
|
* |
1049 |
|
* For less critical issues built-in functions may also raise parser |
1050 |
|
* warnings instead. |
1051 |
|
* |
1052 |
|
* It is recommended that classes implementing (that is overriding) this |
1053 |
|
* method should always call their super class's implementation of this |
1054 |
|
* method to ensure their potential parse time checks are always |
1055 |
|
* performed as well. |
1056 |
|
* |
1057 |
|
* @param args - function arguments going to be passed for executing |
1058 |
|
* this built-in function later on |
1059 |
|
* @param err - the parser's error handler to be called by this method |
1060 |
|
* implementation to trigger a parser error with the |
1061 |
|
* respective error message text |
1062 |
|
* @param wrn - the parser's warning handler to be called by this method |
1063 |
|
* implementation to trigger a parser warning with the |
1064 |
|
* respective warning message text |
1065 |
|
*/ |
1066 |
|
virtual void checkArgs(VMFnArgs* args, |
1067 |
|
std::function<void(String)> err, |
1068 |
|
std::function<void(String)> wrn); |
1069 |
|
|
1070 |
|
/** |
1071 |
* Implements the actual function execution. This exec() method is |
* Implements the actual function execution. This exec() method is |
1072 |
* called by the VM whenever this function implementation shall be |
* called by the VM whenever this function implementation shall be |
1073 |
* executed at script runtime. This method blocks until the function |
* executed at script runtime. This method blocks until the function |
1353 |
/** @brief Built-in VM 8 bit integer array variable. |
/** @brief Built-in VM 8 bit integer array variable. |
1354 |
* |
* |
1355 |
* Used for defining built-in integer array script variables (8 bit per |
* Used for defining built-in integer array script variables (8 bit per |
1356 |
* array element). Currently there is no support for any other kind of array |
* array element). Currently there is no support for any other kind of |
1357 |
* type. So all integer arrays of scripts use 8 bit data types. |
* built-in array type. So all built-in integer arrays accessed by scripts |
1358 |
|
* use 8 bit data types. |
1359 |
*/ |
*/ |
1360 |
struct VMInt8Array { |
struct VMInt8Array { |
1361 |
int8_t* data; |
int8_t* data; |
1367 |
|
|
1368 |
/** @brief Virtual machine script variable. |
/** @brief Virtual machine script variable. |
1369 |
* |
* |
1370 |
* Common interface for all variables accessed in scripts. |
* Common interface for all variables accessed in scripts, independent of |
1371 |
|
* their precise data type. |
1372 |
*/ |
*/ |
1373 |
class VMVariable : virtual public VMExpr { |
class VMVariable : virtual public VMExpr { |
1374 |
public: |
public: |
1389 |
*/ |
*/ |
1390 |
virtual void assignExpr(VMExpr* expr) = 0; |
virtual void assignExpr(VMExpr* expr) = 0; |
1391 |
}; |
}; |
1392 |
|
|
1393 |
/** @brief Dynamically executed variable (abstract base class). |
/** @brief Dynamically executed variable (abstract base class). |
1394 |
* |
* |
1395 |
* Interface for the implementation of a dynamically generated content of |
* Interface for the implementation of a dynamically generated content of |
1463 |
*/ |
*/ |
1464 |
class VMDynIntVar : virtual public VMDynVar, virtual public VMIntExpr { |
class VMDynIntVar : virtual public VMDynVar, virtual public VMIntExpr { |
1465 |
public: |
public: |
1466 |
MetricPrefix_t unitPrefix(vmuint i) const OVERRIDE { return VM_NO_PREFIX; } |
vmfloat unitFactor() const OVERRIDE { return VM_NO_FACTOR; } |
1467 |
StdUnit_t unitType() const OVERRIDE { return VM_NO_UNIT; } |
StdUnit_t unitType() const OVERRIDE { return VM_NO_UNIT; } |
1468 |
bool isFinal() const OVERRIDE { return false; } |
bool isFinal() const OVERRIDE { return false; } |
1469 |
}; |
}; |
1745 |
case EMPTY_EXPR: return "empty"; |
case EMPTY_EXPR: return "empty"; |
1746 |
case INT_EXPR: return "integer"; |
case INT_EXPR: return "integer"; |
1747 |
case INT_ARR_EXPR: return "integer array"; |
case INT_ARR_EXPR: return "integer array"; |
1748 |
|
case REAL_EXPR: return "real number"; |
1749 |
|
case REAL_ARR_EXPR: return "real number array"; |
1750 |
case STRING_EXPR: return "string"; |
case STRING_EXPR: return "string"; |
1751 |
case STRING_ARR_EXPR: return "string array"; |
case STRING_ARR_EXPR: return "string array"; |
1752 |
} |
} |
1754 |
} |
} |
1755 |
|
|
1756 |
/** |
/** |
1757 |
|
* Returns @c true in case the passed data type is some array data type. |
1758 |
|
*/ |
1759 |
|
inline bool isArray(const ExprType_t& type) { |
1760 |
|
return type == INT_ARR_EXPR || type == REAL_ARR_EXPR || |
1761 |
|
type == STRING_ARR_EXPR; |
1762 |
|
} |
1763 |
|
|
1764 |
|
/** |
1765 |
|
* Returns @c true in case the passed data type is some scalar number type |
1766 |
|
* (i.e. not an array and not a string). |
1767 |
|
*/ |
1768 |
|
inline bool isNumber(const ExprType_t& type) { |
1769 |
|
return type == INT_EXPR || type == REAL_EXPR; |
1770 |
|
} |
1771 |
|
|
1772 |
|
/** |
1773 |
* Convenience function used for converting an StdUnit_t constant to a |
* Convenience function used for converting an StdUnit_t constant to a |
1774 |
* string, i.e. for generating error message by the parser. |
* string, i.e. for generating error message by the parser. |
1775 |
*/ |
*/ |
1888 |
|
|
1889 |
// extended types |
// extended types |
1890 |
bool isIntegerVariable() const; ///< Returns true in case this source token represents an integer variable name (i.e. "$someIntVariable"). |
bool isIntegerVariable() const; ///< Returns true in case this source token represents an integer variable name (i.e. "$someIntVariable"). |
1891 |
|
bool isRealVariable() const; ///< Returns true in case this source token represents a floating point variable name (i.e. "~someRealVariable"). |
1892 |
bool isStringVariable() const; ///< Returns true in case this source token represents an string variable name (i.e. "\@someStringVariable"). |
bool isStringVariable() const; ///< Returns true in case this source token represents an string variable name (i.e. "\@someStringVariable"). |
1893 |
bool isArrayVariable() const; ///< Returns true in case this source token represents an array variable name (i.e. "%someArryVariable"). |
bool isIntArrayVariable() const; ///< Returns true in case this source token represents an integer array variable name (i.e. "%someArrayVariable"). |
1894 |
|
bool isRealArrayVariable() const; ///< Returns true in case this source token represents a real number array variable name (i.e. "?someArrayVariable"). |
1895 |
|
bool isArrayVariable() const DEPRECATED_API; ///< Returns true in case this source token represents an @b integer array variable name (i.e. "%someArrayVariable"). @deprecated This method will be removed, use isIntArrayVariable() instead. |
1896 |
bool isEventHandlerName() const; ///< Returns true in case this source token represents an event handler name (i.e. "note", "release", "controller"). |
bool isEventHandlerName() const; ///< Returns true in case this source token represents an event handler name (i.e. "note", "release", "controller"). |
1897 |
|
|
1898 |
VMSourceToken& operator=(const VMSourceToken& other); |
VMSourceToken& operator=(const VMSourceToken& other); |