2

How do I receive data from Pd to my Android app? I have managed to send a float to Pd, and simulate a receive object on Pd just to see the results I expect. Here is what I have so far.

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();
}
}

And here is my Pd patch,

Pd-Patch

The terminal keeps on showing it receiving 0.0 as a float from the 'highLow' send object from the Pd patch even if I clap or make noise. Could it be, that the patch doesn't have access to the microphone? My Android manifest uses permission Record audio.

Max N
  • 1,134
  • 11
  • 23
Nigel Tiany
  • 71
  • 12

2 Answers2

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);
Nigel Tiany
  • 71
  • 12
0

Found this by looking at the PdTest code at pd-for-android:

Basically, we have to subscribe, for what we are interested in, after setting the receiver. This worked for me; here is an example:

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

NOTE

This works. I find these messages come up once. However, they don't come up continuously unless you trigger from your code. I don't know why. That's probably another question for which I would also like to know the answer! :--) (This is not the case with Pd terminal, which shows the values continuously as they are getting updated.)

rpattabi
  • 9,984
  • 5
  • 45
  • 53
  • It sends multiple messages for me. And i just noticed my issue was something else, Something very obvious i should slap myself :). When initializing pdaudio i set output channels to 4 and input channels 0, so pd wasn't using any input channel. Check out my answer @rpattabi – Nigel Tiany Jan 16 '16 at 17:00
  • With your sample I see the results are continuous. Must be issue with my patch. – rpattabi Jan 17 '16 at 01:29