Android Open Source - dynamic-sound-android Pd Interface






From Project

Back to project page dynamic-sound-android.

License

The source code is released under:

MIT License

If you think the Android project dynamic-sound-android 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.tackmobile.spacerocks.audio;
//from  w ww  . j av a  2s .  com
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.puredata.android.io.AudioParameters;
import org.puredata.android.io.PdAudio;
import org.puredata.core.PdBase;
import org.puredata.core.utils.IoUtils;

import com.tackmobile.spacerocks.R;

import android.content.Context;

public class PdInterface {
  private static final int MIN_SAMPLE_RATE  = 44100;
  private static final String THRUSTER    = "thruster";
  private static final String MISSILE     = "missile";
  private static final String ASTEROID     = "asteroid";
  private static final String SHIP       = "ship";

  private static PdInterface ourInstance = new PdInterface();
  private Context context;

  public static PdInterface getInstance() {
    return ourInstance;
  }

  private PdInterface() { }

  public void initialize(Context context) {
    this.context = context;
    try {
      initializePd();
    } catch (IOException e) {
      // Unable to initialize audio. Ignore silently as this is not fatal to the app
      e.printStackTrace();
    }
  }

  public void destroy() {
    cleanup();
  }
  
  public void fire() {
    PdBase.sendList(MISSILE, 300, 400);
  }
  
  public void shipHit() {
    PdBase.sendList(SHIP, 1000, 50);
  }

  public void asteroidHit() {
    PdBase.sendList(ASTEROID, 300, 30);
  }

  public void thrusterOn() {
    PdBase.sendFloat(THRUSTER, 1);
  }
  
  public void thrusterOff() {
    PdBase.sendFloat(THRUSTER, 0);
  }

  private void initializePd() throws IOException {
    AudioParameters.init(context);
    int sampleRate = Math.max(MIN_SAMPLE_RATE, AudioParameters.suggestSampleRate());
    int outChannels = AudioParameters.suggestOutputChannels();
    PdAudio.initAudio(sampleRate, 0, outChannels, 1, true);
    File dir = context.getFilesDir();
    File patchFile = new File(dir, "game_sounds.pd");
    InputStream patchStream = context.getResources().openRawResource(R.raw.patches);
    IoUtils.extractZipResource(patchStream, dir, true);

    PdBase.openPatch(patchFile.getAbsolutePath());
  }

  private void cleanup() {
    PdAudio.release();
    PdBase.release();
  }

}




Java Source Code List

com.tackmobile.androidpiano.MainActivity.java
com.tackmobile.androidpiano.audio.PdInterface.java
com.tackmobile.androidpiano.view.Key.java
com.tackmobile.androidpiano.view.KeysView.java
com.tackmobile.spacerocks.GameBoard.java
com.tackmobile.spacerocks.MainActivity.java
com.tackmobile.spacerocks.audio.PdInterface.java
com.tackmobile.spacerocks.models.Asteroid.java
com.tackmobile.spacerocks.models.Missile.java