无法从Pd库和Android应用程序

时间:2015-06-09 15:38:22

标签: android cordova puredata libpd

我正在尝试使用我在eclipse环境中集成的Pd库来使用一个简单的Android应用程序。

我已成功构建并编译了一个名为" CircleOfFifths"的示例项目。这证明图书馆已经在日食中得到了很好的整合。

我正在使用cordova / phonegap并尝试为libpd创建一个插件。所有与前端javascript文件的插件通信都正常工作。

代码似乎初始化并加载补丁很好。它也进入play()方法,但我没有得到任何输出。我期待听到正弦波。

我遵循了tutorial on YouTube

我的代码可以here查看,补丁是here

任何人都可以帮我弄清楚哪里出错了吗?

我的最终目标是使用来自麦克风的现场音频并处理音频并立即使用已应用的Pd补丁返回(补丁准备好并为此创建)但在我进入该阶段之前我想确保我得到一些现有补丁的输出排序。与YouTube教程中的上述补丁相同。

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 个答案:

答案 0 :(得分:0)

您的代码永远不会启动音频处理。

无论出于何种原因你都没有注释掉这行

PdAudio.startAudio(ctx)