chris@160
|
1 /*
|
chris@160
|
2 * render.cpp
|
chris@160
|
3 *
|
chris@160
|
4 * Template render.cpp file for on-board heavy compiling
|
chris@160
|
5 *
|
chris@160
|
6 * N.B. this is currently *not* compatible with foleyDesigner source files!
|
chris@160
|
7 *
|
chris@160
|
8 * Created on: November 5, 2015
|
chris@160
|
9 *
|
chris@160
|
10 * Christian Heinrichs
|
chris@160
|
11 *
|
chris@160
|
12 */
|
chris@160
|
13
|
giuliomoro@329
|
14 #include <Bela.h>
|
giuliomoro@198
|
15 #include <Midi.h>
|
giuliomoro@482
|
16 #include <Scope.h>
|
chris@160
|
17 #include <cmath>
|
giuliomoro@482
|
18 #include <Heavy_bbb.h>
|
chris@190
|
19 #include <string.h>
|
chris@190
|
20 #include <stdlib.h>
|
chris@190
|
21 #include <string.h>
|
giuliomoro@480
|
22 #include <DigitalChannelManager.h>
|
giuliomoro@480
|
23
|
chris@160
|
24 /*
|
chris@160
|
25 * HEAVY CONTEXT & BUFFERS
|
chris@160
|
26 */
|
chris@160
|
27
|
chris@160
|
28 Hv_bbb *gHeavyContext;
|
chris@160
|
29 float *gHvInputBuffers = NULL, *gHvOutputBuffers = NULL;
|
giuliomoro@480
|
30 unsigned int gHvInputChannels = 0, gHvOutputChannels = 0;
|
chris@160
|
31
|
chris@160
|
32 float gInverseSampleRate;
|
chris@160
|
33
|
chris@160
|
34 /*
|
chris@160
|
35 * HEAVY FUNCTIONS
|
chris@160
|
36 */
|
chris@160
|
37
|
giuliomoro@480
|
38 // TODO: rename this
|
giuliomoro@480
|
39 #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
|
giuliomoro@480
|
40
|
chris@160
|
41 void printHook(double timestampSecs, const char *printLabel, const char *msgString, void *userData) {
|
giuliomoro@480
|
42 rt_printf("Message from Heavy patch: [@ %.3f] %s: %s\n", timestampSecs, printLabel, msgString);
|
chris@160
|
43 }
|
chris@160
|
44
|
giuliomoro@480
|
45
|
giuliomoro@480
|
46 // digitals
|
giuliomoro@480
|
47 static DigitalChannelManager dcm;
|
giuliomoro@480
|
48
|
giuliomoro@480
|
49 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
|
giuliomoro@480
|
50 hv_sendFloatToReceiver(gHeavyContext, hv_stringToHash((char*)receiverName), (float)state);
|
giuliomoro@480
|
51 // rt_printf("%s: %d\n", (char*)receiverName, state);
|
giuliomoro@480
|
52 }
|
giuliomoro@480
|
53
|
giuliomoro@480
|
54 // TODO: turn them into hv hashes and adjust sendDigitalMessage accordingly
|
giuliomoro@480
|
55 char hvDigitalInHashes[16][21]={
|
giuliomoro@480
|
56 {"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
|
giuliomoro@480
|
57 {"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
|
giuliomoro@480
|
58 {"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
|
giuliomoro@480
|
59 {"bela_digitalIn26"}
|
giuliomoro@480
|
60 };
|
giuliomoro@480
|
61
|
chris@160
|
62 static void sendHook(
|
giuliomoro@480
|
63 double timestamp, // in milliseconds
|
giuliomoro@480
|
64 const char *receiverName,
|
giuliomoro@480
|
65 const HvMessage *const m,
|
giuliomoro@480
|
66 void *userData) {
|
chris@160
|
67
|
giuliomoro@480
|
68 // Bela digital
|
giuliomoro@480
|
69
|
giuliomoro@480
|
70 // Bela digital run-time messages
|
chris@160
|
71
|
giuliomoro@480
|
72 // TODO: this first block is almost an exact copy of libpd's code, should we add this to the class?
|
giuliomoro@480
|
73 // let's make this as optimized as possible for built-in digital Out parsing
|
giuliomoro@480
|
74 // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
|
giuliomoro@480
|
75 static int prefixLength = 15; // strlen("bela_digitalOut")
|
giuliomoro@480
|
76 if(strncmp(receiverName, "bela_digitalOut", prefixLength)==0){
|
giuliomoro@480
|
77 if(receiverName[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
|
giuliomoro@480
|
78 if(receiverName[prefixLength + 1] != 0){
|
giuliomoro@480
|
79 // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
|
giuliomoro@480
|
80 int receiver = ((receiverName[prefixLength] - 48) * 10);
|
giuliomoro@480
|
81 receiver += (receiverName[prefixLength+1] - 48);
|
giuliomoro@480
|
82 unsigned int channel = receiver - LIBPD_DIGITAL_OFFSET; // go back to the actual Bela digital channel number
|
giuliomoro@480
|
83 bool value = hv_msg_getFloat(m, 0);
|
giuliomoro@480
|
84 if(channel < 16){ //16 is the hardcoded value for the number of digital channels
|
giuliomoro@480
|
85 dcm.setValue(channel, value);
|
giuliomoro@480
|
86 }
|
giuliomoro@480
|
87 }
|
giuliomoro@480
|
88 }
|
giuliomoro@480
|
89 }
|
giuliomoro@480
|
90
|
giuliomoro@480
|
91 // Bela digital initialization messages
|
giuliomoro@480
|
92 if(strcmp(receiverName, "bela_setDigital") == 0){
|
giuliomoro@480
|
93 // Third argument (optional) can be ~ or sig for signal-rate, message-rate otherwise.
|
giuliomoro@480
|
94 // [in 14 ~(
|
giuliomoro@480
|
95 // |
|
giuliomoro@480
|
96 // [s bela_setDigital]
|
giuliomoro@480
|
97 // is signal("sig" or "~") or message("message", default) rate
|
giuliomoro@480
|
98 bool isMessageRate = true; // defaults to message rate
|
giuliomoro@480
|
99 bool direction = 0; // initialize it just to avoid the compiler's warning
|
giuliomoro@480
|
100 bool disable = false;
|
giuliomoro@480
|
101 int numArgs = hv_msg_getNumElements(m);
|
giuliomoro@480
|
102 if(numArgs < 2 || numArgs > 3 || !hv_msg_isSymbol(m, 0) || !hv_msg_isFloat(m, 1))
|
giuliomoro@480
|
103 return;
|
giuliomoro@480
|
104 if(numArgs == 3 && !hv_msg_isSymbol(m,2))
|
giuliomoro@480
|
105 return;
|
giuliomoro@480
|
106 char * symbol = hv_msg_getSymbol(m, 0);
|
giuliomoro@480
|
107
|
giuliomoro@480
|
108 if(strcmp(symbol, "in") == 0){
|
giuliomoro@480
|
109 direction = INPUT;
|
giuliomoro@480
|
110 } else if(strcmp(symbol, "out") == 0){
|
giuliomoro@480
|
111 direction = OUTPUT;
|
giuliomoro@480
|
112 } else if(strcmp(symbol, "disable") == 0){
|
giuliomoro@480
|
113 disable = true;
|
giuliomoro@480
|
114 } else {
|
giuliomoro@480
|
115 return;
|
giuliomoro@480
|
116 }
|
giuliomoro@480
|
117 int channel = hv_msg_getFloat(m, 1) - LIBPD_DIGITAL_OFFSET;
|
giuliomoro@480
|
118 if(disable == true){
|
giuliomoro@480
|
119 dcm.unmanage(channel);
|
giuliomoro@480
|
120 return;
|
giuliomoro@480
|
121 }
|
giuliomoro@480
|
122 if(numArgs >= 3){
|
giuliomoro@480
|
123 char* s = hv_msg_getSymbol(m, 2);
|
giuliomoro@480
|
124 if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
|
giuliomoro@480
|
125 isMessageRate = false;
|
giuliomoro@480
|
126 }
|
giuliomoro@480
|
127 }
|
giuliomoro@480
|
128 dcm.manage(channel, direction, isMessageRate);
|
giuliomoro@480
|
129 }
|
chris@160
|
130 }
|
chris@160
|
131
|
giuliomoro@480
|
132
|
chris@160
|
133 /*
|
chris@166
|
134 * SETUP, RENDER LOOP & CLEANUP
|
chris@160
|
135 */
|
chris@160
|
136
|
giuliomoro@482
|
137 // 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
|
giuliomoro@482
|
138 static const unsigned int gChannelsInUse = 30;
|
giuliomoro@482
|
139 static unsigned int gAnalogChannelsInUse = 8; // hard-coded for the moment, TODO: get it at run-time from hv_context
|
giuliomoro@482
|
140 //static const unsigned int gFirstAudioChannel = 0;
|
giuliomoro@482
|
141 static const unsigned int gFirstAnalogChannel = 2;
|
giuliomoro@482
|
142 static const unsigned int gFirstDigitalChannel = 10;
|
giuliomoro@482
|
143 static const unsigned int gFirstScopeChannel = 26;
|
giuliomoro@482
|
144 static unsigned int gDigitalSigInChannelsInUse;
|
giuliomoro@482
|
145 static unsigned int gDigitalSigOutChannelsInUse;
|
giuliomoro@480
|
146
|
giuliomoro@482
|
147 // Bela Midi
|
giuliomoro@198
|
148 Midi midi;
|
giuliomoro@480
|
149 unsigned int hvMidiHashes[7];
|
giuliomoro@482
|
150 // Bela Scope
|
giuliomoro@482
|
151 Scope scope;
|
giuliomoro@482
|
152 const unsigned int gMaxScopeChannels = 4;
|
giuliomoro@482
|
153 unsigned int gScopeChannelsInUse;
|
giuliomoro@482
|
154 float* gScopeOut;
|
giuliomoro@480
|
155
|
giuliomoro@480
|
156
|
giuliomoro@329
|
157 bool setup(BelaContext *context, void *userData) {
|
chris@160
|
158 /* HEAVY */
|
giuliomoro@480
|
159 hvMidiHashes[kmmNoteOn] = hv_stringToHash("__hv_notein");
|
giuliomoro@480
|
160 hvMidiHashes[kmmNoteOff] = hv_stringToHash("noteoff"); // this is handled differently, see the render function
|
giuliomoro@480
|
161 hvMidiHashes[kmmControlChange] = hv_stringToHash("__hv_ctlin");
|
giuliomoro@480
|
162 hvMidiHashes[kmmProgramChange] = hv_stringToHash("pgmin");
|
giuliomoro@480
|
163 hvMidiHashes[kmmPolyphonicKeyPressure] = hv_stringToHash("polytouchin");
|
giuliomoro@480
|
164 hvMidiHashes[kmmChannelPressure] = hv_stringToHash("touchin");
|
giuliomoro@480
|
165 hvMidiHashes[kmmPitchBend] = hv_stringToHash("bendin");
|
chris@160
|
166
|
chris@160
|
167 gHeavyContext = hv_bbb_new(context->audioSampleRate);
|
chris@160
|
168
|
chris@160
|
169 gHvInputChannels = hv_getNumInputChannels(gHeavyContext);
|
chris@160
|
170 gHvOutputChannels = hv_getNumOutputChannels(gHeavyContext);
|
chris@160
|
171
|
giuliomoro@482
|
172 gScopeChannelsInUse = gHvOutputChannels > gFirstScopeChannel ?
|
giuliomoro@482
|
173 gHvOutputChannels - gFirstScopeChannel : 0;
|
giuliomoro@482
|
174 gDigitalSigInChannelsInUse = gHvInputChannels > gFirstDigitalChannel ?
|
giuliomoro@482
|
175 gHvInputChannels - gFirstDigitalChannel : 0;
|
giuliomoro@482
|
176 gDigitalSigOutChannelsInUse = gHvOutputChannels > gFirstDigitalChannel ?
|
giuliomoro@482
|
177 gHvOutputChannels - gFirstDigitalChannel - gScopeChannelsInUse: 0;
|
giuliomoro@482
|
178
|
giuliomoro@482
|
179 printf("Starting Heavy context with %d input channels and %d output channels\n",
|
chris@160
|
180 gHvInputChannels, gHvOutputChannels);
|
giuliomoro@482
|
181 printf("Channels in use:\n");
|
giuliomoro@482
|
182 printf("Digital in : %u, Digital out: %u\n", gDigitalSigInChannelsInUse, gDigitalSigOutChannelsInUse);
|
giuliomoro@482
|
183 printf("Scope out: %u\n", gScopeChannelsInUse);
|
chris@160
|
184
|
chris@160
|
185 if(gHvInputChannels != 0) {
|
chris@160
|
186 gHvInputBuffers = (float *)calloc(gHvInputChannels * context->audioFrames,sizeof(float));
|
chris@160
|
187 }
|
chris@160
|
188 if(gHvOutputChannels != 0) {
|
chris@160
|
189 gHvOutputBuffers = (float *)calloc(gHvOutputChannels * context->audioFrames,sizeof(float));
|
chris@160
|
190 }
|
chris@160
|
191
|
chris@160
|
192 gInverseSampleRate = 1.0 / context->audioSampleRate;
|
chris@160
|
193
|
chris@160
|
194 // Set heavy print hook
|
giuliomoro@480
|
195 hv_setPrintHook(gHeavyContext, printHook);
|
chris@160
|
196 // Set heavy send hook
|
chris@160
|
197 hv_setSendHook(gHeavyContext, sendHook);
|
chris@160
|
198
|
giuliomoro@480
|
199 // TODO: change these hardcoded port values and actually change them in the Midi class
|
giuliomoro@198
|
200 midi.readFrom(0);
|
giuliomoro@198
|
201 midi.writeTo(0);
|
giuliomoro@198
|
202 midi.enableParser(true);
|
giuliomoro@480
|
203
|
giuliomoro@482
|
204 if(gScopeChannelsInUse > 0){
|
giuliomoro@482
|
205 // block below copy/pasted from libpd, except
|
giuliomoro@482
|
206 scope.setup(gScopeChannelsInUse, context->audioSampleRate);
|
giuliomoro@482
|
207 gScopeOut = new float[gScopeChannelsInUse];
|
giuliomoro@482
|
208 }
|
giuliomoro@480
|
209 // Bela digital
|
giuliomoro@480
|
210 dcm.setCallback(sendDigitalMessage);
|
giuliomoro@480
|
211 if(context->digitalChannels > 0){
|
giuliomoro@480
|
212 for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
|
giuliomoro@480
|
213 dcm.setCallbackArgument(ch, hvDigitalInHashes[ch]);
|
giuliomoro@480
|
214 }
|
giuliomoro@480
|
215 }
|
giuliomoro@480
|
216 // unlike libpd, no need here to bind the bela_digitalOut.. receivers
|
giuliomoro@480
|
217
|
chris@160
|
218 return true;
|
chris@160
|
219 }
|
chris@160
|
220
|
chris@160
|
221
|
giuliomoro@329
|
222 void render(BelaContext *context, void *userData)
|
chris@160
|
223 {
|
giuliomoro@480
|
224 {
|
giuliomoro@480
|
225 int num;
|
giuliomoro@480
|
226 while((num = midi.getParser()->numAvailableMessages()) > 0){
|
giuliomoro@480
|
227 static MidiChannelMessage message;
|
giuliomoro@480
|
228 message = midi.getParser()->getNextChannelMessage();
|
giuliomoro@480
|
229 switch(message.getType()){
|
giuliomoro@480
|
230 case kmmNoteOn: {
|
giuliomoro@480
|
231 // message.prettyPrint();
|
giuliomoro@480
|
232 float noteNumber = message.getDataByte(0);
|
giuliomoro@480
|
233 float velocity = message.getDataByte(1);
|
giuliomoro@480
|
234 float channel = message.getChannel();
|
giuliomoro@480
|
235 // rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel);
|
giuliomoro@480
|
236 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff", noteNumber, velocity, channel);
|
giuliomoro@480
|
237 break;
|
giuliomoro@480
|
238 }
|
giuliomoro@480
|
239 case kmmNoteOff:
|
giuliomoro@480
|
240 {
|
giuliomoro@480
|
241 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
|
giuliomoro@480
|
242 * so that the noteoff velocity is ignored. Here we convert them to noteon
|
giuliomoro@480
|
243 * with a velocity of 0.
|
giuliomoro@480
|
244 */
|
giuliomoro@480
|
245 float noteNumber = message.getDataByte(0);
|
giuliomoro@480
|
246 // int velocity = message.getDataByte(1); // would be ignored by Pd
|
giuliomoro@480
|
247 float channel = message.getChannel();
|
giuliomoro@480
|
248 // note we are sending the below to hvHashes[kmmNoteOn] !!
|
giuliomoro@480
|
249 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmNoteOn], 0, "fff", noteNumber, 0, channel);
|
giuliomoro@480
|
250 break;
|
giuliomoro@480
|
251 }
|
giuliomoro@480
|
252 case kmmControlChange: {
|
giuliomoro@480
|
253 int channel = message.getChannel();
|
giuliomoro@480
|
254 int controller = message.getDataByte(0);
|
giuliomoro@480
|
255 int value = message.getDataByte(1);
|
giuliomoro@480
|
256 //TODO: maybe the order of the arguments is wrong here?
|
giuliomoro@480
|
257 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmControlChange], 0, "fff",
|
giuliomoro@480
|
258 value, controller, channel);
|
giuliomoro@480
|
259 break;
|
giuliomoro@480
|
260 }
|
giuliomoro@480
|
261 case kmmProgramChange: {
|
giuliomoro@480
|
262 int channel = message.getChannel();
|
giuliomoro@480
|
263 int program = message.getDataByte(0);
|
giuliomoro@480
|
264 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmProgramChange], 0, "ff",
|
giuliomoro@480
|
265 program, channel);
|
giuliomoro@480
|
266 //TODO: maybe the order of the arguments is wrong here?
|
giuliomoro@480
|
267 break;
|
giuliomoro@480
|
268 }
|
giuliomoro@480
|
269 case kmmPolyphonicKeyPressure: {
|
giuliomoro@480
|
270 int channel = message.getChannel();
|
giuliomoro@480
|
271 int pitch = message.getDataByte(0);
|
giuliomoro@480
|
272 int value = message.getDataByte(1);
|
giuliomoro@480
|
273 //TODO: maybe the order of the arguments is wrong here?
|
giuliomoro@480
|
274 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPolyphonicKeyPressure], 0, "fff",
|
giuliomoro@480
|
275 channel, pitch, value);
|
giuliomoro@480
|
276 break;
|
giuliomoro@480
|
277 }
|
giuliomoro@480
|
278 case kmmChannelPressure:
|
giuliomoro@480
|
279 {
|
giuliomoro@480
|
280 int channel = message.getChannel();
|
giuliomoro@480
|
281 int value = message.getDataByte(0);
|
giuliomoro@480
|
282 //TODO: maybe the order of the arguments is wrong here?
|
giuliomoro@480
|
283 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmChannelPressure], 0, "ff",
|
giuliomoro@480
|
284 channel, value);
|
giuliomoro@480
|
285 break;
|
giuliomoro@480
|
286 }
|
giuliomoro@480
|
287 case kmmPitchBend:
|
giuliomoro@480
|
288 {
|
giuliomoro@480
|
289 int channel = message.getChannel();
|
giuliomoro@480
|
290 int value = ((message.getDataByte(1) << 7) | message.getDataByte(0)) + 8192;
|
giuliomoro@480
|
291 //TODO: is the value range correct?
|
giuliomoro@480
|
292 //TODO: maybe the order of the arguments is wrong here?
|
giuliomoro@480
|
293 hv_vscheduleMessageForReceiver(gHeavyContext, hvMidiHashes[kmmPitchBend], 0, "ff",
|
giuliomoro@480
|
294 channel, value);
|
giuliomoro@480
|
295 break;
|
giuliomoro@480
|
296 }
|
giuliomoro@480
|
297 case kmmNone:
|
giuliomoro@480
|
298 case kmmAny:
|
giuliomoro@480
|
299 break;
|
giuliomoro@480
|
300 }
|
giuliomoro@480
|
301 }
|
giuliomoro@480
|
302 }
|
chris@160
|
303
|
chris@160
|
304 // De-interleave the data
|
chris@160
|
305 if(gHvInputBuffers != NULL) {
|
giuliomoro@480
|
306 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
giuliomoro@480
|
307 for(unsigned int ch = 0; ch < gHvInputChannels; ch++) {
|
chris@160
|
308 if(ch >= context->audioChannels+context->analogChannels) {
|
chris@160
|
309 // THESE ARE PARAMETER INPUT 'CHANNELS' USED FOR ROUTING
|
chris@160
|
310 // 'sensor' outputs from routing channels of dac~ are passed through here
|
chris@160
|
311 break;
|
chris@160
|
312 } else {
|
chris@160
|
313 // If more than 2 ADC inputs are used in the pd patch, route the analog inputs
|
chris@160
|
314 // i.e. ADC3->analogIn0 etc. (first two are always audio inputs)
|
chris@160
|
315 if(ch >= context->audioChannels) {
|
chris@160
|
316 int m = n/2;
|
chris@160
|
317 float mIn = context->analogIn[m*context->analogChannels + (ch-context->audioChannels)];
|
chris@160
|
318 gHvInputBuffers[ch * context->audioFrames + n] = mIn;
|
chris@160
|
319 } else {
|
chris@160
|
320 gHvInputBuffers[ch * context->audioFrames + n] = context->audioIn[n * context->audioChannels + ch];
|
chris@160
|
321 }
|
chris@160
|
322 }
|
chris@160
|
323 }
|
chris@160
|
324 }
|
chris@160
|
325 }
|
chris@160
|
326
|
giuliomoro@480
|
327 // Bela digital in
|
giuliomoro@480
|
328 // note: in multiple places below we assume that the number of digital frames is same as number of audio
|
giuliomoro@482
|
329 // Bela digital in at message-rate
|
giuliomoro@480
|
330 dcm.processInput(context->digital, context->digitalFrames);
|
giuliomoro@480
|
331
|
giuliomoro@480
|
332 // Bela digital in at signal-rate
|
giuliomoro@482
|
333 if(gDigitalSigInChannelsInUse > 0)
|
giuliomoro@482
|
334 {
|
giuliomoro@482
|
335 unsigned int j, k;
|
giuliomoro@482
|
336 float *p0, *p1;
|
giuliomoro@482
|
337 const unsigned int gLibpdBlockSize = context->audioFrames;
|
giuliomoro@482
|
338 const unsigned int audioFrameBase = 0;
|
giuliomoro@482
|
339 float* gInBuf = gHvInputBuffers;
|
giuliomoro@482
|
340 // block below copy/pasted from libpd, except
|
giuliomoro@482
|
341 // 16 has been replaced with gDigitalSigInChannelsInUse
|
giuliomoro@482
|
342 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@482
|
343 unsigned int digitalFrame = audioFrameBase + j;
|
giuliomoro@482
|
344 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
|
giuliomoro@482
|
345 k < gDigitalSigInChannelsInUse; ++k, p1 += gLibpdBlockSize) {
|
giuliomoro@482
|
346 if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
|
giuliomoro@482
|
347 *p1 = digitalRead(context, digitalFrame, k);
|
giuliomoro@482
|
348 }
|
giuliomoro@482
|
349 }
|
giuliomoro@482
|
350 }
|
giuliomoro@482
|
351 }
|
giuliomoro@482
|
352
|
giuliomoro@480
|
353
|
chris@160
|
354 // replacement for bang~ object
|
chris@160
|
355 //hv_vscheduleMessageForReceiver(gHeavyContext, "bbb_bang", 0.0f, "b");
|
chris@160
|
356
|
chris@160
|
357 hv_bbb_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames);
|
chris@160
|
358
|
giuliomoro@480
|
359 // Bela digital out
|
giuliomoro@482
|
360 // Bela digital out at signal-rate
|
giuliomoro@482
|
361 if(gDigitalSigOutChannelsInUse > 0)
|
giuliomoro@482
|
362 {
|
giuliomoro@482
|
363 unsigned int j, k;
|
giuliomoro@482
|
364 float *p0, *p1;
|
giuliomoro@482
|
365 const unsigned int gLibpdBlockSize = context->audioFrames;
|
giuliomoro@482
|
366 const unsigned int audioFrameBase = 0;
|
giuliomoro@482
|
367 float* gOutBuf = gHvOutputBuffers;
|
giuliomoro@482
|
368 // block below copy/pasted from libpd, except
|
giuliomoro@482
|
369 // context->digitalChannels has been replaced with gDigitalSigOutChannelsInUse
|
giuliomoro@482
|
370 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
|
giuliomoro@482
|
371 unsigned int digitalFrame = (audioFrameBase + j);
|
giuliomoro@482
|
372 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
|
giuliomoro@482
|
373 k < gDigitalSigOutChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@482
|
374 if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
|
giuliomoro@482
|
375 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
|
giuliomoro@482
|
376 }
|
giuliomoro@482
|
377 }
|
giuliomoro@482
|
378 }
|
giuliomoro@482
|
379 }
|
giuliomoro@482
|
380 // Bela digital out at message-rate
|
giuliomoro@482
|
381 dcm.processOutput(context->digital, context->digitalFrames);
|
giuliomoro@480
|
382
|
giuliomoro@482
|
383 // Bela scope
|
giuliomoro@482
|
384 if(gScopeChannelsInUse > 0)
|
giuliomoro@482
|
385 {
|
giuliomoro@482
|
386 unsigned int j, k;
|
giuliomoro@482
|
387 float *p0, *p1;
|
giuliomoro@482
|
388 const unsigned int gLibpdBlockSize = context->audioFrames;
|
giuliomoro@482
|
389 const unsigned int audioFrameBase = 0;
|
giuliomoro@482
|
390 float* gOutBuf = gHvOutputBuffers;
|
giuliomoro@482
|
391
|
giuliomoro@482
|
392 // block below copy/pasted from libpd
|
giuliomoro@482
|
393 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
|
giuliomoro@482
|
394 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@482
|
395 gScopeOut[k] = *p1;
|
giuliomoro@482
|
396 }
|
giuliomoro@482
|
397 scope.log(gScopeOut[0], gScopeOut[1], gScopeOut[2], gScopeOut[3]);
|
giuliomoro@482
|
398 }
|
giuliomoro@482
|
399 }
|
giuliomoro@480
|
400
|
chris@160
|
401 // Interleave the output data
|
chris@160
|
402 if(gHvOutputBuffers != NULL) {
|
giuliomoro@480
|
403 for(unsigned int n = 0; n < context->audioFrames; n++) {
|
chris@160
|
404
|
giuliomoro@480
|
405 for(unsigned int ch = 0; ch < gHvOutputChannels; ch++) {
|
chris@160
|
406 if(ch >= context->audioChannels+context->analogChannels) {
|
chris@160
|
407 // THESE ARE SENSOR OUTPUT 'CHANNELS' USED FOR ROUTING
|
chris@160
|
408 // they are the content of the 'sensor output' dac~ channels
|
chris@160
|
409 } else {
|
chris@160
|
410 if(ch >= context->audioChannels) {
|
chris@160
|
411 int m = n/2;
|
chris@160
|
412 context->analogOut[m * context->analogFrames + (ch-context->audioChannels)] = constrain(gHvOutputBuffers[ch*context->audioFrames + n],0.0,1.0);
|
chris@160
|
413 } else {
|
chris@160
|
414 context->audioOut[n * context->audioChannels + ch] = gHvOutputBuffers[ch * context->audioFrames + n];
|
chris@160
|
415 }
|
chris@160
|
416 }
|
chris@160
|
417 }
|
chris@160
|
418 }
|
chris@160
|
419 }
|
chris@160
|
420
|
chris@160
|
421 }
|
chris@160
|
422
|
chris@160
|
423
|
giuliomoro@329
|
424 void cleanup(BelaContext *context, void *userData)
|
chris@160
|
425 {
|
chris@160
|
426
|
chris@160
|
427 hv_bbb_free(gHeavyContext);
|
chris@160
|
428 if(gHvInputBuffers != NULL)
|
chris@160
|
429 free(gHvInputBuffers);
|
chris@160
|
430 if(gHvOutputBuffers != NULL)
|
chris@160
|
431 free(gHvOutputBuffers);
|
giuliomoro@482
|
432 delete[] gScopeOut;
|
chris@160
|
433 }
|