Snippets (Ledger)


(Ledger) #1

Thought I`d try and improve the re-usability of my code, so just a thread for dumping some functions that might be useful to people. If not at least I might be able to find them as my Lua folders have become a bit of a mess :slight_smile:

NOTE: may need to add error checks but should work out of the box if used as advertised:


–function to keep the dsp devices from 2 tracks in sync. It first checks for changes between the template track
–and the target track. If changes are found the target track devices are trashed then re-copied from the template track.
–function takes 2 int parameters for the two track indexes: template_track_index and target_track_index

Click to view contents
-------------------------------------
--function to keep the dsp devices from 2 tracks in sync. It first checks for changes between the `template track`
--and the `target track`. If changes are found the target track devices are trashed then re-copied from the template track.
--function takes 2 int parameters for the two track indexes: template_track_index and target_track_index
-------------------------------------
local function sync_track_devices(template_track_index , target_track_index)
 
  --loadable devices start at 2, (renoise mixer device is index 1)
  local FIRST_DEVICE_INDEX = 2
  --song object
  local song = renoise.song()
  --track objects
  local template_track = song:track(template_track_index)
  local target_track = song:track(target_track_index)
 
  --flag to set whether devices need refreshing/ reordering/ reoloading
  local refresh_devices = false
 
  --first do checks
  --------------------------------------
  --1) make sure number of devices in each send chain matches up, set flag if not
  if #template_track.devices ~= #target_track.devices then
    refresh_devices = true
  end
 
  --2) Check the order, that device types/names match up (skip this loop if flag already triggered to true)
  if refresh_devices == false then
    --loop devices
    for i = FIRST_DEVICE_INDEX, #template_track.devices do
      --make sure presets are already matched
      if template_track:device(i).name ~= target_track:device(i).name then
        refresh_devices = true
        break
      end
    end
  end
 
  --3) match all active/bypassed states, done by default rather than triggering final loop
  if refresh_devices == false then
    for i = FIRST_DEVICE_INDEX,#template_track.devices do
 if target_track:device(i).is_active ~= template_track:device(i).is_active then
 target_track:device(i).is_active = template_track:device(i).is_active
 end
    end
  end
 
  --return if flag not tripped, else reload/ copy template track devices to target track
  if refresh_devices == false then
    return
  else
    --loop backwards through target devices and delete them
    for i = #target_track.devices,FIRST_DEVICE_INDEX,-1 do
      target_track:delete_device_at(i)
    end
    --copy fx from template track to target track
    for i = FIRST_DEVICE_INDEX,(#template_track.devices) do
      --copy device
      target_track:insert_device_at(template_track:device(i).device_path, i)
      --copy preset data
      target_track:device(i).active_preset_data = template_track:device(i).active_preset_data
      --copy enabled state
      target_track:device(i).is_active = template_track:device(i).is_active
    end
  end
end--sync_track_devices()

(Ledger) #2

–function to write a renoise pattern[].line from a string representation.
–(arg 1: the target renoise pattern_track_line object, arg2: line in string form) i.e.
–(arg 1: renoise.song():pattern(1):track(2):line(9), arg2: C-40020…0000 | D#40030…0000 | G-400…0000 | —…etc.)
–passed string must be a complete line of 269 chars

Click to view contents
-------------------------------------------------------------------------------
  --function to write a renoise pattern line from a string representation.
  --(arg 1: the target renoise pattern_track_line object, arg2: line in string form) i.e.
  --(arg 1: renoise.song():pattern(1):track(2):line(9), arg2: C-40020....0000 | D#40030....0000 | G-400......0000 | ---..etc.) 
  --passed string must be a complete line of 269 chars
  -------------------------------------------------------------------------------
  local function write_line_string_to_pattern_line(target_line_ob,cur_line_string)  
  --------------------------------------------------
    local cur_col = target_line_ob:note_column(1)
    local sub = string.sub
    
    cur_col.note_string = sub(cur_line_string,1,3) -- using string.sub `chop` the saved line up and add chopped parts into pattern (sub is a local declared earlier)
    cur_col.instrument_string = sub(cur_line_string,4,5)
    cur_col.volume_string = sub(cur_line_string,6,7)
    cur_col.panning_string = sub(cur_line_string,8,9)
    cur_col.delay_string = sub(cur_line_string,10,11)
    cur_col.effect_amount_string = sub(cur_line_string,12,13)
    cur_col.effect_amount_string = sub(cur_line_string,14,15)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(2)
       
    cur_col.note_string = sub(cur_line_string,19,21)
    cur_col.instrument_string = sub(cur_line_string,22,23)
    cur_col.volume_string = sub(cur_line_string,24,25)
    cur_col.panning_string = sub(cur_line_string,26,27)
    cur_col.delay_string = sub(cur_line_string,28,29)
    cur_col.effect_amount_string = sub(cur_line_string,30,31)
    cur_col.effect_amount_string = sub(cur_line_string,32,33)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(3)
    
    cur_col.note_string = sub(cur_line_string,37,39)
    cur_col.instrument_string = sub(cur_line_string,40,41)
    cur_col.volume_string = sub(cur_line_string,42,43)
    cur_col.panning_string = sub(cur_line_string,44,45)
    cur_col.delay_string = sub(cur_line_string,46,47)
    cur_col.effect_amount_string = sub(cur_line_string,48,49)
    cur_col.effect_amount_string = sub(cur_line_string,50,51)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(4)
    
    cur_col.note_string = sub(cur_line_string,55,57)
    cur_col.instrument_string = sub(cur_line_string,58,59)
    cur_col.volume_string = sub(cur_line_string,60,61)
    cur_col.panning_string = sub(cur_line_string,62,63)
    cur_col.delay_string = sub(cur_line_string,64,65)
    cur_col.effect_amount_string = sub(cur_line_string,66,67)
    cur_col.effect_amount_string = sub(cur_line_string,68,69)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(5)
    
    cur_col.note_string = sub(cur_line_string,73,75)
    cur_col.instrument_string = sub(cur_line_string,76,77)
    cur_col.volume_string = sub(cur_line_string,78,79)
    cur_col.panning_string = sub(cur_line_string,80,81)
    cur_col.delay_string = sub(cur_line_string,82,83)
    cur_col.effect_amount_string = sub(cur_line_string,84,85)
    cur_col.effect_amount_string = sub(cur_line_string,86,87)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(6)
    
    cur_col.note_string = sub(cur_line_string,91,93)
    cur_col.instrument_string = sub(cur_line_string,94,95)
    cur_col.volume_string = sub(cur_line_string,96,97)
    cur_col.panning_string = sub(cur_line_string,98,99)
    cur_col.delay_string = sub(cur_line_string,100,101)
    cur_col.effect_amount_string = sub(cur_line_string,102,103)
    cur_col.effect_amount_string = sub(cur_line_string,104,105)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(7)
    
    cur_col.note_string = sub(cur_line_string,109,111)
    cur_col.instrument_string = sub(cur_line_string,112,113)
    cur_col.volume_string = sub(cur_line_string,114,115)
    cur_col.panning_string = sub(cur_line_string,116,117)
    cur_col.delay_string = sub(cur_line_string,118,119)
    cur_col.effect_amount_string = sub(cur_line_string,120,121)
    cur_col.effect_amount_string = sub(cur_line_string,122,123)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(8)
    
    cur_col.note_string = sub(cur_line_string,127,129)
    cur_col.instrument_string = sub(cur_line_string,130,131)
    cur_col.volume_string = sub(cur_line_string,132,133)
    cur_col.panning_string = sub(cur_line_string,134,135)
    cur_col.delay_string = sub(cur_line_string,136,137)
    cur_col.effect_amount_string = sub(cur_line_string,138,139)
    cur_col.effect_amount_string = sub(cur_line_string,140,141)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(9)
    
    cur_col.note_string = sub(cur_line_string,145,147)
    cur_col.instrument_string = sub(cur_line_string,148,149)
    cur_col.volume_string = sub(cur_line_string,150,151)
    cur_col.panning_string = sub(cur_line_string,152,153)
    cur_col.delay_string = sub(cur_line_string,154,155)
    cur_col.effect_amount_string = sub(cur_line_string,156,157)
    cur_col.effect_amount_string = sub(cur_line_string,158,159)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(10)
    
    cur_col.note_string = sub(cur_line_string,163,165)
    cur_col.instrument_string = sub(cur_line_string,166,167)
    cur_col.volume_string = sub(cur_line_string,168,169)
    cur_col.panning_string = sub(cur_line_string,170,171)
    cur_col.delay_string = sub(cur_line_string,172,173)
    cur_col.effect_amount_string = sub(cur_line_string,174,175)
    cur_col.effect_amount_string = sub(cur_line_string,176,177)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(11)
    
    cur_col.note_string = sub(cur_line_string,181,183)
    cur_col.instrument_string = sub(cur_line_string,184,185)
    cur_col.volume_string = sub(cur_line_string,186,187)
    cur_col.panning_string = sub(cur_line_string,188,189)
    cur_col.delay_string = sub(cur_line_string,190,191)
    cur_col.effect_amount_string = sub(cur_line_string,192,193)
    cur_col.effect_amount_string = sub(cur_line_string,194,195)
    
    --update --current note_col
    cur_col = target_line_ob:note_column(12)
    
    cur_col.note_string = sub(cur_line_string,199,201)
    cur_col.instrument_string = sub(cur_line_string,202,203)
    cur_col.volume_string = sub(cur_line_string,204,205)
    cur_col.panning_string = sub(cur_line_string,206,207)
    cur_col.delay_string = sub(cur_line_string,208,209)
    cur_col.effect_amount_string = sub(cur_line_string,210,211)
    cur_col.effect_amount_string = sub(cur_line_string,212,213)
  
    --EFFECT COLUMNS
    --update --current effect_col
    cur_col = target_line_ob:effect_column(1)
    
    cur_col.number_string = sub(cur_line_string,217,218)
    cur_col.amount_string = sub(cur_line_string,219,220)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(2)
    
    cur_col.number_string = sub(cur_line_string,224,225)
    cur_col.amount_string = sub(cur_line_string,226,227)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(3)
    
    cur_col.number_string = sub(cur_line_string,231,232)
    cur_col.amount_string = sub(cur_line_string,233,234)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(4)
    
    cur_col.number_string = sub(cur_line_string,238,239)
    cur_col.amount_string = sub(cur_line_string,240,241)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(5)
    
    cur_col.number_string = sub(cur_line_string,245,246)
    cur_col.amount_string = sub(cur_line_string,247,248)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(6)
    
    cur_col.number_string = sub(cur_line_string,252,253)
    cur_col.amount_string = sub(cur_line_string,254,255)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(7)
    
    cur_col.number_string = sub(cur_line_string,259,260)
    cur_col.amount_string = sub(cur_line_string,261,262)
    
    --update --current effect_col
    cur_col = target_line_ob:effect_column(8)
    
    cur_col.number_string = sub(cur_line_string,266,267)
    cur_col.amount_string = sub(cur_line_string,268,269)
  end