|
| 1 | +#include <Bela.h> |
| 2 | +#include <libraries/OscSender/OscSender.h> |
| 3 | +#include <libraries/OscReceiver/OscReceiver.h> |
| 4 | +#include <cmath> |
| 5 | + |
| 6 | +OscSender oscSender; |
| 7 | +OscReceiver oscReceiver; |
| 8 | + |
| 9 | +const char* remoteIp = "192.168.7.1"; |
| 10 | +const int remotePort = 7563; |
| 11 | +const int localPort = 7562; |
| 12 | + |
| 13 | +const int OSC_PACKET_LEN = 300; |
| 14 | +const int OUT_CHANNELS = 2; |
| 15 | + |
| 16 | +std::vector<float> oscInBuffer; |
| 17 | +std::vector<float> oscOutBuffer; |
| 18 | + |
| 19 | +float gIn1, gIn2; |
| 20 | + |
| 21 | +int writePointer = 0; |
| 22 | +int packetsSent = 0; |
| 23 | +int gAudioFramesPerAnalogFrame = 0; |
| 24 | + |
| 25 | +void on_receive(oscpkt::Message* msg, void* arg) { |
| 26 | + if(msg->match("/bela")) { |
| 27 | + auto argReader = msg->match("/bela"); |
| 28 | + for (int i=0; i<OUT_CHANNELS * OSC_PACKET_LEN; i++) |
| 29 | + argReader.popFloat(oscInBuffer[i]); |
| 30 | + argReader.isOkNoMoreArgs(); |
| 31 | + } |
| 32 | + printf("Printing oscInBuffer: "); |
| 33 | + for(auto f : oscInBuffer) |
| 34 | + printf("%f ", f); |
| 35 | + printf("\n"); |
| 36 | +} |
| 37 | + |
| 38 | +bool setup(BelaContext *context, void *userData) { |
| 39 | + oscSender.setup(remotePort, remoteIp); |
| 40 | + oscReceiver.setup(localPort, on_receive); |
| 41 | + |
| 42 | + oscInBuffer.resize(OUT_CHANNELS * OSC_PACKET_LEN); |
| 43 | + oscOutBuffer.resize(OUT_CHANNELS * OSC_PACKET_LEN); |
| 44 | + |
| 45 | + if (context->analogFrames) |
| 46 | + gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames; |
| 47 | + |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +void render(BelaContext *context, void *userData) { |
| 52 | + for (unsigned int n = 0; n < context->audioFrames; ++n) { |
| 53 | + if (gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) { |
| 54 | + gIn1 = analogRead(context, n / gAudioFramesPerAnalogFrame, 0); |
| 55 | + gIn2 = analogRead(context, n / gAudioFramesPerAnalogFrame, 1); |
| 56 | + } |
| 57 | + |
| 58 | + oscOutBuffer[writePointer] = gIn1; |
| 59 | + oscOutBuffer[OSC_PACKET_LEN+writePointer] = gIn2; |
| 60 | + |
| 61 | + if (writePointer + 1 == OSC_PACKET_LEN) { |
| 62 | + oscSender.newMessage("/bela"); |
| 63 | + oscSender.add(packetsSent); |
| 64 | + for (auto v : oscOutBuffer) |
| 65 | + oscSender.add(v); |
| 66 | + oscSender.send(); |
| 67 | + packetsSent += 1; |
| 68 | + } |
| 69 | + writePointer = (writePointer+1)%OSC_PACKET_LEN; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void cleanup(BelaContext*context, void *userData){} |
0 commit comments