Advertisement
Guest User

Untitled

a guest
Jun 9th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. public class Libpd extends CordovaPlugin {
  2.     private static final String TAG = "libpd_plugin";
  3.     private Exception exception;
  4.     private Context AppContext;
  5.     private PdUiDispatcher dispatcher;
  6.     private static final int MIN_SAMPLE_RATE = 44100;
  7.  
  8.     public Libpd() { // constructor
  9.     }
  10.  
  11.     private Context getApplicationContext() {
  12.         return this.cordova.getActivity().getApplicationContext();
  13.     }
  14.  
  15.     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  16.         super.initialize(cordova, webView);
  17.         Log.v(TAG, "Init LIBPD PLUGIN");
  18.     }
  19.  
  20.     public boolean execute(final String action, final JSONArray args,
  21.             final CallbackContext callbackContext) throws JSONException {
  22.  
  23.         cordova.getActivity().runOnUiThread(new Runnable() {
  24.             // @SuppressLint("NewApi")
  25.             public void run() {
  26.  
  27.                 Log.v(TAG, "Plugin received:" + action);
  28.                 Libpd libpd = new Libpd();
  29.                 libpd.AppContext = getApplicationContext();
  30.  
  31.                 try {
  32.                     libpd.initPd(libpd.AppContext);
  33.                     libpd.loadPatch(libpd.AppContext);
  34.                 } catch (IOException e) {
  35.                     Log.e(TAG, e.toString());
  36.                     // libpd.finish();
  37.  
  38.                 }
  39.                
  40.                 if (action.equals("playRec")) {
  41.                     // MediaRecordMethod mediaRecordMethod = new
  42.                     // MediaRecordMethod(action);
  43.                     // mediaRecordMethod.init();
  44.  
  45.                     libpd.play();
  46.                 }
  47.  
  48.                 if (action.equals("stopRec")) {
  49.                     libpd.onPause();
  50.                 }
  51.  
  52.                 if (action.equals("startRec")) {
  53.                     libpd.record("start");
  54.                     libpd.play();
  55.                 }
  56.  
  57.             }
  58.  
  59.         });
  60.         return true;
  61.     }
  62.  
  63.     private void initPd(Context ctx) throws IOException {
  64.         AudioParameters.init(ctx);
  65.         // Configure the audio glue
  66.         int sampleRate = AudioParameters.suggestSampleRate();
  67.         int srate = Math.max(MIN_SAMPLE_RATE, AudioParameters.suggestSampleRate());
  68.         // int sampleRate = 64;
  69.  
  70.         int inpch = AudioParameters.suggestInputChannels();
  71.         int outpch = AudioParameters.suggestOutputChannels();
  72.  
  73.         PdAudio.initAudio(srate, 0, outpch, 8, true);
  74.        
  75.         // Create and install the dispatcher
  76.         dispatcher = new PdUiDispatcher();
  77.         PdBase.setReceiver(dispatcher);
  78.     }
  79.  
  80.     private void loadPatch(Context ctx) throws IOException {
  81.  
  82.         File dir = ctx.getFilesDir();
  83.         Log.v(TAG, "path:" + dir.getAbsolutePath().toString());
  84.  
  85.         //File dir = new File("/sdcard/mypatches");
  86.  
  87.         IoUtils.extractZipResource(
  88.                 ctx.getResources().openRawResource(R.raw.simplepatch), dir, true);
  89.         File patchFile = new File(dir, "simplepatch.pd");
  90.         PdBase.openPatch(patchFile.getAbsolutePath());
  91.         //PdAudio.startAudio(ctx);
  92.         //this.record("start");
  93.     }
  94.  
  95.     private void record(String startorStop) {
  96.         if (startorStop.equals("start")) {
  97.             PdBase.sendSymbol("recordfilename", "x.aiff");
  98.             PdBase.sendBang("openfile");
  99.             PdBase.sendBang("record");
  100.         } else {
  101.             PdBase.sendBang("stoprecording");
  102.         }
  103.     }
  104.  
  105.     // play back the last recording from the file called 'recorded'
  106.     public void play() {
  107.         //PdBase.sendSymbol("playfilename", "x.aiff");
  108.         //PdBase.sendBang("readfile");
  109.         //PdBase.sendBang("play");
  110.        
  111.         Float val = 0.0f;
  112.         PdBase.sendFloat("onOFF", val);
  113.     }
  114.  
  115.     protected void onPause() {
  116.         PdAudio.stopAudio();
  117.         PdAudio.release();
  118.         PdBase.release();
  119.     }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement