Error Handling

Hi,

I am trying to use the following function to remove existing sample key mappings:

renoise.song().instruments[]:delete_sample_mapping_at( layer, sample_index )

It all works ok, except for when there are no sample mappings to delete, if this happens I get the following error:

*** std::logic_error: ‘invalid sample_mapping index ‘1’. no sample_mapping’s are available. 0 may be allowed.’

The script then breaks at this point.

What I want the script to do is not break at this point but instead move on to the next bit of the script…

Can anyone suggest how I could do this.

Cheers

Just to give you some insight, i picked instrument 7 which was empty but had a default mapping, then i removed the mapping and did a bound check on its first table, then did the same with instrument 8 where i didn’t removed the mapping yet:

   
>>> oprint(renoise.song().instruments[7].sample_mappings)  
table: 0000000011CCD0D0  
>>> rprint(renoise.song().instruments[7].sample_mappings)  
[1] => table  
 [1] => userdata: 0x0000000011CB68E8 (SampleMapping object)  
[2] => table  
>>> rprint(renoise.song().instruments[7].sample_mappings)  
[1] => table  
[2] => table  
>>> rprint(#renoise.song().instruments[7].sample_mappings)  
2  
>>> rprint(#renoise.song().instruments[7].sample_mappings[1])  
0  
>>> rprint(#renoise.song().instruments[8].sample_mappings[1])  
1  
  

You don’t necessarily need rprint to get table bound figures returned though.

LUA has not proper error handling, so you always have to check that the critical statements won’t fail by enclosing them into an if…then clause; in this case:

  
if(#renoise.song().instruments[int_sample_number].sample_mappings>0)  
  

will check if there are sample mappings in the instrument at position int_sample_number

Thank you both, this is exactly what I needed.

I have created the following function that detects how many sample mappings there are and then deletes them:

function delete_mappings ( ins, sample_index, note_layer )  
  
 local mappings = #renoise.song().instruments[ins].sample_mappings[note_layer]  
  
 for n = 0, (mappings - 1), 1 do  
 renoise.song().instruments[ins]:delete_sample_mapping_at( note_layer, sample_index )  
 end  
  
end  

Seems to do the job, the only thing I have noticed is that while the ‘sample_index’ is required it doesn’t actually have any effect. The ‘delete_sample_mapping_at’ command will delete all mappings on all samples in an instrument regardless of which sample_index you specify, seems a bit strange!

Also just for my understanding what does the ‘#’ before a command actually do?

Many thanks to you both.

Gives you a count of how many there are.

EG #renoise.song().tracks how many tracks in the song or #renoise.song().sequencer.pattern_sequence how many patterns there are in the song sequence (think those are real examples but should give you the idea.)

Can’t comment on the rest but it looks very similar to how I did it with my Unison tool, where I needed to clear existing layers when randomly generating new unison settings, otherwise the instrument would get bigger and bigger (well it was the solution I went for out of the possible options anyway.)

Don’t know why you need sample_index I’m afraid.

Are you ever planning on doing this on anything but the current instrument? If not you could remove “instruments[ins]” and replace it with “selected_instrument” also doing away with the variable ins.

OK it’s not sample_index, which is used when generating Keyzones, it’s just index, which is the keyzone number.

Also looking at your code you are going it a weird way. Maybe…

for n=0, mappings-1 – makes sense to get you back to the single layer default.
renoise.song().instruments[ins]:delete_sample_mapping_at( note_layer, sample_index ) – you’re not actually using n anywhere.

I assume you have your sample_index=1 and thus you are always deleting keyzone with an index of 1, which then moves the rest of the keyzones down a level to fill in the hole. No harm in this really, as long as you know the one you will keep will be the last one and not the first one. If you actually want to blank things you may wish to create a keyzone at some point and remove all others anyway… Thing is, if you ever try a sample_index>1 you will find you get an error when it tries to delete a keyzone that doesn’t exist any more.

Think I did:

for n=mappings, 1, -1 do  
 renoise.song()selected_instrument:delete_sample_mapping_at(note_layer, n)  
end  
  

Or something very similar at least…

Thanks kazakore,

I was just using ‘n’ to count through the loop, but now that you have explained the index your example makes sense and is a more elegant way of doing it. I have updated my code accordingly :)

Thanks for the tips, I’m new to the coding side of things so it really helps.

Cheers