Flutter — let’s make some noise II

part II — very basic pd-for-android

lipd (in short term here, the real name is pd-for-android) is an open source library for generative audio in android. The main project is puredata by Miller Puckette and strong communities at various level.

Pure data

It’s for electronic musicians, performers and in general for everyone interested in sensors, noise/sound production and more.
There are many multimedia programming languages out there, from open source as the case to ready packaged paid products.
Puredata is my choice for many reasons, e.g. it’s really cross-platform and there are searchable infos for every detail you may need to know.

Puredata offers a UI system (they call GUI objects), so why the need to seek a companion like flutter? The first having in mind to me is your app is not entirely pd focused, you may need a more flexible UI and/or deal with advanced net protocol furthermore derive your graphics by other systems (flutter has good support for svg and direct drawing canvas), that can be a good opportunity for team groups glueing pd stuff with creators and devs.
You can combine the power of flutter and libpd!

Before we start

It’s got to be you have a ready to run toolchain, flutter (I suggest dev channel), git, android studio at least

There are very good videos by Raphael Hernandez on youtube, they cover the main things you need to start, even quite old I suggest you to take a look.
By now you can ignore midi references in those videos since libpd tied it in pdcore, anyway we’ll don’t use here.

  1. clone repo: https://github.com/libpd/pd-for-android
  2. we need .aar, so locate the topic in readme.md, clone submodules and compile the snapshot using gradlew, it may take dozens of minutes to accomplish, then look into pdCore/build/outputs/aar folder, you should find pd-core-n.n.n-SHAPSHOT.aar
  3. create a fresh new flutter application project in android studio, for convenience let name flutter_pd_wrap_demo and bundleID it.pixeldump.pocs.flutterpdwrapdemo, no kotlin or swift to check
  4. attach your device (better for performance) or your vm, run as is to check all is ok, then close debug
  5. get into android/app folder, create libs folder and paste pd…aar file

open build.gradle in app folder, add these lines to dependencies at bottom:

dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
...
implementation fileTree(include: ['*.aar'], dir: 'libs')
}

to let compiler find the lib and appcompat useful for R resource id search, version number depend on your actual build version, so it may need adjustement

7. we also need to adjust the minSdkVersion to 17 for lib compliance:

defaultConfig {
...
minSdkVersion 17
...

8. patch

you know, puredata is kinda programming language, it means that you code something, the code is saved (mainly) in text files with .pd as extension called patches, a patch is a program itself, with interaction in/out etc., you really don’t code by hand, just use the visual gem-based editor, so my suggestion is to download puredata, install it and start to enjoy generative music.
As shorthand we’ll use the same example as in Hdez video, a simple beep on/off to A4 sine wave note:

save as soundtest.pd

#N canvas 564 140 677 526 10;
#X obj 107 78 osc~ 440;
#X obj 107 105 *~ 0.25;
#X obj 106 155 dac~;
#X obj 194 112 r onOff;
#X obj 107 126 *~;
#X obj 218 46 s onOff;
#X obj 218 15 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0
1;
#X connect 0 0 1 0;
#X connect 1 0 4 0;
#X connect 3 0 4 1;
#X connect 4 0 2 1;
#X connect 4 0 2 0;
#X connect 6 0 5 0;

now it’s a matter to load this patch, run into app and toggle sound.

9. include asset, zip it (yes, zip) preferably with same name: soundtest.zip,
in android studio open MainActivity located in android/app/src/main … and click on top right“open for editing in android studio” as new window, this will switch to native droid java perspective, wait gradle sync, when finished create raw folder of type raw as shown:

copy/paste your zip patch, if you do with IDE, confirm the default.

10. coding in java
if you followed the previous story: Flutter — let’s make some noise you should find details on what we’re doing, now it’s a time to load the patch an let toggle sound with flutter communication, edit MainActivity.java

remember “zip” patch? Here is the meaning: pd has api to extract and load, there are other ways, but this is straightforward:

...
private void loadPdPatch() throws IOException {
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.soundtest), dir, true);
File patchFile = new File(dir, "soundtest.pd");
PdBase.openPatch(patchFile.getAbsolutePath());
}
...

we can already test just uncommenting this:

PdBase.sendFloat("onOff", 1.0f);

after launch you’ll hear a continuous sound, weird we’ve sent a float to a toggle, that’s just the way puredata hear about toggle value. After test, delete or comment that line

11. dart code

edit main.dart, replace the whole content with just few lines to complete:

a simple switch and a couple of platformChannel invokes to the final result, that will be a thing like that

thanx for reading, pls don’t forget to clap, thanx thanx.

Next: advancing patches and bidirectional support