BelaPlatform / Bela

Bela: core code, IDE and lots of fun!
Other
490 stars 139 forks source link

GuiController.setSliderValue does not work #681

Closed jarmitage closed 2 years ago

jarmitage commented 2 years ago

Test below lightly adapted from the Gui sliders example

No change seen in the Gui.

#include <Bela.h>
#include <libraries/Oscillator/Oscillator.h>
#include <libraries/Gui/Gui.h>
#include <libraries/GuiController/GuiController.h>
#include <cmath>

Gui gui;
GuiController controller;
Oscillator oscillator;

int count = 0;
int countmax = 44100;

unsigned int gPitchSliderIdx;
unsigned int gAmplitudeSliderIdx;

bool setup(BelaContext *context, void *userData)
{
    oscillator.setup(context->audioSampleRate);

    // Set up the GUI
    gui.setup(context->projectName);
    // and attach to it
    controller.setup(&gui, "Controls");

    // Arguments: name, default value, minimum, maximum, increment
    // store the return value to read from the slider later on
    gPitchSliderIdx = controller.addSlider("Pitch (MIDI note)", 60, 48, 84, 1); // step is 1: quantized semitones
    gAmplitudeSliderIdx = controller.addSlider("Amplitude", 0.1, 0, 0.5, 0.0001);
    return true;
}

void render(BelaContext *context, void *userData)
{

    // Access the sliders specifying the index we obtained when creating then
    float pitch = controller.getSliderValue(gPitchSliderIdx);
    float amplitude = controller.getSliderValue(gAmplitudeSliderIdx);

    float frequency = 440 * powf(2, (pitch-69)/12); // compute the frequency based on the MIDI pitch
    oscillator.setFrequency(frequency);
    // notice: no smoothing for amplitude and frequency, you will get clicks when the values change

    for(unsigned int n = 0; n < context->audioFrames; n++) {
        if (++count >= countmax) {
            controller.setSliderValue(gPitchSliderIdx, 65);
            controller.setSliderValue(gAmplitudeSliderIdx, 0.4);
            printf("update sliders\n");
            count=0;
        }
        float out = oscillator.process() * amplitude;
        for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
            // Write the sample to every audio output channel
            audioWrite(context, n, channel, out);
        }
    }
}

void cleanup(BelaContext *context, void *userData)
{}
giuliomoro commented 2 years ago

correct. I just somehow fixed this in the latest dev. @adanlbenito can you have a look if I did anything silly?

giuliomoro commented 2 years ago

btw @jarmitage you should not call GuiController::setSliderValue() from the audio thread, as it creates a JSON, thus allocates memory, and then it writes to the socket, so it is bad for real time (as denoted by the mode switches you will get).

This is more in general true of sendControl() as well.

jarmitage commented 2 years ago

Ok! Love that commit message! 😄

And yes I did notice the mode switches, thanks for the tip

jarmitage commented 2 years ago

Can confirm this works for me now