Advertisement
free1000

libpd record sample and play

Jan 14th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. public class SampleGuppy extends Activity implements OnClickListener {
  2.  
  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         setContentView(R.layout.activity_sampleguppy);
  7.          
  8.         initSound();
  9.         initialiseModel();
  10.        
  11.         //Set up our buttons
  12.         recordButton = (Button) findViewById(R.id.record_button);
  13.         recordButton.setOnClickListener(this);
  14.         playButton   = (Button) findViewById(R.id.playback_button);
  15.         playButton.setOnClickListener(this);
  16.        
  17.     }
  18.  
  19.     @Override
  20.     public boolean onCreateOptionsMenu(Menu menu) {
  21.         // Inflate the menu; this adds items to the action bar if it is present.
  22.         getMenuInflater().inflate(R.menu.activity_sampleguppy, menu);
  23.         return true;
  24.     }
  25.    
  26.    
  27.       /**
  28.      * Custom controls
  29.      */
  30.     private Button recordButton;
  31.     private Button playButton;
  32.    
  33.    
  34.     /**
  35.      * Custom behavior
  36.      */
  37.    
  38.     private void initialiseModel() {
  39.        // CaptureSingleton.INSTANCE.production = new Production();
  40.        
  41.         File f = getFilesDir();
  42.         String path = f.getAbsolutePath();
  43.         File newFile = new File(path + "/sampleguppy.settings");
  44.         try {
  45.             newFile.createNewFile();
  46.         }
  47.         catch(IOException e){
  48.             Log.e("Create File", e.getLocalizedMessage());
  49.         }
  50.     }
  51.      
  52.     /**
  53.      * UI Actions
  54.      */
  55.    
  56.     boolean isRecording = false;
  57.    
  58.     //Make a recording and save it to a file
  59.     private void record() {
  60.         if(!isRecording){
  61.             PdBase.sendSymbol("recordfilename", "x.aiff");
  62.             PdBase.sendBang("openfile");
  63.             PdBase.sendBang("record");
  64.         }
  65.         else {
  66.             PdBase.sendBang("stoprecording");
  67.         }
  68.         isRecording = !isRecording;
  69.     }
  70.    
  71.     //play back the last recording from the file called 'recorded'
  72.     public void play() {
  73.         PdBase.sendSymbol("playfilename", "x.aiff");
  74.         PdBase.sendBang("readfile");
  75.         PdBase.sendBang("play");
  76.     }
  77.    
  78.    
  79.     @Override
  80.     public void onClick(View v) {
  81.         switch(v.getId()){
  82.         case R.id.record_button:
  83.             record();
  84.             break;
  85.         case R.id.playback_button:
  86.             play();
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * Lib PD
  92.      */
  93.      private PdUiDispatcher dispatcher;
  94.    
  95.      private void initPd() throws IOException {
  96.          //configure audio glue
  97.          int sampleRate = AudioParameters.suggestSampleRate();
  98.          PdAudio.initAudio(sampleRate, 0, 2, 8, true);
  99.          
  100.          //setup the dispatcher
  101.          dispatcher = new PdUiDispatcher();
  102.          PdBase.setReceiver(dispatcher);
  103.      }
  104.      
  105.      private void loadPatch() throws IOException {
  106.          File dir = getFilesDir();
  107.          File patchFile = new File(dir, "recorder5.pd");
  108.          IoUtils.extractZipResource(getResources().openRawResource(R.raw.recorder), dir, true);
  109.          PdBase.openPatch(patchFile.getAbsolutePath());
  110.      }
  111.  
  112.      private void initSound(){
  113.          try {
  114.              initPd();
  115.              loadPatch();
  116.          }
  117.          catch(IOException e){
  118.               Log.e("Guppyizer", e.toString());
  119.              finish();
  120.          }
  121.      }
  122.      
  123.      
  124.      /* Lifecycle methods PD needs */
  125.     @Override
  126.     protected void onStart() {
  127.         super.onStart();
  128.         PdAudio.startAudio(this);
  129.     }
  130.    
  131.     @Override
  132.     protected void onStop() {
  133.         PdAudio.stopAudio();
  134.         super.onStop();
  135.     }
  136.    
  137.  
  138.      @Override
  139.      protected void onResume() {
  140.          super.onResume();
  141.          PdAudio.startAudio(this);
  142.      }
  143.    
  144.    
  145.      @Override
  146.      protected void onPause(){
  147.          super.onPause();
  148.          PdAudio.stopAudio();
  149.      }
  150.      
  151.      @Override
  152.      protected void onDestroy() {
  153.          super.onDestroy();
  154.          PdAudio.release();
  155.          PdBase.release();
  156.      }
  157.        
  158.      
  159.      
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement