2

How could I play a wave file in Android using libpd? I can do this with soundpool.play, but I'd like to try libpd. I followed this tutorial to implement libpd, but it's not working. What can be wrong? The code or the PD patch?

This is my activity code:

public class MainActivity extends ActionBarActivity implements OnTouchListener {

    private PdUiDispatcher dispatcher;

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

        dispatcher = new PdUiDispatcher();
        PdBase.setReceiver(dispatcher);
    }

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

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

        Button bangButton = (Button) findViewById(R.id.bangButton);
        bangButton.setOnTouchListener(this);

        try {
            initPD();
            loadPDPatch();
            PdAudio.startAudio(this);

        } catch (IOException e) {
            finish();
        }
    }

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

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

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
            if(v.getId() == R.id.bangButton) {
                PdBase.sendBang("mybang");
            }

        return false;
    }
}

This is my pd patch:

PureData Patch

#N canvas 0 22 902 577 24;
#X obj 46 24 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1 1
;
#X obj 47 248 dac~;
#X obj 48 193 readsf~;
#X obj 49 118 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
-1;
#X msg 42 61 \; pd dsp \$1;
#X msg 50 155 open myfile.wav \, 1;
#X connect 0 0 4 0;
#X connect 2 0 1 0;
#X connect 3 0 5 0;
#X connect 5 0 2 0;
msampaio
  • 3,394
  • 6
  • 33
  • 53

3 Answers3

1

The error is in the Pd-patch:

You are sending a bang to the mybang symbol within the Pd-patch.

However, there doesn't seem to be a receiver attached to that name in the patch, so the even never triggers anything within Pd.

  • You have another [send mybang] that is triggered by the bng object (but that will also just send to the void).

  • The [bng] object might have a receive-label set, but

    • according to the screenshot it has no receive-label set at all (the inlet would vanish if so; but it's a bit hard to read).

    • If you did have a receive-label mybang, then clicking on [bng] would trigger an infinite recursion ([bng] => [s mybang] -> [bng] ...)

general errors

output of [bng]: you should never have a fan-out of messages (where you connect a single outlet to multiple message inlets) as this results in undefined order of execution; use [trigger] in these cases.

last outlet of [readsf~]: [readsf~] is a mono player by default; the last outlet of this object gives you a bang whenever the soundfile has finished playing; sending a bang to the 2nd inlet of [dac~] (expecting a signal) is an error. If you want a stereo soundfile player, use [readsf~ 2]

solution

So the patch should instead look like:

[bng]
|
[s mybang]

[r mybang]
|
[open myfile.wav, 1(
|
[readsf~ 2]
|     |
[dac~ ]
umläute
  • 28,885
  • 9
  • 68
  • 122
0

Did the patch alone work, before integrating to android? If so, try enabling the DSP ON via android with another button and send it to patch or remove DSP ON toggle box permanently and make DSP ON.

iBharath
  • 31
  • 9
0

In the book "Making Musical Apps" by Peter Brinkmann

he suggests:

"If your patch uses additional resources, such as wav files or abstractions, then it is good practice to package those resources with your patch and to refer to them by relative paths only."

In other words: Compress all your folder content in one zip file, so when this is uncompressed in Android, all the resources will be on the same folder, and the pd patch will be able to find your .wav or other files.

luizMello
  • 2,793
  • 20
  • 17