Help with some logic ofSoundPlayer toggle button

Hello everyone,
I am trying to make a simple app with a button that plays a sequence of mp3.
It should be simple, right. But I am having a strange behaviour and I can’t find the problem so I hope someone can help me.

what I am expecting:
the central toggle button (key == spacebar) on active state should activate a sequence of notes (ofSoundPlayers).
what happens:
the sequence starts after I deactivate the toggle.

It is like the toggle is inverted, however it is not…

I am enclosing the source files if anyone has some spare time and wants to have a look.
it shouldn’t really be a big problem but I can’t see where the code is wrong!

I do have some issues with ofSoundPlayer sequence but I’ll leave that for later!

I hope someone can make light on this

Cheers! (1.5 MB)

Hi, I didn’t run the code, just read it, but the logic is a bit confusing.
Your code performance and cleanliness will improve a lot if you use events, both for the button registering to the mouse and keyboard as well as for the button notifying a change of state.
The button code has some redundancies like bActive and bToggle that could bring errors of the kind you are experiencing.

just for debugging purposes, dont use the buttons at all, comment these out of the ofApp code and check if the logic of the ofApp works just using a key press. If it is fine then probably the problem is in the button code…

BTW, If you put your code inline it is much easier to review than attaching the files.


Hello @roymacdonald thanks for replying.
Yes, the button code has some redundancy. those bool flags were initially used to understand when the code started and finished… probably bad practice as, I agree, it’s very confusing and probably the issue is there… still couldn’t find it!

For debugging I am actually just using the keyboard keys to toggle the buttons…
I’ll clean it up and get back.

last thing…
I don’t understand what you mean with > events, both for the button registering to the mouse and the keyboard


I mean that for debugging dont use at all the buttons, take them out of your code. (you dont need to delete these just comment these out). add some bool flags that you’ll need to check state. if this works as expected you will be sure that the problem is in the button. if it doesn´t fix it and only add buttons when it is working correctly.

There is an events system running in OF, it is used internally for a lot of things and you can use it for whatever you might want to. The idea is that a certain action hapenning somewhere in the code notifies a new event. If there´s any other piece of code that registered (through ofAddListener(…) for example) to listener to this event it will be notified and a specific function will get triggered.

take a look at the examples. theres a folder with events examples.

Oh, I see!
cool, I’ll have a look.
thanks Roy

Just an update.
all seems to work nicely for now.
I have refreshed the code, cleaned it and simplified it. I didn’t use the ofEvents at the end. I think as @roymacdonald said one of the issues was having too many bool flags creating conflicts here and there… but also, I realised that the keyboard and mouse functions are actually treated as events, correct me if I am wrong.
This is why if I use a keyRelease to call an function that needs an update that function will never be updated as it happens just at the release… KeyRelease is enough as event for now.

the code is a work in progress and available on my github if anyone wants to have a look and tinkering with it.
Thanks for your help!

right, mouse and keys are driven by events.
I’ve been writting a new chapter for the ofBook about events. It’s still unmerged but you can read it here.

I dont get what you mean for the keyReleased stuff. you can call any function you want from there.


Hi @roymacdonald, maybe it was obvious but I just realised it now. what I mean is that on KeyReleased() you can call a function just once, that is the time you release the ‘key’. If that function needs to run in loop, the same function needs to go in the ofApp::update().
In my specific case: on aey release I want a sequence of samples to play one after another.
I have a function playSequence() with a counter in it. If I call this function in the keyRelease() that will be executed just once and the counter will ++ just once.

void ofApp::playSequence(){
    //this function runs in the update loop so that the file can play
    //check vector size
    if (player.size() > 0 && randomSequence.size() > 0) {
        if (playerCounter < counter - 1){

workaround this is to launch the on a keyRelease() and flagging it as started. Then controlling the counter and the sequence in the update() function.
At least, this is how I understand it should work.
I hope what I said makes sense.
Thanks for the book chapter.


Good to know you sorted it out.
What you describe is a very common pattern; just using the key event to flag a change and perform the change in the update loop.
Let me know if I can be of more help.


all good for now! thank you very much!:beers: