View Single Post
Old 11-22-2016, 03:04 AM   #4
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

Code:
  FX_chain_file_path = [[C:\001.RfxChain]]
  track_id = 1
  
  function ExtractBracketsBlock(str)
    local s = ''
    local count = 1
    count_lines = 0
    for line in str:gmatch('[^\n]+') do
      count_lines = count_lines + 1
      s = s..'\n'..line
      if line:find('<') then count = count +1 end
      if line:find('>') then count = count -1 end 
      if count == 1 then return s, count_lines end     
    end
  end  
  
  -- get some chain file, ex. from GetUserFileForRead()
    file = io.open(FX_chain_file_path)
    external_FX_chain_content = file:read('a')
    file:close()
  
  -- get track chunk
    track = reaper.GetTrack(0,track_id-1)
    _, chunk = reaper.GetTrackStateChunk(track, '', true)

  -- split chunk by lines into table
    local t = {} 
    for line in chunk:gmatch('[^\n]+') do       if line:find('<FXCHAIN') then fx_chain_id0 = #t end       t[#t+1] = line     end      

  --  find size of FX chain and where it placed
    _, cnt_lines = ExtractBracketsBlock(chunk:match('<FXCHAIN.*'))
    fx_chain_id1 = fx_chain_id0 + cnt_lines -1
  
  -- insert FX chain
    new_chunk = table.concat(t,'\n',  1, fx_chain_id1)..'\n'..
                external_FX_chain_content..
                table.concat(t,'\n',  fx_chain_id1)
  
  -- apply new chunk                
    reaper.SetTrackStateChunk(track, new_chunk, false) 
    reaper.UpdateArrange()

Last edited by mpl; 11-22-2016 at 12:00 PM.
mpl is offline   Reply With Quote