|
| 1 | +#include <Bela.h> |
| 2 | +#include <libraries/OscSender/OscSender.h> |
| 3 | +#include <libraries/OscReceiver/OscReceiver.h> |
| 4 | +#include "Resonator.h" // https://github.com/jarmitage/resonators |
| 5 | + |
| 6 | +OscReceiver oscReceiver; |
| 7 | +const char* remoteIp = "192.168.7.1"; |
| 8 | +const int localPort = 7562; |
| 9 | + |
| 10 | +Resonator res; |
| 11 | +ResonatorOptions options; // will initialise to default |
| 12 | + |
| 13 | +void onReceive(oscpkt::Message* msg, void* arg) { |
| 14 | + |
| 15 | + if(msg->match("/resonator")) { |
| 16 | + float freq, gain, decay; |
| 17 | + msg->match("/resonator") |
| 18 | + .popFloat(freq) |
| 19 | + .popFloat(gain) |
| 20 | + .popFloat(decay) |
| 21 | + .isOkNoMoreArgs(); |
| 22 | + res.setParameters(freq, gain, decay); |
| 23 | + res.update(); |
| 24 | + printf("Freq: %f, Gain: %f, Decay: %f\n", freq, gain, decay); |
| 25 | + |
| 26 | + } else if (msg->match("/resonator/freq")) { |
| 27 | + |
| 28 | + float freq; |
| 29 | + msg->match("/resonator/freq").popFloat(freq).isOkNoMoreArgs(); |
| 30 | + res.setParameter(0, freq); |
| 31 | + res.update(); |
| 32 | + printf("Freq: %f\n", freq); |
| 33 | + |
| 34 | + } else if (msg->match("/resonator/gain")) { |
| 35 | + |
| 36 | + float gain; |
| 37 | + msg->match("/resonator/gain").popFloat(gain).isOkNoMoreArgs(); |
| 38 | + res.setParameter(1, gain); |
| 39 | + res.update(); |
| 40 | + printf("Gain: %f\n", gain); |
| 41 | + |
| 42 | + } else if (msg->match("/resonator/decay")) { |
| 43 | + |
| 44 | + float decay; |
| 45 | + msg->match("/resonator/decay").popFloat(decay).isOkNoMoreArgs(); |
| 46 | + res.setParameter(2, decay); |
| 47 | + res.update(); |
| 48 | + printf("Decay: %f\n", decay); |
| 49 | + |
| 50 | + } else { |
| 51 | + printf("Message address not recognised\n"); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +bool setup (BelaContext *context, void *userData) { |
| 56 | + |
| 57 | + oscReceiver.setup(localPort, onReceive); |
| 58 | + |
| 59 | + res.setup(options, context->audioSampleRate, context->audioFrames); |
| 60 | + res.setParameters(440, 0.1, 0.5); // freq, gain, decay |
| 61 | + res.update(); // update the state of the resonator based on the new parameters |
| 62 | + |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +void render (BelaContext *context, void *userData) { |
| 67 | + |
| 68 | + for (unsigned int n = 0; n < context->audioFrames; ++n) { |
| 69 | + |
| 70 | + float in = audioRead(context, n, 0); // an excitation signal |
| 71 | + float out = 0.0f; |
| 72 | + |
| 73 | + out = res.render(in); |
| 74 | + |
| 75 | + audioWrite(context, n, 0, out); |
| 76 | + audioWrite(context, n, 1, out); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +void cleanup (BelaContext *context, void *userData) { } |
0 commit comments