Advertisement
Guest User

martin_cork_processing

a guest
Jun 9th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. // library for audio
  4. import ddf.minim.*;
  5. import processing.serial.*;
  6.  
  7. //audio
  8. Minim minim;
  9. AudioPlayer player;
  10.  
  11. Serial myPort;
  12.  
  13. int[] serialInArray = new int[3];
  14. int serialCount = 0;
  15. int rfid, rotary, cardPresent;
  16. boolean firstContact = false;
  17.  
  18. int[] churnArray = {0,0,0,0};
  19. float value;
  20. int churnInt;
  21.  
  22. //the lenght of time between ms and begin time is
  23. //the amount of time between the track being turned down
  24. //and it being ejected
  25. //2nd June: ms = 3000
  26. int begintime = 10;
  27. int window = 0;
  28. int start;
  29.  
  30. PFont font;
  31. //figure out which track to play
  32. boolean playerPlaying = false;
  33. boolean playStory = false;
  34. boolean ejected = false;
  35.  
  36. void setup()
  37. {
  38. size(640,480);
  39. noStroke();
  40. fill(0);
  41.  
  42. loop();
  43. frameRate(1);
  44. background(0);
  45.  
  46. hint(ENABLE_NATIVE_FONTS);
  47. font = createFont("Arial", 18);
  48.  
  49. minim = new Minim(this);
  50.  
  51. println(Serial.list());
  52. String portName = Serial.list()[0];
  53. myPort = new Serial(this, portName, 9600);
  54.  
  55. player = minim.loadFile("sean_1.mp3");
  56. println("player gain is " + player.getGain());
  57.  
  58. }
  59.  
  60. void draw()
  61. {
  62.  
  63. if(window == 0){
  64. background(#FF0000);
  65. }
  66.  
  67. //Get the rotary encoder
  68. churnArray = splice(churnArray, rotary, 0);
  69. churnArray = shorten(churnArray);
  70. //println(churnArray);
  71. //println("draw fnt, player.getGain() "+player.getGain());
  72.  
  73. //if there is no change begin the stop music playing function
  74. if(churnArray[0] == churnArray[3]){
  75. //println("someone has stopped churning");
  76. //minim.stop();
  77. //playerPlaying = false;
  78. //churningStopped();
  79. }
  80. //CHECK IF SOMEONE IS CHURNING
  81. //if there is a change begin to play the music
  82. //if(churnArray[0] != churnArray[1]){
  83. //change to if a card is present and there is a difference in churning
  84. if((churnArray[0] != churnArray[1]) && (cardPresent == 1)){
  85.  
  86. churningStarted();
  87. player.play();
  88. player.setGain(10.0);
  89. /*
  90. //this turns the volume (gain) up gradually - didn't work that well
  91. if(player.getGain() < 2.0){
  92. //println("playerGain on churn " + player.getGain());
  93. player.shiftGain(-10.0, 6.0, 2000);
  94. }
  95. */
  96. playerPlaying = true;
  97. window = 1;
  98. start = millis();
  99. //myPort.write('3');
  100. }
  101.  
  102. if (window == 1) {
  103. background(#FFFFFF);
  104.  
  105. int ms = millis()-start;
  106. println("ms = " + ms);
  107. //myPort.write('3');
  108. //println("writing 3 to port");
  109. int sec = ms/1000;
  110.  
  111. int timer = begintime - sec;
  112.  
  113. //get the current position of the player to see wheter or not to
  114. float currentPos = player.position();
  115. float num = currentPos / 1000;
  116. float posRound = round(num);
  117. println("seconds since start " + posRound);
  118.  
  119. if(posRound == 10){
  120. println("its 10 seconds in");
  121. println("print");
  122. myPort.write('5');
  123. }
  124. if(posRound == 20){
  125. println("its 20 seconds in");
  126. println("print again");
  127. myPort.write('6');
  128. }
  129. if(posRound == 30){
  130. println("tell them the butter has been made");
  131. myPort.write('7');
  132. }
  133. if(posRound == 44){
  134. //player.rewind();
  135. player.mute();
  136. posRound = 0;
  137. //player = minim.loadFile("sean_1.mp3");
  138. //the person has churned their way to the end of the track
  139. println("eject and print");
  140. //stopPlaying();
  141. //minim.stop();
  142. myPort.write('0');
  143. ejected = true;
  144. //myPort.stop();
  145. //setup();
  146. }
  147.  
  148. if(ms > 6000){
  149. println("start to turn down");
  150. println("turn down. player.getGain() is now : " +player.getGain());
  151. if(player.getGain() > 6.0){
  152. player.shiftGain(6.0, -10.0, 1000);
  153. }
  154. //myPort.write('2');
  155. }
  156.  
  157.  
  158. if ( (timer <= 0) || (posRound == 45) ) {
  159. println("Stop the track and eject the token.");
  160. //println("player gain " + player.getGain());
  161. //programming for if someone churns full lenght of track
  162. //if(currentPos < 25000){
  163. stopPlaying();
  164. myPort.write('1');
  165. window = 2;
  166. //}
  167. }
  168. }
  169.  
  170. if((playerPlaying == true) && (churnArray[0] == churnArray[2])){
  171. println("someone has stopped churning as the track is playing");
  172. myPort.write('4');
  173. }
  174.  
  175. if (window == 2) {
  176. //println("change bg");
  177. background(0);
  178. }
  179. }
  180.  
  181.  
  182.  
  183. void keyPressed(){
  184. if (key == 'p') {
  185. churningStarted();
  186.  
  187. player.play();
  188.  
  189. //get the controls available to the player
  190. //println(player.getControls());
  191. //and shift the volume up
  192. //only turn it up if the sound has been off
  193. if(player.getGain() < 6.0){
  194. player.shiftGain(-10.0, 6.0, 5000);
  195. }
  196. //player.setGain(1.0);
  197. //float volumeLevel;
  198. //volumeLevel = player.getVolume();
  199. //println("volumeLevel is " + volumeLevel);
  200. playerPlaying = true;
  201. println("playing from keypress");
  202.  
  203. window = 1;
  204. start = millis();
  205. }
  206.  
  207. if (key == 's') player.pause();
  208.  
  209. if (key == 'e'){
  210. //minim.stop();
  211. //playerPlaying = false;
  212. player.shiftGain(6.0, -10.0, 5000);
  213. //churningStopped();
  214. println("e");
  215. //window = 1;
  216. //start = millis();
  217. }
  218. if (key == 'g'){
  219. println("g");
  220. window = 1;
  221. start = millis();
  222. }
  223. }
  224.  
  225. void churningStarted(){
  226. if(cardPresent == 0){
  227. minim.stop();
  228. }
  229. if(rfid == 0){
  230. //println("rfid == 0, stop minim, set player to false");
  231. minim.stop();
  232. playerPlaying = false;
  233. //no card has been inserted, so if someone churns play some cows mooing
  234. //player = minim.loadFile("cow2.mp3");
  235. }
  236. if(rfid == 1){
  237. //println("rfid card 1");
  238. if(playerPlaying == false){
  239. player = minim.loadFile("sean_nar_1.mp3");
  240. playerPlaying = true;
  241. }
  242. }
  243.  
  244. if(rfid == 2){
  245. //println("rfid == 2");
  246. if(playerPlaying == false){
  247. player = minim.loadFile("sean_nar_2.mp3");
  248. playerPlaying = true;
  249. }
  250. }
  251.  
  252. if(rfid == 3){
  253. //println("rfid == 3");
  254. if(playerPlaying == false){
  255. //player = minim.loadFile("sean_nar_3.mp3");
  256. playerPlaying = true;
  257. }
  258. }
  259.  
  260. if(rfid == 4){
  261. //println("rfid == 4");
  262. if(playerPlaying == false){
  263. player = minim.loadFile("sean_nar_4.mp3");
  264. playerPlaying = true;
  265. }
  266. }
  267.  
  268. if(rfid == 5){
  269. //println("rfid == 5");
  270. if(playerPlaying == false){
  271. player = minim.loadFile("sean_nar_5.mp3");
  272. playerPlaying = true;
  273. }
  274. }
  275.  
  276. if(rfid == 6){
  277. //println("rfid == 6");
  278. if(playerPlaying == false){
  279. player = minim.loadFile("sean_nar_6.mp3");
  280. playerPlaying = true;
  281. }
  282. }
  283.  
  284. if(rfid == 7){
  285. //println("rfid == 7");
  286. if(playerPlaying == false){
  287. player = minim.loadFile("story_7.mp3");
  288. playerPlaying = true;
  289. }
  290. }
  291. }
  292.  
  293. //inside in this loop, we'll first begin to turn the sound down
  294. //over the course of 5 seconds. If noone has churned since then
  295. //then stop the track altogether
  296. void churningStopped(){
  297.  
  298. println("turn sound down and get ready to stop");
  299. player.shiftGain(6.0, -10.0, 5000);
  300. //stopPlaying();
  301. }
  302.  
  303.  
  304. void stopPlaying(){
  305. //now stop the track from playing
  306. println("stop minim object");
  307. minim.stop();
  308. playerPlaying = false;
  309. //myPort.write('0');
  310. //myPort.write('2');
  311. }
  312.  
  313. void serialEvent(Serial myPort) {
  314. // read a byte from the serial port:
  315. int inByte = myPort.read();
  316. // if this is the first byte received, and it's an A,
  317. // clear the serial buffer and note that you've
  318. // had first contact from the microcontroller.
  319. // Otherwise, add the incoming byte to the array:
  320. if (firstContact == false) {
  321. if (inByte == 'A') {
  322. myPort.clear(); // clear the serial port buffer
  323. firstContact = true; // you've had first contact from the microcontroller
  324. myPort.write('A'); // ask for more
  325. }
  326. }
  327. else {
  328. // Add the latest byte from the serial port to array:
  329. serialInArray[serialCount] = inByte;
  330. serialCount++;
  331.  
  332. // If we have 3 bytes:
  333. if (serialCount > 2 ) {
  334. //println("rfid track is " + rfid);
  335. rfid = serialInArray[0];
  336. rotary = serialInArray[1];
  337. cardPresent = serialInArray[2];
  338. //println(serialInArray);
  339. // print the values (for debugging purposes only):
  340. //println(rfid + "\t" + rotary + "\t");
  341.  
  342. // Send a capital A to request new sensor readings:
  343. myPort.write('A');
  344. // Reset serialCount:
  345. serialCount = 0;
  346. }
  347. }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement