Tools/com.renoise.xStream.xrnx/userdata/models/ColumnMixer.lua
--[[===========================================================================
ColumnMixer.lua
===========================================================================]]--

return {
arguments = {
  {
      ["locked"] = false,
      ["name"] = "volume.col1",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "misc.use_key_velocity",
      ["linked"] = false,
      ["value"] = true,
      ["properties"] = {
          ["display_as"] = "checkbox",
      },
      ["description"] = "Use Renoise key-velocity to control overall volume",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col2",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col3",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col4",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col5",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col6",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col7",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col8",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col9",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col10",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col11",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "volume.col12",
      ["linked"] = false,
      ["value"] = 128,
      ["properties"] = {
          ["min"] = 0,
          ["display_as"] = "minislider",
          ["max"] = 128,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col1",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col2",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col3",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col4",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col5",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col6",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col7",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col8",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col9",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col10",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col11",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
  {
      ["locked"] = false,
      ["name"] = "panning.col12",
      ["linked"] = false,
      ["value"] = 65,
      ["properties"] = {
          ["max"] = 128,
          ["zero_based"] = true,
          ["display_as"] = "hex",
          ["min"] = 0,
      },
      ["description"] = "",
  },
},
presets = {
  {
      ["panning.col6"] = 65,
      ["panning.col10"] = 65,
      ["name"] = "Default",
      ["panning.col9"] = 65,
      ["volume.col9"] = 128,
      ["panning.col12"] = 65,
      ["panning.col5"] = 65,
      ["panning.col1"] = 65,
      ["panning.col3"] = 65,
      ["misc.use_key_velocity"] = true,
      ["panning.col11"] = 65,
      ["volume.col5"] = 128,
      ["panning.col2"] = 65,
      ["volume.col1"] = 128,
      ["panning.col4"] = 65,
      ["volume.col6"] = 128,
      ["panning.col8"] = 65,
      ["volume.col7"] = 128,
      ["volume.col4"] = 128,
      ["volume.col3"] = 128,
      ["volume.col11"] = 128,
      ["volume.col2"] = 128,
      ["volume.col12"] = 128,
      ["volume.col8"] = 128,
      ["volume.col10"] = 128,
      ["panning.col7"] = 65,
  },
},
data = {
  ["recent"] = [[--------------------------------------------------------------------------------- -- table - reflects the last output values
-- (initialized to preset values to begin with...)
-------------------------------------------------------------------------------
return {
  volume = {},
  panning = {},
}]],
  ["initialize"] = [[-------------------------------------------------------------------------------- function - initialize to current preset values 
-------------------------------------------------------------------------------
return function()
  if table.is_empty(data.recent.volume) then
    for k = 1,12 do
      data.recent.volume[k] = args.volume["col"..k]
    end
  end  
  if table.is_empty(data.recent.panning) then
    for k = 1,12 do
      data.recent.panning[k] = args.panning["col"..k]
    end
  end    
end]],
  ["do_output"] = [[-------------------------------------------------------------------------------
-- function - write to pattern (invoked on each line...)
-- @param val, number 
-- @param recent_val, number or nil
-- @param col_idx, number
-- @param target, string ("panning" or "volume")
-------------------------------------------------------------------------------
return function(col_idx,target)
  local col = xline.note_columns[col_idx]
  local val = args[target]["col"..col_idx]
  
  -- apply the keyboard velocity to overall volume? 
  if (target == "volume")
    and args.misc.use_key_velocity 
    and rns.transport.keyboard_velocity_enabled 
  then 
    val = val * rns.transport.keyboard_velocity / 0x80
  end
  
  local recent = data.recent[target][col_idx]
  local target_name = target.."_value"
  local changed = recent and (recent ~= val) or false
  if not recent or changed then 
  
    -- value was changed
    col[target_name] = val
    
    -- update recent once we're done writing ahead
    -- (without this, values ahead of playback pos would be erased 
    -- and as a result, changes would not be audible in realtime)
    local ahead_by = xpos.line - rns.transport.playback_pos.line
    if (ahead_by == math.min(1,xStreamPos.determine_writeahead()-1)) then
      data.recent[target][col_idx] = val
    end
    
  elseif (col.instrument_value < 255) then 
    
    -- output on #instrument 
    col[target_name] = val
    
  else 
    
    -- clear existing vol/pan commands (but leave others intact)
    if (target=="volume") and (col.volume_value <= 0x80) 
      or (target == "panning") and (col.panning_value <= 0x80)
    then
      col[target_name] = 255
    end
    
  end
end]],
},
events = {
},
options = {
 color = 0x000000,
},
callback = [[
-------------------------------------------------------------------------------
-- A mixing console for your note-columns
-- (as arguments change) write vol/pan into visible note columns 
-- (always) add vol/pan to notes with an instrument number 
-------------------------------------------------------------------------------
data.initialize()
local track = rns.tracks[track_index]
for col_idx = 1,track.visible_note_columns do
  data.do_output(col_idx,"volume") 
  data.do_output(col_idx,"panning") 
end
]],
}