Playing multiple sound files simultaneously.

@dave1707 Thanks for this suggestion. However, I can still produce the problem by simply enlarging the size of the key – large enough to easily get two fat fingers on at the same time. @West had also suggested adding MOVING to key:touched, but the same problem happens.

In my accordion program, with many keys, I set the key size somewhat small and still get stuck keys. I’ve added code to insert the id numbers into a table but I don’t see any duplicates.

Hopefully this is not a “your iPad it too old” problem.

Still Stymied.

Thanks.

@Scotty I doubt that it’s a too old iPad problem. I haven’t tried this yet, but I wonder if checking if a key is already being pressed and ignore any other touches for that key.

@dave1707 As @West mentioned above, “Tilting the screen does not visit the touched function.”, that may be why I’m not seeing the duplicates in my idTable.

I’m storing them simply to figure out what’s going on. Perhaps, as @West mentioned above, I should store the “active” touches and not use an id that is currently “active”. That may be what he is referring to.

The way it stands right now is I can play a complete tune without a lockup and sometimes the second note locks up.

Thanks.

@Scotty In the draw key:draw() function, after self.k:stop(), maybe you should check the self.id and make sure it’s not 0 before setting self.k=sound. You need to check everywhere that self.k is getting set with a sound to make sure that it’s valid to set it.

@Scotty This is what I consider to be the fun part of writing code. Something isn’t working the way you want, and you have to figure out why. The more programs you write and the more problems you have to figure out, the more you learn. Each problem makes you a better programmer because you start to learn the pitfalls of writing code. And no matter how much you learn, you’ll always run into problems that you have to figure out. You’ll start writing more complex programs and it won’t seem that hard. They may not work exactly how you want them in the beginning, but they will eventually as you work out the problems.

@Scotty Here’s another version to try. I made the key big so multiple fingers will fit for the test. I made self.id a table to keep track of multiple fingers touching or releasing from the same key.

function setup()
    rectMode(CENTER)
    k1=key(100,200,"Game Sounds One:Wind 2","Game Sounds One:Crowd Boo")
end

function draw()
    background(40, 40, 50)
    k1:draw()
end

function touched(t)
    k1:touched(t)
end

key=class()

function key:init(x,y,s1,s2)
    self.x=x
    self.y=y
    self.w=200
    self.h=200
    self.g=g
    self.id={}  -- touch id
    self.s1=s1    -- sound 1 to play for this key
    self.s2=s2    -- sound 2 to play for this key
    self.k=nil  -- sound thats playing
end

function key:draw()
    if Gravity.x<0 then
        fill(255,0,0)
        if self.k~=nil and self.g==1 then
            self.k:stop()
            self.k=sound(self.s1,1,1,1,true)
        end
        self.g=-1
    else
        fill(0,255,0)
        if self.k~=nil and self.g==-1 then
            self.k:stop()
            self.k=sound(self.s2,1,1,1,true)
        end
        self.g=1
    end
    rect(self.x,self.y,self.w,self.h)
end

function key:touched(t)
    if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
            t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
        if t.state==BEGAN or t.state==MOVING then
            table.insert(self.id,t.id)
            if self.g<0 then
                if self.k==nil then
                    self.k=sound(self.s1,1,1,1,true)
                end
            else
                if self.k==nil then
                    self.k=sound(self.s2,1,1,1,true)
                end
            end
        end
    elseif t.state==MOVING then
        for z=#self.id,1,-1 do
            if self.id[z]==t.id then
                table.remove(self.id,z)
            end
        end 
        if #self.id==0 and self.k~=nil then
            self.k:stop()  
            self.k=nil 
        end
    end
    if t.state==ENDED then
        for z=#self.id,1,-1 do
            if self.id[z]==t.id then
                table.remove(self.id,z)
            end
        end 
        if #self.id==0 and self.k~=nil then
            self.k:stop()  
            self.k=nil 
        end
    end
end

@dave1707 This looks like a slick solution. I’ve modified my program and can now whale on the keys with only very infrequent lock-ups. Far fewer than before. However, my testing has come to an abrupt halt.

I added a few more sounds to the program but can not hear them. I tried re-syncing Dropbox and now cannot hear any of the sounds. Not a single one of the 58 sounds. I hope it’s just pilot error and not something Dropbox decided change that effects the functionality.

I’ve learned a great deal on this project and appreciate all your help. That’s a very creative use of tables. I’m starting to believe that there is nothing tables cannot do.

I’ve also learned that the last 10 percent of a project’s development takes 90 percent of the effort.

If I can get a program-generated collection of sounds, rather than my sampled sounds, I’ll send it your way.

Thanks again.

@dave1707 It was Pilot-error with Dropbox. My OCD tendencies made me put all my sound files in a separate folder just to be neat. Unfortunately, Codea could not find them. Life is good again.

…Scott

@Scotty Can you hear any sounds at all or just the ones you download from Dropbox. If it’s just the Dropbox sounds, can you download one of them to some other program and hear it. As for tables, they simplify a program a lot and are something that should be learned how to use. You say that you can still lockup a sound. I’ll have to add more keys to the above code and see if I can lock it up.

@Scotty I changed my code above to show 6 keys and the only way so far that I can get a stuck key is to touch the screen with 4 fingers which starts the sound but also allows me to slide the screen left or right to show other screens. Moving the current screen just a little affects the touch and locks the sound when I remove my fingers. Touching the screen with multiple fingers and sliding them is a built in option to do different things.

@dave1707 As far as Dropbox is concerned, I moved all the sounds back to the Codea folder and now all the sounds work just fine. Thanks for looking into that.

And for the locked keys issue, using four fingers and getting sloppy is probably the problem. I’ll play with the program and note what I’m doing when the lockup happens. I have two rows of keys. Ten in the key of F and 11 in the key of C so I have plenty to work with.

Thanks again.

@Scotty You might want to try updating

if t.state==ENDED then

to

if t.state==ENDED or t.state==CANCELLED then

If you run the multi touch demo as it is and do a five fingered pinch to close, then reopen Codea, you will see all (some) of the old finger circles frozen to the screen. If you update to include “or touch.state==CANCELLED” these old circles get flushed on the restart. You may be experiencing something similar?

@West @Scotty I added CANCELLED to my test code and when I try the 4 finger touch that allows me to slide the current screen left or right, all of the sounds do stop even though I still have my fingers on the keys. So I’m not sure which way is better, a stuck key or all the sounds suddenly stop. I don’t think there’s a way to override the 4 or 5 finger options. Maybe we just need to find a better option.

Yes it will depend on preference.

You can switch the 4/5 finger option off in the settings menu under Gestures. Other things (eg incoming calls and maybe notifications) might still cause an issue

@West Thanks for the Gestures info. I thought there was an option, but I couldn’t find it when I was looking for it yesterday. It seems like anytime there’s an iOS update, something gets moved and you have to start looking thru everything. If the Gestures are turned off, that might take care of the stuck key when using or not using the CANCELLED update.

In case it’s of help with designing your own solution, here’s an old write-up of how my touch handler code works.

https://loopspace.mathforge.org/HowDidIDoThat/Codea/TouchTutorial/

Sorry for the delayed response to all of your suggestions but a stretch of fine weather got me outdoors doing projects.

@West 's suggestion of changing the if statement to include CANCELLED as in: “if t.state==ENDED or t.state==CANCELLED then” seems to work. I don’t mind the sounds stopping if I slide off the keys, that’s way better than embarrassingly “stuck” key. Yes, without the “CANCELLED” portion of the if statement things behave in a bizarre fashion if I 4-finger gesture the app off the screen as West describes.

As a reminder, my program has a total of 29 keys: 21 treble keys and 8 base keys. It represents all the keys, or buttons, on my melodeon. Think small accordion. If you follow this link: https://m.youtube.com/watch?v=2zfsTxvm3yU you’ll get the idea of the instrument I’m trying to simulate. The sounds I use in my project were sampled from my melodeon and uploaded to Dropbox (all 58 sounds) so it sounds nearly identical to to the real thing.

I’ve tested the program by pressing the keys with four fingers of both left and right hand and tilting the iPad left and right, to simulate pushing and pulling. I’ve done this in both a wild, random fashion and by playing simple tunes. After three days of testing I’ve had NO key lockups!

Thank You, @dave1707 and @West . All your help if greatly appreciated. I still have much to learn but have learned a great deal on this project.

As for @LoopSpace , thank you as well, I will study your tutorial and, no doubt, learn more.

Thanks again.

By the way, that is NOT me playing that melodeon on YouTube.