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]