Audio performance using Separate Channels

Using this code below, I have two sounds where a_sound uses Channel 1 and b_sound uses Channel 2.  

When I touch the graphics I get the corresponding correct sounds.  No problem.

BUT if I try and touch both graphics at the same time triggering both sounds simultaneously,  I only hear one of the sounds.   I thought if I put each sound on its own channel, I’d be able to hear the sounds together.

I should also mention I have a music track set up on Channel 21.  I can play that music track and then tap the graphics above to the music track.  So, it is possible to hear multiple sounds together but I can’t START two sounds together.

Is it because only one “began” of the event.phase is allowed at a time??

Here’s the test code I have:

local a_btn = display.newImage( “images/a_red_btn.png”, 105, 70 ) btnGroup:insert(a_btn)

local a_btnPush = display.newImage( “images/a_red_btnPush.png”, 105, 70 ); a_btnPush.alpha = 0, btnGroup:insert(a_btnPush)

local b_btn = display.newImage( “images/b_blue_btn.png”, 315, 70 ) btnGroup:insert(b_btn)

local b_btnPush = display.newImage( “images/b_blue_btnPush.png”, 315, 70 ); b_btnPush.alpha = 0, btnGroup:insert(b_btnPush)

a_sound = audio.loadSound( “sounds/a_sound.m4a” )

b_sound = audio.loadSound( “sounds/b_sound.m4a” )

local function showPush_a ( event )

    if event.phase == “began” then

        audio.play( a_sound, {channel=1} )

        a_btn.alpha = 0

        a_btnPush.alpha = 1

    elseif event.phase == “ended” then

        a_btn.alpha = 1

        a_btnPush.alpha = 0

    end

end

a_btn:addEventListener( “touch”, showPush_a )

a_btnPush:addEventListener( “touch”, showPush_a )

local function showPush_b ( event )

        if event.phase == “began” then

            b_btn.alpha = 0

            b_btnPush.alpha = 1

            audio.play( b_sound, {channel=2} )

        elseif event.phase == “ended” then

            b_btn.alpha = 1

            b_btnPush.alpha = 0

        end

    end

    b_btn:addEventListener( “touch”, showPush_b )

    b_btnPush:addEventListener( “touch”, showPush_b )

Hi @rshanlon,

I’m a bit confused with how/why you’re using two overlapping buttons in each case, and swapping their alpha and such. Can you explain more clearly how this scenario is intended to work? Are these supposed to be simple on/off buttons, with an “on” state and an “off” state? If so, you should consider just using a widget button for each, for simplicity.

Regards,

Brent

Okay, I’ll check out the widgets then.  

That said, how about the Channels and the ability to play two sounds at the same time during the “began” phase. Is this possible?  With widgets, will this be possible much like a drummer can hit the snare and cymbals at the same time and both sounds happen simultaneously.

I tried this with both sounds on separate channels but no luck playing them at the exact same time.

If both sounds are loaded in advance (which they typically should be), playing them at the same time should be simple.

Brent

Thanks for the reply, Brent.  

Yeah, it seems very straight forward that putting them on separate channels should allow immediate simultaneous play of both audio files when the “touch” is applied to both graphics.  But I can’t get the two audio files to play together.  Everytime I try, only one audio file responds.

Just to be clear, I’m trying to play both sounds at the same time using the above code.  Granted it’s not a widget but it should still work since the audio channels have been indicated for each sound.

So, I went ahead and made widgets instead and assigned channels as seen below.  But I get the same behavior.    I touch both graphics simultaneously and I can’t get both of them to sound together.  They play separately just fine.

All of this is done on the actual iPad.  I’m using the latest build of Corona.

Here’s the basic code I’m using:

display.setStatusBar( display.HiddenStatusBar )

local widget = require( “widget” )

–Background

local background = display.newImage( “background.jpg” )

background.x = display.contentWidth /2

background.y = display.contentHeight /2

local a_sound = audio.loadSound( “a_sound.m4a” )

local b_sound = audio.loadSound( “b_sound.m4a” )

–Functions 

local function play_a_btn ( event )

    if ( “began” == event.phase ) then

    audio.play (a_sound, {channel=1})

    end

end

local function play_b_btn ( event )

    if ( “began” == event.phase ) then

    audio.play (b_sound, {channel=2})

    end

end

local a_btn = widget.newButton

{

    left = 105,

    top = 70,

    width = 120,

    height = 121,

    defaultFile = “a_red_btn.png”,

    overFile = “a_red_btnPush.png”,

    onEvent = play_a_btn

}

local b_btn = widget.newButton

{

    left = 315,

    top = 70,

    width = 120,

    height = 121,

    defaultFile = “b_blue_btn.png”,

    overFile = “b_blue_btnPush.png”,

    onEvent = play_b_btn

}

Hi @rshanlon,

Have you enabled multi-touch in your app? If not, then the system itself won’t process both touches at exactly the same time.

Brent

That did the trick!  Thanks!

For others seeking the same info, this needs to be in your code:

system.activate( “multitouch” )

Hi @rshanlon,

I’m a bit confused with how/why you’re using two overlapping buttons in each case, and swapping their alpha and such. Can you explain more clearly how this scenario is intended to work? Are these supposed to be simple on/off buttons, with an “on” state and an “off” state? If so, you should consider just using a widget button for each, for simplicity.

Regards,

Brent

Okay, I’ll check out the widgets then.  

That said, how about the Channels and the ability to play two sounds at the same time during the “began” phase. Is this possible?  With widgets, will this be possible much like a drummer can hit the snare and cymbals at the same time and both sounds happen simultaneously.

I tried this with both sounds on separate channels but no luck playing them at the exact same time.

If both sounds are loaded in advance (which they typically should be), playing them at the same time should be simple.

Brent

Thanks for the reply, Brent.  

Yeah, it seems very straight forward that putting them on separate channels should allow immediate simultaneous play of both audio files when the “touch” is applied to both graphics.  But I can’t get the two audio files to play together.  Everytime I try, only one audio file responds.

Just to be clear, I’m trying to play both sounds at the same time using the above code.  Granted it’s not a widget but it should still work since the audio channels have been indicated for each sound.

So, I went ahead and made widgets instead and assigned channels as seen below.  But I get the same behavior.    I touch both graphics simultaneously and I can’t get both of them to sound together.  They play separately just fine.

All of this is done on the actual iPad.  I’m using the latest build of Corona.

Here’s the basic code I’m using:

display.setStatusBar( display.HiddenStatusBar )

local widget = require( “widget” )

–Background

local background = display.newImage( “background.jpg” )

background.x = display.contentWidth /2

background.y = display.contentHeight /2

local a_sound = audio.loadSound( “a_sound.m4a” )

local b_sound = audio.loadSound( “b_sound.m4a” )

–Functions 

local function play_a_btn ( event )

    if ( “began” == event.phase ) then

    audio.play (a_sound, {channel=1})

    end

end

local function play_b_btn ( event )

    if ( “began” == event.phase ) then

    audio.play (b_sound, {channel=2})

    end

end

local a_btn = widget.newButton

{

    left = 105,

    top = 70,

    width = 120,

    height = 121,

    defaultFile = “a_red_btn.png”,

    overFile = “a_red_btnPush.png”,

    onEvent = play_a_btn

}

local b_btn = widget.newButton

{

    left = 315,

    top = 70,

    width = 120,

    height = 121,

    defaultFile = “b_blue_btn.png”,

    overFile = “b_blue_btnPush.png”,

    onEvent = play_b_btn

}

Hi @rshanlon,

Have you enabled multi-touch in your app? If not, then the system itself won’t process both touches at exactly the same time.

Brent

That did the trick!  Thanks!

For others seeking the same info, this needs to be in your code:

system.activate( “multitouch” )