Audio Rotator

// Program created by Pete Rogers www.pete-rogers.co.uk
// Takes 6 audio inputs and reads ints from serial port from a rotary encoder and
// plays sounds depending on position of rotation
// ESS by Krister Olsson  

import krister.Ess.*;
import processing.serial.*;

Serial port;
String buf="";
String portname = "COM1";  // find the name of your serial port in your system setup!
int baudrate = 9600;  //  set baudrate here
int serialVar;
int value;
int rot = 0;
AudioChannel myChannel1, myChannel2, myChannel3, myChannel4, myChannel5, myChannel6;

void setup() {
  size(256,200);
  
  port = new Serial(this, Serial.list()[ 1], 9600);

   println(Serial.list());
   Ess.start(this);

  myChannel1=new AudioChannel("sound1.aif");
  myChannel2=new AudioChannel("sound2.aif");
  myChannel3=new AudioChannel("sound3.aif");
  myChannel4=new AudioChannel("sound4.aif");
  myChannel5=new AudioChannel("sound5.aif");
  myChannel6=new AudioChannel("sound6.aif");
  initSounds();
}

void draw() {
   if(port.available() > 0){
      //println("fefef");
        value = port.read();
        serialEvent(value);
    }
}

void serialEvent(int serial){

    if(serial!=10) {        
          buf += char(serial);          
          } else {
          //extract the value from the string 'buf'
          buf = buf.substring(1,buf.length());
          //cast the value to an integer
          serialVar = int(buf);
          buf="";
          int s = serialVar * 360/ 4120;
          checkRotation (s);
      }
     }


void initSounds(){
    myChannel1.play(Ess.FOREVER);
    myChannel2.play(Ess.FOREVER);
    myChannel3.play(Ess.FOREVER);
    myChannel4.play(Ess.FOREVER);
    myChannel5.play(Ess.FOREVER);
    myChannel6.play(Ess.FOREVER);
    myChannel1.volume(0);
    myChannel2.volume(0);
    myChannel3.volume(0);
    myChannel4.volume(0);
    myChannel5.volume(0);
    myChannel6.volume(0);
    
}

// clean up Ess before exiting

public void stop() {
  myChannel1.pause();
}

public void checkRotation(int num){
    float i = num / 60f;
   
    println(i);
    if (i > 0 && i < 1){
      myChannel1.volume(i);
      myChannel6.volume(1-i);
    }
    
    if (i > 1 && i < 2){
      myChannel1.volume(2 - i);
    }
    
    if (i < 0 && 0 > 2){
      myChannel1.volume(0);
   
}
//
i = i - 1;
  
    if (i > 0 && i < 1){
      myChannel2.volume(i);
    }
    
    if (i > 1 && i < 2){
      myChannel2.volume(2 - i);
    }
    
    if (i < 0 && 0 > 2){
      myChannel2.volume(0);

}
//
i = i - 1;
   
    if (i > 0 && i < 1){
      myChannel3.volume(i);
    }
    
    if (i > 1 && i < 2){
      myChannel3.volume(2 - i);
    }
    
    if (i < 0 && 0 > 2){
      myChannel3.volume(0);
   
}
//
i = i - 1;
   
    if (i > 0 && i < 1){
      myChannel4.volume(i);
      
    }
    
    if (i > 1 && i < 2){
      myChannel4.volume(2 - i);
    }
    
    if (i < 0 && 0 > 2){
      myChannel4.volume(0);
    
}
//
i = i - 1;

    if (i > 0 && i < 1){
      myChannel5.volume(i);
    }
    
    if (i > 1 && i < 2){
      myChannel5.volume(2 - i);
    }
    
    if (i < 0 && 0 > 2){
      myChannel5.volume(0);  
}

//
i = i - 1;

    if (i > 0 && i < 1){
      myChannel6.volume(i);
      myChannel1.volume(1-i);
    }
    

    
    if (i < 0 && 0 > 2){
      myChannel6.volume(0);  
}
 
 
}