简体   繁体   中英

How to play wav file in libPd?

I have a simple libPd project. Now, I have downloaded a .wav file and I want to play it instead the pure-data file that I have.

Is it possible to do that?

If it does, how can I do that?

This is my project:

public class MainActivity extends AppCompatActivity {

private Button kickButton, bassButton;
private PdUiDispatcher dispatcher;
private boolean isClicked;

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

    kickButton = (Button) findViewById(R.id.kickButton);
    bassButton = (Button) findViewById(R.id.bassButton);

    int sampleRat = AudioParameters.suggestSampleRate();
    try {
        PdAudio.initAudio(sampleRat,0,2,8,true);
        dispatcher = new PdUiDispatcher();
        PdBase.setReceiver(dispatcher);

        File dir = getFilesDir();
        IoUtils.extractZipResource(getResources().openRawResource(R.raw.simplepatch),dir,true);
        File pdPatch = new File(dir, "simplepatch.pd");
        PdBase.openPatch(pdPatch.getAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }



    kickButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            isClicked = !isClicked;

            float val = (isClicked) ? 1.0f : 0.0f;
            PdBase.sendFloat("onOff", val);
        }
    });

}

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

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

To load and play a wav file, you need to zip your pd patch and the wav file into one zip file. In your java code, your zip file is called simplepatch.zip and then you open the patch simplepatch.pd from inside the zip file.

File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.simplepatch),dir,true);
File pdPatch = new File(dir, "simplepatch.pd");
PdBase.openPatch(pdPatch.getAbsolutePath());

All you have to do is put the wav file into the simplepach.zip file and then using a button in your android app, play the file. A sample pd patch for it would look like this:

click to view image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM