Jump to content


Photo

Error Handling


  • Please log in to reply
6 replies to this topic

#1 afta8

afta8

    Big Super GrandMasta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 713 posts
  • Gender:Male
  • Location:London

Posted 20 April 2012 - 23:41

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

#2 vV

vV

    God's or Borg's Chief and Ruler of All Species

  • Moderator
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 16855 posts
  • Gender:Male
  • Location:Netherlands

Posted 21 April 2012 - 08:23

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:
[lua]
>>> 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
[/lua]

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

Edited by vV, 21 April 2012 - 08:24.

Vv....


cpu Intel Core i7-4820K @3.70GHz
chipset Intel X79 chipset
mem 32 Gb 2400Mhz
os Win 7 64-bit SP1, DirectX 11
audio ESI ESP1010e - > Driver rev:2.0.6.0
video nVidia Geforce GTX780 - > Driver rev:335.23


Renoise Official Manual

#3 It-Alien

It-Alien

    Probably More God or Borg Than Human Member

  • Moderator
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 9593 posts
  • Gender:Male
  • Location:Avellino, Italy
  • Interests:trackers, basketball, chess, tabletennis, programming, writing, drumming, burping

Posted 21 April 2012 - 09:47

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:
[lua]
if(#renoise.song().instruments[int_sample_number].sample_mappings>0)
[/lua]
will check if there are sample mappings in the instrument at position int_sample_number
my specs:
cpu:Intel Core2 Quad Q9550 (4x2.83Ghz, FSB 1333Mhz)
os:Win7 Home Premium 64bit - OSX 10.9.1 64bit
audio: M-Audio Audiophile 24/96 PCI


Unschemed, hypertechnical tracked music. Sounding different since 1993.

my latest song: Lavender Town Syndrome (11'33", Psychedelic Beast)


where to find more music by me: soundcloud , jamendo , last.fm , napodano.com , facebook

RTFM.gif

("Lavender Town Syndrome, 11'33" Psychedelic Beast)

#4 afta8

afta8

    Big Super GrandMasta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 713 posts
  • Gender:Male
  • Location:London

Posted 21 April 2012 - 14:40

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.

Edited by fathand, 21 April 2012 - 14:41.


#5 kazakore

kazakore

    Probably More God or Borg Than Human Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 6140 posts
  • Gender:Male

Posted 21 April 2012 - 14:51

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


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.

#6 kazakore

kazakore

    Probably More God or Borg Than Human Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 6140 posts
  • Gender:Male

Posted 21 April 2012 - 15:00

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

-- Delete an existing mapping at the given index (see property 'sample_mappings').
renoise.song().instruments[]:delete_sample_mapping_at(layer, index)


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...

#7 afta8

afta8

    Big Super GrandMasta Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 713 posts
  • Gender:Male
  • Location:London

Posted 21 April 2012 - 17:28

Thanks kazakore,

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 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