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@230
|
9 #include <cmath>
|
giuliomoro@230
|
10 #include <Utilities.h>
|
giuliomoro@230
|
11 #include <I2c_Codec.h>
|
giuliomoro@230
|
12 #include <PRU.h>
|
giuliomoro@230
|
13 #include <stdio.h>
|
giuliomoro@230
|
14 #include "z_libpd.h"
|
giuliomoro@230
|
15 #include <UdpServer.h>
|
giuliomoro@324
|
16 #include <Midi.h>
|
giuliomoro@324
|
17
|
giuliomoro@230
|
18
|
giuliomoro@230
|
19 // setup() is called once before the audio rendering starts.
|
giuliomoro@230
|
20 // Use it to perform any initialisation and allocation which is dependent
|
giuliomoro@230
|
21 // on the period size or sample rate.
|
giuliomoro@230
|
22 //
|
giuliomoro@230
|
23 // userData holds an opaque pointer to a data structure that was passed
|
giuliomoro@230
|
24 // in from the call to initAudio().
|
giuliomoro@230
|
25 //
|
giuliomoro@230
|
26 // Return true on success; returning false halts the program.
|
giuliomoro@230
|
27 #define DEFDACBLKSIZE 8u //make sure this matches the one used to compile libpd
|
giuliomoro@230
|
28
|
giuliomoro@232
|
29 int gChannelsInUse = 10;
|
giuliomoro@230
|
30 int gBufLength;
|
giuliomoro@230
|
31
|
giuliomoro@230
|
32 float* gInBuf;
|
giuliomoro@230
|
33 float* gOutBuf;
|
giuliomoro@230
|
34
|
giuliomoro@230
|
35 void pdnoteon(int ch, int pitch, int vel) {
|
giuliomoro@230
|
36 printf("noteon: %d %d %d\n", ch, pitch, vel);
|
giuliomoro@230
|
37 }
|
giuliomoro@230
|
38
|
giuliomoro@301
|
39 void Bela_printHook(const char *recv){
|
giuliomoro@230
|
40 rt_printf("%s", recv);
|
giuliomoro@230
|
41 }
|
giuliomoro@230
|
42
|
giuliomoro@230
|
43 UdpServer udpServer;
|
giuliomoro@230
|
44
|
giuliomoro@230
|
45 void udpRead(){
|
giuliomoro@230
|
46 char dest[100] = {0};
|
giuliomoro@230
|
47 while(!gShouldStop){
|
giuliomoro@230
|
48 libpd_sys_microsleep(0);
|
giuliomoro@230
|
49 usleep(1000);
|
giuliomoro@230
|
50 }
|
giuliomoro@230
|
51 }
|
giuliomoro@325
|
52 #define PARSE_MIDI
|
giuliomoro@230
|
53 AuxiliaryTask udpReadTask;
|
giuliomoro@324
|
54 Midi midi;
|
giuliomoro@301
|
55 bool setup(BelaContext *context, void *userData)
|
giuliomoro@230
|
56 {
|
giuliomoro@324
|
57 midi.readFrom(0);
|
giuliomoro@324
|
58 midi.writeTo(0);
|
giuliomoro@325
|
59 #ifdef PARSE_MIDI
|
giuliomoro@324
|
60 midi.enableParser(true);
|
giuliomoro@325
|
61 #else
|
giuliomoro@325
|
62 midi.enableParser(false);
|
giuliomoro@325
|
63 #endif /* PARSE_MIDI */
|
giuliomoro@232
|
64 gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
|
giuliomoro@230
|
65 udpServer.bindToPort(1234);
|
giuliomoro@230
|
66
|
giuliomoro@230
|
67 // check that we are not running with a blocksize smaller than DEFDACBLKSIZE
|
giuliomoro@230
|
68 // it would still work, but the load would be executed unevenly between calls to render
|
giuliomoro@230
|
69 if(context->audioFrames < DEFDACBLKSIZE){
|
giuliomoro@230
|
70 fprintf(stderr, "Error: minimum block size must be %d\n", DEFDACBLKSIZE);
|
giuliomoro@230
|
71 return false;
|
giuliomoro@230
|
72 }
|
giuliomoro@230
|
73
|
giuliomoro@230
|
74 // check that the sampling rate of the analogs is the same as audio if running with
|
giuliomoro@230
|
75 // more than 2 channels (that is with analog). If we fix the TODO in render, then
|
giuliomoro@230
|
76 // this test is not needed.
|
giuliomoro@232
|
77 // if(context->analogFrames != context->audioFrames){
|
giuliomoro@232
|
78 // fprintf(stderr, "Error: analog and audio sampling rates must be the same\n");
|
giuliomoro@232
|
79 // return false;
|
giuliomoro@232
|
80 // }
|
giuliomoro@230
|
81 //following lines borrowed from libpd/samples/c/pdtest/pdtest.c
|
giuliomoro@230
|
82 // init pd
|
giuliomoro@301
|
83 libpd_set_printhook(Bela_printHook); // set this before calling libpd_init
|
giuliomoro@230
|
84 libpd_set_noteonhook(pdnoteon);
|
giuliomoro@230
|
85 libpd_init();
|
giuliomoro@230
|
86 //TODO: analyse the ASCII of the patch file and find the in/outs to use
|
giuliomoro@230
|
87 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
|
giuliomoro@230
|
88
|
giuliomoro@230
|
89 libpd_start_message(1); // one entry in list
|
giuliomoro@230
|
90 libpd_add_float(1.0f);
|
giuliomoro@230
|
91 libpd_finish_message("pd", "dsp");
|
giuliomoro@230
|
92
|
giuliomoro@230
|
93 gBufLength = max(DEFDACBLKSIZE, context->audioFrames);
|
giuliomoro@230
|
94 unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength;
|
giuliomoro@230
|
95 gInBuf = (float*)malloc(bufferSize);
|
giuliomoro@230
|
96 gOutBuf = (float*)malloc(bufferSize);
|
giuliomoro@230
|
97 // no need to memset to zero
|
giuliomoro@230
|
98
|
giuliomoro@230
|
99 char file[] = "_main.pd";
|
giuliomoro@230
|
100 char folder[] = "./";
|
giuliomoro@230
|
101 // open patch [; pd open file folder(
|
giuliomoro@230
|
102 libpd_openfile(file, folder);
|
giuliomoro@230
|
103
|
giuliomoro@301
|
104 udpReadTask = Bela_createAuxiliaryTask(udpRead, 60, "udpReadTask");
|
giuliomoro@301
|
105 Bela_scheduleAuxiliaryTask(udpReadTask);
|
giuliomoro@230
|
106 return true;
|
giuliomoro@230
|
107 }
|
giuliomoro@230
|
108
|
giuliomoro@230
|
109 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@230
|
110 // Input and output are given from the audio hardware and the other
|
giuliomoro@230
|
111 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
giuliomoro@230
|
112 // will be 0.
|
giuliomoro@301
|
113 BelaContext *c;
|
giuliomoro@301
|
114 void render(BelaContext *context, void *userData)
|
giuliomoro@230
|
115 {
|
giuliomoro@324
|
116 int num;
|
giuliomoro@325
|
117 #ifdef PARSE_MIDI
|
giuliomoro@324
|
118 while((num = midi.getParser()->numAvailableMessages()) > 0){
|
giuliomoro@324
|
119 static MidiChannelMessage message;
|
giuliomoro@324
|
120 message = midi.getParser()->getNextChannelMessage();
|
giuliomoro@325
|
121 // message.prettyPrint(); // use this to print beautified message (channel, data bytes)
|
giuliomoro@324
|
122 switch(message.getType()){
|
giuliomoro@325
|
123 case kmmNoteOn:
|
giuliomoro@325
|
124 {
|
giuliomoro@324
|
125 int noteNumber = message.getDataByte(0);
|
giuliomoro@324
|
126 int velocity = message.getDataByte(1);
|
giuliomoro@324
|
127 int channel = message.getChannel();
|
giuliomoro@324
|
128 libpd_noteon(channel, noteNumber, velocity);
|
giuliomoro@325
|
129 break;
|
giuliomoro@324
|
130 }
|
giuliomoro@325
|
131 case kmmNoteOff:
|
giuliomoro@325
|
132 {
|
giuliomoro@325
|
133 /* PureData does not seem to handle noteoff messages as per the MIDI specs,
|
giuliomoro@325
|
134 * so that the noteoff velocity is ignored. Here we convert them to noteon
|
giuliomoro@325
|
135 * with a velocity of 0.
|
giuliomoro@325
|
136 */
|
giuliomoro@325
|
137 int noteNumber = message.getDataByte(0);
|
giuliomoro@325
|
138 // int velocity = message.getDataByte(1); // would be ignored by Pd
|
giuliomoro@325
|
139 int channel = message.getChannel();
|
giuliomoro@325
|
140 libpd_noteon(channel, noteNumber, 0);
|
giuliomoro@325
|
141 break;
|
giuliomoro@324
|
142 }
|
giuliomoro@325
|
143 case kmmControlChange:
|
giuliomoro@325
|
144 {
|
giuliomoro@325
|
145 int channel = message.getChannel();
|
giuliomoro@325
|
146 int controller = message.getDataByte(0);
|
giuliomoro@325
|
147 int value = message.getDataByte(1);
|
giuliomoro@325
|
148 libpd_controlchange(channel, controller, value);
|
giuliomoro@325
|
149 break;
|
giuliomoro@325
|
150 }
|
giuliomoro@325
|
151 case kmmProgramChange:
|
giuliomoro@325
|
152 {
|
giuliomoro@325
|
153 int channel = message.getChannel();
|
giuliomoro@325
|
154 int program = message.getDataByte(0);
|
giuliomoro@325
|
155 libpd_programchange(channel, program);
|
giuliomoro@325
|
156 break;
|
giuliomoro@325
|
157 }
|
giuliomoro@325
|
158 case kmmPolyphonicKeyPressure:
|
giuliomoro@325
|
159 {
|
giuliomoro@325
|
160 int channel = message.getChannel();
|
giuliomoro@325
|
161 int pitch = message.getDataByte(0);
|
giuliomoro@325
|
162 int value = message.getDataByte(1);
|
giuliomoro@325
|
163 libpd_polyaftertouch(channel, pitch, value);
|
giuliomoro@325
|
164 break;
|
giuliomoro@325
|
165 }
|
giuliomoro@325
|
166 case kmmChannelPressure:
|
giuliomoro@325
|
167 {
|
giuliomoro@325
|
168 int channel = message.getChannel();
|
giuliomoro@325
|
169 int value = message.getDataByte(0);
|
giuliomoro@325
|
170 libpd_aftertouch(channel, value);
|
giuliomoro@325
|
171 break;
|
giuliomoro@325
|
172 }
|
giuliomoro@325
|
173 case kmmPitchBend:
|
giuliomoro@325
|
174 {
|
giuliomoro@325
|
175 int channel = message.getChannel();
|
giuliomoro@325
|
176 int value = (message.getDataByte(1) << 7)| message.getDataByte(0);
|
giuliomoro@325
|
177 libpd_pitchbend(channel, value);
|
giuliomoro@325
|
178 break;
|
giuliomoro@325
|
179 }
|
giuliomoro@324
|
180 }
|
giuliomoro@324
|
181 }
|
giuliomoro@325
|
182 #else
|
giuliomoro@325
|
183 int input;
|
giuliomoro@325
|
184 while((input = midi.getInput()) >= 0){
|
giuliomoro@325
|
185 libpd_midibyte(0, input);
|
giuliomoro@325
|
186 rt_printf("input: %d\n", input);
|
giuliomoro@325
|
187 }
|
giuliomoro@325
|
188 #endif /* PARSE_MIDI */
|
giuliomoro@230
|
189 static int inW = 0;
|
giuliomoro@230
|
190 static int outR = 0;
|
giuliomoro@230
|
191 /*
|
giuliomoro@230
|
192 * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers
|
giuliomoro@230
|
193 * and the blocksize of Bela is the same as DEFDACBLKSIZE, then you probably
|
giuliomoro@230
|
194 * do not need the for loops before and after libpd_process_float, so you can save quite some
|
giuliomoro@230
|
195 * memory operations.
|
giuliomoro@230
|
196 */
|
giuliomoro@232
|
197 static int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels);
|
giuliomoro@232
|
198 // rt_printf("channelsInUse: %d, analogChannels in Use: %d\n", gChannelsInUse, analogChannelsInUse);
|
giuliomoro@232
|
199 for(unsigned int n = 0; n < context->audioFrames; ++n){ //pd buffers are interleaved
|
giuliomoro@232
|
200 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){ //first two channels are audio
|
andrewm@308
|
201 gInBuf[inW++] = audioRead(context, n, ch);
|
giuliomoro@230
|
202 }
|
giuliomoro@232
|
203 // then analogs
|
giuliomoro@232
|
204 // this loop resamples by ZOH, as needed, using m
|
giuliomoro@232
|
205 if(context->analogChannels == 8 ){ //hold the value for two frames
|
giuliomoro@232
|
206 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
207 gInBuf[inW++] = analogRead(context, n/2, analogCh); // n/2 wil be the same for n and n+1 when n is even
|
giuliomoro@232
|
208 }
|
giuliomoro@232
|
209 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@232
|
210 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
211 gInBuf[inW++] = analogRead(context, n, analogCh);
|
giuliomoro@232
|
212 }
|
giuliomoro@232
|
213 } else if(context->analogChannels == 2){ //drop every other frame
|
giuliomoro@232
|
214 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
215 gInBuf[inW++] = analogRead(context, n*2, analogCh);
|
giuliomoro@232
|
216 }
|
giuliomoro@230
|
217 }
|
giuliomoro@230
|
218 if(inW == gBufLength * gChannelsInUse){
|
giuliomoro@230
|
219 inW = 0;
|
giuliomoro@230
|
220 }
|
giuliomoro@230
|
221 }
|
giuliomoro@232
|
222 // rt_printf("inW %d\n", inW);
|
giuliomoro@230
|
223 if(inW == 0){ //if the buffer is full, process it
|
giuliomoro@232
|
224 static int numberOfPdBlocksToProcess = gBufLength/DEFDACBLKSIZE;
|
giuliomoro@230
|
225 libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf);
|
giuliomoro@232
|
226 outR = 0; // reset the read pointer. NOTE: hopefully this is needed only the first time
|
giuliomoro@230
|
227 }
|
giuliomoro@230
|
228
|
giuliomoro@230
|
229 for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved
|
giuliomoro@232
|
230 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){
|
andrewm@308
|
231 audioWrite(context, n, ch, gOutBuf[outR++]);
|
giuliomoro@230
|
232 }
|
giuliomoro@232
|
233 //and analogs
|
giuliomoro@232
|
234 if(context->analogChannels == 8){
|
giuliomoro@232
|
235 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
236 float analogOut = gOutBuf[outR++];
|
giuliomoro@232
|
237 if((n&1) == 0){//write every two frames
|
andrewm@308
|
238 analogWrite(context, n/2, analogCh, analogOut);
|
giuliomoro@232
|
239 } else {
|
giuliomoro@232
|
240 // discard this sample
|
giuliomoro@232
|
241 }
|
giuliomoro@232
|
242 }
|
giuliomoro@232
|
243 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@232
|
244 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
245 float analogOut = gOutBuf[outR++];
|
andrewm@308
|
246 analogWrite(context, n, analogCh, analogOut);
|
giuliomoro@232
|
247 }
|
giuliomoro@232
|
248 } else if(context->analogChannels == 2){ //write twice every frame
|
giuliomoro@232
|
249 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
250 float analogOut = gOutBuf[outR++];
|
andrewm@308
|
251 analogWrite(context, 2*n, analogCh, analogOut);
|
andrewm@308
|
252 analogWrite(context, 2*n + 1, analogCh, analogOut);
|
giuliomoro@232
|
253 }
|
giuliomoro@230
|
254 }
|
giuliomoro@230
|
255 if(outR == gBufLength * gChannelsInUse){
|
giuliomoro@230
|
256 outR = 0;
|
giuliomoro@230
|
257 }
|
giuliomoro@230
|
258 }
|
giuliomoro@232
|
259 // rt_printf("outR %d, analogChannelsInUse %d, channelsInUse %d\n",
|
giuliomoro@232
|
260 // outR , analogChannelsInUse, gChannelsInUse);
|
giuliomoro@230
|
261 }
|
giuliomoro@232
|
262
|
giuliomoro@230
|
263 // cleanup() is called once at the end, after the audio has stopped.
|
giuliomoro@230
|
264 // Release any resources that were allocated in setup().
|
giuliomoro@230
|
265
|
giuliomoro@301
|
266 void cleanup(BelaContext *context, void *userData)
|
giuliomoro@230
|
267 {
|
giuliomoro@230
|
268 free(gInBuf);
|
giuliomoro@230
|
269 free(gOutBuf);
|
giuliomoro@230
|
270 }
|