Controlling An Instrument Using Lua.

How can I use Lua with the Renoise API to play or manipulate a loaded instrument or sample? I’ve been looking through docs and source code but I haven’t seen how to do what I want.

If I have an instrument or sample loaded into one of the slots in the upper right side I can use the computer keyboard to play notes. I can do this even if Renoise is playing a song, basically allowing for a live performance.

I want to be able to trigger notes the same way but using Lua. I’m trying to put together some basic Renoise demos showing how to manipulate it using OSC. I want to add a custom OSC handler and have it use values passed via OSC to play a loaded instrument or sample.

I have not seen any way, though, to use the Renoise API to play a loaded instrument as if it were being played from the keyboard, providing note, velocity, and duration.

Is this possible?

If so, how can I do it?

Thanks!

If you are using OSC then triggering notes is quite simple. Open up Edit -> Preferences -> OSC then scroll down to the bottom of the list and you will see /renoise/trigger/note_on(x,x,x,x) . That’s the OSC handler for triggering notes. Just make sure that the parameters in the parentheses are packed together before being sent to Renoise.

Thank you for this, but that doesn’t quite allow for what I want.

Given some parameters I want, in the Lua for a custom OSC handler, to decide what note, with what velocity, and for what duration, and possible select the instrument it is meant for.

OSC is just a way to get some values to some Lua code. I’m trying to figure out how, in Lua, to then manipulate the loaded instruments and samples.

AFAIK, there is no part of the scripting API that specifically allows for triggering samples.
However, you could achieve the same effect by sending an OSC message from inside your script to Renoise that triggers the note-on with the proper parameters.

Like this:

  
 ______________ ____________ ________________  
| | Custom OSC Handler | | Renoise built-in OSC | |  
| External OSC | ===================> | Lua Script | =====================> | Note triggered |  
|______________| |____________| |________________|  
  

I was afraid of that. Seems odd, though, that something so basic isn’t exposed in the API.

True, though it feels a bit too hacky. It also means I need a way to handle note duration, since I would need to follow note_on with a note_off note_off after some period of time.

I’ll have to think over my options.

This post (Questions About Raw Midi Events In Osc) has some very useful info, in particular how to encode mulit-byte MIDI command info into a single integer

LUA scripting in Renoise is currently not executed in real-time. this is no trivial point in this scenario, since you have no warranty that the commands you give via scripting will be executed immediately.

this should also explain the problems with fast note triggering in the above linked tool.

to be more specific, LUA is not executed in the audio thread (probably it is executed in the GUI thread). what can happen at least is that, when ramping a value, only certain points of the ramp will be actually used. Moreover, it is highly probable that the speed of execution varies with the GUI refresh rate, which is configurable in Renoise GUI preferences

No, but Lua allows changing sample velocity layer assignments, so by creating velocity arrays in Lua, you can simply attach sample slots to those and this way more or less control samples through lua.
But as IT-Alien already said, no realtime stuff here but if you can live with the minor descrepancies, that should not be really a problem.
When i was fumlbing with OSC triggering, i actually didn’t noticed much of the delays.

If you deal with external hardware, you can trigger notes (and decide their note, velocity and destination track) with near-zero latency.
But when a lua script needs to trigger notes by itself, a noticeable latency is introduced, because then the event is generated in non-audio thread.
So, essentially what you want to do is possible - and precise - as long as you’re using something external to control the triggering.
A fixed duration is the only tricky thing to implement among the things you mention, because then you are back into the non-audio thread. But, it’s possible too - within a ~10ms time window.

Well, apart from the duration thing you could check out the Duplex Keyboard ;)

Ah. Still, there are those MIDI OSC commands, but I guess then one has to account for some possible lag.

I managed to write some code that sends note_on and note_off OSC messages. There’s no sleep in between (I have to figure out the right way to do that) but since there’s already some delay when sending that note_on command the result is a note with a usable duration.

I also need to look at sending raw MIDI messages since I may be able to use that for MIDI-mapped controller commands.

Thanks for helping my understanding of Renoise scripting.

Thanks, really good idea. That may solve some problems for me. I’m writing a book on using the XBox Kinect for music and graphics, and wanted to show some interesting but relative simple Renoise examples. One using OSC messages, the other by using MIDI mapping. The Duplex Keyboard may cover the things I wanted to do, which makes set-up for the read much easier.