Getting Started
This tutorial shows how to setup a Max for Live device to run JavaScript code using Max 9's new
v8
object. To follow along, you need Ableton
Live Suite (which comes with Max for Live) or Live Standard edition with the
Max for Live add-on. You probably need to be using Live version
12 or higher.
As of December 2024, you also need a standalone copy of Max 9, or wait for Max 9 to be included with Live (which may already be true by the time you read this). If Max 9 is not yet included with Live, you need to change Live's settings to use your standalone version of Max 9 instead of the version bundled with Live (under Settings → File & Folder → Max Application).
Enabling the Monaco Code Editor in Max
Max 9 ships with a new Monaco code editor that Microsoft built for Visual Studio Code. As of December 2024, this is an experimental feature and only available on macOS (Windows support is coming). It is / will be a better code editing experience, so I suggest giving it a try. It's in Max's Preferences for Text Editing.
Selecting the Max device type
We create a new Max device by adding an empty Max device from the Live library, similar to adding any instrument, effect, or plug-in. Select "Max for Live" in the Live browser sidebar, and the empty Max devices will be listed first (highlighted in the following screenshot), followed by the ready-to-use Max devices:
There are three types of Max devices to choose from:
- Max Audio Effect
- Max Instrument
- Max MIDI Effect
In these tutorials, we will always use a Max MIDI Effect device. As explained in the overview's section on limitations, JavaScript is not suitable for synthesizing instruments or implementing effects.
Even though JavaScript isn't fast enough to directly implement instruments and effects, there may be reasons to put JavaScript in a Max Audio
Effect or Max Instrument device. For example, if complex logic is
needed to translate the state of the UI controls into parameters for the instrument/effect, it's conceivable that part of the logic could be done in JavaScript because UI events happen at a reasonably slow rate.
Regardless, I recommend using Max MIDI Effect devices for your JavaScript in Live projects until there is a good reason to use
another device type.
Creating a Max Device
Now that we've decided to use a Max MIDI Effect device, let's create a new one:
- Select "Max for Live" in the browser sidebar
- Drag a "Max MIDI Effect" device from that folder onto a MIDI track (or onto the empty "Drop Files and Devices Here" space to create a new MIDI track)
- Click the edit button on the Max MIDI Effect device's title bar (it looks like this: ) and wait for the Max editor to open your device's patch:
- Cleanup by deleting the "MIDI from Live", "Build your MIDI effect here", "MIDI to Live", and "Device vertical limit" comments (if you have any trouble editing the patch, make sure you understand the Max patcher basics).
- Save the device patch.
We left the midiin
and midiout
objects there so MIDI
will pass through the device.
Creating a v8
Max object
In the Max patch editor for our new device:
- Add an object to the Max patch (either drag an "Object" from the top toolbar or type "n" in an unlocked patch)
- Type
v8
into the object box: - Lock the patch: Either click the lock icon in the lower left () or type command+E (macOS) or ctrl+E (Windows).
- Double click the
v8
object to open Max's JavaScript editor
Unlike the old js
object, you are no longer required to save your JavaScript code as a
separate file. By default, v8
embeds your JavaScript code directly in the Max
patch.
If you want to use your own code editor instead of Max's
built-in code editor, then you need to provide a filename like v8 my-filename.js
and save the script to that file. This is done
the same way as the old js object.
Note you can enable a new Monaco editor in Max that brings some of the power of Visual Studio Code to Max's built-in code editor.
Testing it out
That's the basic device setup. Now, every time you save the code in Max's JavaScript editor, it will run it. Type this into the JavaScript editor and save:
post("Hello World!");
You should see "Hello World!" in the Max Console (you can open the console from the Max patch's right sidebar or in the "Window" menu):
You can change the code and save it again:
post("Hello Live!");
Now it should print "Hello Live!" in the Max console. You should see it appear next to the first "Hello World!" message, like this:
v8 • Hello World! Hello Live!
Were you expecting "Hello Live!" to appear on its own line? Later we'll learn how to get more control over
how things are logged to the Max console. For now, be aware you can write code like
post("Hello Live!\n");
with an explicit "\n"
newline
to print messages to their own line in the Max console.
Code directly in the patch with v8.codebox
Max 9 brings a new way to edit JavaScript code with v8.codebox
by placing the code
editor directly into the patch instead of using a separate window. You might prefer using it for smaller scripts. It
might work well for live coding by using
v8.codebox
as the interface for a Max for Live device.
Note that v8.codebox
does not support
the new Monaco editor, so that may be a reason to use the
v8
object.
It's easier for me to take screenshots in these tutorials with v8.codebox
, so you'll see me using it a lot. Feel free to use v8
anywhere I am using v8.codebox
(or vice-versa).
Re-running Code in v8.codebox
There's two ways I typically re-run code in v8.codebox
:
- After making any changes to the code, click somewhere else in the Max patch (away from the
v8.codebox
). If you've changed the code, this will trigger a re-run. - Click the hammer icon () in the lower left of
the
v8.codebox
to force a re-run at any time. Normally, you can only click this when the patch is locked. You can command+click (macOS) or ctrl+click (Windows) the icon to trigger it in an unlocked patch.
Also note that you can only actually edit the code inside v8.codebox
if the patch is
unlocked. Remember, you can toggle the patch lock/unlock state with the lock icon in the lower left of the Max patch
editor window (), or with the command+E (macOS) or ctrl+E
(Windows) keyboard shortcuts.
Next steps
We're ready to build stuff! The next tutorial, "Real Time MIDI Processing", shows how to use JavaScript to alter MIDI notes as they are played.