Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = 0.0f;
- PdBase.sendFloat("onOFF", val);
- }
- protected void onPause() {
- PdAudio.stopAudio();
- PdAudio.release();
- PdBase.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement