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@230
|
52
|
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@324
|
59 midi.enableParser(true);
|
giuliomoro@232
|
60 gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
|
giuliomoro@230
|
61 udpServer.bindToPort(1234);
|
giuliomoro@230
|
62
|
giuliomoro@230
|
63 // check that we are not running with a blocksize smaller than DEFDACBLKSIZE
|
giuliomoro@230
|
64 // it would still work, but the load would be executed unevenly between calls to render
|
giuliomoro@230
|
65 if(context->audioFrames < DEFDACBLKSIZE){
|
giuliomoro@230
|
66 fprintf(stderr, "Error: minimum block size must be %d\n", DEFDACBLKSIZE);
|
giuliomoro@230
|
67 return false;
|
giuliomoro@230
|
68 }
|
giuliomoro@230
|
69
|
giuliomoro@230
|
70 // check that the sampling rate of the analogs is the same as audio if running with
|
giuliomoro@230
|
71 // more than 2 channels (that is with analog). If we fix the TODO in render, then
|
giuliomoro@230
|
72 // this test is not needed.
|
giuliomoro@232
|
73 // if(context->analogFrames != context->audioFrames){
|
giuliomoro@232
|
74 // fprintf(stderr, "Error: analog and audio sampling rates must be the same\n");
|
giuliomoro@232
|
75 // return false;
|
giuliomoro@232
|
76 // }
|
giuliomoro@230
|
77 //following lines borrowed from libpd/samples/c/pdtest/pdtest.c
|
giuliomoro@230
|
78 // init pd
|
giuliomoro@301
|
79 libpd_set_printhook(Bela_printHook); // set this before calling libpd_init
|
giuliomoro@230
|
80 libpd_set_noteonhook(pdnoteon);
|
giuliomoro@230
|
81 libpd_init();
|
giuliomoro@230
|
82 //TODO: analyse the ASCII of the patch file and find the in/outs to use
|
giuliomoro@230
|
83 libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
|
giuliomoro@230
|
84
|
giuliomoro@230
|
85 libpd_start_message(1); // one entry in list
|
giuliomoro@230
|
86 libpd_add_float(1.0f);
|
giuliomoro@230
|
87 libpd_finish_message("pd", "dsp");
|
giuliomoro@230
|
88
|
giuliomoro@230
|
89 gBufLength = max(DEFDACBLKSIZE, context->audioFrames);
|
giuliomoro@230
|
90 unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength;
|
giuliomoro@230
|
91 gInBuf = (float*)malloc(bufferSize);
|
giuliomoro@230
|
92 gOutBuf = (float*)malloc(bufferSize);
|
giuliomoro@230
|
93 // no need to memset to zero
|
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@230
|
99
|
giuliomoro@301
|
100 udpReadTask = Bela_createAuxiliaryTask(udpRead, 60, "udpReadTask");
|
giuliomoro@301
|
101 Bela_scheduleAuxiliaryTask(udpReadTask);
|
giuliomoro@230
|
102 return true;
|
giuliomoro@230
|
103 }
|
giuliomoro@230
|
104
|
giuliomoro@230
|
105 // render() is called regularly at the highest priority by the audio engine.
|
giuliomoro@230
|
106 // Input and output are given from the audio hardware and the other
|
giuliomoro@230
|
107 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
|
giuliomoro@230
|
108 // will be 0.
|
giuliomoro@301
|
109 BelaContext *c;
|
giuliomoro@301
|
110 void render(BelaContext *context, void *userData)
|
giuliomoro@230
|
111 {
|
giuliomoro@324
|
112 int num;
|
giuliomoro@324
|
113 while((num = midi.getParser()->numAvailableMessages()) > 0){
|
giuliomoro@324
|
114 static MidiChannelMessage message;
|
giuliomoro@324
|
115 message = midi.getParser()->getNextChannelMessage();
|
giuliomoro@324
|
116 message.prettyPrint();
|
giuliomoro@324
|
117 switch(message.getType()){
|
giuliomoro@324
|
118 case kmmNoteOn: {
|
giuliomoro@324
|
119 int noteNumber = message.getDataByte(0);
|
giuliomoro@324
|
120 int velocity = message.getDataByte(1);
|
giuliomoro@324
|
121 int channel = message.getChannel();
|
giuliomoro@324
|
122 rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel);
|
giuliomoro@324
|
123 libpd_noteon(channel, noteNumber, velocity);
|
giuliomoro@324
|
124 }
|
giuliomoro@324
|
125 break;
|
giuliomoro@324
|
126 case kmmControlChange: {
|
giuliomoro@324
|
127 int channel = message.getChannel();
|
giuliomoro@324
|
128 int controller = message.getDataByte(0);
|
giuliomoro@324
|
129 int value = message.getDataByte(1);
|
giuliomoro@324
|
130 libpd_controlchange(channel, controller, value);
|
giuliomoro@324
|
131 }
|
giuliomoro@324
|
132 break;
|
giuliomoro@324
|
133 case kmmProgramChange:
|
giuliomoro@324
|
134 int channel = message.getChannel();
|
giuliomoro@324
|
135 int program = message.getDataByte(0);
|
giuliomoro@324
|
136 libpd_programchange(channel, program);
|
giuliomoro@324
|
137 break;
|
giuliomoro@324
|
138 }
|
giuliomoro@324
|
139 }
|
giuliomoro@230
|
140 static int inW = 0;
|
giuliomoro@230
|
141 static int outR = 0;
|
giuliomoro@230
|
142 /*
|
giuliomoro@230
|
143 * NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers
|
giuliomoro@230
|
144 * and the blocksize of Bela is the same as DEFDACBLKSIZE, then you probably
|
giuliomoro@230
|
145 * do not need the for loops before and after libpd_process_float, so you can save quite some
|
giuliomoro@230
|
146 * memory operations.
|
giuliomoro@230
|
147 */
|
giuliomoro@232
|
148 static int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels);
|
giuliomoro@232
|
149 // rt_printf("channelsInUse: %d, analogChannels in Use: %d\n", gChannelsInUse, analogChannelsInUse);
|
giuliomoro@232
|
150 for(unsigned int n = 0; n < context->audioFrames; ++n){ //pd buffers are interleaved
|
giuliomoro@232
|
151 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){ //first two channels are audio
|
andrewm@308
|
152 gInBuf[inW++] = audioRead(context, n, ch);
|
giuliomoro@230
|
153 }
|
giuliomoro@232
|
154 // then analogs
|
giuliomoro@232
|
155 // this loop resamples by ZOH, as needed, using m
|
giuliomoro@232
|
156 if(context->analogChannels == 8 ){ //hold the value for two frames
|
giuliomoro@232
|
157 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
158 gInBuf[inW++] = analogRead(context, n/2, analogCh); // n/2 wil be the same for n and n+1 when n is even
|
giuliomoro@232
|
159 }
|
giuliomoro@232
|
160 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@232
|
161 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
162 gInBuf[inW++] = analogRead(context, n, analogCh);
|
giuliomoro@232
|
163 }
|
giuliomoro@232
|
164 } else if(context->analogChannels == 2){ //drop every other frame
|
giuliomoro@232
|
165 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
andrewm@308
|
166 gInBuf[inW++] = analogRead(context, n*2, analogCh);
|
giuliomoro@232
|
167 }
|
giuliomoro@230
|
168 }
|
giuliomoro@230
|
169 if(inW == gBufLength * gChannelsInUse){
|
giuliomoro@230
|
170 inW = 0;
|
giuliomoro@230
|
171 }
|
giuliomoro@230
|
172 }
|
giuliomoro@232
|
173 // rt_printf("inW %d\n", inW);
|
giuliomoro@230
|
174 if(inW == 0){ //if the buffer is full, process it
|
giuliomoro@232
|
175 static int numberOfPdBlocksToProcess = gBufLength/DEFDACBLKSIZE;
|
giuliomoro@230
|
176 libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf);
|
giuliomoro@232
|
177 outR = 0; // reset the read pointer. NOTE: hopefully this is needed only the first time
|
giuliomoro@230
|
178 }
|
giuliomoro@230
|
179
|
giuliomoro@230
|
180 for(unsigned int n = 0; n < context->audioFrames; n++){ //pd buffers are interleaved
|
giuliomoro@232
|
181 for(unsigned int ch = 0; ch < context->audioChannels; ++ch){
|
andrewm@308
|
182 audioWrite(context, n, ch, gOutBuf[outR++]);
|
giuliomoro@230
|
183 }
|
giuliomoro@232
|
184 //and analogs
|
giuliomoro@232
|
185 if(context->analogChannels == 8){
|
giuliomoro@232
|
186 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
187 float analogOut = gOutBuf[outR++];
|
giuliomoro@232
|
188 if((n&1) == 0){//write every two frames
|
andrewm@308
|
189 analogWrite(context, n/2, analogCh, analogOut);
|
giuliomoro@232
|
190 } else {
|
giuliomoro@232
|
191 // discard this sample
|
giuliomoro@232
|
192 }
|
giuliomoro@232
|
193 }
|
giuliomoro@232
|
194 } else if(context->analogChannels == 4){ //write every frame
|
giuliomoro@232
|
195 for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
196 float analogOut = gOutBuf[outR++];
|
andrewm@308
|
197 analogWrite(context, n, analogCh, analogOut);
|
giuliomoro@232
|
198 }
|
giuliomoro@232
|
199 } else if(context->analogChannels == 2){ //write twice every frame
|
giuliomoro@232
|
200 for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
|
giuliomoro@232
|
201 float analogOut = gOutBuf[outR++];
|
andrewm@308
|
202 analogWrite(context, 2*n, analogCh, analogOut);
|
andrewm@308
|
203 analogWrite(context, 2*n + 1, analogCh, analogOut);
|
giuliomoro@232
|
204 }
|
giuliomoro@230
|
205 }
|
giuliomoro@230
|
206 if(outR == gBufLength * gChannelsInUse){
|
giuliomoro@230
|
207 outR = 0;
|
giuliomoro@230
|
208 }
|
giuliomoro@230
|
209 }
|
giuliomoro@232
|
210 // rt_printf("outR %d, analogChannelsInUse %d, channelsInUse %d\n",
|
giuliomoro@232
|
211 // outR , analogChannelsInUse, gChannelsInUse);
|
giuliomoro@230
|
212 }
|
giuliomoro@232
|
213
|
giuliomoro@230
|
214 // cleanup() is called once at the end, after the audio has stopped.
|
giuliomoro@230
|
215 // Release any resources that were allocated in setup().
|
giuliomoro@230
|
216
|
giuliomoro@301
|
217 void cleanup(BelaContext *context, void *userData)
|
giuliomoro@230
|
218 {
|
giuliomoro@230
|
219 free(gInBuf);
|
giuliomoro@230
|
220 free(gOutBuf);
|
giuliomoro@230
|
221 }
|