Lua Scripting with REAPER 5 - Part. 9: Preventing Bugs
-
0:00 - 0:04HI, this is X-Raym, for the 8th part
of our ReaScript Tutorial. -
0:04 - 0:08Scripts can define new values
to REAPER objects. -
0:08 - 0:10But you have to be careful.
-
0:11 - 0:14Because these values
can be invalid. -
0:14 - 0:17No error message may appear,
-
0:17 - 0:21but the value inserted in the project
will be invalid. -
0:21 - 0:25This is not fatal, be reassured,
-
0:25 - 0:29it cannot corrupt a project
and make it unable to be open. -
0:29 - 0:33We will take back our script
which modify fade-out length -
0:33 - 0:36In order to show you
how to prevent this kind of error. -
0:36 - 0:39In order to ease
the understanding of the phenomenon, -
0:39 - 0:40I will simplify our script.
-
0:40 - 0:44To do that, I will not
run the Main function -
0:44 - 0:47but a new function that
we will call Test. -
0:47 - 0:51I declare this function
before it's execution. -
0:52 - 0:53Function Test()
-
0:54 - 0:57And this function will only act
on the first selected item. -
1:07 - 1:080 for the current project,
-
1:08 - 1:100 for the first item.
-
1:13 - 1:15I check that there is at least
one selected item. -
1:25 - 1:27If there is one item selected
-
1:27 - 1:30Our script will execute a custom function,
-
1:30 - 1:33I call it SetltemFadeOut
-
1:34 - 1:36She will have as parameter
-
1:38 - 1:39an item
-
1:44 - 1:45But also an item length
-
1:50 - 1:53and a new value of fade out length.
-
2:00 - 2:08We will get the item length
thanks to this function here. -
2:13 - 2:16I replace item by
first_sel_item. -
2:16 - 2:20We will write the new value here...
-
2:21 - 2:22and it will be 0.
-
2:24 - 2:26Now I declare this function
-
2:32 - 2:34It has 3 parameters
-
2:34 - 2:37The first parameter is an item.
-
2:37 - 2:43The 2nd will be a length
I will name it item_len -
2:44 - 2:46the 3rd parameter
a new fade-out length value. -
2:51 - 2:56This function will define the
fade-out length of the first selected item. -
2:56 - 2:58I copy this function here.
-
3:01 - 3:07I replace the variables by
the parameters of our function. -
3:10 - 3:13I add UpdateArrange
-
3:15 - 3:19A define a fade on our item
and if all happen as expected, -
3:19 - 3:24The fade-out length value of the first
selected item should be equal to 0. -
3:24 - 3:26I run the script.
-
3:26 - 3:28All happen as expected.
-
3:29 - 3:31Now imagine the following thing.
-
3:32 - 3:36If fade_out_new_value is
equal to 100 -
3:36 - 3:41Knowing that this value is in seconds
and that the item length is only 1 second. -
3:41 - 3:42Let's see what happens.
-
3:45 - 3:50As you can see the fade-out length
seems to match the item length. -
3:50 - 3:58However, if I display the item properties, I can see
that its fade-out length value is equal to 100 seconds. -
3:59 - 4:07And you can see that the mouse doesn't behave
like usual, because of this invalid value. -
4:07 - 4:13Usually, you can see the fade icon-cursor
being displayed hover the fade. -
4:13 - 4:18But in our case, there is a bug
because of an invalid value. -
4:19 - 4:23So, we will use this function here
to prevent this kind of error. -
4:27 - 4:29We will write the following thing.
-
4:29 - 4:36If the fade length is bigger than
the item length, -
4:36 - 4:44then the fade-out length
will be equal to the item length. -
4:44 - 4:46I run the script.
-
4:46 - 4:51Nothing change visually,
but it seems that it is corrected. -
4:51 - 4:54I check the item properties.
-
4:54 - 4:57The fade-out is not
bigger than the item length. -
4:58 - 5:03Let's see a more complex case:
what if there was a fade-in? -
5:03 - 5:04I run the script.
-
5:08 - 5:11It seems to be good
-
5:11 - 5:15but as you can see
we can feel a problem. -
5:15 - 5:20If I check the item properties I can see
that the fade-out as an invalid value -
5:21 - 5:25because it is as long as the item, and this
is not possible as there is a fade-in. -
5:25 - 5:27So, I will write the following thing.
-
5:35 - 5:40Here I will get
the fade-in length value. -
5:48 - 5:54The parameter is "D_FADEINLENGTH"
-
5:56 - 5:58and I write the following condition:
-
5:58 - 6:06If fade-out length is bigger
than item_length minus the fade-in length -
6:08 - 6:14then the fade-out length will be equal
to item length minus fade-in length. -
6:14 - 6:21Rather that making the calculation twice
I will write a max_value variable -
6:23 - 6:27and I place there here.
-
6:27 - 6:29Oops, small mistake here
-
6:29 - 6:31I run the script.
-
6:31 - 6:34Nothing has visually change,
but if I check the properties, -
6:34 - 6:37We see that the fade-out length
has a vlaid value. -
6:39 - 6:42I can now use this function here
-
6:44 - 6:46in our Main function.
-
6:46 - 6:49Rather than setting a new fade-out length
value directly here, -
6:49 - 6:52I use our function
-
6:52 - 6:55which take as first parameter: the item
-
6:56 - 6:582nd parameter: the item length
-
6:58 - 7:00which has already been calculated here
-
7:00 - 7:04and as new value, the fade-out length
of the item udner mouse -
7:04 - 7:08It was here.
-
7:12 - 7:16I don't need the test function anymore
so I delete it. -
7:18 - 7:22I replace the execution of the
Test function by Main. -
7:25 - 7:31Main works on item in time selection
on selected tracks. -
7:37 - 7:45I define a big fade-out length
on our reference item. -
7:47 - 7:50I make these items sshorter.
-
7:54 - 7:56I pute some fades-in.
-
8:00 - 8:05I propagate the length value of this fade-out
on these items and check if there is no errors. -
8:06 - 8:12As you can see, all seems to be fine.
-
8:12 - 8:14I check.
-
8:15 - 8:18It seems to be good,
the sums seems to be good. -
8:18 - 8:25Voilà, with simple conditions, we have prevented
invalid values insertions in our project. -
8:25 - 8:27In general, the rule is simple:
-
8:28 - 8:34determine the minimum and maximum value
that a parameter can have, -
8:34 - 8:37compare this to the new value
we want to set, -
8:37 - 8:45and write some conditions to avoid this value to be
lower than the minimum or greater than the maximum. -
8:46 - 8:50In the next video, we will see
how to use tables. See ya!
- Title:
- Lua Scripting with REAPER 5 - Part. 9: Preventing Bugs
- Description:
-
Source Article and Exercice:
http://extremraym.com/en/reascript-video-preventing-bugsPlease consider making a donation if you liked it:
http//extremraym.com/en/donationCheers !
- Video Language:
- French
- Duration:
- 08:53
Extrem Raym edited English, British subtitles for Lua Scripting with REAPER 5 - Part. 9: Preventing Bugs |