Android Open Source - musetoolkit Pnoise






From Project

Back to project page musetoolkit.

License

The source code is released under:

GNU General Public License

If you think the Android project musetoolkit listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.musetoolkit;
//??????File --> New --> Class?????
//????????PinkNoise????????
//w  w  w.java 2  s .  c om
import java.io.File;        //File???????
import java.io.IOException;      //IOException?????????
import java.io.InputStream;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.view.View.OnClickListener;    //OnClickListener????
import android.widget.Button;      //Button?????
import android.widget.TextView;
import android.widget.Toast;

import org.puredata.core.PdBase;
import org.puredata.core.PdListener;
import org.puredata.core.utils.IoUtils;
import org.puredata.android.io.AudioParameters;
import org.puredata.android.io.PdAudio;
import org.puredata.android.service.PdPreferences;
import org.puredata.android.service.PdService;
import org.puredata.android.utils.PdUiDispatcher;  //PdUiDispatcher?????

import com.github.musetoolkit.Sine.ButtionBackOnClick;

import android.view.View;
import android.view.Window;
import android.util.Log;        //Log?????

public class Pnoise extends Activity implements OnClickListener, OnSharedPreferenceChangeListener{
  private static final String TAG = "Pink Noise Wave";

  private TextView tv_NavigateBack;    
  
  private Button pnoise_Button;
  
  private PdService pdService = null;
  
  private Toast toast = null;
  
  private void toast(final String msg) {
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        if (toast == null) {
          toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
        }
        toast.setText(TAG + ": " + msg);
        toast.show();
      }
    });
  }
  
  private final ServiceConnection pdConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      pdService = ((PdService.PdBinder)service).getService();
      initPd();
    }
    
    @Override
    public void onServiceDisconnected(ComponentName name) {
      //This method will never be called
    }
  };
  
  @Override
  protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); // ??Activity??????
    
    AudioParameters.init(this);
    PdPreferences.initPreferences(getApplicationContext());
    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    initGui();
    bindService(new Intent(this, PdService.class), pdConnection, BIND_AUTO_CREATE);
    
  };
  
  @Override
  protected void onDestroy() {
    super.onDestroy();
    cleanup();
  }
  
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (pdService.isRunning()) {
      startAudio();
    }
  }
  
  private void initGui() {
    setContentView(R.layout.pnoise);
    pnoise_Button = (Button) findViewById(R.id.pnoise_button);
    pnoise_Button.setOnClickListener(this);

    tv_NavigateBack = (TextView)findViewById(R.id.NavigateBack);
    tv_NavigateBack.setOnClickListener(new ButtionBackOnClick());  
  }
  
  private void initPd() {
    Resources res = getResources();
    File patchFile = null;
    try {
      PdBase.sendBang("trigger");
//      PdBase.sendFloat("midinote", 64);
//      InputStream in = res.openRawResource(R.raw.pnoise);
//      patchFile = IoUtils.extractResource(in, "pnoise.pd", getCacheDir());
//      PdBase.openPatch(patchFile);
      File dir = getFilesDir();
      IoUtils.extractZipResource(
          getResources().openRawResource(R.raw.pnoise), dir, true);
      File pFile = new File(dir, "pnoise.pd");
      PdBase.openPatch(pFile.getAbsolutePath());
    
    } catch (IOException e) {
      Log.e(TAG, e.toString());
      finish();
    } finally {
      if (patchFile != null) patchFile.delete();
    }
  }

  private void CloseCurrUI(){
    finish();
  }
  
  class ButtionBackOnClick implements OnClickListener{
    @Override
    public void onClick(View v) {
      CloseCurrUI();
    }
  }  
  
  private void startAudio() {
//    String name = getResources().getString(R.string.app_name);
    try {
      pdService.initAudio(-1, -1, -1, -1);
//      pdService.startAudio(new Intent(this, Sine.class), R.drawable.icon, name, "Return to " + name + ".");
      pdService.startAudio();      
    } catch (IOException e) {
      toast(e.toString());
    }
  }
  
  private void stopAudio() {
    pdService.stopAudio();
  }
  
  private void cleanup() {
    try {
      unbindService(pdConnection);
    } catch (IllegalArgumentException e) {
      //already unbound
      pdService = null;
    }
  }
  
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.pnoise_button:
      startAudio();
      PdBase.sendBang("trigger");
//      PdBase.sendFloat("midinote", 64);
      break;
//      if (pdService.isRunning()) {
//        stopAudio();
//      } else {
//        startAudio();
//      }
//    default:
//      stopAudio();
//      break;    
    }
  }

}

/*
public class Pnoise extends Activity implements OnClickListener {
  private static final String TAG = "Pnoise";
  private PdUiDispatcher dispatcher;
  private Button pnoise_Button;    //??Pink Noise??????

  private PdService pdService = null;  
  
  private final ServiceConnection pdConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
      pdService = ((PdService.PdBinder)service).getService();
      try {
        initPd();      //????????????initPd()???
        loadPatch();    //????????????loadPatch()???
      } catch (IOException e) {
        Log.e(TAG, e.toString());
        finish();
      }
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
      // this method will never be called
    }
  };
  
  @Override
  public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    initGui();
  }
  
  @Override
  public void onDestroy(){
    super.onDestroy();
  }
  
  //??????initGui()??
  private void initGui() {
    setContentView(R.layout.pnoise);  //??pnoise?????????????
    pnoise_Button = (Button)findViewById(R.id.pnoise_button);
    //????
    pnoise_Button.setOnClickListener(this);    //??????
    //???????????setOnClickListener()???????????OnClickListener????????
    //???Activity?????????????triggerNote()???????
    //???????????????????????implements OnClickListener??
    //public class Sine extends Activity implements OnClickListener
  }
  
  //??????initPd()??
  private void initPd() throws IOException {
    //Configure the audio glue
    //?????Audio Glue?
    AudioParameters.init(this);
    int sampleRate = AudioParameters.suggestSampleRate();
    //?????????????????
    pdService.initAudio(sampleRate, 1, 2, 10.0f);
    //??PdAudio?????
    
    //Create and install the dispatcher
    //???????dispatcher
    dispatcher = new PdUiDispatcher();
    PdBase.setReceiver(dispatcher);
  }
  
  //??????loadPatch()??
  private void loadPatch() throws IOException {
    File dir = getFilesDir();      //????????
    IoUtils.extractZipResource(
        getResources().openRawResource(R.raw.pnoise),dir,true);
        //?????????
    File patchFile = new File(dir,"pnoise.pd");
    //?????????patch????
    PdBase.openPatch(patchFile.getAbsolutePath());
    //??????????????patch??

    Resources res = getResources();
    File patchFile = null;    
    InputStream in = res.openRawResource(R.raw.pnoise);
    patchFile = IoUtils.extractResource(in, "pnoise.pd", getCacheDir());
    PdBase.openPatch(patchFile);
  }
  
  //??????triggerNote()??
//  private void triggerNote(int n){
//    PdBase.sendFloat("midinote", n);  //libpd???Pd?????MIDI Note????
//    PdBase.sendBang("trigger");      //libpd???Pd?????Bang????
//  }
  
  //?????????????????
  public void onClick(View v){
    switch (v.getId()){
    case R.id.pnoise_button:
      PdBase.sendBang("trigger");
//      PdBase.sendFloat("midinote",64);  //??64?MIDI Note E
//      triggerNote(64);
      break;
    }
  }
}
*/
/*  
@Override
protected void onResume(){
  super.onResume();
  PdAudio.startAudio(this);    //Activity???????????????
}

@Override
protected void onPause(){
  super.onPause();
  PdAudio.stopAudio();      //Activity?????????????????
}
*/  
//????????Activity??????????????patch??????Pd?????
//??????????patch????????????????????????

/*
@Override
public void onDestroy(){
  super.onDestroy();
  PdAudio.release();
  PdBase.release();
}
*/

//???????????????sine.xml?????Sine??????
//?string.xml?????????Sine?????????????
//?????AndroidManifest.xml????????Activity





Java Source Code List

com.github.musetoolkit.About.java
com.github.musetoolkit.CalcActivity.java
com.github.musetoolkit.CopyOfMainActivity.java
com.github.musetoolkit.MainActivity.java
com.github.musetoolkit.Metro.java
com.github.musetoolkit.MoreActivity.java
com.github.musetoolkit.Piano.java
com.github.musetoolkit.Pnoise.java
com.github.musetoolkit.Sawtooth.java
com.github.musetoolkit.SignalActivity.java
com.github.musetoolkit.Sine.java
com.github.musetoolkit.Spl.java
com.github.musetoolkit.Splash.java
com.github.musetoolkit.Sweep.java
com.github.musetoolkit.TestActivity.java
com.github.musetoolkit.Test.java
com.github.musetoolkit.TheoryActivity.java
com.github.musetoolkit.Touch.java
com.github.musetoolkit.Triangle.java
com.github.musetoolkit.Wnoise.java