|
| 1 | +#include <stdlib.h> |
| 2 | +#include <Bela.h> |
| 3 | +#include <libraries/OscSender/OscSender.h> |
| 4 | +#include <libraries/OscReceiver/OscReceiver.h> |
| 5 | +#include "ResonatorBank.h" // https://github.com/jarmitage/resonators |
| 6 | + |
| 7 | +OscReceiver oscReceiver; |
| 8 | +const char* remoteIp = "192.168.7.1"; |
| 9 | +const int localPort = 7562; |
| 10 | + |
| 11 | +ResonatorBank resBank; |
| 12 | +ResonatorBankOptions resBankOptions; |
| 13 | +std::vector<ResonatorParams> resModelParams; |
| 14 | +int resModelSize = 20; |
| 15 | +bool resModelReceived = false; |
| 16 | + |
| 17 | +float impulseInterval = 44100; |
| 18 | +float impulseWidth = 0.1; |
| 19 | +float impulseCount = 0; |
| 20 | +bool audioInput = false; |
| 21 | + |
| 22 | +void onReceive(oscpkt::Message* msg, const char* addr, void* arg) { |
| 23 | + |
| 24 | + if(msg->match("/resonators")) { |
| 25 | + |
| 26 | + printf("Model received...\n"); |
| 27 | + auto argReader = msg->match("/resonators"); |
| 28 | + for (int i = 0; i < resModelSize; i++) { |
| 29 | + argReader.popFloat(resModelParams[i].freq); |
| 30 | + argReader.popFloat(resModelParams[i].gain); |
| 31 | + argReader.popFloat(resModelParams[i].decay); |
| 32 | + } |
| 33 | + argReader.isOkNoMoreArgs(); |
| 34 | + printf("\n"); |
| 35 | + |
| 36 | + resBank.setBank(resModelParams); |
| 37 | + resBank.update(); |
| 38 | + resModelReceived = true; |
| 39 | + |
| 40 | + } else { |
| 41 | + printf("Message address not recognised\n"); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +bool setup (BelaContext *context, void *userData) { |
| 46 | + |
| 47 | + oscReceiver.setup(localPort, onReceive); |
| 48 | + |
| 49 | + resModelParams.resize(resModelSize); |
| 50 | + resBankOptions.total = resModelSize; |
| 51 | + resBank.setup(resBankOptions, context->audioSampleRate, context->audioFrames); |
| 52 | + |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +void render (BelaContext *context, void *userData) { |
| 57 | + |
| 58 | + if (resModelReceived) { |
| 59 | + |
| 60 | + for (unsigned int n = 0; n < context->audioFrames; ++n) { |
| 61 | + |
| 62 | + float in = 0.0f; |
| 63 | + |
| 64 | + if (audioInput) in = audioRead(context, n, 0); // an excitation signal |
| 65 | + else { |
| 66 | + if (++impulseCount >= impulseInterval * (1-impulseWidth)) |
| 67 | + in = 1.0f * ( rand() / (float)RAND_MAX * 2.f - 1.f ); // noise |
| 68 | + if (impulseCount >= impulseInterval) impulseCount = 0; |
| 69 | + } |
| 70 | + float out = 0.0f; |
| 71 | + |
| 72 | + out = resBank.render(in); |
| 73 | + |
| 74 | + audioWrite(context, n, 0, out); |
| 75 | + audioWrite(context, n, 1, out); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +void cleanup (BelaContext *context, void *userData) { } |
0 commit comments