Return to Video

Lua Scripting with REAPER 5 - Part. 9: Preventing Bugs

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

Please consider making a donation if you liked it:
http//extremraym.com/en/donation

Cheers !

more » « less
Video Language:
French
Duration:
08:53

English, British subtitles

Revisions