|
| 1 | +/* |
| 2 | +Based on \example Gui/sliders/render.cpp |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <Bela.h> |
| 6 | +#include <libraries/Oscillator/Oscillator.h> |
| 7 | +#include <libraries/Gui/Gui.h> |
| 8 | +#include <libraries/GuiController/GuiController.h> |
| 9 | +#include <libraries/OscSender/OscSender.h> |
| 10 | +#include <libraries/OscReceiver/OscReceiver.h> |
| 11 | +#include <cmath> |
| 12 | + |
| 13 | +Gui gui; |
| 14 | +GuiController controller; |
| 15 | +Oscillator oscillator; |
| 16 | + |
| 17 | +OscSender oscSender; |
| 18 | +OscReceiver oscReceiver; |
| 19 | +const char* remoteIp = "192.168.7.1"; |
| 20 | +const int localPort = 7562; |
| 21 | +const int remotePort = 7563; |
| 22 | + |
| 23 | +unsigned int gPitchSliderIdx; |
| 24 | +unsigned int gAmplitudeSliderIdx; |
| 25 | +int gPitch = 60; |
| 26 | +float gAmplitude = 0.1; |
| 27 | + |
| 28 | +void onReceive(oscpkt::Message* msg, void* arg) { |
| 29 | + |
| 30 | + if (msg->match("/pitch")) { |
| 31 | + |
| 32 | + int pitch; |
| 33 | + msg->match("/pitch").popInt32(pitch).isOkNoMoreArgs(); |
| 34 | + if (gPitch != pitch) { |
| 35 | + gPitch = pitch; |
| 36 | + controller.setSliderValue(gPitchSliderIdx, gPitch); |
| 37 | + printf("Pitch %i\n", gPitch); |
| 38 | + } |
| 39 | + |
| 40 | + } else if (msg->match("/amplitude")) { |
| 41 | + |
| 42 | + float amplitude; |
| 43 | + msg->match("/amplitude").popFloat(amplitude).isOkNoMoreArgs(); |
| 44 | + if (gAmplitude != amplitude) { |
| 45 | + gAmplitude = amplitude; |
| 46 | + controller.setSliderValue(gAmplitudeSliderIdx, gAmplitude); |
| 47 | + printf("Amplitude %f\n", gAmplitude); |
| 48 | + } |
| 49 | + |
| 50 | + } else { |
| 51 | + printf("Address not recognised\n"); |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +bool setup(BelaContext *context, void *userData) |
| 57 | +{ |
| 58 | + oscReceiver.setup(localPort, onReceive); |
| 59 | + oscSender.setup(remotePort, remoteIp); |
| 60 | + |
| 61 | + oscillator.setup(context->audioSampleRate); |
| 62 | + |
| 63 | + // Set up the GUI |
| 64 | + gui.setup(context->projectName); |
| 65 | + // and attach to it |
| 66 | + controller.setup(&gui, "Controls"); |
| 67 | + |
| 68 | + // Arguments: name, default value, minimum, maximum, increment |
| 69 | + // store the return value to read from the slider later on |
| 70 | + gPitchSliderIdx = controller.addSlider("Pitch (MIDI note)", gPitch, 48, 84, 1); // step is 1: quantized semitones |
| 71 | + gAmplitudeSliderIdx = controller.addSlider("Amplitude", gAmplitude, 0, 0.5, 0.0001); |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +void render(BelaContext *context, void *userData) |
| 76 | +{ |
| 77 | + // Access the sliders specifying the index we obtained when creating then |
| 78 | + int pitch = (int) controller.getSliderValue(gPitchSliderIdx); |
| 79 | + float amplitude = controller.getSliderValue(gAmplitudeSliderIdx); |
| 80 | + if (gPitch != pitch) { |
| 81 | + gPitch = pitch; |
| 82 | + oscSender.newMessage("/pitch").add(gPitch).send(); |
| 83 | + } |
| 84 | + if (gAmplitude != amplitude) { |
| 85 | + gAmplitude = amplitude; |
| 86 | + oscSender.newMessage("/amplitude").add(gAmplitude).send(); |
| 87 | + } |
| 88 | + |
| 89 | + float frequency = 440 * powf(2, (gPitch-69)/12); // compute the frequency based on the MIDI pitch |
| 90 | + oscillator.setFrequency(frequency); |
| 91 | + // notice: no smoothing for amplitude and frequency, you will get clicks when the values change |
| 92 | + |
| 93 | + for(unsigned int n = 0; n < context->audioFrames; n++) { |
| 94 | + float out = oscillator.process() * gAmplitude; |
| 95 | + for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) { |
| 96 | + // Write the sample to every audio output channel |
| 97 | + audioWrite(context, n, channel, out); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void cleanup(BelaContext *context, void *userData) |
| 103 | +{} |
0 commit comments