Playing multiple sound files simultaneously.

@Scotty Here’s what I think you were saying. One button, two sounds, two colors depending on which way the iPad is tilted. If this is what you want, I can probably make the code smaller because I did a quick chop job just to make it work this way.

function setup()
    rectMode(CENTER)
    k1=key(WIDTH/2,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=50
    self.h=50
    self.g=g
    self.id=0   -- 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 then
            self.id=t.id
            if self.g<0 then
                self.k=sound(self.s1,1,1,1,true)
            else
                self.k=sound(self.s2,1,1,1,true)
            end
        end
    end
    if t.state==ENDED and t.id==self.id then
        self.id=nil
        self.k:stop()  
        self.k=nil      
    end
end

@dave1707, your description of my problem looks to be spot on. I ran your modified example and that seems to work as advertised.

I control the color in draw( ) so it stands to reason I should control the sound there as well. I will modify my program and see how it works and report back. Hopefully I can figure out how to share Dropbox sound files then I can share the results.

By the way, accordions work just like a harmonica. Draw breath in produces one sound, blowing out produces a different sound on the same hole.

You can’t imagine how much I appreciate your help. I also will thank @West for his contribution. I’ve learned a great deal in this exercise.

…Scott

@Scotty As for sharing Dropbox files, there’s a Public folder in Dropbox. Any file you put in that folder gets it own link that can be shared with anyone, even non Dropbox users. Once you go into the Public folder, there’s a file there that explains how to do it. As for the accordion, I thought that the keys on one side was for pushing, and the other side for pulling. Now I know.

@Bri_G The Public folder seems to work. I moved a file there and then selected create a link. I then copied the link and was able to paste it in a test discussion. From the test discussion I selected the link which took me to the file.

@dave1707 - I thought the Dropbox public folder was no longer public. I think you can provide links from any Dropbox folder but need email addresses to give better access (listing/read/write).

@dave1707 and @Bri_G, thanks for the Dropbox tip. I had not seen the public folder. I’ll see if I can go public with my sound files.

If I can I’ll share the whole project. It’s fairly large because the accordion I’m simulating has 21 treble buttons and 8 bass buttons for a total of 58 sounds. I’m happy to share given how much guidance I received.

By the way, I could have spent 10 bucks and bought an accordion app from Apple’s App Store but I’m too frugal (cheap). But it would not have come with all the knowledge I’ve gained from this exercise.

Thanks again.

@Scotty The whole fun of it is learning to do it yourself. For me, the fun is figuring out how to help others with their code.

@Scotty Creating 58 Dropbox links and pasting them in a discussion might be a lot of trouble. I know I wouldn’t copy them into a folder so the Codea project could read them. A lot of times, I won’t even copy 1 file for an example project. Maybe you could ask how many users are interested before you do all that work.

Good point, @dave1707. If I had to link, copy and paste one file at a time, believe me, it wouldn’t get done.

I appreciate people like you who take the time to tackle a crazy project like mine.

@Scotty I don’t know how you created your sound files, but maybe similar files, just for your example, could be created mathematically. Set up a 58 count for loop and create 58 sound files based on some frequency calculation.

Thanks for the advice @dave1707, but believe it or not, I sampled all the sounds my accordion produces using an app called Hokusai. Then, after editing, I uploaded them into Dropbox. It didn’t take as long as you would imagine. After a quick re-sync I was in business.

Given the amount of effort that would be required to provide all the sound files I may just have to do as you suggest and create mathematically generated sounds. Knowing that every note in a musical scale is a specific frequency it may not be that big a deal.

By the way, I tending to believe @Bri_G when he says that Dropbox’s Public folder is no longer public. I’m seeing in their doco that as of September 2017 it’s no longer available for general use. Looks like they are asking for specific email addresses, as per Bri_G. If I can ask, when had you tried your quick Dropbox test? I’m wondering if things changed after your test.

More things to ponder, more things to learn.

Thanks again.

@Scotty I tried the Dropbox test a few days ago but I’ll try it here. I moved an image to the public folder, tapped the three dots on the right of the file, selected ‘share‘ from the list. It brought up an email type screen and in the lower right of the page was ‘create a link‘. I tapped that and it said a link was created and showed some options. One was copy link. I selected ‘copy link‘ and then pasted it below. It’s one of the Dropbox sample images of a parakeet. See if selecting the link below show a parakeet. I think I listed the correct steps.

Removed link and image from my public folder

Wow, Parakeet playing chess. That’s the image I see also. @dave1707, I’ll give it another shot. If it works I’ll upload the 8 sampled sounds for the 4-key test program. Knowing that you do not like to copy in more than a couple files I’ll understand if don’t want.

By the way, my 29-key program works quite well. One slight problem is that every so often a key will get “hung up” and stays locked in. It requires a restart of the program. I’m wondering how random those touch id’s are and maybe that’s causing the hang up.

@Scotty It might be possible to get duplicate id’s. I’ll have to run one of my other programs that does multiple id’s and see if a duplicate shows up. If not, I’ll load my above example and see if I get a stuck key with that. If I do, I can give you a fix for it.

@Scotty Duplicate id’s are happening. I wrote a quick test and after tapping the screen 40 times, only 2 id’s were being used. I was also able to tap the screen 100 times before a third id popped up.

@dave1707 I was starting to suspect as much. Thanks.

@Scotty Even though I’m getting duplicate ID’s with my test program, I don’t know if I can get a duplicate ID while one is being used. I don’t know how the ID’s are calculated, but it doesn’t seem to be a random number. I’ll have to try writing another test and see if I can get the same ID while it’s being used by another touch.

@Scotty I haven’t been able to get a duplicate ID of an ID that’s in use. I get plenty of duplicate ID numbers, but as long as they’re not being used then it doesn’t matter. As for the stuck key, then something isn’t working the way it should. I’ll see if I can make it happen in the code I gave you above.

@dave1707 Thanks. I can get the “struck key” to happen while running the 29-key version. I’ll try trapping the ID numbers in a table and look at them when it happens.

@Scotty I was able to get a stuck key with my single key program above. To do it, tap the button then slide your finger off the button and while still touching the screen, touch the button with another finger. Then lift your finger off the button, then lift the other finger. The sound will still be playing even though nothing is touching the screen. I changed the above code to this and I think the only thing that changed was in the key:touched function.

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=50
    self.h=50
    self.g=g
    self.id=0   -- 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 then
            self.id=t.id
            if self.g<0 then
                self.k=sound(self.s1,1,1,1,true)
            else
                self.k=sound(self.s2,1,1,1,true)
            end
        end
    elseif t.state==MOVING and t.id==self.id then
        self.id=0
        self.k:stop()  
        self.k=nil      
    end
    if t.state==ENDED and t.id==self.id then
        self.id=0
        self.k:stop()  
        self.k=nil      
    end
end