/[svn]/linuxsampler/trunk/src/scriptvm/common.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/scriptvm/common.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3561 - (show annotations) (download)
Fri Aug 23 11:44:00 2019 UTC (4 years, 9 months ago) by schoenebeck
File size: 5896 byte(s)
NKSP: Added standard units support for numbers and final "!" operator:

* NKSP strictness: Variable names, function names and preprocessor condition
  names must start with a regular character (a-z or A-Z); starting them with
  a digit or underscore is no longer allowed.

* NKSP parser fix: equal comparison operator "=" and not equal comparison
  operator "#" must only accept integer operands.

* NKSP language: Implemented support for standard units like Hertz, seconds,
  Bel including support for metric unit prefixes; so one can now e.g.
  conveniently use numbers in scripts like "5us" meaning "5 microseconds",
  or e.g. "12kHz" meaning "12 kilo Hertz", or e.g. "-14mdB" meaning
  "minus 14 Millidecibel", or e.g. "28c" meaning "28 cents" (for tuning).

* NKSP language: Introduced "final" operator "!" which is specifically
  intended for synthesis parameter values to denote that the synthesis
  parameter value is intended to be the "final" value for that synthesis
  parameter that should explicitly be used by the engine and thus causing
  the sampler engine to ignore all other modulation sources for the same
  synthesis parameter (like e.g. LFO, EG); by simply prefixing a value,
  variable or formula with this new "!" operator the expression is marked as
  being "final".

* Bumped version (2.1.1.svn4).

1 /*
2 * Copyright (c) 2014-2019 Christian Schoenebeck
3 *
4 * http://www.linuxsampler.org
5 *
6 * This file is part of LinuxSampler and released under the same terms.
7 * See README file for details.
8 */
9
10 #include "common.h"
11 #include <iostream>
12 #include "editor/SourceToken.h"
13
14 namespace LinuxSampler {
15
16 ///////////////////////////////////////////////////////////////////////
17 // class 'VMUnit'
18
19 static vmfloat _unitFactor(MetricPrefix_t prefix) {
20 switch (prefix) {
21 case VM_NO_PREFIX: return 1.f;
22 case VM_KILO: return 1000.f;
23 case VM_HECTO: return 100.f;
24 case VM_DECA: return 10.f;
25 case VM_DECI: return 0.1f;
26 case VM_CENTI: return 0.01f;
27 case VM_MILLI: return 0.001f;
28 case VM_MICRO: return 0.000001f;
29 }
30 return 1.f;
31 }
32
33 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix) {
34 return _unitFactor(prefix);
35 }
36
37 vmfloat VMUnit::unitFactor(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
38 return _unitFactor(prefix1) * _unitFactor(prefix2);
39 }
40
41 vmfloat VMUnit::unitFactor() const {
42 vmfloat f = 1.f;
43 for (vmuint i = 0; unitPrefix(i); ++i)
44 f *= _unitFactor(unitPrefix(i));
45 return f;
46 }
47
48 ///////////////////////////////////////////////////////////////////////
49 // class 'VMExpr'
50
51 VMIntExpr* VMExpr::asInt() const {
52 return const_cast<VMIntExpr*>( dynamic_cast<const VMIntExpr*>(this) );
53 }
54
55 VMStringExpr* VMExpr::asString() const {
56 return const_cast<VMStringExpr*>( dynamic_cast<const VMStringExpr*>(this) );
57 }
58
59 VMIntArrayExpr* VMExpr::asIntArray() const {
60 return const_cast<VMIntArrayExpr*>( dynamic_cast<const VMIntArrayExpr*>(this) );
61 }
62
63 bool VMExpr::isModifyable() const {
64 const VMVariable* var = dynamic_cast<const VMVariable*>(this);
65 return (!var) ? false : var->isAssignable();
66 }
67
68 bool VMFunction::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
69 return type == VM_NO_UNIT;
70 }
71
72 bool VMFunction::acceptsArgUnitPrefix(vmint iArg) const {
73 return false;
74 }
75
76 bool VMFunction::acceptsArgFinal(vmint iArg) const {
77 return false;
78 }
79
80 void VMFunction::wrnMsg(const String& txt) {
81 std::cout << "[ScriptVM] " << txt << std::endl;
82 }
83
84 void VMFunction::errMsg(const String& txt) {
85 std::cerr << "[ScriptVM] " << txt << std::endl;
86 }
87
88 ///////////////////////////////////////////////////////////////////////
89 // class 'VMIntExpr'
90
91 vmint VMIntExpr::evalInt(MetricPrefix_t prefix) {
92 vmfloat f = (vmfloat) evalInt();
93 vmfloat factor = unitFactor() / _unitFactor(prefix);
94 return (vmint) f * factor;
95 }
96
97 vmint VMIntExpr::evalInt(MetricPrefix_t prefix1, MetricPrefix_t prefix2) {
98 vmfloat f = (vmfloat) evalInt();
99 vmfloat factor = unitFactor() /
100 ( _unitFactor(prefix1) * _unitFactor(prefix2) );
101 return (vmint) f * factor;
102 }
103
104 ///////////////////////////////////////////////////////////////////////
105 // class 'VMSourceToken'
106
107 VMSourceToken::VMSourceToken() : m_token(NULL) {
108 }
109
110 VMSourceToken::VMSourceToken(SourceToken* ct) : m_token(ct) {
111 }
112
113 VMSourceToken::VMSourceToken(const VMSourceToken& other) {
114 if (other.m_token) {
115 m_token = new SourceToken;
116 *m_token = *other.m_token;
117 } else m_token = NULL;
118 }
119
120 VMSourceToken::~VMSourceToken() {
121 if (m_token) {
122 delete m_token;
123 m_token = NULL;
124 }
125 }
126
127 VMSourceToken& VMSourceToken::operator=(const VMSourceToken& other) {
128 if (m_token) delete m_token;
129 if (other.m_token) {
130 m_token = new SourceToken;
131 *m_token = *other.m_token;
132 } else m_token = NULL;
133 return *this;
134 }
135
136 String VMSourceToken::text() const {
137 return (m_token) ? m_token->text() : "";
138 }
139
140 int VMSourceToken::firstLine() const {
141 return (m_token) ? m_token->firstLine() : 0;
142 }
143
144 int VMSourceToken::firstColumn() const {
145 return (m_token) ? m_token->firstColumn() : 0;
146 }
147
148 bool VMSourceToken::isEOF() const {
149 return (m_token) ? m_token->isEOF() : true;
150 }
151
152 bool VMSourceToken::isNewLine() const {
153 return (m_token) ? m_token->isNewLine() : false;
154 }
155
156 bool VMSourceToken::isKeyword() const {
157 return (m_token) ? m_token->isKeyword() : false;
158 }
159
160 bool VMSourceToken::isVariableName() const {
161 return (m_token) ? m_token->isVariableName() : false;
162 }
163
164 bool VMSourceToken::isIdentifier() const {
165 return (m_token) ? m_token->isIdentifier() : false;
166 }
167
168 bool VMSourceToken::isNumberLiteral() const {
169 return (m_token) ? m_token->isNumberLiteral() : false;
170 }
171
172 bool VMSourceToken::isStringLiteral() const {
173 return (m_token) ? m_token->isStringLiteral() : false;
174 }
175
176 bool VMSourceToken::isComment() const {
177 return (m_token) ? m_token->isComment() : false;
178 }
179
180 bool VMSourceToken::isPreprocessor() const {
181 return (m_token) ? m_token->isPreprocessor() : false;
182 }
183
184 bool VMSourceToken::isOther() const {
185 return (m_token) ? m_token->isOther() : true;
186 }
187
188 bool VMSourceToken::isIntegerVariable() const {
189 return (m_token) ? m_token->isIntegerVariable() : false;
190 }
191
192 bool VMSourceToken::isStringVariable() const {
193 return (m_token) ? m_token->isStringVariable() : false;
194 }
195
196 bool VMSourceToken::isArrayVariable() const {
197 return (m_token) ? m_token->isArrayVariable() : false;
198 }
199
200 bool VMSourceToken::isEventHandlerName() const {
201 return (m_token) ? m_token->isEventHandlerName() : false;
202 }
203
204 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC