forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
315 lines (270 loc) · 8.12 KB
/
engine.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <chrono>
#include <thread>
#include <xmmintrin.h>
#include <pmmintrin.h>
#include "engine.hpp"
namespace rack {
bool gPaused = false;
std::vector<Module*> gModules;
std::vector<Wire*> gWires;
bool gPowerMeter = false;
static bool running = false;
static float sampleRate = 44100.f;
static float sampleTime = 1.f / sampleRate;
static float sampleRateRequested = sampleRate;
static Module *resetModule = NULL;
static Module *randomizeModule = NULL;
static std::mutex mutex;
static std::thread thread;
static VIPMutex vipMutex;
// Parameter interpolation
static Module *smoothModule = NULL;
static int smoothParamId;
static float smoothValue;
float Light::getBrightness() {
// LEDs are diodes, so don't allow reverse current.
// For some reason, instead of the RMS, the sqrt of RMS looks better
return powf(fmaxf(0.f, value), 0.25f);
}
void Light::setBrightnessSmooth(float brightness, float frames) {
float v = (brightness > 0.f) ? brightness * brightness : 0.f;
if (v < value) {
// Fade out light with lambda = framerate
value += (v - value) * sampleTime * frames * 60.f;
}
else {
// Immediately illuminate light
value = v;
}
}
void Wire::step() {
float value = outputModule->outputs[outputId].value;
inputModule->inputs[inputId].value = value;
}
void engineInit() {
}
void engineDestroy() {
// Make sure there are no wires or modules in the rack on destruction. This suggests that a module failed to remove itself before the RackWidget was destroyed.
assert(gWires.empty());
assert(gModules.empty());
}
static void engineStep() {
// Sample rate
if (sampleRateRequested != sampleRate) {
sampleRate = sampleRateRequested;
sampleTime = 1.f / sampleRate;
for (Module *module : gModules) {
module->onSampleRateChange();
}
}
// Events
if (resetModule) {
resetModule->onReset();
resetModule = NULL;
}
if (randomizeModule) {
randomizeModule->onRandomize();
randomizeModule = NULL;
}
// Param smoothing
{
Module *localSmoothModule = smoothModule;
int localSmoothParamId = smoothParamId;
float localSmoothValue = smoothValue;
if (localSmoothModule) {
float value = localSmoothModule->params[localSmoothParamId].value;
const float lambda = 60.0; // decay rate is 1 graphics frame
float delta = localSmoothValue - value;
float newValue = value + delta * lambda * sampleTime;
if (value == newValue) {
// Snap to actual smooth value if the value doesn't change enough (due to the granularity of floats)
localSmoothModule->params[localSmoothParamId].value = localSmoothValue;
smoothModule = NULL;
}
else {
localSmoothModule->params[localSmoothParamId].value = newValue;
}
}
}
// Step modules
for (Module *module : gModules) {
std::chrono::high_resolution_clock::time_point startTime;
if (gPowerMeter) {
startTime = std::chrono::high_resolution_clock::now();
module->step();
auto stopTime = std::chrono::high_resolution_clock::now();
float cpuTime = std::chrono::duration<float>(stopTime - startTime).count() * sampleRate;
module->cpuTime += (cpuTime - module->cpuTime) * sampleTime / 0.5f;
}
else {
module->step();
}
// Step ports
for (Input &input : module->inputs) {
if (input.active) {
float value = input.value / 5.f;
input.plugLights[0].setBrightnessSmooth(value);
input.plugLights[1].setBrightnessSmooth(-value);
}
}
for (Output &output : module->outputs) {
if (output.active) {
float value = output.value / 5.f;
output.plugLights[0].setBrightnessSmooth(value);
output.plugLights[1].setBrightnessSmooth(-value);
}
}
}
// Step cables by moving their output values to inputs
for (Wire *wire : gWires) {
wire->step();
}
}
static void engineRun() {
// Set CPU to flush-to-zero (FTZ) and denormals-are-zero (DAZ) mode
// https://software.intel.com/en-us/node/682949
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
// Every time the engine waits and locks a mutex, it steps this many frames
const int mutexSteps = 64;
// Time in seconds that the engine is rushing ahead of the estimated clock time
double ahead = 0.0;
auto lastTime = std::chrono::high_resolution_clock::now();
while (running) {
vipMutex.wait();
if (!gPaused) {
std::lock_guard<std::mutex> lock(mutex);
for (int i = 0; i < mutexSteps; i++) {
engineStep();
}
}
double stepTime = mutexSteps * sampleTime;
ahead += stepTime;
auto currTime = std::chrono::high_resolution_clock::now();
const double aheadFactor = 2.0;
ahead -= aheadFactor * std::chrono::duration<double>(currTime - lastTime).count();
lastTime = currTime;
ahead = fmaxf(ahead, 0.0);
// Avoid pegging the CPU at 100% when there are no "blocking" modules like AudioInterface, but still step audio at a reasonable rate
// The number of steps to wait before possibly sleeping
const double aheadMax = 1.0; // seconds
if (ahead > aheadMax) {
std::this_thread::sleep_for(std::chrono::duration<double>(stepTime));
}
}
}
void engineStart() {
running = true;
thread = std::thread(engineRun);
}
void engineStop() {
running = false;
thread.join();
}
void engineAddModule(Module *module) {
assert(module);
VIPLock vipLock(vipMutex);
std::lock_guard<std::mutex> lock(mutex);
// Check that the module is not already added
auto it = std::find(gModules.begin(), gModules.end(), module);
assert(it == gModules.end());
gModules.push_back(module);
}
void engineRemoveModule(Module *module) {
assert(module);
VIPLock vipLock(vipMutex);
std::lock_guard<std::mutex> lock(mutex);
// If a param is being smoothed on this module, stop smoothing it immediately
if (module == smoothModule) {
smoothModule = NULL;
}
// Check that all wires are disconnected
for (Wire *wire : gWires) {
assert(wire->outputModule != module);
assert(wire->inputModule != module);
}
// Check that the module actually exists
auto it = std::find(gModules.begin(), gModules.end(), module);
assert(it != gModules.end());
// Remove it
gModules.erase(it);
}
void engineResetModule(Module *module) {
resetModule = module;
}
void engineRandomizeModule(Module *module) {
randomizeModule = module;
}
static void updateActive() {
// Set everything to inactive
for (Module *module : gModules) {
for (Input &input : module->inputs) {
input.active = false;
}
for (Output &output : module->outputs) {
output.active = false;
}
}
// Set inputs/outputs to active
for (Wire *wire : gWires) {
wire->outputModule->outputs[wire->outputId].active = true;
wire->inputModule->inputs[wire->inputId].active = true;
}
}
void engineAddWire(Wire *wire) {
assert(wire);
VIPLock vipLock(vipMutex);
std::lock_guard<std::mutex> lock(mutex);
// Check wire properties
assert(wire->outputModule);
assert(wire->inputModule);
// Check that the wire is not already added, and that the input is not already used by another cable
for (Wire *wire2 : gWires) {
assert(wire2 != wire);
assert(!(wire2->inputModule == wire->inputModule && wire2->inputId == wire->inputId));
}
// Add the wire
gWires.push_back(wire);
updateActive();
}
void engineRemoveWire(Wire *wire) {
assert(wire);
VIPLock vipLock(vipMutex);
std::lock_guard<std::mutex> lock(mutex);
// Check that the wire is already added
auto it = std::find(gWires.begin(), gWires.end(), wire);
assert(it != gWires.end());
// Set input to 0V
wire->inputModule->inputs[wire->inputId].value = 0.0;
// Remove the wire
gWires.erase(it);
updateActive();
}
void engineSetParam(Module *module, int paramId, float value) {
module->params[paramId].value = value;
}
void engineSetParamSmooth(Module *module, int paramId, float value) {
// If another param is being smoothed, jump value
if (smoothModule && !(smoothModule == module && smoothParamId == paramId)) {
smoothModule->params[smoothParamId].value = smoothValue;
}
smoothParamId = paramId;
smoothValue = value;
smoothModule = module;
}
void engineSetSampleRate(float newSampleRate) {
sampleRateRequested = newSampleRate;
}
float engineGetSampleRate() {
return sampleRate;
}
float engineGetSampleTime() {
return sampleTime;
}
} // namespace rack