Generating, adding text in the song comments through lua

I’m adding features to an instrument sorting tool from Joule and right now I’m trying to collect housekeeping info from the used sample instruments inside of the song comments window;

Here are snippits of the added code;

function append_housekeeping_info_to_song_comments()
  local comments = renoise.song().comments
  
  -- Start with the song title and artist
  local song_title = renoise.song().name
  local artist_name = renoise.song().artist
  local housekeeping_info = string.format("Title: %s\nArtist: %s\n\nSample Housekeeping Info:\n", song_title, artist_name)

  for i, instrument in ipairs(renoise.song().instruments) do
    if #instrument.samples > 0 then
      local instrument_info = string.format("Instrument %d: %s\n", i, instrument.name)
      
      -- Append bit depth and sample rate
      local sample = instrument.samples[1]
      if sample.sample_buffer.has_sample_data then
        local bit_depth = sample.sample_buffer.bit_depth
        local sample_rate = sample.sample_buffer.sample_rate
        instrument_info = instrument_info .. string.format("  Bit Depth: %d-bit, Sample Rate: %d Hz\n", bit_depth, sample_rate)
        
        -- Append total sample duration
        local total_duration = 0
        for _, sample in ipairs(instrument.samples) do
          if sample.sample_buffer.has_sample_data then
            total_duration = total_duration + (sample.sample_buffer.number_of_frames / sample.sample_buffer.sample_rate)
          end
        end
        instrument_info = instrument_info .. string.format("  Total Duration: %.2f s\n", total_duration)

        -- Append stereo/mono info
        local channels = sample.sample_buffer.number_of_channels
        local stereo_mono = (channels == 2) and "Stereo" or "Mono"
        instrument_info = instrument_info .. string.format("  Channels: %s\n", stereo_mono)
        
        -- Append sample memory size
        local total_size = 0
        for _, sample in ipairs(instrument.samples) do
          if sample.sample_buffer.has_sample_data then
            total_size = total_size + (sample.sample_buffer.number_of_frames * sample.sample_buffer.number_of_channels * (sample.sample_buffer.bit_depth / 8))
          end
        end
        instrument_info = instrument_info .. string.format("  Memory Size: %.2f KB\n", total_size / 1024)

        -- Append loop points if looping is enabled
        if sample.loop_mode ~= renoise.Sample.LOOP_MODE_OFF then
          local loop_start = sample.loop_start / sample.sample_buffer.sample_rate
          local loop_end = sample.loop_end / sample.sample_buffer.sample_rate
          instrument_info = instrument_info .. string.format("  Loop Points: %.2fs - %.2fs\n", loop_start, loop_end)
        end
      end
      
      -- Append the info to the housekeeping summary
      housekeeping_info = housekeeping_info .. instrument_info .. "\n"
    end
  end

  -- Update the song comments with the housekeeping info
  table.insert(comments, housekeeping_info)
  renoise.song().comments = comments
end

renoise.tool():add_menu_entry {
  name = "Instrument Box:Sample Info...:Update Housekeeping Info in Song Comments",
  invoke = function() append_housekeeping_info_to_song_comments() end
}

Using it does add info into the song comments window, but it seems to be capped at a certain amount of characters. At first I thought this was just a limitation of how much can be written down there, but when manually inserting characters I can add much more then what can be inserted from the tool. Also after generating the content for it, I can’t seem to delete, select or edit any of the text.

Is this a renoise bug or expected behavior?

Hello,
Comments is an array of strings and you add a long one at the end.
Put each one separately in the comments

table.insert(comments, instrument_info)
table.insert(comments, total_size)

of course without “\n” in each string.

Thanks for the info, I’ll throw your answer in chatgpt and see what it’ll come up with :slight_smile: . The code above does seem to result in weird song comment window behavior which might need to be fixed? For example text overlapping when double mouse clicking inside the result;

image

As matblek said the comments are a list of paragraphs. The paragraphs itself should not contain new line characters. I’ll make this an err when trying to add newlines here, as the editors don’t expect and thus also don’t handle those newlines.

Something like this should work instead:

function append_housekeeping_info_to_song_comments()
  -- Append to existing comments
  local comments = table.create(renoise.song().comments)
  -- Start with the song title and artist
  comments:insert("")
  comments:insert(string.format("Title: %s", renoise.song().name))
  comments:insert(string.format("Artist: %s", renoise.song().artist))
  comments:insert("")
  comments:insert("Sample Housekeeping Info:")
  for i, instrument in ipairs(renoise.song().instruments) do
    if #instrument.samples > 0 then
      comments:insert(string.format("Instrument %d: %s", i, instrument.name))
      -- Append bit depth and sample rate
      local sample = instrument.samples[1]
      if sample.sample_buffer.has_sample_data then
        local bit_depth = sample.sample_buffer.bit_depth
        local sample_rate = sample.sample_buffer.sample_rate
        comments:insert(string.format("  Bit Depth: %d-bit, Sample Rate: %d Hz", bit_depth, sample_rate))
        
        -- Append total sample duration
        local total_duration = 0
        for _, sample in ipairs(instrument.samples) do
          if sample.sample_buffer.has_sample_data then
            total_duration = total_duration + (sample.sample_buffer.number_of_frames / sample.sample_buffer.sample_rate)
          end
        end
        comments:insert(string.format("  Total Duration: %.2f s", total_duration))

        -- Append stereo/mono info
        local channels = sample.sample_buffer.number_of_channels
        local stereo_mono = (channels == 2) and "Stereo" or "Mono"
        comments:insert(string.format("  Channels: %s", stereo_mono))
        
        -- Append sample memory size
        local total_size = 0
        for _, sample in ipairs(instrument.samples) do
          if sample.sample_buffer.has_sample_data then
            total_size = total_size + (sample.sample_buffer.number_of_frames * sample.sample_buffer.number_of_channels * (sample.sample_buffer.bit_depth / 8))
          end
        end
        comments:insert(string.format("  Memory Size: %.2f KB", total_size / 1024))

        -- Append loop points if looping is enabled
        if sample.loop_mode ~= renoise.Sample.LOOP_MODE_OFF then
          local loop_start = sample.loop_start / sample.sample_buffer.sample_rate
          local loop_end = sample.loop_end / sample.sample_buffer.sample_rate
          comments:insert(string.format("  Loop Points: %.2fs - %.2fs\n", loop_start, loop_end))
        end
      end
      
      comments:insert("")
    end
  end

  -- Update the song comments with the housekeeping info
  renoise.song().comments = comments
end

append_housekeeping_info_to_song_comments()
1 Like

Thanks for the help, this works well indeed!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.