Make all samples 8 bit-tool

Hello! I am pseudo coding my first tool which makes all your samples 8 bit. Basically, if you want it to sound lo-fi or maybe sound more like a very old sampler, it could be one way to do it. Anyways, here is m y code.

local function make_8bitize()
--for do[].samples[].sample_buffer.bit_depth
-- -> [read-only, 8]

It is my first one, and I am new to Lua, but I have some programming experience. I need the program to iterate through all the instrument slots and see if there is a sample, if there is, change it to 8 bit.

Can I have some advice on how to go about doing this? Is there a keyword for the maximum amount of instruments in the project?

for i = 0 and i < do ?

Hi Kommandotolken,

You can use the Lua Length operator # in front of tables to get the number of entries, so:

for i = 1, do

Note: the count would be from 1 and not 0 aswell.

You would also need to do an internal loop for the list of samples in each instrument, so:

for i = 1, do
  for j = 1, do
    --access bit depth 

edit: it looks like the .bit_depth is read only from the docs so it looks like you will need to work with the sample data directly. A part of the API I`ve not delved into but here are the relevant docs:

-- renoise.SampleBuffer

-------- Constants


-------- Functions

-- Create new sample data with the given rate, bit-depth, channel and frame
-- count. Will trash existing sample data. Initial buffer is all zero.
-- Will only return false when memory allocation fails (you're running out
-- of memory). All other errors are fired as usual.[].samples[].sample_buffer:create_sample_data(
  sample_rate, bit_depth, num_channels, num_frames)
    -> [boolean, success]
-- Delete existing sample data.[].samples[].sample_buffer:delete_sample_data()

-- Read access to samples in a sample data buffer.[].samples[].sample_buffer:sample_data(
  channel_index, frame_index)
  -> [number -1-1]

-- Write access to samples in a sample data buffer. New samples values must be
-- within [-1, 1] and will be clipped automatically. Sample buffers may be 
-- read-only (see property 'read_only'). Attempts to write on such buffers 
-- will result into errors.
-- IMPORTANT: before modifying buffers, call 'prepare_sample_data_changes'.
-- When you are done, call 'finalize_sample_data_changes' to generate undo/redo
-- data for your changes and update sample overview caches![].samples[].sample_buffer:set_sample_data(
  channel_index, frame_index, sample_value)

-- To be called once BEFORE sample data gets manipulated via 'set_sample_data'.
-- This will prepare undo/redo data for the whole sample. See also
-- 'finalize_sample_data_changes'.[].samples[].sample_buffer:prepare_sample_data_changes()
-- To be called once AFTER the sample data is manipulated via 'set_sample_data'.
-- This will create undo/redo data for the whole sample, and also update the
-- sample view caches for the sample. The reason this isn't automatically
-- invoked is to avoid performance overhead when changing sample data 'sample by
-- sample'. Don't forget to call this after any data changes, or changes may not
-- be visible in the GUI and can not be un/redone![].samples[].sample_buffer:finalize_sample_data_changes()

-- Load sample data from a file. Files can be any audio format Renoise supports.
-- Possible errors are shown to the user, otherwise success is returned.[].samples[].sample_buffer:load_from(filename)
  -> [boolean, success]
-- Export sample data to a file. Possible errors are shown to the user,
-- otherwise success is returned. Valid export types are 'wav' or 'flac'.[].samples[].sample_buffer:save_as(filename, format)
  -> [boolean, success]

-------- Properties

-- Has sample data?[].samples[].sample_buffer.has_sample_data
  -> [read-only, boolean]

-- _NOTE: All following properties are invalid when no sample data is present,
-- 'has_sample_data' returns false:_

-- True, when the sample buffer can only be read, but not be modified. true for
-- sample aliases of sliced samples. To modify such sample buffers, modify the 
-- sliced master sample buffer instead.[].samples[].sample_buffer.read_only
  -> [read-only, boolean]
-- The current sample rate in Hz, like 44100.[].samples[].sample_buffer.sample_rate
  -> [read-only, number]

-- The current bit depth, like 32, 16, 8.[].samples[].sample_buffer.bit_depth
  -> [read-only, number]

-- The number of sample channels (1 or 2)[].samples[].sample_buffer.number_of_channels
  -> [read-only, number]

-- The sample frame count (number of samples per channel)[].samples[].sample_buffer.number_of_frames
  -> [read-only, number]

-- The first sample displayed in the sample editor view. Set together with
-- DisplayLength to control zooming.[].samples[].sample_buffer.display_start, _observable
  -> [number >= 1 <= number_of_frames]

-- The number of samples displayed in the sample editor view. Set together with
-- DisplayStart to control zooming.[].samples[].sample_buffer.display_length, _observable
  -> [number >= 1 <= number_of_frames]

-- The start and end points of the sample editor display.[].samples[].sample_buffer.display_range[], _observable
  -> [array of two numbers, 1-number_of_frames]

-- The vertical zoom level where 1.0 is fully zoomed out.[].samples[].sample_buffer.vertical_zoom_factor, _observable
   -> [number, 0.0-1.0]

-- Selection range as visible in the sample editor. always valid. returns the entire
-- buffer when no selection is present in the UI.[].samples[].sample_buffer.selection_start, _observable
  -> [number >= 1 <= number_of_frames][].samples[].sample_buffer.selection_end, _observable
  -> [number >= 1 <= number_of_frames][].samples[].sample_buffer.selection_range[], _observable
  -> [array of two numbers, 1-number_of_frames]

-- The selected channel.[].samples[].sample_buffer.selected_channel, _observable
for instr_index = 1, do

#table is the same as table.getn(table),

or table:getn() if you have created the table by table.create() and thus enabled fancy table methods.

  1. You can’t convert to 8-bit by changing the bit_depth property in sample_buffer, as it is read-only. I’m pretty sure the only way to do it is to replace the old sample with a completely new sample, and populating it with sample data spit out from your very own 8-bit conversion function. Finally delete the old sample :stuck_out_tongue:

I think this tool does what you want using sox;