giuliomoro@230
|
1 /*
|
giuliomoro@230
|
2 * render.cpp
|
giuliomoro@230
|
3 *
|
giuliomoro@230
|
4 * Created on: Oct 24, 2014
|
giuliomoro@230
|
5 * Author: parallels
|
giuliomoro@230
|
6 */
|
giuliomoro@230
|
7
|
giuliomoro@301
|
8 #include <Bela.h>
|
giuliomoro@353
|
9 #include <DigitalChannelManager.h>
|
giuliomoro@230
|
10 #include <cmath>
|
giuliomoro@230
|
11 #include <I2c_Codec.h>
|
giuliomoro@230
|
12 #include <PRU.h>
|
giuliomoro@230
|
13 #include <stdio.h>
|
giuliomoro@467
|
14 #include <libpd/z_libpd.h>
|
giuliomoro@467
|
15 #include <libpd/s_stuff.h>
|
giuliomoro@230
|
16 #include <UdpServer.h>
|
giuliomoro@324
|
17 #include <Midi.h>
|
giuliomoro@474
|
18 #include <Scope.h>
|
giuliomoro@474
|
19
|
giuliomoro@345
|
20 // if you are 100% sure of what value was used to compile libpd/puredata, then
|
giuliomoro@474
|
21 // you could #define gBufLength instead of getting it at runtime. It has proved to give some 0.3%
|
giuliomoro@343
|
22 // performance boost when it is 8 (thanks to vectorize optimizations I guess).
|
giuliomoro@230
|
23 int gBufLength;
|
giuliomoro@230
|
24
|
giuliomoro@230
|
25 float* gInBuf;
|
giuliomoro@230
|
26 float* gOutBuf;
|
giuliomoro@230
|
27
|
giuliomoro@230
|
28 void pdnoteon(int ch, int pitch, int vel) {
|
giuliomoro@230
|
29 printf("noteon: %d %d %d\n", ch, pitch, vel);
|
giuliomoro@230
|
30 }
|
giuliomoro@230
|
31
|
giuliomoro@301
|
32 void Bela_printHook(const char *recv){
|
giuliomoro@230
|
33 rt_printf("%s", recv);
|
giuliomoro@230
|
34 }
|
giuliomoro@230
|
35
|
giuliomoro@474
|
36 //TODO: remove this function
|
giuliomoro@340
|
37 void libpdReadFilesLoop(){
|
giuliomoro@504
|
38 return;
|
giuliomoro@230
|
39 while(!gShouldStop){
|
giuliomoro@337
|
40 // check for modified sockets/file descriptors
|
giuliomoro@337
|
41 // (libpd would normally do this every block WITHIN the audio thread)
|
giuliomoro@337
|
42 // not sure if this is thread-safe at the moment
|
giuliomoro@230
|
43 libpd_sys_microsleep(0);
|
giuliomoro@230
|
44 usleep(1000);
|
giuliomoro@230
|
45 }
|
giuliomoro@230
|
46 }
|
giuliomoro@340
|
47
|
giuliomoro@325
|
48 #define PARSE_MIDI
|
giuliomoro@353
|
49 static AuxiliaryTask libpdReadFilesTask;
|
giuliomoro@353
|
50 static Midi midi;
|
giuliomoro@353
|
51 static DigitalChannelManager dcm;
|
giuliomoro@342
|
52 //UdpServer udpServer;
|
giuliomoro@337
|
53
|
giuliomoro@345
|
54 void sendDigitalMessage(bool state, unsigned int delay, void* receiverName){
|
giuliomoro@345
|
55 libpd_float((char*)receiverName, (float)state);
|
giuliomoro@345
|
56 // rt_printf("%s: %d\n", (char*)receiverName, state);
|
giuliomoro@345
|
57 }
|
giuliomoro@350
|
58
|
giuliomoro@352
|
59 #define LIBPD_DIGITAL_OFFSET 11 // digitals are preceded by 2 audio and 8 analogs (even if using a different number of analogs)
|
giuliomoro@352
|
60
|
giuliomoro@352
|
61 void Bela_messageHook(const char *source, const char *symbol, int argc, t_atom *argv){
|
giuliomoro@352
|
62 if(strcmp(source, "bela_setDigital") == 0){
|
giuliomoro@352
|
63 // symbol is the direction, argv[0] is the channel, argv[1] (optional)
|
giuliomoro@363
|
64 // is signal("sig" or "~") or message("message", default) rate
|
giuliomoro@353
|
65 bool isMessageRate = true; // defaults to message rate
|
giuliomoro@354
|
66 bool direction = 0; // initialize it just to avoid the compiler's warning
|
giuliomoro@354
|
67 bool disable = false;
|
giuliomoro@352
|
68 if(strcmp(symbol, "in") == 0){
|
giuliomoro@352
|
69 direction = INPUT;
|
giuliomoro@352
|
70 } else if(strcmp(symbol, "out") == 0){
|
giuliomoro@352
|
71 direction = OUTPUT;
|
giuliomoro@354
|
72 } else if(strcmp(symbol, "disable") == 0){
|
giuliomoro@354
|
73 disable = true;
|
giuliomoro@352
|
74 } else {
|
giuliomoro@354
|
75 return;
|
giuliomoro@352
|
76 }
|
giuliomoro@352
|
77 if(argc == 0){
|
giuliomoro@354
|
78 return;
|
giuliomoro@352
|
79 } else if (libpd_is_float(&argv[0]) == false){
|
giuliomoro@352
|
80 return;
|
giuliomoro@352
|
81 }
|
giuliomoro@352
|
82 int channel = libpd_get_float(&argv[0]) - LIBPD_DIGITAL_OFFSET;
|
giuliomoro@354
|
83 if(disable == true){
|
giuliomoro@354
|
84 dcm.unmanage(channel);
|
giuliomoro@355
|
85 return;
|
giuliomoro@354
|
86 }
|
giuliomoro@352
|
87 if(argc >= 2){
|
giuliomoro@352
|
88 t_atom* a = &argv[1];
|
giuliomoro@352
|
89 if(libpd_is_symbol(a)){
|
giuliomoro@352
|
90 char *s = libpd_get_symbol(a);
|
giuliomoro@352
|
91 if(strcmp(s, "~") == 0 || strncmp(s, "sig", 3) == 0){
|
giuliomoro@353
|
92 isMessageRate = false;
|
giuliomoro@352
|
93 }
|
giuliomoro@352
|
94 }
|
giuliomoro@352
|
95 }
|
giuliomoro@354
|
96 dcm.manage(channel, direction, isMessageRate);
|
giuliomoro@352
|
97 }
|
giuliomoro@352
|
98 }
|
giuliomoro@352
|
99
|
giuliomoro@350
|
100 void Bela_floatHook(const char *source, float value){
|
giuliomoro@350
|
101 // let's make this as optimized as possible for built-in digital Out parsing
|
giuliomoro@350
|
102 // the built-in digital receivers are of the form "bela_digitalOutXX" where XX is between 11 and 26
|
giuliomoro@350
|
103 static int prefixLength = 15; // strlen("bela_digitalOut")
|
giuliomoro@350
|
104 if(strncmp(source, "bela_digitalOut", prefixLength)==0){
|
giuliomoro@350
|
105 if(source[prefixLength] != 0){ //the two ifs are used instead of if(strlen(source) >= prefixLength+2)
|
giuliomoro@350
|
106 if(source[prefixLength + 1] != 0){
|
giuliomoro@357
|
107 // quickly convert the suffix to integer, assuming they are numbers, avoiding to call atoi
|
giuliomoro@350
|
108 int receiver = ((source[prefixLength] - 48) * 10);
|
giuliomoro@350
|
109 receiver += (source[prefixLength+1] - 48);
|
giuliomoro@350
|
110 unsigned int channel = receiver - 11; // go back to the actual Bela digital channel number
|
giuliomoro@474
|
111 if(channel < 16){ //16 is the hardcoded value for the number of digital channels
|
giuliomoro@354
|
112 dcm.setValue(channel, value);
|
giuliomoro@350
|
113 }
|
giuliomoro@350
|
114 }
|
giuliomoro@350
|
115 }
|
giuliomoro@350
|
116 }
|
giuliomoro@350
|
117 }
|
giuliomoro@350
|
118
|
giuliomoro@345
|
119 char receiverNames[16][21]={
|
giuliomoro@345
|
120 {"bela_digitalIn11"},{"bela_digitalIn12"},{"bela_digitalIn13"},{"bela_digitalIn14"},{"bela_digitalIn15"},
|
giuliomoro@345
|
121 {"bela_digitalIn16"},{"bela_digitalIn17"},{"bela_digitalIn18"},{"bela_digitalIn19"},{"bela_digitalIn20"},
|
giuliomoro@345
|
122 {"bela_digitalIn21"},{"bela_digitalIn22"},{"bela_digitalIn23"},{"bela_digitalIn24"},{"bela_digitalIn25"},
|
giuliomoro@345
|
123 {"bela_digitalIn26"}
|
giuliomoro@345
|
124 };
|
giuliomoro@345
|
125
|
giuliomoro@474
|
126 static unsigned int gAnalogChannelsInUse;
|
giuliomoro@345
|
127 static unsigned int gLibpdBlockSize;
|
giuliomoro@474
|
128 // 2 audio + (up to)8 analog + (up to) 16 digital + 4 scope outputs
|
giuliomoro@474
|
129 static const unsigned int gChannelsInUse = 30;
|
giuliomoro@476
|
130 //static const unsigned int gFirstAudioChannel = 0;
|
giuliomoro@474
|
131 static const unsigned int gFirstAnalogChannel = 2;
|
giuliomoro@474
|
132 static const unsigned int gFirstDigitalChannel = 10;
|
giuliomoro@474
|
133 static const unsigned int gFirstScopeChannel = 26;
|
giuliomoro@345
|
134
|
giuliomoro@474
|
135 Scope scope;
|
giuliomoro@483
|
136 unsigned int gScopeChannelsInUse = 4;
|
giuliomoro@476
|
137 float* gScopeOut;
|
giuliomoro@476
|
138
|
giuliomoro@301
|
139 bool setup(BelaContext *context, void *userData)
|
giuliomoro@230
|
140 {
|
giuliomoro@483
|
141 scope.setup(gScopeChannelsInUse, context->audioSampleRate);
|
giuliomoro@483
|
142 gScopeOut = new float[gScopeChannelsInUse];
|
giuliomoro@474
|
143
|
giuliomoro@470
|
144 // Check first of all if file exists. Will actually open it later.
|
giuliomoro@470
|
145 char file[] = "_main.pd";
|
giuliomoro@470
|
146 char folder[] = "./";
|
giuliomoro@470
|
147 unsigned int strSize = strlen(file) + strlen(folder) + 1;
|
giuliomoro@470
|
148 char* str = (char*)malloc(sizeof(char) * strSize);
|
giuliomoro@470
|
149 snprintf(str, strSize, "%s%s", folder, file);
|
giuliomoro@470
|
150 if(access(str, F_OK) == -1 ) {
|
giuliomoro@470
|
151 printf("Error file %s/%s not found. The %s file should be your main patch.\n", folder, file, file);
|
giuliomoro@470
|
152 return false;
|
giuliomoro@470
|
153 }
|
giuliomoro@480
|
154
|
giuliomoro@480
|
155 // analog setup
|
giuliomoro@480
|
156 gAnalogChannelsInUse = context->analogChannels;
|
giuliomoro@480
|
157
|
giuliomoro@480
|
158 // digital setup
|
giuliomoro@353
|
159 dcm.setCallback(sendDigitalMessage);
|
giuliomoro@345
|
160 if(context->digitalChannels > 0){
|
giuliomoro@345
|
161 for(unsigned int ch = 0; ch < context->digitalChannels; ++ch){
|
giuliomoro@353
|
162 dcm.setCallbackArgument(ch, receiverNames[ch]);
|
giuliomoro@345
|
163 }
|
giuliomoro@345
|
164 }
|
giuliomoro@480
|
165
|
giuliomoro@324
|
166 midi.readFrom(0);
|
giuliomoro@324
|
167 midi.writeTo(0);
|
giuliomoro@325
|
168 #ifdef PARSE_MIDI
|
giuliomoro@324
|
169 midi.enableParser(true);
|
giuliomoro@325
|
170 #else
|
giuliomoro@325
|
171 midi.enableParser(false);
|
giuliomoro@325
|
172 #endif /* PARSE_MIDI */
|
giuliomoro@342
|
173 // udpServer.bindToPort(1234);
|
giuliomoro@230
|
174
|
giuliomoro@340
|
175 gLibpdBlockSize = libpd_blocksize();
|
giuliomoro@337
|
176 // check that we are not running with a blocksize smaller than gLibPdBlockSize
|
giuliomoro@474
|
177 // We could still make it work, but the load would be executed unevenly between calls to render
|
giuliomoro@340
|
178 if(context->audioFrames < gLibpdBlockSize){
|
giuliomoro@340
|
179 fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
|
giuliomoro@230
|
180 return false;
|
giuliomoro@230
|
181 }
|
giuliomoro@349
|
182 // set hooks before calling libpd_init
|
giuliomoro@349
|
183 libpd_set_printhook(Bela_printHook);
|
giuliomoro@349
|
184 libpd_set_floathook(Bela_floatHook);
|
giuliomoro@352
|
185 libpd_set_messagehook(Bela_messageHook);
|
giuliomoro@349
|
186 libpd_set_noteonhook(pdnoteon);
|
giuliomoro@340
|
187 //TODO: add hooks for other midi events and generate MIDI output appropriately
|
giuliomoro@349
|
188 libpd_init();
|
giuliomoro@357
|
189 //TODO: ideally, we would analyse the ASCII of the patch file and find out which in/outs to use
|
giuliomoro@230
|
190 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
|
giuliomoro@350
|
191 gInBuf = libpd_get_sys_soundin();
|
giuliomoro@350
|
192 gOutBuf = libpd_get_sys_soundout();
|
giuliomoro@230
|
193
|
giuliomoro@230
|
194 libpd_start_message(1); // one entry in list
|
giuliomoro@230
|
195 libpd_add_float(1.0f);
|
giuliomoro@230
|
196 libpd_finish_message("pd", "dsp");
|
giuliomoro@230
|
197
|
giuliomoro@340
|
198 gBufLength = max(gLibpdBlockSize, context->audioFrames);
|
giuliomoro@230
|
199
|
giuliomoro@350
|
200
|
giuliomoro@350
|
201 // bind your receivers here
|
giuliomoro@350
|
202 libpd_bind("bela_digitalOut11");
|
giuliomoro@350
|
203 libpd_bind("bela_digitalOut12");
|
giuliomoro@350
|
204 libpd_bind("bela_digitalOut13");
|
giuliomoro@350
|
205 libpd_bind("bela_digitalOut14");
|
giuliomoro@350
|
206 libpd_bind("bela_digitalOut15");
|
giuliomoro@350
|
207 libpd_bind("bela_digitalOut16");
|
giuliomoro@350
|
208 libpd_bind("bela_digitalOut17");
|
giuliomoro@350
|
209 libpd_bind("bela_digitalOut18");
|
giuliomoro@350
|
210 libpd_bind("bela_digitalOut19");
|
giuliomoro@350
|
211 libpd_bind("bela_digitalOut20");
|
giuliomoro@350
|
212 libpd_bind("bela_digitalOut21");
|
giuliomoro@350
|
213 libpd_bind("bela_digitalOut22");
|
giuliomoro@350
|
214 libpd_bind("bela_digitalOut23");
|
giuliomoro@350
|
215 libpd_bind("bela_digitalOut24");
|
giuliomoro@350
|
216 libpd_bind("bela_digitalOut25");
|
giuliomoro@350
|
217 libpd_bind("bela_digitalOut26");
|
giuliomoro@352
|
218 libpd_bind("bela_setDigital");
|
giuliomoro@350
|
219
|
giuliomoro@353
|
220 // open patch [; pd open file folder(
|
giuliomoro@470
|
221 void* patch = libpd_openfile(file, folder);
|
giuliomoro@470
|
222 if(patch == NULL){
|
giuliomoro@470
|
223 printf("Error: file %s/%s is corrupted.\n", folder, file);
|
giuliomoro@470
|
224 return false;
|
giuliomoro@470
|
225 }
|
giuliomoro@340
|
226 libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
|
giuliomoro@340
|
227 Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
|
giuliomoro@340
|
228
|
giuliomoro@349
|
229
|
giuliomoro@230
|
230 return true;
|
giuliomoro@230
|
231 }
|
giuliomoro@230
|
232
|
giuliomoro@230
|
233 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@230
|
234 // Input and output are given from the audio hardware and the other
|
giuliomoro@230
|
235 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
giuliomoro@230
|
236 // will be 0.
|
giuliomoro@345
|
237
|
giuliomoro@301
|
238 void render(BelaContext *context, void *userData)
|
giuliomoro@230
|
239 {
|
giuliomoro@324
|
240 int num;
|
giuliomoro@341
|
241 // the safest thread-safe option to handle MIDI input is to process the MIDI buffer
|
giuliomoro@341
|
242 // from the audio thread.
|
giuliomoro@325
|
243 #ifdef PARSE_MIDI
|
giuliomoro@324
|
244 while((num = midi.getParser()->numAvailableMessages()) > 0){
|
giuliomoro@324
|
245 static MidiChannelMessage message;
|
giuliomoro@324
|
246 message = midi.getParser()->getNextChannelMessage();
|
giuliomoro@341
|
247 //message.prettyPrint(); // use this to print beautified message (channel, data bytes)
|
giuliomoro@324
|
248 switch(message.getType()){
|
giuliomoro@325
|
249 case kmmNoteOn:
|
giuliomoro@325
|
250 {
|
giuliomoro@324
|
251 int noteNumber = message.getDataByte(0);
|
giuliomoro@324
|
252 int velocity = message.getDataByte(1);
|
giuliomoro@324
|
253 int channel = message.getChannel();
|
giuliomoro@324
|
254 libpd_noteon(channel, noteNumber, velocity);
|
giuliomoro@325
|
255 break;
|
giuliomoro@324
|
256 }
|
giuliomoro@325
|
257 case kmmNoteOff:
|
giuliomoro@325
|
258 {
|
giuliomoro@325
|
259 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
|
giuliomoro@325
|
260 * so that the noteoff velocity is ignored. Here we convert them to noteon
|
giuliomoro@325
|
261 * with a velocity of 0.
|
giuliomoro@325
|
262 */
|
giuliomoro@325
|
263 int noteNumber = message.getDataByte(0);
|
giuliomoro@325
|
264 // int velocity = message.getDataByte(1); // would be ignored by Pd
|
giuliomoro@325
|
265 int channel = message.getChannel();
|
giuliomoro@325
|
266 libpd_noteon(channel, noteNumber, 0);
|
giuliomoro@325
|
267 break;
|
giuliomoro@324
|
268 }
|
giuliomoro@325
|
269 case kmmControlChange:
|
giuliomoro@325
|
270 {
|
giuliomoro@325
|
271 int channel = message.getChannel();
|
giuliomoro@325
|
272 int controller = message.getDataByte(0);
|
giuliomoro@325
|
273 int value = message.getDataByte(1);
|
giuliomoro@325
|
274 libpd_controlchange(channel, controller, value);
|
giuliomoro@325
|
275 break;
|
giuliomoro@325
|
276 }
|
giuliomoro@325
|
277 case kmmProgramChange:
|
giuliomoro@325
|
278 {
|
giuliomoro@325
|
279 int channel = message.getChannel();
|
giuliomoro@325
|
280 int program = message.getDataByte(0);
|
giuliomoro@325
|
281 libpd_programchange(channel, program);
|
giuliomoro@325
|
282 break;
|
giuliomoro@325
|
283 }
|
giuliomoro@325
|
284 case kmmPolyphonicKeyPressure:
|
giuliomoro@325
|
285 {
|
giuliomoro@325
|
286 int channel = message.getChannel();
|
giuliomoro@325
|
287 int pitch = message.getDataByte(0);
|
giuliomoro@325
|
288 int value = message.getDataByte(1);
|
giuliomoro@325
|
289 libpd_polyaftertouch(channel, pitch, value);
|
giuliomoro@325
|
290 break;
|
giuliomoro@325
|
291 }
|
giuliomoro@325
|
292 case kmmChannelPressure:
|
giuliomoro@325
|
293 {
|
giuliomoro@325
|
294 int channel = message.getChannel();
|
giuliomoro@325
|
295 int value = message.getDataByte(0);
|
giuliomoro@325
|
296 libpd_aftertouch(channel, value);
|
giuliomoro@325
|
297 break;
|
giuliomoro@325
|
298 }
|
giuliomoro@325
|
299 case kmmPitchBend:
|
giuliomoro@325
|
300 {
|
giuliomoro@325
|
301 int channel = message.getChannel();
|
giuliomoro@495
|
302 int value = ((message.getDataByte(1) << 7)| message.getDataByte(0)) - 8192;
|
giuliomoro@325
|
303 libpd_pitchbend(channel, value);
|
giuliomoro@325
|
304 break;
|
giuliomoro@325
|
305 }
|
giuliomoro@337
|
306 case kmmNone:
|
giuliomoro@337
|
307 case kmmAny:
|
giuliomoro@337
|
308 break;
|
giuliomoro@324
|
309 }
|
giuliomoro@324
|
310 }
|
giuliomoro@325
|
311 #else
|
giuliomoro@325
|
312 int input;
|
giuliomoro@325
|
313 while((input = midi.getInput()) >= 0){
|
giuliomoro@325
|
314 libpd_midibyte(0, input);
|
giuliomoro@325
|
315 }
|
giuliomoro@325
|
316 #endif /* PARSE_MIDI */
|
giuliomoro@340
|
317
|
giuliomoro@341
|
318 static unsigned int numberOfPdBlocksToProcess = gBufLength / gLibpdBlockSize;
|
giuliomoro@350
|
319
|
giuliomoro@343
|
320 for(unsigned int tick = 0; tick < numberOfPdBlocksToProcess; ++tick){
|
giuliomoro@345
|
321 unsigned int audioFrameBase = gLibpdBlockSize * tick;
|
giuliomoro@343
|
322 unsigned int j;
|
giuliomoro@343
|
323 unsigned int k;
|
giuliomoro@343
|
324 float* p0;
|
giuliomoro@343
|
325 float* p1;
|
giuliomoro@343
|
326 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@343
|
327 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
328 *p1 = audioRead(context, audioFrameBase + j, k);
|
giuliomoro@232
|
329 }
|
giuliomoro@343
|
330 }
|
giuliomoro@343
|
331 // then analogs
|
giuliomoro@343
|
332 // this loop resamples by ZOH, as needed, using m
|
giuliomoro@343
|
333 if(context->analogChannels == 8 ){ //hold the value for two frames
|
giuliomoro@343
|
334 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@474
|
335 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
336 unsigned int analogFrame = (audioFrameBase + j) / 2;
|
giuliomoro@343
|
337 *p1 = analogRead(context, analogFrame, k);
|
giuliomoro@341
|
338 }
|
giuliomoro@343
|
339 }
|
giuliomoro@343
|
340 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@343
|
341 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@474
|
342 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
343 unsigned int analogFrame = audioFrameBase + j;
|
giuliomoro@343
|
344 *p1 = analogRead(context, analogFrame, k);
|
giuliomoro@341
|
345 }
|
giuliomoro@343
|
346 }
|
giuliomoro@343
|
347 } else if(context->analogChannels == 2){ //drop every other frame
|
giuliomoro@343
|
348 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@474
|
349 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; ++k, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
350 unsigned int analogFrame = (audioFrameBase + j) * 2;
|
giuliomoro@343
|
351 *p1 = analogRead(context, analogFrame, k);
|
giuliomoro@341
|
352 }
|
giuliomoro@232
|
353 }
|
giuliomoro@230
|
354 }
|
giuliomoro@345
|
355
|
giuliomoro@480
|
356 // Bela digital input
|
giuliomoro@353
|
357 // note: in multiple places below we assume that the number of digitals is same as number of audio
|
giuliomoro@348
|
358 // digital in at message-rate
|
giuliomoro@353
|
359 dcm.processInput(&context->digital[audioFrameBase], gLibpdBlockSize);
|
giuliomoro@353
|
360
|
giuliomoro@348
|
361 // digital in at signal-rate
|
giuliomoro@348
|
362 for (j = 0, p0 = gInBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@353
|
363 unsigned int digitalFrame = audioFrameBase + j;
|
giuliomoro@474
|
364 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
|
giuliomoro@348
|
365 k < 16; ++k, p1 += gLibpdBlockSize) {
|
giuliomoro@353
|
366 if(dcm.isSignalRate(k) && dcm.isInput(k)){ // only process input channels that are handled at signal rate
|
giuliomoro@353
|
367 *p1 = digitalRead(context, digitalFrame, k);
|
giuliomoro@353
|
368 }
|
giuliomoro@348
|
369 }
|
giuliomoro@348
|
370 }
|
giuliomoro@345
|
371
|
giuliomoro@342
|
372 libpd_process_sys(); // process the block
|
giuliomoro@345
|
373
|
giuliomoro@348
|
374 //digital out
|
giuliomoro@353
|
375 // digital out at signal-rate
|
giuliomoro@345
|
376 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
|
giuliomoro@345
|
377 unsigned int digitalFrame = (audioFrameBase + j);
|
giuliomoro@474
|
378 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstDigitalChannel;
|
giuliomoro@345
|
379 k < context->digitalChannels; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@353
|
380 if(dcm.isSignalRate(k) && dcm.isOutput(k)){ // only process output channels that are handled at signal rate
|
giuliomoro@353
|
381 digitalWriteOnce(context, digitalFrame, k, *p1 > 0.5);
|
giuliomoro@353
|
382 }
|
giuliomoro@345
|
383 }
|
giuliomoro@345
|
384 }
|
giuliomoro@348
|
385
|
giuliomoro@350
|
386 // digital out at message-rate
|
giuliomoro@353
|
387 dcm.processOutput(&context->digital[audioFrameBase], gLibpdBlockSize);
|
giuliomoro@350
|
388
|
giuliomoro@345
|
389 //audio
|
giuliomoro@343
|
390 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@343
|
391 for (k = 0, p1 = p0; k < context->audioChannels; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
392 audioWrite(context, audioFrameBase + j, k, *p1);
|
giuliomoro@341
|
393 }
|
giuliomoro@343
|
394 }
|
giuliomoro@474
|
395 //scope
|
giuliomoro@474
|
396 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
|
giuliomoro@483
|
397 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstScopeChannel; k < gScopeChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@476
|
398 gScopeOut[k] = *p1;
|
giuliomoro@474
|
399 }
|
giuliomoro@476
|
400 scope.log(gScopeOut[0], gScopeOut[1], gScopeOut[2], gScopeOut[3]);
|
giuliomoro@474
|
401 }
|
giuliomoro@474
|
402
|
giuliomoro@354
|
403
|
giuliomoro@345
|
404 //analog
|
giuliomoro@343
|
405 if(context->analogChannels == 8){
|
giuliomoro@343
|
406 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j += 2, p0 += 2) { //write every two frames
|
giuliomoro@345
|
407 unsigned int analogFrame = (audioFrameBase + j) / 2;
|
giuliomoro@474
|
408 for (k = 0, p1 = p0 + gLibpdBlockSize * gFirstAnalogChannel; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
409 analogWriteOnce(context, analogFrame, k, *p1);
|
giuliomoro@341
|
410 }
|
giuliomoro@343
|
411 }
|
giuliomoro@343
|
412 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@343
|
413 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; ++j, ++p0) {
|
giuliomoro@345
|
414 unsigned int analogFrame = (audioFrameBase + j);
|
giuliomoro@474
|
415 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
416 analogWriteOnce(context, analogFrame, k, *p1);
|
giuliomoro@341
|
417 }
|
giuliomoro@343
|
418 }
|
giuliomoro@343
|
419 } else if(context->analogChannels == 2){ //write every frame twice
|
giuliomoro@343
|
420 for (j = 0, p0 = gOutBuf; j < gLibpdBlockSize; j++, p0++) {
|
giuliomoro@474
|
421 for (k = 0, p1 = p0 + gLibpdBlockSize * context->audioChannels; k < gAnalogChannelsInUse; k++, p1 += gLibpdBlockSize) {
|
giuliomoro@345
|
422 int analogFrame = audioFrameBase * 2 + j * 2;
|
giuliomoro@345
|
423 analogWriteOnce(context, analogFrame, k, *p1);
|
giuliomoro@345
|
424 analogWriteOnce(context, analogFrame + 1, k, *p1);
|
giuliomoro@232
|
425 }
|
giuliomoro@232
|
426 }
|
giuliomoro@230
|
427 }
|
giuliomoro@230
|
428 }
|
giuliomoro@230
|
429 }
|
giuliomoro@232
|
430
|
giuliomoro@230
|
431 // cleanup() is called once at the end, after the audio has stopped.
|
giuliomoro@230
|
432 // Release any resources that were allocated in setup().
|
giuliomoro@230
|
433
|
giuliomoro@301
|
434 void cleanup(BelaContext *context, void *userData)
|
giuliomoro@230
|
435 {
|
giuliomoro@476
|
436 delete [] gScopeOut;
|
giuliomoro@230
|
437 }
|