Trouble with setBackgroundColours while streaming


#1

Hi all,

I’ve just started exploring Juce’s capabilities beyond simple buttons + sliders, and recently I’ve been trying to make a DrawableButton blink using setBackgroundColours. I’m using juce with portaudio, but I don’t think my simple audio streams are related to my trouble here.

Below is part of my button handling code - basically, I want to play a beep twice (Pa_StartStream( stream ) plays a 500 ms beep) while flashing a DrawableButton (leftBlinker) in sync with the beeps.

        leftBlinker->setBackgroundColours (Colours::yellow, Colours::yellow);
        Pa_StartStream( stream );
        sleep(500);
        Pa_StopStream( stream );
        leftBlinker->setBackgroundColours (Colours::slategrey, Colours::slategrey);
        sleep(100);
        leftBlinker->setBackgroundColours (Colours::yellow, Colours::yellow);
        Pa_StartStream( stream );
        sleep(500);
        Pa_StopStream( stream );
        leftBlinker->setBackgroundColours (Colours::slategrey, Colours::slategrey);

Is there a better way to do simple animations like this in Juce? It seems like what I’m doing must be wrong. If I simplify the code to a single color change (below), the button doesn’t turn yellow until after the beep has played:

        leftBlinker->setBackgroundColours (Colours::yellow, Colours::yellow);
        Pa_StartStream( stream );
        sleep(500);
        Pa_StopStream( stream );

Thanks,

Ben


#2

Ouch! If you write code that sits there blocking the message thread, then no UI updates or events can possibly happen until your stuff returns and allows everything to carry on… You need to use a timer - if you’re new to event-based programming, take a look at the demo apps and try to get a feel for how everything works.

(…but why would you mix juce with portaudio, anyway? If there’s anything that PA does which my stuff doesn’t, just let me know and I’ll add it!)


#3

Thanks Jules! I moved the blinking to a timer callback and it works beautifully.

I’m using portaudio only because i’m not very familiar with juce’s audio classes. I may switch over to juce if i need to read wavs…