Posted by & filed under Bitwig Studio, Blog, Tutorials.

Last time we took a look at creating observers and using them to get data from Bitwig out to our controllers. While doing that we started to gather a decent amount of functions throughout our script. Eventually this will become messy, hard to read, and time consuming to update. Luckily, we can create external javascript files that can contain function, variable, and object definitions. In this post we will group our transport control code into a single function and host it in an external file.

First we need to create a new javascript file in our ‘MyController’ directory. You can do this with any text editor. Let’s name it ‘MyController_functions.js’, and add a small test function, so we can verify that the import process is working:



Whenever this function is called, it will print this text to the console in Bitwig.

In order to get access to the function we just defined in our script we have to import this file into our main control script. As long as the file is in the same directory all we need to do is call the load() function somewhere in the global scope of our script where we load the API and define our MIDI ports:



Now we should place our test() function somewhere in our OnMidiPort1() function to verify that this import process has been successful:



If we send any type of MIDI data from our controller we will see the text appear in our console and we have successfully defined our first external function. This is great, but it’s not doing much except flooding the console. Let’s wrap up all of our conditionals regarding the transport into one function. You can do that easily by copying the code that controls our transport state, and placing it into a function definition in our functions.js file:


We have defined a function called transport_functions that takes the the two data bytes of a MIDI message as it’s arguments. Now that we have our transport_functions() defined, we can call this function in the note portion of our onMidiPort1() function and have the same transport functionality that we had previously. The differences are that our script is much more readable on a macro level, we can modify the function in our external file, and we won’t have to touch our main script.

This is a great way to consolidate and abstract pieces of code and it allows you to separate different collections of functionality into different files. When a script starts to reach any level of sophistication it will become very important to keep your code organized and clear. If the code is all put in one file it will become a huge undertaking to modify and troubleshoot.

In the next article, we will start looking at another way to consolidate code and one of the most powerful features of javascript: objects.

Direct any questions, comments or concerns to