# Lua beginner problems

**URL:** <https://forum.electra.one/t/lua-beginner-problems/2163>\
**Category:** General discussion\
**Created:** [June 11, 2023, 7:39am UTC](https://forum.electra.one/t/lua-beginner-problems/2163 "2023-06-11T07:39:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 11, 2023, 7:39am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/1 "2023-06-11T07:39:55Z")

</div>

Hi!

~~I want to add a controller for choosing the MIDI channel for a device. But I fail much earlier: How can I create multiple devices for one preset? I manage to create _one_ device with one MIDI channel - more is not possible - at least I can’t find a way.~~ Got it, just 5 minutes after writing…

But even if I have only this one device: how do I find out the ID from the device name to change the MIDI channel?

My previous test was a failure… probably there are a lot of errors and it’s much easier than that:

```auto
print ("Hello World!!!")

function idByName (name)
    local id
    
    for id = 1,32 do
        local device = devices.get (id)
        if(device) then
            if (device:getName (id) == name) then
                return id
            end
        end
    end
    return 0
end

print (idByName("Test2"))

```

Has anyone a hint what I am doing wrong?

TIA, Holger

---

<div class="post-metadata">

**Author:** ![NewIgnis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/newignis/32/767_2.png) [@NewIgnis](https://forum.electra.one/u/NewIgnis)\
**Post date:** [June 11, 2023, 8:05am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/2 "2023-06-11T08:05:35Z")

</div>

Well it looks like you want to make controls on your preset that are solely expressed in synth names, but you want to avoid that the mapping between device ID and the synth names are to be managed on two places.

Why not the other way round, and express code and controls mainly in the device ID’s ( these are at least fixed from 1…32) and then use getName to find its name?

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 11, 2023, 8:45am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/3 "2023-06-11T08:45:23Z")

</div>

> [@NewIgnis](#):
>
> Why not the other way round, and express code and controls mainly in the device ID’s ( these are at least fixed from 1…32) and then use getName to find its name?

Ok, this seems to be the way.

Is the assignment of device-id to MIDI channel unique? Can I assume that device ID 1 corresponds to port 1/MIDI channel 1 and device ID 18 to port 2/MIDI channel 2?

But why does this not work (I created two Devices: `Test1` on `port1/channel1` and `Test2` on `port1/channel2`):

```auto
print ("Hello World!!!")
local d=devices.get(1)
print(d.getName())

```

This throws an error for devices.get():

```auto
10:44:15.749 ---- START ----
10:44:15.761 lua: Hello World!!!
10:44:15.772 loadLuaModule: error loading file: filename=ctrlv2/slots/p001.lua, error=ctrlv2/slots/p001.lua:17: failed: device 1 does not exist
10:44:15.777

```

---

<div class="post-metadata">

**Author:** ![martin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/martin/32/145_2.png) [@martin](https://forum.electra.one/u/martin)\
**Post date:** [June 11, 2023, 8:53am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/4 "2023-06-11T08:53:56Z")

</div>

There are two important things to mention:

`devices.get(1)` returns an object. Therefore the `getName()` must be called using the object:method notation. `print(d:getName())`

Also, a device is added to the preset model when it has at least one control assigned. It might be that you do not have any controls in your preset, therefore the `devices.get(1)` does not return any instance of device. This is to save resources on E1 side. I guess it should be mentioned in the docs.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/electraone/original/2X/f/f502f7baee709cbd54f103e2c6c190e133294986.jpeg)

---

<div class="post-metadata">

**Author:** ![NewIgnis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/newignis/32/767_2.png) [@NewIgnis](https://forum.electra.one/u/NewIgnis)\
**Post date:** [June 11, 2023, 8:57am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/5 "2023-06-11T08:57:42Z")

</div>

The relationship between device-ID 's and MIDI channels is not fixed. That is useful, for instance when you make a preset for a bitimbral or a multitimbral synth: you then can reuse the same controls for another timbre, just by changing the MIDI channel of the device.

Let’s take the A-station preset as an example . I’ve assigned all the controls to the first device in the list (the one with ID=1), and then renamed that device to “A-station” and assigned it MIDI channel 11.

When you click here, it’ll bring you to all 32 devices:

![image](https://europe1.discourse-cdn.com/flex017/uploads/electraone/original/2X/9/9a27abba7c8b2982ff0e6a54f7466cd72c7c069f.png)

Unfortunately the devices here are ranked according to MIDI channel, which I think is better not to do that way and just keep the device with ID=1 on the first place. Bear that in mind.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/electraone/original/2X/7/75ec43bbad97c91c9d8441ce014c150cd72a8116.png)

If someone with an A-station wants to use this preset as well, all he needs to do is change the MIDI channel of “A-station” device in the one he uses.

In the lua code, I assume no one will change the ID’s of the devices, but they are allowed to change the name of the device and the channel.

Therefor, a typical lua code starts with verifying the main characteristics of the device that is being used:

```auto
deviceId = 1 -- the preset is build with all controls for device 1
device = devices.get(deviceId)
devPort = device:getPort()
channel = device:getChannel()

```

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 11, 2023, 9:06am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/6 "2023-06-11T09:06:28Z")

</div>

> [@martin](#):
>
> There are two important things to mention:
> 
> `devices.get(1)` returns an object. Therefore the `getName()` must be called using the object:method notation. `print(d:getName())`

Sure… too much C++ programmed the last years 🙂

> [@martin](#):
>
> Also, a device is added to the preset model when it has at least one control assigned. It might be that you do not have any controls in your preset, therefore the `devices.get(1)` does not return any instance of device.

Ahhh - got it!!! That was my mistake!I used a complete empty preset as default - only th two devices.

Thanks!

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 11, 2023, 9:25am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/7 "2023-06-11T09:25:21Z")

</div>

> [@NewIgnis](#):
>
> If someone with an A-station wants to use this preset as well, all he needs to do is change the MIDI channel of “A-station” device in the one he uses.

Ok, thanks @NewIgnis for the great explanation with the pictures. I think I have understood it. Unfortunately this doesn’t solve my problem, because I could only change the MIDI channel using the web app.

I am trying to build a preset for my Zynthian and keep it as flexible as possible. With the Zynthian I can put many different software synths (LV2 instances) on different MIDI channels and save them as a snapshot. I use different snapshots on which the software synth ZynAddSubFX (e.g.) is sometimes on MIDI channel 2 and sometimes on 7 and 8.

So I could build a suitable preset for each snapshot - but it would be much more flexible for me to have a generic preset, where I can select the MIDI channel for the ZynAddSubFx by a E1 controller and then read in the current parameters. This would then work without an external computer (what is necessary for me). Do you think this is possible?

---

<div class="post-metadata">

**Author:** ![NewIgnis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/newignis/32/767_2.png) [@NewIgnis](https://forum.electra.one/u/NewIgnis)\
**Post date:** [June 11, 2023, 9:28am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/8 "2023-06-11T09:28:34Z")

</div>

It is certainly possible. I don’t know anything about Zynthian (I’m more a hardware person). What are the possibilities for Zynthian to read out current parameters ? Does it send all of its parameters in CC or NRPN in one go? Does it use SysEx and performs edit buffer sound dumps?

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 11, 2023, 9:41am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/9 "2023-06-11T09:41:15Z")

</div>

> [@NewIgnis](#):
>
> What are the possibilities for Zynthian to read out current parameters ? Does it send all of its parameters in CC or NRPN in one go? Does it use SysEx and performs edit buffer sound dumps?

It depends on which software synth you use. This is a different problem for me for now. Zynthian is a standalone Synth based on a Raspberry Pi (4) running a meanwhile quite mature and flexible interface. With this you can start all possible software synths (LV2 based) or samplers and use it multitimbral.

I ported Dexed for Zynthian to LV2 back then and it could be prompted for parameter output using SYSEX. Other synths react differently or may not have such a thing implemented at all…

For me there is also the other way: One preset via Zynthian snapshot. The preparation is more complex and you are not flexible enough without a computer, but you could also just learn the MIDI-CCs of the E1 controllers into the Zynthian… this is easier, but with more “stupid” mapping. I think this is what I will tackle first. Then I can see how complex I want to make it…

---

<div class="post-metadata">

**Author:** ![oldgearguy](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@oldgearguy](https://forum.electra.one/u/oldgearguy)\
**Post date:** [June 11, 2023, 10:52am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/10 "2023-06-11T10:52:50Z")

</div>

The way i try to handle it is with a user setup/configuration screen.

I try to determine what I need when the preset starts up and if I can’t, I jump the user to a config page where they have to choose things like the port, MIDI channel, and in my particular case, which OS version is in the device.

If the units are all connected and powered up in the right sequence I can usually query the device to find it out automatically, but all of that isnt guaranteed and I don’t want to force them to always have the computer and web UI running

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 12, 2023, 10:49am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/11 "2023-06-12T10:49:52Z")

</div>

> [@oldgearguy](#):
>
> I try to determine what I need when the preset starts up and if I can’t, I jump the user to a config page where they have to choose things like the port, MIDI channel, and in my particular case, which OS version is in the device.

Do you have a hint where I can take a look at this code or can you send me some code where I can try to learn from?

---

<div class="post-metadata">

**Author:** ![oldgearguy](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@oldgearguy](https://forum.electra.one/u/oldgearguy)\
**Post date:** [June 12, 2023, 11:15am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/12 "2023-06-12T11:15:13Z")

</div>

If you can wait until later today, I can put together a quick subset of what I have.  
Basic idea - create a momentary button, label it config.  
Create a function (I call it jumpPage) and add it to the button. You can do this without the button if the user is willing to use the soft button in the bottom right, plus select a page.

On the config page, create the controls you need like MIDI channel, port, etc.  
When the user selects that value, save it as a global.

in the LUA code, I typically define my globals like that with some default value so that if the user just tries to use the preset, it may do something, but the config page is where it can be changed.

This all reads harder than it is to do. For now, think about what kinds of controls/prompts you’ll need to interact with the user.

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 12, 2023, 11:27am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/13 "2023-06-12T11:27:43Z")

</div>

> [@oldgearguy](#):
>
> If you can wait until later today, I can put together a quick subset of what I have.

That would be fantastic. No hurry! Many thanks!!!

> [@oldgearguy](#):
>
> This all reads harder than it is to do. For now, think about what kinds of controls/prompts you’ll need to interact with the user.

Currently I would be very happy if I could change the MIDI channel of a synth via a control element. I program a lot of C/C++ and bash. I understand Lua, but I haven’t got into the depths of the E.1 Lua API yet. Unfortunately, I have found very little information about the use of the `Devices`. I would try to collect my findings and then make them available as a demo preset.

---

<div class="post-metadata">

**Author:** ![oldgearguy](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@oldgearguy](https://forum.electra.one/u/oldgearguy)\
**Post date:** [June 12, 2023, 7:42pm UTC](https://forum.electra.one/t/lua-beginner-problems/2163/14 "2023-06-12T19:42:01Z")

</div>

Take a look through this example. [Config example](https://app.electra.one/preset/XQQXTs5bHPxl6rLnKzcm)

Basic use - press the Get Dev Info button to populate the data.  
Press the Config button to jump to the config page.

Mess around with the port, channel, and rate.  
Go back to the main page and press the Get Dev info button again.

You can use the CC control to send Volume data to the selected MIDI channel and port.

Look through the functions, ask questions.

I was using virtual controls so they would not send out MIDI, but I was storing/using the parameterMap.  
The volume control sends CCs.

---

<div class="post-metadata">

**Author:** ![oldgearguy](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@oldgearguy](https://forum.electra.one/u/oldgearguy)\
**Post date:** [June 12, 2023, 7:43pm UTC](https://forum.electra.one/t/lua-beginner-problems/2163/15 "2023-06-12T19:43:56Z")

</div>

The device stuff is interesting. I tried using a control to scan through all the devices, but if you don’t have anything, it comes back undefined. Not really sure how you can identify the device ID on startup other than by trying to get the name or something from each one and ignoring the errors or something.

---

<div class="post-metadata">

**Author:** ![markus.schloesser](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@markus.schloesser](https://forum.electra.one/u/markus.schloesser)\
**Post date:** [June 13, 2023, 2:37am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/16 "2023-06-13T02:37:21Z")

</div>

> [@oldgearguy](#):
>
> Not really sure how you can identify the device ID on startup other than by trying to get the name or something from each one and ignoring the errors or something.

This is a rabbit hole I’m not sure you should jump into 😂  
If you still do after that warning, check what @kris is doing in knobkraft wrt device identification (tldr all synths behave differently)

---

<div class="post-metadata">

**Author:** ![C0d3man](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/c0d3man/32/1511_2.png) [@C0d3man](https://forum.electra.one/u/C0d3man)\
**Post date:** [June 13, 2023, 9:01am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/17 "2023-06-13T09:01:51Z")

</div>

> [@oldgearguy](#):
>
> Take a look through this example. [Config example](https://app.electra.one/preset/XQQXTs5bHPxl6rLnKzcm)

Many thanks @oldgearguy! I will take a look at this at evening (now have to do something for getting beer into the fridge).

Regards, Holger

---

<div class="post-metadata">

**Author:** ![oldgearguy](https://avatars.discourse-cdn.com/v4/letter/o/f4b2a3/32.png) [@oldgearguy](https://forum.electra.one/u/oldgearguy)\
**Post date:** [June 13, 2023, 9:06am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/18 "2023-06-13T09:06:02Z")

</div>

Sometimes I open a beer to celebrate some success with a preset and sometimes I open many beers to dull the pain of figuring out why nothing is working. lol

---

<div class="post-metadata">

**Author:** ![martin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/martin/32/145_2.png) [@martin](https://forum.electra.one/u/martin)\
**Post date:** [June 13, 2023, 9:25am UTC](https://forum.electra.one/t/lua-beginner-problems/2163/19 "2023-06-13T09:25:34Z")

</div>

I stopped doing this when I moved to a place close to Dutch/Belgian border. Celebrating the success with one Belgian beer ruins productivity for whole rest of the day 🙂

---

<div class="post-metadata">

**Author:** ![NewIgnis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.electra.one/newignis/32/767_2.png) [@NewIgnis](https://forum.electra.one/u/NewIgnis)\
**Post date:** [June 13, 2023, 7:46pm UTC](https://forum.electra.one/t/lua-beginner-problems/2163/20 "2023-06-13T19:46:49Z")

</div>

After three Duvels it gets better again 😃

[Next page](https://forum.electra.one/t/lua-beginner-problems/2163.md?page=2)
