/*
  iphonesound.pde
  
  simple example to show how an iphone with oscemote
  can act as an interface to a processing sketch
  
  this example changes the freq of a sine wave based on
  the iphone's accelerometer
  
  rob@rahji.com 
  http://www.robduarte.com 
  http://creativecommons.org/licenses/by/3.0/us/
*/

import ddf.minim.*;
import ddf.minim.signals.*;

import oscP5.*;
import netP5.*;

Minim minim;
AudioOutput out;
SineWave sine;

OscP5 oscP5;

void setup()
{
  minim = new Minim(this);
  out = minim.getLineOut(Minim.STEREO);
  // create a sine wave Oscillator, set to 440 Hz, at 1.0 amplitude, sample rate from line out
  sine = new SineWave(440, 1, out.sampleRate());
  sine.portamento(200);
  out.addSignal(sine);
  oscP5 = new OscP5(this,8000); // start listening on port 8000 for incoming osc messages
  // call freqchange() in response to an incoming accelerometer osc message
  oscP5.plug(this,"freqchange","/acceleration/xyz","fff"); 
}

public void freqchange(float x, float y, float z) {
  float freq = map(x, -3.0, 3.0, 60, 1500);
  sine.setFreq(freq);
}

void draw() {
}

// shut down the audio properly when the stop button is pressed
void stop() 
{
  out.close();
  minim.stop();
  super.stop();
}