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