One of my students is trying to make a drum loop machine and it has a major memory leak.
I copied code from the Codea Drum Machine example and tried tweaking it for Corona.
I commented out some of the areas to show the basic grid with the playhead looping. I printed collectgarbage(“count”) to show the memory building – it’s outrageous. It works perfect on the Codea app, though.
Any ideas as to why it plays slowly to begin with but also lags as time goes on (because of the memory leak)?
thanks.
[lua]display.setStatusBar(display.HiddenStatusBar)
local currentStep = 1
local t = 0
local numChannels = 4
local numSteps = 16
– multiply by 4 to convert from quarter notes to sixteenth notes
local bpm = 60/8
local pattern = {}
–watch(“currentStep”)
– fill the pattern with empties to begin with
for i = 1, numChannels do
pattern[i] = {}
for j = 1, numSteps do
pattern[i][j] = false
end
end
local background = display.newRect( 0, 0, 320, 480)
background.strokeWidth = 0
background:setFillColor( 0, 0, 0 )
background:setStrokeColor( 0, 0, 0 )
– This function gets called once every frame
local floor = math.floor
function drawgrid()
t = t+1
local fps = 30
local currentSecond = t/fps
local beatsPerSecond = bpm/60
local newStep = floor(currentSecond/beatsPerSecond)%16
if newStep ~= currentStep then
currentStep = newStep
–triggerStep(currentStep) - play sound
end
local w = display.contentWidth / numSteps
local h = display.contentHeight / numChannels
– print(“here”)
for i=1,numChannels do
for j=1,numSteps do
x, y = (j-1)*w, (i-1)*h
–noStroke();
local rect = display.newRect(x, y, w, h)
rect:setFillColor(239, 116, 146)
rect.strokeWidth = 10
rect:setStrokeColor(0, 0, 0)
if pattern[i][j] then
rect:setFillColor(239, 116, 146)
else
rect:setFillColor(131, 107, 64)
end
–rect = display.newRect(x, y, w, h)
– if this is the current step, then draw a stroke around it
– print(j-1, currentStep)
if j-1 == currentStep then
rect.strokeWidth = 10
rect:setStrokeColor(43, 229, 78)
rect = display.newRect(x, y, w, h)
end
end
end
–[[
if CurrentTouch.state == BEGAN and not touchHandled then
touchHandled = true
checkFingerDown(CurrentTouch.x, CurrentTouch.y)
elseif CurrentTouch.state == ENDED then
touchHandled = false
end
–]]
collectgarbage()
print( "MemUsage: " … collectgarbage(“count”) )
local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000
–print( "TexMem: " … textMem )
–t=nil
–print(t)
print(newstep)
print(currentStep)
print(numChannels)
print(numSteps)
return true
end
–[[
function checkFingerDown(x, y)
– convert the finger to the grid location
x = math.floor(x/WIDTH*numSteps)
y = math.floor(y/HEIGHT*numChannels)
if pattern[y+1][x+1] then
pattern[y+1][x+1] = false
else
pattern[y+1][x+1] = true
end
end
function triggerStep(step)
local chanMap = {SOUND_BLIT, SOUND_JUMP, SOUND_PICKUP, SOUND_SHOOT}
for i=1,numChannels do
if pattern[i][step+1] then
sound(chanMap[i], 1);
end
end
end
–]]
timer.performWithDelay( 33, drawgrid, 0 )
–Runtime:addEventListener( “enterFrame”, drawgrid )[/lua] [import]uid: 41353 topic_id: 22550 reply_id: 322550[/import]
