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