Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- // library for audio
- import ddf.minim.*;
- import processing.serial.*;
- //audio
- Minim minim;
- AudioPlayer player;
- Serial myPort;
- int[] serialInArray = new int[3];
- int serialCount = 0;
- int rfid, rotary, cardPresent;
- boolean firstContact = false;
- int[] churnArray = {0,0,0,0};
- float value;
- int churnInt;
- //the lenght of time between ms and begin time is
- //the amount of time between the track being turned down
- //and it being ejected
- //2nd June: ms = 3000
- int begintime = 10;
- int window = 0;
- int start;
- PFont font;
- //figure out which track to play
- boolean playerPlaying = false;
- boolean playStory = false;
- boolean ejected = false;
- void setup()
- {
- size(640,480);
- noStroke();
- fill(0);
- loop();
- frameRate(1);
- background(0);
- hint(ENABLE_NATIVE_FONTS);
- font = createFont("Arial", 18);
- minim = new Minim(this);
- println(Serial.list());
- String portName = Serial.list()[0];
- myPort = new Serial(this, portName, 9600);
- player = minim.loadFile("sean_1.mp3");
- println("player gain is " + player.getGain());
- }
- void draw()
- {
- if(window == 0){
- background(#FF0000);
- }
- //Get the rotary encoder
- churnArray = splice(churnArray, rotary, 0);
- churnArray = shorten(churnArray);
- //println(churnArray);
- //println("draw fnt, player.getGain() "+player.getGain());
- //if there is no change begin the stop music playing function
- if(churnArray[0] == churnArray[3]){
- //println("someone has stopped churning");
- //minim.stop();
- //playerPlaying = false;
- //churningStopped();
- }
- //CHECK IF SOMEONE IS CHURNING
- //if there is a change begin to play the music
- //if(churnArray[0] != churnArray[1]){
- //change to if a card is present and there is a difference in churning
- if((churnArray[0] != churnArray[1]) && (cardPresent == 1)){
- churningStarted();
- player.play();
- player.setGain(10.0);
- /*
- //this turns the volume (gain) up gradually - didn't work that well
- if(player.getGain() < 2.0){
- //println("playerGain on churn " + player.getGain());
- player.shiftGain(-10.0, 6.0, 2000);
- }
- */
- playerPlaying = true;
- window = 1;
- start = millis();
- //myPort.write('3');
- }
- if (window == 1) {
- background(#FFFFFF);
- int ms = millis()-start;
- println("ms = " + ms);
- //myPort.write('3');
- //println("writing 3 to port");
- int sec = ms/1000;
- int timer = begintime - sec;
- //get the current position of the player to see wheter or not to
- float currentPos = player.position();
- float num = currentPos / 1000;
- float posRound = round(num);
- println("seconds since start " + posRound);
- if(posRound == 10){
- println("its 10 seconds in");
- println("print");
- myPort.write('5');
- }
- if(posRound == 20){
- println("its 20 seconds in");
- println("print again");
- myPort.write('6');
- }
- if(posRound == 30){
- println("tell them the butter has been made");
- myPort.write('7');
- }
- if(posRound == 44){
- //player.rewind();
- player.mute();
- posRound = 0;
- //player = minim.loadFile("sean_1.mp3");
- //the person has churned their way to the end of the track
- println("eject and print");
- //stopPlaying();
- //minim.stop();
- myPort.write('0');
- ejected = true;
- //myPort.stop();
- //setup();
- }
- if(ms > 6000){
- println("start to turn down");
- println("turn down. player.getGain() is now : " +player.getGain());
- if(player.getGain() > 6.0){
- player.shiftGain(6.0, -10.0, 1000);
- }
- //myPort.write('2');
- }
- if ( (timer <= 0) || (posRound == 45) ) {
- println("Stop the track and eject the token.");
- //println("player gain " + player.getGain());
- //programming for if someone churns full lenght of track
- //if(currentPos < 25000){
- stopPlaying();
- myPort.write('1');
- window = 2;
- //}
- }
- }
- if((playerPlaying == true) && (churnArray[0] == churnArray[2])){
- println("someone has stopped churning as the track is playing");
- myPort.write('4');
- }
- if (window == 2) {
- //println("change bg");
- background(0);
- }
- }
- void keyPressed(){
- if (key == 'p') {
- churningStarted();
- player.play();
- //get the controls available to the player
- //println(player.getControls());
- //and shift the volume up
- //only turn it up if the sound has been off
- if(player.getGain() < 6.0){
- player.shiftGain(-10.0, 6.0, 5000);
- }
- //player.setGain(1.0);
- //float volumeLevel;
- //volumeLevel = player.getVolume();
- //println("volumeLevel is " + volumeLevel);
- playerPlaying = true;
- println("playing from keypress");
- window = 1;
- start = millis();
- }
- if (key == 's') player.pause();
- if (key == 'e'){
- //minim.stop();
- //playerPlaying = false;
- player.shiftGain(6.0, -10.0, 5000);
- //churningStopped();
- println("e");
- //window = 1;
- //start = millis();
- }
- if (key == 'g'){
- println("g");
- window = 1;
- start = millis();
- }
- }
- void churningStarted(){
- if(cardPresent == 0){
- minim.stop();
- }
- if(rfid == 0){
- //println("rfid == 0, stop minim, set player to false");
- minim.stop();
- playerPlaying = false;
- //no card has been inserted, so if someone churns play some cows mooing
- //player = minim.loadFile("cow2.mp3");
- }
- if(rfid == 1){
- //println("rfid card 1");
- if(playerPlaying == false){
- player = minim.loadFile("sean_nar_1.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 2){
- //println("rfid == 2");
- if(playerPlaying == false){
- player = minim.loadFile("sean_nar_2.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 3){
- //println("rfid == 3");
- if(playerPlaying == false){
- //player = minim.loadFile("sean_nar_3.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 4){
- //println("rfid == 4");
- if(playerPlaying == false){
- player = minim.loadFile("sean_nar_4.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 5){
- //println("rfid == 5");
- if(playerPlaying == false){
- player = minim.loadFile("sean_nar_5.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 6){
- //println("rfid == 6");
- if(playerPlaying == false){
- player = minim.loadFile("sean_nar_6.mp3");
- playerPlaying = true;
- }
- }
- if(rfid == 7){
- //println("rfid == 7");
- if(playerPlaying == false){
- player = minim.loadFile("story_7.mp3");
- playerPlaying = true;
- }
- }
- }
- //inside in this loop, we'll first begin to turn the sound down
- //over the course of 5 seconds. If noone has churned since then
- //then stop the track altogether
- void churningStopped(){
- println("turn sound down and get ready to stop");
- player.shiftGain(6.0, -10.0, 5000);
- //stopPlaying();
- }
- void stopPlaying(){
- //now stop the track from playing
- println("stop minim object");
- minim.stop();
- playerPlaying = false;
- //myPort.write('0');
- //myPort.write('2');
- }
- void serialEvent(Serial myPort) {
- // read a byte from the serial port:
- int inByte = myPort.read();
- // if this is the first byte received, and it's an A,
- // clear the serial buffer and note that you've
- // had first contact from the microcontroller.
- // Otherwise, add the incoming byte to the array:
- if (firstContact == false) {
- if (inByte == 'A') {
- myPort.clear(); // clear the serial port buffer
- firstContact = true; // you've had first contact from the microcontroller
- myPort.write('A'); // ask for more
- }
- }
- else {
- // Add the latest byte from the serial port to array:
- serialInArray[serialCount] = inByte;
- serialCount++;
- // If we have 3 bytes:
- if (serialCount > 2 ) {
- //println("rfid track is " + rfid);
- rfid = serialInArray[0];
- rotary = serialInArray[1];
- cardPresent = serialInArray[2];
- //println(serialInArray);
- // print the values (for debugging purposes only):
- //println(rfid + "\t" + rotary + "\t");
- // Send a capital A to request new sensor readings:
- myPort.write('A');
- // Reset serialCount:
- serialCount = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement