Issue With Recording W/ Pattern Sync..

Ok, I really don’t know what to do next so I’ll just post this bit of code here. I’ll try and explain what it does.
You can only use it, if you click “Pattern Sync” on and “Create a new Instrument on each take” in the Sample Recorder dialogue.
Now, ok, that’s all really. the shortcut starts and stops recording in pattern sync, dumps forth the correct-base-note (hardwired to C-4) note of the currently_sampled_in instrument, and a 0501 (glide) command to make sure that you can record for instance 32*64 row pattern, press stop on the 32nd repetition of the 64 row pattern you are sammpling into, and then when playback restarts on the pattern “properly”, this 32 times longer than 64 row pattern sample will start playing and stop when you run out of sampledata, then the next pattern row00 will re-start playback of this (fairly long) wavefile.
now,at first i got it working in a specific way – this way was that if you sample a 32 row pattern’s worth of sync, i.e. sample and stop sampling before the end of the 32 row pattern, this pattern will only play on the second repeat of the pattern. so it doesn’t “start immediately”.
i tried to fix this a bit, and kind of got it working (i.e., at the end of disk_buffer.observable (or however it was), it would actually restart playback of song. but this led to non-pattern-length samples. )) it worked otherwise! the samples started when they should. but the pattern-length was ruined and would result in silent repetitions of the pattern – i.e., delivering a panic and a restart playback seems to mess up pattern_sync_based recording inside a script. i hope you made it up until here of this mail, since i tried to restart with the italics.
anyway, is there a solution? Can I eat my cake and have it too (=have a shortcut/midimapping which starts sampling and stops sampling, and results in pattern-sync-length samplings which work, and which start at the start of the pattern and run until the end of the pattern-sync-amount-samplings… and after pressing stop on sampling, the rest of the pattern-sync-amount will be recordd, but then playback restarts with the sample already playing… instead of playback stopping till samplebuffer has been saved to, and thene restart… or instead of waiting for a second pattern repeat for the sample to start playing?

--This records to currently selected track and outputs 0501 and the c-4 and the selected_instrument  
function recordtocurrenttrack()  
 local s=renoise.song()  
 local ss=s.selected_sample  
 local t=renoise.song().transport  
 local w=renoise.app().window   
 local currTrak=s.selected_track_index  
 local currSamp=renoise.song().selected_sample_index  
  
 if renoise.song().transport.playing==false then  
 renoise.song().transport.playing=true end  
  
 local name = nil  
 if renoise.song().selected_track_index<10 then   
 name="Track 0"..renoise.song().selected_track_index  
 else  
 name="Track "..renoise.song().selected_track_index  
 end  
  
 if renoise.song().tracks[currTrak].name==name  
 then renoise.song().tracks[currTrak].name=" 1"  
 end  
  
 if renoise.song().tracks[currTrak].name==" 12" then   
 local nexttrack=renoise.song().selected_track_index+1  
 renoise.song():insert_track_at(nexttrack)  
 renoise.song().selected_track_index=renoise.song().selected_track_index+1  
 renoise.song().tracks[renoise.song().selected_track_index].name=" 1"  
 end  
  
 for i=1,(table.count(renoise.song().instruments)) do  
 if renoise.song().instruments[renoise.song().selected_instrument_index].samples[renoise.song().selected_sample_index].sample_buffer.has_sample_data==true then   
local result=renoise.song().selected_instrument_index+1  
 if result>(table.count(renoise.song().instruments)) then renoise.song():insert_instrument_at((table.count(renoise.song().instruments))+1)  
 end  
  
 renoise.song().selected_instrument_index=renoise.song().selected_instrument_index+1  
 if (table.count(renoise.song().instruments))==renoise.song().selected_instrument_index then  
 for i=1,8 do   
 renoise.song():insert_instrument_at((table.count(renoise.song().instruments))+1)  
 end  
 end  
 end  
 end  
  
 w.sample_record_dialog_is_visible=true   
 t:start_stop_sample_recording()  
  
 if ss.sample_buffer_observable:has_notifier(finalrecord) == false then   
 ss.sample_buffer_observable:add_notifier(finalrecord)  
 end  
-- delay(3)  
-- renoise.song().transport:trigger_sequence(1)  
end  
  
function finalrecord()  
 local s=renoise.song()  
 local ss=s.selected_sample  
 local currTrak=s.selected_track_index  
 local currSamp=renoise.song().selected_sample_index  
 local currInst=s.selected_instrument_index  
 local currPatt=s.selected_pattern_index  
 local w=renoise.app().window  
 local rightinstrument=nil  
 local place=nil   
 local zero=nil  
 local o=nil  
 local rightinstrument=renoise.song().selected_instrument_index-1  
 local nc=s.patterns[currPatt].tracks[currTrak].lines[1].note_columns  
 local selnotcol=renoise.song().selected_note_column_index  
 local vnc=renoise.song().tracks[currTrak].visible_note_columns  
  
 w.active_lower_frame=2  
 w.sample_record_dialog_is_visible=false   
 ss.autoseek=true  
 s.patterns[currPatt].tracks[currTrak].lines[1].effect_columns[1].number_string="05"  
 s.patterns[currPatt].tracks[currTrak].lines[1].effect_columns[1].amount_string="01"  
  
 for o = 1,12 do   
 if nc[o].note_string=="---" then   
 s.patterns[currPatt].tracks[currTrak].lines[1].note_columns[o].note_string="C-4"  
 s.patterns[currPatt].tracks[currTrak].lines[1].note_columns[o].instrument_value=rightinstrument  
 if vnc < o then  
 s.tracks[currTrak].visible_note_columns=o  
 renoise.song().tracks[currTrak].name=" " .. o  
 end  
 break  
 end   
 end  
 local t=renoise.song().transport  
 local seq=renoise.song().selected_sequence_index  
 local startpos = renoise.song().transport.playback_pos   
 local t=renoise.song().transport  
 renoise.song().transport.follow_player=true  
 w.lock_keyboard_focus=true  
--t:panic()  
--t:start(renoise.Transport.PLAYMODE_RESTART_PATTERN)  
  
 if renoise.song().transport.playing==false then renoise.song().transport.playing=true end  
 t.loop_block_enabled=false  
-- startpos.line = renoise.song().selected_line_index  
-- startpos.sequence = renoise.song().selected_sequence_index  
-- renoise.song().transport.playback_pos = startpos  
-- t:start(renoise.Transport.PLAYMODE_CONTINUE_PATTERN)  
-- ss.sample_buffer_observable:remove_notifier(finalrecord)   
 end  
  
renoise.tool():add_midi_mapping{name="Paketti:Record to Current Track x[Toggle]", invoke = function() recordtocurrenttrack()   
 if renoise.song().transport.playing==false then renoise.song().transport.playing=true end  
 local seq=renoise.song().selected_sequence_index  
 local startpos = renoise.song().transport.playback_pos   
 local t=renoise.song().transport  
 --t:panic()  
 t.loop_block_enabled=false  
 --startpos.line = renoise.song().selected_line_index  
 --startpos.sequence = renoise.song().selected_sequence_index  
 --renoise.song().transport.playback_pos = startpos  
 --t:start(renoise.Transport.PLAYMODE_CONTINUE_PATTERN)  
 renoise.song().transport.follow_player=true  
 renoise.app().window.active_lower_frame=2  
 renoise.app().window.lower_frame_is_visible=true  
end}  
renoise.tool():add_keybinding {name="Global:Paketti:Record to Current Track", invoke = function() recordtocurrenttrack()   
 if renoise.song().transport.playing==false then renoise.song().transport.playing=true end  
 local seq=renoise.song().selected_sequence_index  
 local startpos = renoise.song().transport.playback_pos   
 local t=renoise.song().transport  
 --t:panic()  
 t.loop_block_enabled=false  
 --startpos.line = renoise.song().selected_line_index  
 --startpos.sequence = renoise.song().selected_sequence_index  
 --renoise.song().transport.playback_pos = startpos  
 --t:start(renoise.Transport.PLAYMODE_CONTINUE_PATTERN)  
 renoise.song().transport.follow_player=true  
 renoise.app().window.lower_frame_is_visible=true  
 renoise.app().window.active_lower_frame=2  
 end}  
  

It seems that this results in files such as this:

rprint (renoise.song().selected_sample.sample_buffer.number_of_frames)
264352
rprint (renoise.song().selected_sample.sample_buffer.number_of_frames)
264704

The 264352 length sample is fine but the 264704 has hmm 352 extra frames which, if removed, would make it loop very nicely. I’m not quite sure how to modify a sample like this, but could try it…