Unable to get output from Pd library and Android app

246 views Asked by At

I am trying to get a simple Android app working with the Pd library I have integrated within my eclipse environment.

I have successfully build and compiled an example project called "CircleOfFifths" which proves that the library has been integrated fine in eclipse.

I am using cordova/phonegap and trying to create a plugin for libpd. All the plugin communication with the javascript file on the front-end is working fine.

The code seems to initialise and load the patch fine. It also goes into the play() method but I don't get any output. I am expecting to hear the sine wave.

I followed this tutorial on YouTube.

My code can be viewed here, and the patch is here.

Can anyone help me figure out where I can be going wrong?

My ultimate objective is to use a live audio from microphone and process that audio and return immediately with applied Pd patch (patch is ready and created for this) but before I go to that stage I want to make sure that I get some sort of output from the existing patch. Same patch as the one above in the YouTube tutorial.

package com.test.libpd;

import java.io.File;
import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.puredata.android.io.AudioParameters;
import org.puredata.android.io.PdAudio;
import org.puredata.android.utils.PdUiDispatcher;
import org.puredata.core.PdBase;
import org.puredata.core.utils.IoUtils;

import android.content.Context;
import android.util.Log;

import com.sgil.libpddemo.R;

public class Libpd extends CordovaPlugin {
private static final String TAG = "libpd_plugin";
private Exception exception;
private Context AppContext;
private PdUiDispatcher dispatcher;
private static final int MIN_SAMPLE_RATE = 44100;

public Libpd() { // constructor
}

private Context getApplicationContext() {
    return this.cordova.getActivity().getApplicationContext();
}

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    Log.v(TAG, "Init LIBPD PLUGIN");
}

public boolean execute(final String action, final JSONArray args,
        final CallbackContext callbackContext) throws JSONException {

    cordova.getActivity().runOnUiThread(new Runnable() {
        // @SuppressLint("NewApi")
        public void run() {

            Log.v(TAG, "Plugin received:" + action);
            Libpd libpd = new Libpd();
            libpd.AppContext = getApplicationContext();

            try {
                libpd.initPd(libpd.AppContext);
                libpd.loadPatch(libpd.AppContext);
            } catch (IOException e) {
                Log.e(TAG, e.toString());
                // libpd.finish();

            }

            if (action.equals("playRec")) {
                // MediaRecordMethod mediaRecordMethod = new
                // MediaRecordMethod(action);
                // mediaRecordMethod.init();

                libpd.play();
            }

            if (action.equals("stopRec")) {
                libpd.onPause();
            }

            if (action.equals("startRec")) {
                libpd.record("start");
                libpd.play();
            }

        }

    });
    return true;
}

private void initPd(Context ctx) throws IOException {
    AudioParameters.init(ctx);
    // Configure the audio glue
    int sampleRate = AudioParameters.suggestSampleRate();
    int srate = Math.max(MIN_SAMPLE_RATE, AudioParameters.suggestSampleRate());
    // int sampleRate = 64;

    int inpch = AudioParameters.suggestInputChannels();
    int outpch = AudioParameters.suggestOutputChannels();

    PdAudio.initAudio(srate, 0, outpch, 8, true);

    // Create and install the dispatcher
    dispatcher = new PdUiDispatcher();
    PdBase.setReceiver(dispatcher);
}

private void loadPatch(Context ctx) throws IOException {

    File dir = ctx.getFilesDir();
    Log.v(TAG, "path:" + dir.getAbsolutePath().toString());

    //File dir = new File("/sdcard/mypatches");

    IoUtils.extractZipResource(
            ctx.getResources().openRawResource(R.raw.simplepatch), dir, true);
    File patchFile = new File(dir, "simplepatch.pd");
    PdBase.openPatch(patchFile.getAbsolutePath());
    //PdAudio.startAudio(ctx);
    //this.record("start");
}

private void record(String startorStop) {
    if (startorStop.equals("start")) {
        PdBase.sendSymbol("recordfilename", "x.aiff");
        PdBase.sendBang("openfile");
        PdBase.sendBang("record");
    } else {
        PdBase.sendBang("stoprecording");
    }
}

// play back the last recording from the file called 'recorded'
public void play() {
    //PdBase.sendSymbol("playfilename", "x.aiff");
    //PdBase.sendBang("readfile");
    //PdBase.sendBang("play");

    Float val = 1.0f;
    PdBase.sendFloat("onOff", val);
}

protected void onPause() {
    PdAudio.stopAudio();
    PdAudio.release();
    PdBase.release();
}

}
1

There are 1 answers

0
umläute On

your code never starts the audio-processing.

For whatever reasons youhave uncommented the line

PdAudio.startAudio(ctx)