
-- these functions are called once every frame (automatically) -----------------------------------------------------------

local rk_parttrack
local rk_parts={}

function rk_init()
  local data= unpod( fetch("tracks.pod") )
  local pos= 0

  local function get16(cnt)
    cnt= cnt or 1
    local tmp= pos
    pos+=cnt
    return data:get(tmp, cnt)
  end

  local function getfloat()
    local frac,int= get16(),get16()
    return ((int<<16)+(frac&0xffff))/65536.0
  end

local function rk_loadtrack()
  local track={}

  local numkeys= get16()
  local lastval= 0
  for i=1,numkeys do
    local row= get16()
    local value= getfloat()
    local interp= get16()
    track[i]={row,value,interp}
    lastval= value
  end
  track[numkeys+1]= {0x7fff, lastval, 0}
  track[0]=1  -- current key
  return track
end

  local numparts= get16()

--  get16() -- number of tracks: must be 1 for part-track
  rk_parttrack= rk_loadtrack()

  -- read parts
  for partid=0,numparts-1 do
    local numtracks= get16()

    local tracks={}
    for trackid=0,numtracks-1 do
      tracks[trackid]= rk_loadtrack()
    end
    rk_parts[partid]= tracks
  end
end

function rk_update(params, row)

  local function rk_gettrack(track, row)
    if (track == nil) return 0
    local idx= track[0] or 0
    local num= #track
    while(idx<num and row>track[idx+1][1]) idx+=1

    track[0]=idx
    local key1=track[idx]
    local type=key1[3]
    if(type==0) return key1[2]
    local key2=track[idx+1]
    local t=(row-key1[1])/(key2[1]-key1[1])
    if(type==2) t=t*t*(3-2*t) 
    if(type==3) t*=t 
    return key1[2]+(key2[2]-key1[2])*t
  end

  local part= rk_gettrack(rk_parttrack, row)
  if part>=0 and part<#rk_parts then
    params[0]= row
    params[1]= part
    local tracks= rk_parts[part]
    for i=0,#tracks do
      params[i+2]= rk_gettrack(tracks[i], row)
    end
  end
  return part
end



function _init()
  vid(0)
  rk_init()
  msx_init()
end

local text=""
local row=0

function _update()
  local cnt= msx_update()

  local params={}
  part= rk_update(params, row)
  row+=0.01

  text=row.." ["..params[1].."] "
  for i=2,#params do
    local val= params[i]
    text= text..(flr(val*1000)/1000).."  "
  end

end

function _draw()
  cls(0)

  print(text)
end




--[[



function _init()
  vid(3)
  cls()

  msx_init()
end


function _update()
  framecnt+=1
end


local cpumax=0 

function _draw()


  cls()
  local t1= stat(1)
  local cnt= msx_update()
  local t2= stat(1)
  local cpu= flr((t2-t1)*1000)/10
  if (cpu>cpumax) cpumax=cpu

    local function getmix(buf, x)
      local v= buf[x]
      if (v>=128) v-=255
      return (v/500)
    end

  color(9)
  for c=0,3 do
    local buf= chnbuffer[c]
    local cnty= 32*c+23

    local v1= getmix(buf, 0) + cnty
    for x=1,240 do
      local v2= getmix(buf, x*2) + cnty
      line(x-1, v1, x, v2)
      v1= v2
    end
  end

  color(10)
  for c=0,3 do
    local chn= mod_channels[c]
    local cnty= 32*c+20
    print(chn.period.." "..chn.vol, 0, cnty-4)
  end

  color(15)
  print(" ord:"..mod_order..
        " row:"..mod_row..
        " tempo:"..bpm.."/"..ticks_per_line.."/"..mod_tick..
        " cpu:"..cpu.."("..cnt..")"..
        " buf:"..next_row
        ,
        0,0
  )

  -- alive?
--  print( chr( (framecnt&63)+65 ) )
--  print("conv:"..totalbuf/framecnt)
end

]]
