Does anybody know of a project that does/did Python bindings for the Tracktion Engine?
Thanks.
Do you mean something like this?
# Python bindings for Tracktion Engine using pybind11
# Note: This is a high-level example covering key Tracktion Engine components.
# Tracktion Engine is a C++ framework, and complete bindings would be extensive.
# This code focuses on core classes (Engine, Edit, Track, Clip) for demonstration.
# Assumes Tracktion Engine is installed and headers are accessible.
# Requires pybind11 for binding C++ to Python.
# File: tracktion_engine_bindings.cpp
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "tracktion_engine/tracktion_engine.h"
namespace py = pybind11;
using namespace tracktion::engine;
// Helper function to initialize Tracktion Engine
std::unique_ptr<Engine> create_engine(const std::string& app_name) {
return std::make_unique<Engine>(app_name, std::make_unique<ExtendedUIBehaviour>(), nullptr);
}
PYBIND11_MODULE(tracktion_engine, m) {
m.doc() = "Python bindings for Tracktion Engine, a framework for building DAW-like audio applications.";
// Engine class binding
py::class_<Engine, std::unique_ptr<Engine, py::nodelete>>(m, "Engine")
.def(py::init(&create_engine), py::arg("app_name"))
.def("get_version", &Engine::getVersion, "Returns the current version of Tracktion Engine.")
.def("get_device_manager", &Engine::getDeviceManager, py::return_value_policy::reference,
"Returns the DeviceManager for handling audio/MIDI devices.")
.def("get_temporary_file_manager", &Engine::getTemporaryFileManager, py::return_value_policy::reference,
"Returns the TemporaryFileManager for handling temporary files.");
// Edit class binding
py::class_<Edit, std::unique_ptr<Edit, py::nodelete>>(m, "Edit")
.def(py::init([](Engine& engine, juce::ValueTree tree, Edit::LoadContext context) {
return std::make_unique<Edit>(engine, tree, context);
}), py::arg("engine"), py::arg("tree"), py::arg("context"),
"Creates an Edit object for managing a session.")
.def("get_all_tracks", &Edit::getAllTracks, py::return_value_policy::reference,
"Returns a list of all tracks in the Edit.")
.def("create_empty_edit", [](Edit& edit) {
return Edit::createEmptyEdit(edit.getEngine());
}, "Creates a new empty Edit.")
.def("play", &Edit::play, "Starts playback of the Edit.")
.def("stop", &Edit::stop, "Stops playback of the Edit.");
// Track class binding
py::class_<Track, std::unique_ptr<Track, py::nodelete>>(m, "Track")
.def("get_name", &Track::getName, "Returns the name of the track.")
.def("get_clips", &Track::getClips, py::return_value_policy::reference,
"Returns a list of clips in the track.")
.def("insert_clip", [](Track& track, Clip& clip, double position) {
track.insertClip(clip, position);
}, py::arg("clip"), py::arg("position"), "Inserts a clip at the specified position.");
// AudioTrack class binding (inherits from Track)
py::class_<AudioTrack, Track, std::unique_ptr<AudioTrack, py::nodelete>>(m, "AudioTrack")
.def("insert_wave_clip", [](AudioTrack& track, const juce::File& file, double position) {
return track.insertWaveClip(file, position);
}, py::arg("file"), py::arg("position"), "Inserts a wave clip from a file at the specified position.");
// Clip class binding
py::class_<Clip, std::unique_ptr<Clip, py::nodelete>>(m, "Clip")
.def("get_name", &Clip::getName, "Returns the name of the clip.")
.def("get_position", &Clip::getPosition, "Returns the position of the clip in seconds.")
.def("set_position", &Clip::setPosition, py::arg("position"), "Sets the position of the clip in seconds.");
// AudioClipBase class binding (inherits from Clip)
py::class_<AudioClipBase, Clip, std::unique_ptr<AudioClipBase, py::nodelete>>(m, "AudioClipBase")
.def("get_source_file", &AudioClipBase::getSourceFile, "Returns the source file of the audio clip.")
.def("set_loop_range", &AudioClipBase::setLoopRange, py::arg("start"), py::arg("end"),
"Sets the loop range for the clip in seconds.");
// DeviceManager class binding
py::class_<DeviceManager, std::unique_ptr<DeviceManager, py::nodelete>>(m, "DeviceManager")
.def("initialise", &DeviceManager::initialise, py::arg("num_input_channels"), py::arg("num_output_channels"),
"Initializes the device manager with the specified number of input and output channels.")
.def("get_output_devices", &DeviceManager::getOutputDevices, py::return_value_policy::reference,
"Returns a list of available output devices.");
// ValueTree class binding (used for Edit state)
py::class_<juce::ValueTree>(m, "ValueTree")
.def(py::init<juce::Identifier>(), py::arg("type"), "Creates a ValueTree with the specified type.")
.def("set_property", &juce::ValueTree::setProperty, py::arg("name"), py::arg("value"), py::arg("undo_manager") = nullptr,
"Sets a property on the ValueTree.");
// LoadContext enum binding
py::enum_<Edit::LoadContext>(m, "LoadContext")
.value("none", Edit::LoadContext::none)
.value("all", Edit::LoadContext::all)
.export_values();
// ExtendedUIBehaviour class binding (for customization)
py::class_<ExtendedUIBehaviour, juce::UIBehaviour, std::unique_ptr<ExtendedUIBehaviour, py::nodelete>>(m, "ExtendedUIBehaviour")
.def(py::init<>(), "Creates a default ExtendedUIBehaviour for customizing UI behavior.");
}
# Compilation instructions:
# 1. Install pybind11: `pip install pybind11`
# 2. Ensure Tracktion Engine and JUCE headers are in your include path.
# 3. Compile with a C++ compiler, linking against Tracktion Engine and JUCE libraries.
# Example command (adjust paths as needed):
# g++ -shared -std=c++20 -I/usr/include/python3.8 -I/path/to/tracktion_engine -I/path/to/juce -I/path/to/pybind11/include tracktion_engine_bindings.cpp -o tracktion_engine.so -fPIC `python3-config --cflags --ldflags`
# Usage example in Python:
"""
import tracktion_engine
# Create an Engine instance
engine = tracktion_engine.Engine("MyApp")
# Initialize DeviceManager
device_manager = engine.get_device_manager()
device_manager.initialise(2, 2) # 2 input, 2 output channels
# Create an empty Edit
value_tree = tracktion_engine.ValueTree("EDIT")
edit = tracktion_engine.Edit(engine, value_tree, tracktion_engine.LoadContext.all)
# Add an AudioTrack
track = edit.get_all_tracks()[0] # Assuming an empty track exists
audio_track = tracktion_engine.AudioTrack(track)
# Insert a wave clip
clip = audio_track.insert_wave_clip("/path/to/audio.wav", 0.0)
# Set clip properties
clip.set_position(1.0) # Move to 1 second
clip.set_loop_range(1.0, 5.0) # Loop from 1 to 5 seconds
# Playback
edit.play()
"""
Cool, did you just write that? Thanks, and yes, something like that, though I suspect that won’t have enough functionality for what I need.
I want to be able to script an entire song in Python, including loading plugins, reading their input and output properties and parameter properties, using buses to switch between tracks, arranging notes, and changing effect parameters over time, and I also want to be able to generate the song as quickly as possible or play it back in real time, and in the latter case, I want to be able to record audio and/or input midi notes and/or change effect parameters with midi using a midi device or k/b or mouse or whatever, and to be able to display plugin UIs and change parameters that way, and save all that input data alongside the song program for later song output (and how and when any of that happens would be controlled programmatically). Of course I’d do as much of the work as I can in Python, just needing the basic underlying bindings to Tracktion functions.
I don’t have confidence in my ability to understand the library well enough to make all the working Python bindings I need without too much effort. I am not an expert C++ programmer, probably not even intermediate.
No, I didn’t write that - Grok did. But, it works .. and could be quite easily extended towards your desired full-featured framework, in my opinion. There are some issues to be addressed on the horizon with regards to efficiency of types and so on, but these issues are worth the effort.
I’m personally a little more inclined to do Lua bindings for JUCE and TracktionEngine, though. Far less load, far better performance, and a much nicer language (ymmv). I have a long history with Lua-based Audio/Video frameworks (was a contributor to the MOAI project, which I used to ship a number of highly profitable game and applications projects..)
I have a ‘skunworks’ project I’m doing on the side, to automatically generate Lua bindings from JUCE .. its sort of working, and could feasibly result in a full Lua environment for, at least, the GUI and Parameter classes of JUCE .. maybe not so much for DSP (which should stay C/C++ in my opinion), but for a lot of the scaffolding stuff that JUCE and TE provide, Lua makes a lot of sense.
Both JUCE and TE are ripe for this sort of thing, so I totally encourage you to pursue your dreams, share the details and let some of us, also interested in this approach, contribute and help you out along the way.
Just don’t forget that you can get AI/ML to do a lot of the grunt work for you.
Thanks. Good idea re the AI. Though I expect it to create bugs now and then that would be hard for me to fix without a deep understanding that I don’t have.
The reasons I chose Python are that it’s my favorite programming language, it’s extremely popular, and it has a lot of libraries in case I want to use any for whatever thing I might want to do that JUCE doesn’t provide. I think Lua is only marginally faster than Python, according to Benchmarks I saw a long time ago (and Python is always getting faster), and do you think speed will really be an issue in this project anyway? I mean I see JUCE/Tracktion as doing all the heavy CPU work and Python just doing the more abstract stuff. Oh, and also, I’ve heard that Lua hasn’t been popular as an embedded language for a long time.
Lua is definitely still being used as an embedded language, it is frickin’ everywhere. Anyone making this claim isn’t a serious Lua dev, imho, although I’m willing to be proven wrong.
And it has many advantages over Python, least of which it is a much smaller and tighter package to bind to things. I won’t get into the technical details, but I’ve done projects in both, and I still just prefer the Lua Way™ of Doing Things.
Speed is a bit of an issue, for sure. Remember, we are dealing with a high performance framework in JUCE, every cycle counts. And Lua/LuaJIT can offer a lot of that.
But that said, the world is big enough for both of these approaches, so just go for it. AI/ML can push things very far - and for issues where you might get into the weeds, there are Human Beings™ that can dig things out.
I’d be happy to participate, anyway, if you get things booted up .. whatever mistakes I learn from in the PythonJUCEBinding front, will be applied to my LuaBindings, though .. ![]()
Thanks, good points. I just checked out some Lua code (it’s been a long long time since I’ve studied Lua), and I just like Python’s syntax so much better. I think I’ll just have to risk doing it in Python.
Would be nice to know what those technical details you didn’t want to get into are, but if it’s too much to explain, ok. =d I’d probably just pick Python regardless, hehe.
Well, technically, Lua is small and light, Python comes with a lot of batteries.
Lua, the VM, can very easily be embedded into any application. It doesn’t require a filesystem. It can have a fileystem. It can also just run tightly packed bytecode.
Python can too, but in my experience so far, in more cumbersome ways. The load is higher.
I can understand at first glance, Lua code is different from Python in interesting ways. However, Lua is the basis for a whole lot more interesting domain specific use cases. You can literally just describe your application in terms of tables, and its function calls and data certainty, all the way down.
Lua is great for embedded. It is great for multi-platform scripting and application development. The language itself is a pretty good data file format. Tabs do not make a good data file format.
I am sure pure Python practitioners can have their way with my position, but I have done projects in both. I think the workload is lower, and yes indeed the beauty is higher, in the Lua approach.
I recently wanted to show the JUCE framework to a hacker friend of mine, also a pro Audio, Lua/C/C++ dev in my neighborhood .. so here’s the result of that session:
I mean, its not much, but thats the point. Once you can get everything into a table and do things with it, life is sweet.(EDIT: That said, I’m also an advocate for using whatever floats your boat… so have fun learning this stuff anyway, and lets hear how things go ..)
IMHO, as long as you keep the scripting language out of anything to do with the audio thread, the language speed doesn’t really matter much. Normal Lua (that is, not even LuaJit), JavaScript and Python should all be fine.
I’ve myself also gone with the Python route. Python is only used to set up/control things, never directly from the audio thread or in a way that would block the audio thread. (This approach has some potential limitations that I may regret later, but at the moment I am happy with plain CPython with Pybind11 bindings.)
Yeah, I’m mostly doing Lua bindings for GUI and Editor things, but also around processBlock and the APVT’s, too, at least, with regard to parameter updates. Scripting MIDI with Lua, within JUCE, is also quite productive, and as long as you follow JUCE’ taped path, performant. I have a Lua editor in my current step sequencer project, and its making it fun to do generative and quantize functions rapidly in simple Lua scripts.. which I guess at some point, I’ll package as bytecode structs, if I release an application with this rig.
Certainly nothing gets done within processBlock(), scripting isn’t for DSP, just the setup and control of it ..
I told grok to make me some bindings. I mentioned all the functionality it has to include. I selected extra research & reasoning or whatever it was. The result was kind of short, so I doubt it includes everything I need. Anyway, I have a couple of errors with the code in Visual Studio. One is in some code that’s similar to your code above that gives me the same error.
With your code,
// Helper function to initialize Tracktion Engine std::unique_ptr<Engine> create_engine(const std::string& app_name) { return std::make_unique<Engine>(app_name, std::make_unique<ExtendedUIBehaviour>(), nullptr); }
I get
E0304: no instance of overloaded function "std::make_unique" matches the argument list / argument types are: (const std::string, error_type, std::nullptr_t)
I could try to figure it out myself by looking at the signature of the engine initializer, but since you said your code works, I wonder if I’m doing something wrong..
You need to convert app_name to a juce::String:
// Helper function to initialize Tracktion Engine
std::unique_ptr<tracktion_engine::Engine> create_engine(const std::string& app_name) {
return std::make_unique<tracktion_engine::Engine>(
juce::String(app_name), // Convert std::string to juce::String
std::make_unique<tracktion_engine::ExtendedUIBehaviour>(),
nullptr
);
}
Thanks, I guess I should have just looked at tracktion_engine::Engine. I have other errors in the code I had Grok generate that I don’t understand, but I didn’t want to bother you too much. But now I don’t think it matters. I thought you might want to know that Claude 3.7 Sonnet seems to make better code. I ran across Roo Code Evals, which rates Claude 3.7 Sonnet the best at generating code, so I found Claude 3.7 Sonnet at claude.ai (I used the free version), and it gave me bindings (more of them than Grok gave me), and also a Python example for using them, a CMakeLists.txt, and another cmake setup file for it. (Not that I need the cmake files since I’m using VS, but I might want to make it for Linux too in the future..) And the Python bindings don’t generate any errors that show up in the IDE, unlike Grok. I haven’t tried actually compiling and using them yet (crosses fingers).
Well, I don’t mind helping as long as you’re doing all the heavy lifting.
Its an interesting topic and you can go a long way with AI/ML guidance, only to stumble on something lame, which you maybe didn’t learn about, since AI/ML did all the work for you. So feel free to ask anyway.
And CMake files are for more than just Linux builds - they can be a very good tool when the time comes to do releases and so on ..
I take back what I said about Claude being better. There are over 77 errors, they just weren’t being underlined in the IDE for whatever reason. ![]()