ofxPd icon indicating copy to clipboard operation
ofxPd copied to clipboard

Building ofxPd using Emscripten

Open cuinjune opened this issue 6 years ago • 17 comments

I tried building ofxPd/pdExample project using Emscripten following this instruction: https://openframeworks.cc/setup/emscripten/

After running emmake make, it fails with the following error when it tries to build c files in libpd folder.

error: invalid argument '-std=c++14' not allowed with 'C'
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting

What should I do to fix this error? I would appreciate any advice.

P.S: I used openFrameworks-master branch since other versions have problem building Emscripten.

cuinjune avatar Jul 23 '19 08:07 cuinjune

What should I do to fix this error? I would appreciate any advice.

The first step is to read the error and interpret what it might mean.

openFrameworks is mostly C++ and while is libpd mostly C files. It looks like whoever made the script/makefile which builds the source files for the openFrameworks emscripten target did not test building C files as well as C++ files. The compiler seems to be saying "I can build C files, but not with the C++ stdlib." The quoted flag in the error message needs to be taken out where the compiler is being called to build the C file. I have no idea where and, as this is a general issue, I would ask the openFramneworks enscripten people.

Also, I have no idea if using libpd in emscripten will work, ie. audio in/out.

danomatika avatar Jul 23 '19 08:07 danomatika

Thank you so much for your answer. I will report here if I succeed.

P.S: It seems like there's at least one person who could compile libpd using Emscripten: https://www.reddit.com/r/puredata/comments/8g8ahc/libpd_compiled_to_llvmwasm_has_this_been_done_or/

cuinjune avatar Jul 23 '19 12:07 cuinjune

I could successfully build ofxPd/pdExample project using Emscripten.

Here are the steps I took:

  1. Open ofxPd/addon_config.mk file and remove -DLIBPD_EXTRA flag from common ADDON_CFLAGS.

  2. Remove ofxPd/libs/libpd/pure-data/extra folder.

  3. Open OF/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk file and change PLATFORM_CFLAGS to PLATFORM_CXXFLAGS.

  4. Open ofxPd/libs/libpd/libpd_wrapper/util/ringbuffer.c and change the line 17 to the following:

#if __STDC_VERSION__ >= 201112L && !defined(__EMSCRIPTEN__) // use stdatomic if C11 is available and emscripten is not used
  1. Open ofxPd/libs/libpd/pure-data/src/d_osc.c and change the line 19~20 to the following:
#if defined(__linux__) || defined(__CYGWIN__) || defined(__GNU__) || \
    defined(ANDROID) || defined(__EMSCRIPTEN__)
  1. Open ofxPd/libs/libpd/pure-data/src/d_array.c and change the line 507~508 to the following:
#if defined(__linux__) || defined(__CYGWIN__) || defined(__GNU__) || \
    defined(ANDROID) || defined(__EMSCRIPTEN__)
  1. Open ofxPd/libs/libpd/pure-data/src/x_misc.c and change the line 27 to the following:
#if defined (__linux__) || defined (__CYGWIN__) || defined (ANDROID) || defined(__EMSCRIPTEN__)

After these steps, I could successfully build ofxPd/pdExample project using Emscripten.

However, when I run the project on the browser, it didn't work properly. After some testings, I found out that ofSoundStream doesn't work properly on Emscripten. So I left a question on OF forum and posted an issue. I will post here again when I find a solution to fix the issue.

P.S: I accidentally found this https://mathr.co.uk/empd/#empd

cuinjune avatar Jul 24 '19 08:07 cuinjune

Okay, I could almost successfully build and run ofxPd/pdExample using Emscripten.

Here are the full steps:

  1. Download openFrameworks nightly from https://openframeworks.cc/download/

  2. Download ofxPd and put it under OF/addons directory

  3. Download latest puredata src from https://github.com/pure-data/pure-data, replace the files in ofxPd/libs/libpd/pure-data/src and ofxPd/libs/libpd/pure-data/extra/pd~ folder.

  4. Download puredata src for emscripten from https://github.com/claudeha/pure-data/tree/emscripten, copy and replace d_array.c, d_osc.c, d_ugen.c, m_class.c, m_imp.h, m_pd.c, s_inter.c, x_connective.c, x_list.c, x_misc.c and x_text.c in ofxPd/libs/libpd/pure-data/src folder.

  5. Download https://github.com/claudeha/libpd/tree/emscripten and replace ofxPd/libs/libpd/libpd_wrapper and ofxPd/libs/libpd/cpp folder.

  6. Open ofxPd/libs/libpd/libpd_wrapper/util/ringbuffer.c and change the line 17 to the following:

#if __STDC_VERSION__ >= 201112L && !defined(__EMSCRIPTEN__) // use stdatomic if C11 is available and emscripten is not used

  1. Open OF/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk file and change PLATFORM_CFLAGS to PLATFORM_CXXFLAGS and add -s ERROR_ON_UNDEFINED_SYMBOLS=0 at the end of PLATFORM_OPTIMIZATION_LDFLAGS_RELEASE.

  2. Update ofxPd/pdExample using projectGenerator.

  3. Open ofxPd/pdExample/src/ofApp.cpp and change the line 25 to the following: #if defined(TARGET_LINUX_ARM) || defined(__EMSCRIPTEN__)

  4. Open the Terminal, cd to emscripten folder and run ./emsdk activate latest and then run source ./emsdk_env.sh.

  5. cd to ofxPd/pdExample folder and run emmake make.

  6. Once the compilation is finished, run emrun bin/pdExample.html.

And here's the screenshot:

Screen Shot 2019-08-31 at 12 18 58 PM

Although there are some errors and scope array cannot be found, I could hear the vibrato cosine wave sound and also sound from the incoming microphone which means the Pd patch opens fine at least.

cuinjune avatar Aug 31 '19 16:08 cuinjune

Hey @danomatika, i tried to run the examples with Emscripten following the steps from @cuinjune, but without success. While it is possible to build with Emscripten (if I change the example and the patch here and there), if I use the ofxPd version that is included in OfxOfelia. It would be really great, if the official ofxPd version would be compatible with Emscripten too. Here is a discussion (there is one issue left with pd.closePatch with the ofxPd version from @cuinjune): https://github.com/openframeworks/openFrameworks/issues/6781 I can tell to you how to change the examples for Emscripten (at least pdExample and pitchShifter), if it makes sense.

Jonathhhan avatar Nov 23 '21 15:11 Jonathhhan

Honestly, I am never likely to use Emscripten, so it is up to y’all to make a PR where I can simply review the required changes. If you look on the pure-data Github repo itself, there is an issue about required changes for Emacripted support for the Pd sources, if you’ve need seen it already.

I prefer not to provide local hacks and would much rather accept changes made upstream, ie. libpd/pure-data, so I suggest starting there and working back toward here.

enohp ym morf tnes

Dan Wilcox danomatika.com robotcowboy.com

On Nov 23, 2021, at 4:02 PM, Jonathan @.***> wrote:

 Hey @danomatika, i tried to run the examples with Emscripten following the steps from @cuinjune, but without success. While it is possible to build with Emscripten (if I change the example and the patch here and there), if I use the ofxPd version that is included in OfxOfelia. It would be really great if the official ofxPd version would be compatible with Emscripten too. Here is a discussion (there is one issue left with pd.closePatch with the ofxPd version from @cuinjune). Here is a discussion: openframeworks/openFrameworks#6781

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

danomatika avatar Nov 23 '21 16:11 danomatika