Return to Video

Lua Scripting with REAPER 5 - Part. 3: Get/Set First Selected Items Properties

  • 0:00 - 0:04
    Hello everyone, this is X-Raym,
    for the third part of the tutorial
  • 0:04 - 0:09
    about Lua coding for
    REAPER ReaScript.
  • 0:09 - 0:12
    Today, we will work on Items.
  • 0:12 - 0:16
    We will get infos of first selected items,
  • 0:16 - 0:20
    and then, we will learn
    how to modify them.
  • 0:20 - 0:24
    We will start by the script
    we wrote last time.
  • 0:25 - 0:32
    I will set Notepad++ this way
    so that it stay in foreground.
  • 0:32 - 0:38
    Imagine that I want to get
    the infos of that items.
  • 0:39 - 0:44
    First, I have to return this item
    in a variable.
  • 0:45 - 0:50
    I check the API Doc,
    and I write "Selected Item"
  • 0:50 - 0:55
    We see that there are some
    functions about Item Selection
  • 0:56 - 0:59
    The function that we want
    is this one.
  • 0:59 - 1:01
    "GetSelectedMedialtem"
  • 1:02 - 1:09
    If we add a project number ID
    and an integer
  • 1:09 - 1:10
    we get an Item.
  • 1:12 - 1:15
    Project ID is 0 for current project.
  • 1:15 - 1:20
    And the integer is the place
    of the item in selection.
  • 1:20 - 1:25
    If it is the first item,
    the integer will be 0.
  • 1:25 - 1:31
    Demo. I will create a function "Main"
  • 1:31 - 1:35
    which will contain the core
    of my script.
  • 1:35 - 1:38
    Hop, I put the function end
  • 1:38 - 1:41
    at the end of the script,
    I call "Main".
  • 1:41 - 1:48
    I move the call to that function below
    so that it is more clean
  • 1:48 - 1:51
    just before the call of Main.
  • 1:51 - 1:55
    Remember, ShowConsoleMsg("")
    is used to clean the console.
  • 1:55 - 2:01
    I take this.
    This function return an item.
  • 2:01 - 2:05
    So I will stock this item in a variable.
  • 2:05 - 2:11
    Item = reaper. GetSelectedMedialtem()
  • 2:11 - 2:16
    Here I delete value type reference,
    we don't need it.
  • 2:16 - 2:20
    As first parameter,
    0 for current project,
  • 2:20 - 2:27
    and 0 as ineteger,
    for First Selected Item.
  • 2:29 - 2:31
    If I run the scrip...
  • 2:34 - 2:39
    The console open because of
    ShowConsoleMsg(""), but nothing happen.
  • 2:40 - 2:43
    If I display item in the console...
  • 2:45 - 2:48
    Hop, save, run..
  • 2:48 - 2:53
    We see that the item
    is a special type of data
  • 2:53 - 2:55
    called userdata.
  • 2:55 - 2:59
    Userdata in Lua represent an array value...
  • 2:59 - 3:04
    but all you have to remember is that
  • 3:04 - 3:10
    this userdata is specific to this item
    the project with a unique ID.
  • 3:10 - 3:13
    We will try to get infos on this item.
  • 3:15 - 3:21
    I get back in function list,
    And I enter Item Infos.
  • 3:22 - 3:26
    I see that there is a function
    GetMedialtemlnfo_Value
  • 3:26 - 3:29
    and there is a variant for Take properties.
  • 3:29 - 3:33
    We will focus on item.
    So, I click on GetMedialtemlnfo_Value
  • 3:33 - 3:35
    and I see this function.
  • 3:35 - 3:40
    If I want to get the state of
    Mute of the item.
  • 3:42 - 3:46
    I add a breakline,
    it is more readable.
  • 3:46 - 3:50
    I add item_mute =
  • 3:50 - 3:51
    and I copy this function.
  • 3:55 - 4:03
    The item that we will set as parameter
    is the item stored in item variable.
  • 4:03 - 4:06
    I activate the automatic break lines
    of Notepad++ so it is more readable...
  • 4:08 - 4:13
    As second parameter, it asks for the
    name of the type of value we want
  • 4:13 - 4:18
    It has to be a string
    so I write "".
  • 4:18 - 4:22
    The parameter we want in our case
    is called "B_Mute".
  • 4:22 - 4:25
    I copy "B_Mute"
    and I paste it here.
  • 4:28 - 4:34
    I delete that...
    I add a break line here...
  • 4:39 - 4:43
    and I ask to the console
    to display the value of item_mute.
  • 4:45 - 4:46
    I get back here.
  • 4:47 - 4:49
    I run the script.
  • 4:49 - 4:52
    And we see that the value is 0.
  • 4:52 - 4:57
    If I mute the item
    and I run the script again...
  • 4:57 - 5:02
    we see that when the item is muted,
    the value displayed is one.
  • 5:02 - 5:08
    In other words,
    GetMedialtemlnfo_Value("B_Mute")
  • 5:08 - 5:12
    return 1, when the item is muted.
  • 5:12 - 5:17
    Another parameter very interesting
    is the position.
  • 5:18 - 5:23
    We will duplicate this line.
  • 5:23 - 5:26
    We name this variable item_pos.
  • 5:28 - 5:31
    We add the parameter "D_POS"
  • 5:31 - 5:36
    and we will display the value
    of this variable.
  • 5:36 - 5:40
    So that we can see better,
    I will add...
  • 5:40 - 5:45
    "Item Mute =" ..
  • 5:47 - 5:51
    And here "Item Position =" ..
  • 5:59 - 6:01
    I save and run.
  • 6:03 - 6:08
    It seems that I made a mistake somewhere...
  • 6:08 - 6:14
    Oh I forgot to add points
    to tell we merge two strings.
  • 6:14 - 6:16
    I run the script again.
  • 6:16 - 6:20
    We see Item Mute = 0 and
    Item Position = 0
  • 6:20 - 6:26
    Item Position = 0 because
    I made a mistake here (one trailing space).
  • 6:26 - 6:32
    We will have to be vigilent when coding one small
    space at the wrong place can broke the script.
  • 6:32 - 6:34
    I run the script again.
  • 6:36 - 6:41
    Here we see the item position
    in seconds.
  • 6:41 - 6:48
    As you can see, it is very precise.
  • 6:48 - 6:51
    If I want to display the duration,
    I duplicate
  • 6:53 - 6:56
    I get the parameter value here.
  • 7:00 - 7:02
    I replace.
  • 7:03 - 7:06
    I rename the variable.
  • 7:08 - 7:13
    And I duplicate that too,
    and display the item length.
  • 7:15 - 7:19
    I save, I select the item
    I run the script.
  • 7:20 - 7:25
    We see that the item length
    as the same value as the item pos
  • 7:25 - 7:30
    because I forgot to copy paste
    the variable here...
  • 7:30 - 7:32
    Voilà. I run again.
  • 7:35 - 7:37
    We will see that the
    item length is 9 seconds.
  • 7:37 - 7:45
    If I make it shorter, it display 5s,
    if I extend, it displays 9 seconds.
  • 7:46 - 7:52
    Another item parameter often used
    by scripters is the volume.
  • 7:52 - 7:56
    I will show you how it works,
    because it is a bit special.
  • 7:56 - 7:58
    I duplicate this.
  • 7:58 - 8:00
    "D_VOL"
  • 8:00 - 8:07
    Note that the variable can be named
    with every name that you want.
  • 8:10 - 8:13
    I duplicate this line.
  • 8:16 - 8:18
    And I wrote item_vol here.
  • 8:20 - 8:23
    If I execute the script, I can see that
    item_vol = 1
  • 8:23 - 8:27
    However, I see that item volume
    is set to 0 dB.
  • 8:28 - 8:32
    It is because the scale of the
    vol parameter displayed in the console
  • 8:32 - 8:36
    is completly different.
  • 8:36 - 8:41
    The value returned bu "D_VOL"
    is on a log scale.
  • 8:42 - 8:50
    If item_vol = 0, it means that
    that item volume is -inf dB
  • 8:50 - 8:55
    If item_vol > 1, it means that
    the item had a volume boost.
  • 8:55 - 9:00
    I'll show you, if I set -7 dB,
    and that I run the script again...
  • 9:00 - 9:03
    We see that it is 0.4 (log scale)
  • 9:03 - 9:08
    If I boost at 10 dB, it is equal to 3
    (approx).
  • 9:08 - 9:12
    2 on log scale is for 6 dB boost
    (approx).
  • 9:12 - 9:16
    To convert the log scale value
    into dB
  • 9:16 - 9:19
    We have to do some maths.
  • 9:19 - 9:23
    It is a bit complicated but
    we can use a code snippet
  • 9:23 - 9:31
    a small code chunk that you can use
    without having to write it from scratch
  • 9:31 - 9:35
    You only have to understand
    what variable you have to replace.
  • 9:35 - 9:39
    Here, we just have to replace
    some variable names.
  • 9:39 - 9:47
    We will replace VolDB by item_voldB so
    that it matches our other variable naming
  • 9:47 - 9:51
    Here, the value to put
    is the value of item_vol
  • 9:52 - 9:57
    which is transform
    by this formula.
  • 9:57 - 10:03
    If I display item_volDB here...
  • 10:03 - 10:06
    we can see that here is 0dB.
  • 10:06 - 10:10
    And here is too. If I set to -7db
    it displays -7 in the console.
  • 10:10 - 10:14
    If I go to +6db it displays +6.
  • 10:14 - 10:19
    There is other nice parameters,
  • 10:19 - 10:21
    such as fades length,
  • 10:22 - 10:25
    snap offset position,
  • 10:25 - 10:28
    and color of the object.
  • 10:28 - 10:32
    Now imagine that we want to
    modify these values.
  • 10:32 - 10:38
    There is a sister function of
    GetMedialtemlnfo_Value which is...
  • 10:38 - 10:40
    I'll show you...
  • 10:43 - 10:46
    which is SetMedialtemlnfo_Value
  • 10:46 - 10:52
    we can see that it look like the Get
    version so that there a third parameter
  • 10:52 - 10:55
    which is the new value we want
    to set.
  • 10:55 - 10:56
    I will copy that.
  • 10:59 - 11:00
    Put it here...
  • 11:01 - 11:05
    The item we want to modify
    is still in the item variable.
  • 11:05 - 11:12
    We will start by setting
    the mute parameter.
  • 11:15 - 11:18
    "B_MUTE"
  • 11:18 - 11:22
    As new value, we set 0.
  • 11:22 - 11:23
    If I run the script...
  • 11:24 - 11:26
    Nothing happens.
  • 11:26 - 11:30
    But if I zoom out,
    or click, you see that
  • 11:30 - 11:32
    the item has been unmuted.
  • 11:32 - 11:35
    We have to add at the end of the script
  • 11:35 - 11:38
    a simple function UpdateArrange
  • 11:38 - 11:42
    which allows to update the arrange view
    if some items have been modified.
  • 11:42 - 11:45
    Now, if I change the script...
  • 11:46 - 11:50
    You see that I don't need to click
  • 11:51 - 11:53
    to refresh the arrange view
    (and the item).
  • 11:54 - 11:56
    Now if I want to modify...
  • 11:57 - 12:00
    the position...
  • 12:00 - 12:04
    I write a new position in seconds.
  • 12:05 - 12:10
    I set "D_POSITION" as second parameter.
  • 12:10 - 12:14
    And now my item should go to 1.5 second.
  • 12:14 - 12:17
    The item is nicely placed at 1.5 s
    in the project.
  • 12:21 - 12:23
    If I set 3...
  • 12:24 - 12:25
    it will go there...
  • 12:25 - 12:28
    We can of course use calculations.
  • 12:28 - 12:31
    5 + 10
  • 12:31 - 12:33
    and it will go at position 15 s.
  • 12:33 - 12:35
    To modify the volume,
  • 12:35 - 12:40
    I will advice you to modify the
    volume converted in dB.
  • 12:40 - 12:45
    But you will have to set it back
    to log to make the modification valid.
  • 12:45 - 12:49
    If we want to add
    +1 dB to our item
  • 12:50 - 12:53
    We can do the following thing...
  • 12:53 - 12:54
    And here I do...
  • 12:56 - 12:59
    reaper. SeMedialtemlnfo_Value(item,
    "D_VOL"...
  • 13:03 - 13:06
    reaper. SeMedialtemlnfo_Value(item,
    "D_VOL", item_volDB)
  • 13:06 - 13:12
    It is still express into dB
    we have to convert it in Log scale
  • 13:12 - 13:17
    Here is the code snippet.
  • 13:20 - 13:21
    We put it here.
  • 13:22 - 13:30
    And we replace calc
    the variable we have to change,
  • 13:30 - 13:35
    by what we wanted (our dB value +1)
  • 13:35 - 13:39
    I unmute the item,
    and I run the script.
  • 13:43 - 13:48
    The item goes to position 0,
    its volume had a +1 dB boost,
  • 13:48 - 13:51
    and it is not muted anymore.
    If I run the script again...
  • 13:51 - 13:55
    we will see that the item volume
    gets another extra +1 dB boost.
  • 13:55 - 14:01
    If I want to substract 1 dB,
    I set - here, and when I run the script...
  • 14:02 - 14:04
    The item volume goes down.
  • 14:04 - 14:09
    In the next videos, we will see how to get the
    parameter values of several selected items
  • 14:09 - 14:11
    and how to modify them
    one after the other.
Title:
Lua Scripting with REAPER 5 - Part. 3: Get/Set First Selected Items Properties
Description:

In this tutorial, you will learn how to get and set properties of the first selected items.

Source Article :
http://extremraym.com/reascript-video-getset/

Any feedback would be really appreciated ! :)

more » « less
Video Language:
French
Duration:
14:13

English, British subtitles

Revisions