2

如何从 Pd 接收数据到我的 Android 应用程序?我设法向 Pd 发送了一个浮点数,并在 Pd 上模拟了一个接收对象,只是为了查看我期望的结果。这是我到目前为止所拥有的。

public class MainActivity extends AppCompatActivity {

private PdUiDispatcher dispatcher;
Button newActivity;
Switch highLow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initGUI();

    try{
        initPD();
        loadPD_Patch();
        loadPD_RecordPatch();
    }catch (IOException error){
        Log.e("Pd initialization error", String.valueOf(error));
    }
}

private void initGUI(){
    Switch onOffSwitch = (Switch) findViewById(R.id.onOffSwitch);
    highLow = (Switch)findViewById(R.id.highLow);
    newActivity = (Button) findViewById(R.id.newActivity);
    onOffSwitch.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                float val = (isChecked) ? 1.0f : 0.0F;
                PdBase.sendFloat("onOff", val);
            }
        }
    );
}

private void initPD() throws IOException{
    int sampleRate = AudioParameters.suggestSampleRate();
    PdAudio.initAudio(sampleRate,0,2,8,true);

    dispatcher = new PdUiDispatcher();
    PdBase.setReceiver(dispatcher);
    dispatcher.addListener("highLow", myListener);
}

private void loadPD_Patch() throws IOException{
    File dir = getFilesDir();
    IoUtils.extractZipResource(getResources().openRawResource(R.raw.simple_android_patch), dir, true);
    File pdPatch =  new File(dir,"simple_android_patch.pd");
    PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private void loadPD_RecordPatch() throws IOException{
    File dir = getFilesDir();
    IoUtils.extractZipResource(getResources().openRawResource(R.raw.audio_in_android), dir, true);
    File pdPatch =  new File(dir,"audio_in_android.pd");
    PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private final PdListener myListener = new PdListener() {
    @Override
    public void receiveMessage(String source, String symbol, Object... args) {
        Log.i("receiveMessage symbol:", symbol);
        for (Object arg: args) {
            Log.i("receiveMessage atom:", arg.toString());
        }
    }
    /* Receive a list from Pd */

    @Override
    public void receiveList(String source, Object... args) {
        for (Object arg: args) {
            Log.i("receiveList atom:", arg.toString());
        }
    }

    /* Receive  symbol from Pd */
    @Override public void receiveSymbol(String source, String symbol) {
        Log.i("receiveSymbol", symbol);
    }

    /* Receive float from Pd */
    @Override public void receiveFloat(String source, float x) {
        Log.e("highLow", String.valueOf(x));
        if(x == 1.0){
            highLow.setChecked(true);
        }else {
            highLow.setChecked(false);
        }
    }

    /* Recieve  bang from Pd */
    @Override public void receiveBang(String source) {
        Log.i("receiveBang", "bang!");
    }

};


@Override
protected void onResume(){
    super.onResume();
    PdAudio.startAudio(this);
}

@Override
protected void onPause(){
    super.onPause();
    PdAudio.stopAudio();
}
}

这是我的 Pd 补丁,

Pd贴片

即使我鼓掌或发出噪音,终端也会继续显示它从 Pd 补丁的“highLow”发送对象接收 0.0 作为浮点数。会不会是补丁无法访问麦克风?我的 Android 清单使用权限录制音频。

4

2 回答 2

2
//pdAudio.initAudio(sampleRate,inputchannels,outputchannels,ticksperbuffer,restart?)
PdAudio.initAudio(sampleRate,1,2,8,true);
/* setting Input channels to 2 on a device that has only 1 mic throws Exceptions
 * Code should be improved take advantage of all input channels on different devices
 */
dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
//dispatcher.addListener("senderObjectFromPD",Listener); in my case "highLow"
dispatcher.addListener("highLow", myListener);
于 2016-01-16T16:59:18.387 回答
0

通过查看pd-for-android的 PdTest 代码发现了这一点:

基本上,我们必须在设置接收器之后订阅我们感兴趣的内容。这对我有用;这是一个例子:

PdBase.setReceiver(pdReceiver);
PdBase.subscribe("sa-freq");
PdBase.subscribe("cent");

笔记

这行得通。我发现这些消息出现一次。但是,除非您从代码中触发,否则它们不会连续出现。我不知道为什么。这可能是另一个我也想知道答案的问题!:--) (Pd 终端不是这种情况,它会在更新时连续显示值。)

于 2016-01-16T14:18:37.467 回答