Arm And Record Script, Some Problems

I’ve been working on a session recorder tool, which almost works (code below, not super tidy). It arms a track by a hotkey, automatically records when starting to play pattern and automatically inserts the note (with autoseek) on position where the recording started.

I’m learning to script by reading the documentation and googling a lot, but I take the liberty of asking some questions here. I hope that’s ok.

  1. When using renoise.song().transport:start_stop_sample_recording() to stop recording, AND recording to the same instrument, the sample doesn’t update. Am I making some obvious mistake?
  2. Is there a better way to do the notifier function? Maybe it introduces some latency when recording now?
  3. Is there a way to make global variables that will be accessible the next time I trigger the script? (no0b question)
  4. Proper handling of track colors and inserting on the right column will be fixed, don’t worry :)
  
local trackcolor = false  
local armed_track = false  
local record_pos = false  
local is_armed = false  
  
function initialize()   
 renoise.song().transport.playing = false -- stops playback in case it was playing  
 armed_track = renoise.song().selected_track_index  
 trackcolor = renoise.song().tracks[renoise.song().selected_track_index].color -- save track color so it can be reset after recording  
 arm_and_record()  
end  
  
function arm_and_record()  
 renoise.song().tracks[renoise.song().selected_track_index].color = { 255, 0, 0 } -- sets track color to red to indicate it is armed  
 renoise.song().transport.playing_observable:add_notifier(notifier) -- triggers notifier() when playback is started or stopped  
end  
  
function notifier()  
 if renoise.song().transport.playing then  
 renoise.app().window.sample_record_dialog_is_visible = true -- open record dialogue. unwanted(?) but needed.  
 record_pos = renoise.song().transport.playback_pos -- position of where the recording is being started  
 renoise.song().transport:start_stop_sample_recording() -- starts recording  
 else  
 renoise.song().transport:start_stop_sample_recording() -- stops recording  
 renoise.app().window.sample_record_dialog_is_visible = false -- close record dialogue  
 renoise.song().transport.playback_pos = record_pos -- go to the start of the recording (unwanted?)  
 insert_recorded_instrument() -- insert note where the recording was started  
 disarm() -- disarms and quits  
 end  
end  
  
function insert_recorded_instrument()  
 renoise.song().patterns[record_pos.sequence].tracks[armed_track].lines[1].note_columns[1].note_string = "C-4"  
 renoise.song().patterns[record_pos.sequence].tracks[armed_track].lines[1].note_columns[1].instrument_string = string.sub(bit.tohex(renoise.song().selected_instrument_index-1), 7)  
 renoise.song().instruments[renoise.song().selected_instrument_index].samples[1].autoseek = true -- everyone wants this?  
end  
  
function disarm()   
 renoise.song().tracks[renoise.song().selected_track_index].color = trackcolor -- resets track color  
 renoise.song().transport.playing_observable:remove_notifier(notifier) -- cleanup  
end  
  
renoise.tool():add_menu_entry {  
 name = "Pattern Editor:Arm and record",  
 invoke = initialize  
}  
  
renoise.tool():add_keybinding {  
name = "Pattern Editor:Record:Arm and record",  
invoke = initialize  
}  
  

Perhaps you need the prepare_sample_data_changes() and finalize_sample_data_changes() on the sample-buffer of the target sample to get it updated.

  
local sample_buffer = renoise.song().instruments[instrument].samples[targetslot].sample_buffer  
sample_buffer:prepare_sample_data_changes()  
... Do your stuff....  
sample_buffer:finalize_sample_data_changes()  
  

Hmm. I don’t think so. I am not doing any kind of sample manipulation and the renoise.song().transport:start_stop_sample_recording() should be doing its thing independently?

Sometimes it works and sometimes it doesn’t. I think i need to check my syntax and the structure of the script. Maybe some of the actions are happening too fast for renoise to react properly? :)

These functions are not frankly for manipulating the sample but for preparing the undo buffer. If someone would accidently overwrite an existing sample, he/she can undo the action.
If you don’t use these functions, the overwriting cannot be undone.

That could perhaps be very well a thing.
You could add delay timers to prevent users from acting too quick.

Can you explain what a delay timer is and how to build one?
Does it have something to do with os.clock() ?

Yes it has to do with os.clock.
I believe conner gave you a reasonable snippet in the other thread where you wanted to delay the closure of the sample recorder dialog.

I tried but I couldn’t find the thread conner posted in… Which should say something about the piles and piles of new forumthreads xD

I guess that is me mixing up Conner with Joel. He mentioned os.time instead as well, but os.clock you can go a biut more precise.

Btw:The sample recording only works when the recording dialog is prominent visible according to the API docs.

Yep, that’s why I’m working on a shortcut (which is half-functional now), which
First shortcut press:

  1. Opens Sample Recorder Dialog
  2. Starts Sample Recording
    Second shortcut press:
  3. Stops Sample Recording
  4. Keeps Sample Recorder Dialog open.

Once a functional report of the sample having been saved can be generated, the 4 will change to "detect sample saved, close sample recorder dialog)