Repetitive, but tight sound playback (like a metronome)

I’m coding a metronome app:

--Set up some variables; also, set up a sequence of bars in a table  
local bpm = 120  
local c = 0 --I use this to track beats instead of the timer.count property  
local d = 1 --Keep track of bars (which item in the table is used)  
local bar = {  
 {id = "Bar 1", beats = 4, subdivisions = 4},  
 {id = "Bar 2", beats = 3, subdivisions = 4},  
 {id = "Bar 3", beats = 3, subdivisions = 8},  
 {id = "Bar 4", beats = 4, subdivisions = 8},  
}  
  
--Define methods for starting and stopping the metronome  
function startMetronome()  
 -- Register to call t's timer method 50 times  
 local bpmAdjusted = (60000/bpm)\*(bar[1].subdivisions/bar[d].subdivisions)  
 timer.performWithDelay(bpmAdjusted, t, 0)--0 = call timer infinite number of times  
end  
  
function stopMetronome()  
 timer.cancel(tmr)  
 --Always start at beat 1  
 c = 0  
end  
  
function t:timer(event)  
 --Assign the timer to a variable so that it can be shut off easily by another method  
 tmr = event.source  
  
 -- Number of times the timer has counted off, just in case it is needed  
 local count = event.count  
  
 c = (c+1) % bar[d].beats  
  
  
 if c == 1 then  
 media.playEventSound(h) --created earlier in the script: the downbeat sound  
 end  
  
 if c ~= 1 then  
 media.playEventSound(l) --created earlier in the script: the other beats sound  
 end  
  
 --Reset the metronome and start again in order to change the BPM (cuz there is no method to change delay)  
 if c == 0 then  
 stopMetronome()  
 d = d + 1  
 d = d % #bar  
 if d % #bar == 0 then  
 d = #bar  
 end  
 startMetronome()  
 print((60000/bpm)\*(bar[1].subdivisions/bar[d].subdivisions))  
 end  
end  

I am finding that when tested on the device (iPhone 3G running iOS4), the playback just isn’t as tight as it should be. There are slight flutters in the click, especially when switching to another subdivision. So, this leads me to wonder: are the flutters evidence of some sort of limitation of Corona (not being able to prioritize the sound thread) or is there something I’m missing or doing incorrectly? Looking at the sample Objective-C Metronome App Apple provides, I noticed that there is specific code that prioritizes a thread for sound playback (which makes total sense in terms of making sure that enough attention is paid to the resource devoted to tight and repeated sound playback). I’m curious to know if this is something that can be accomplished with Corona. Anyone? I’m certainly open to my method being flawed within the context of Corona best-practices (and general coding practices).

p.s.: yes, I know that the last part of the code is a bit wonky (it cheats the last beat by an 8th). Regardless of that, the distance between beats still has some unwanted flutter.

thx,

hdez [import]uid: 7947 topic_id: 1789 reply_id: 301789[/import]

The media.playEventSound is really meant to play short systems sounds, which is why there is no volume control API and why may not work as expected in all cases. media.playSound is a better choice but it doesn’t support multiple players and has problems playing sounds less than 1 second in length. We are looking into other options to improve the sound API for the SDK and Game Edition. I know this doesn’t answer you question but we are aware of the limitations.

-Tom [import]uid: 7559 topic_id: 1789 reply_id: 5317[/import]

Thanks for the reply, Tom. I tried it with media.playSound by extending the sound until it was about 1.2 seconds. Performance was even worse but that’s ok. It was something to try and it just looks like I’ll have to roll up my sleeves and get working with Objective-C (or move on to another project that doesn’t have the same kind of requirement of the sound API).

Thanks for the great app framework!

hdez [import]uid: 7947 topic_id: 1789 reply_id: 5318[/import]