Embedding Python in a multiplatform C++ framework (JUCE)

228 views
Skip to first unread message

pi

unread,
Aug 30, 2014, 4:40:52 PM8/30/14
to python-o...@googlegroups.com
Hello python-on-a-chip community!

This enquiry may be unusual.

I am developing musical training software (hobby, not job) which involves real-time audiovisual coding (such as pitch detection).

I'm using JUCE, a multiplatform C++ framework.

JUCE has support for Lua an JavaScript scripting, but I would like to add support for Python scripting.

Gameplay requires complex decision-making, control flow, data structuring. I've already written it in Python. I really don't want to rewrite it in C++. One benefit of Python this is allows me to rapid-prototype new concepts.

So I'm looking to float a Python runtime from within this C++ project, with bidirectional signalling so that my C++ code can load scripts into this runtime and call Python functions in those scripts, and these Python functions are also able to call back out into C++-land, where I can take action.

This is turning out to be much more difficult than I anticipated.

The standard (officially documented here: https://docs.python.org/2/extending/) way of embedding Python is no use to me, as it requires linking libpython.a, then #include-ing "python.h" then using functions such as Py_Initialize() (which would crank up the runtime) etc.

The problem here is that libpython.a has to be compiled per platform. Even assuming I can get hold of iOS/Android/Win/Mac/Linux variants of this, I would then have to battle JUCE's project-structuring tool to get the correct one linked.

It would be really nice if I could avoid a library entirely, and have these Py_Initialize() etc. functions written in C/C++

i.e. A completely platform independent pure C/C++ implementation of a Python runtime.

I've looked at cobbling CPython and I don't fancy it. For a start the core codebase seems to be >2Meg and the modules are ~15Meg. Also the build-system bundles a unique set of #include-d files for each platform. It would be really hard work to abstract this.

Now I don't need a full implementation, I just need something that handles the basic language syntax -- I won't need to be calling any C-API stuff -- even if I did want some C-API command I could just code it up on the C++ side, and have my Python script invoke it.

Also, a full implementation would bloat the JUCE source code horribly. I'm hoping I can find a lightweight solution that might one day make it into the JUCE trunk.

So I have started looking around for more compact implementations.

I found TinyPy, which I'm also investigating. But python-on-a-chip appears to be under active development.

So (and I'm sorry it's taken so long to get to the question), my question is:

Would there be any way of adapting the python-on-a-chip source code to do what I want? And if so, would I have to write the bidirectional Python <--> C++ signalling myself?

If this is indeed possible, I'm very grateful for any pointers towards how I might go about putting it together.

pi

Reply all
Reply to author
Forward
0 new messages