Monday 11 June 2012

Singing bowl code..


//I'm using Eclipse with Android for Processing

package processing.test.bowl;

import processing.core.*; 

import edu.uic.ketai.inputService.*; 

import apwidgets.*; 


public class Bowl extends PApplet {


PMediaPlayer player;
KetaiSensorManager sensorManager;
PFont budd;
PImage stick, bowl;
int value = 0;
int cx = 250;
int cy = 250;
int r = 100;//radius of circular movement


public void setup()
{

  player = new PMediaPlayer(this); //create new PMediaPlayer
  player.setMediaFile("bowlHit.mp3"); //set the file (files are in data folder)
  budd = loadFont("OlympusBold-48.vlw");
  stick = loadImage("stick.png");
  bowl= loadImage("bowl.png");
  textFont(budd, 28);
  smooth();
}

public void draw()
{
 imageMode(CORNER);
  image(bowl, 0, 0, width, height);
  float angle = atan2(mouseY-cy, mouseX-cx);

  float x = cos(angle) * 220 + cx; //amount and width of rotation
  float y = sin(angle) * 220 + cy;
  imageMode(CENTER);
  fill(0);
  image(stick, x+120, y, stick.width/4, stick.height/4);
  fill(0, 255, 0);
  text("press & move the stick to play bowl", 40, height-100);
}



public void mousePressed() { 

  player.start(); //start play back

  player.setVolume(1.0f, 1.0f); //Set left
}


  public int sketchWidth() { return 500; }
  public int sketchHeight() { return 500; }
}

Tibetan Singing Bowl Android Tablet App

Tibetan Singing Bowl Android Tablet App

Tibetan Singing Bowl App







Applet version online here (requires java plugin to work):
http://www.doc.gold.ac.uk/~ma501ed/singingBowl/index.html
will now make an Android version

Tuesday 5 June 2012

new Android tablet

Microscopic anatomy images on my new Android tablet
My new Android tablet has been fun to program with Processing for Android, once I'd set it up to run via Eclipse it has been straightforward to create the APK files and transfer them to the tablet. There's a good sound hack with apwidgets, solved a lot of problems I'd been having with sound files:

import edu.uic.ketai.inputService.*;
import android.content.pm.ActivityInfo;
import apwidgets.*;

PMediaPlayer player;
KetaiSensorManager sensorManager;
PVector  accelerometer;

int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.5;
Ball[] balls = new Ball[numBalls];

void setup()
{
  size(screenWidth, screenHeight);
  orientation(PORTRAIT);
  player = new PMediaPlayer(this); //create new PMediaPlayer
  player.setMediaFile("groupe2_osj.mp3"); //set the file (files are in data folder)

  sensorManager = new KetaiSensorManager(this);
  sensorManager.start();
  smooth();
  noStroke();
  accelerometer = new PVector();

  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
  }
}

void draw()
{
  background(0);
  //
  //   text("Accelerometer data:" 
  //  + nf(accelerometer.x, 2, 2) + "/" 
  //  + nf(accelerometer.y, 2, 2) + "/" 
  //  + nf(accelerometer.z, 2, 2), 5, 20);

  for (int i = 0; i < numBalls; i++) {
    balls[i].collide();
    balls[i].move();
    balls[i].display();
  } 
  if (balls[1].x==width) {

    player.start(); //start play back
    player.start(); //start play back
    player.setLooping(true); //restart playback end reached
    player.setVolume(1.0, 1.0); //Set left and right volumes. Range is from 0.0 to 1.0
  }
}


void onAccelerometerSensorEvent(long time, int accuracy, float x, float y, float z)
{
  accelerometer.set(x, y, z);
}

public void mousePressed() { 
  if (sensorManager.isStarted())
    sensorManager.stop(); 
  else
    sensorManager.start(); 
  println("SensorManager isStarted: " + sensorManager.isStarted());
}


class Ball {
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;

  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  } 

  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }
  }

  void move() {

    float Acc_gravityX = map(accelerometer.x, -10, 10, 0.5, -0.5);
    float Acc_gravityY = map(accelerometer.y, -10, 10, -0.5, 0.5);

    float gravityX = Acc_gravityX;
    float gravityY = Acc_gravityY;

    vy += gravityY;
    vx += gravityX;
    x += vx;
    y += vy;

    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction;
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction;
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
    //println(nf(x, 2, 2)+"   "+nf(y, 2, 2)+"  "+nf(gravityX, 2, 2)+"  "+nf(gravityY, 2, 2)+"  __"+nf(accelerometer.x, 2, 2)+"  ___"+nf(accelerometer.y, 2, 2));
  }

  void display() {
    fill(255, 204);
    ellipse(x, y, diameter, diameter);
  }
}