Advertisement
tameraslan

JukeRock - WallPiano

Dec 19th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. /*
  2. Wall_Piano Processing sketch
  3. The sketch loads in the input file matrix from a text file and loads the sound accordingly. then it creates a matrix of buttons to simulate a climbing wall. Each button/grip is associated with a sound, to mimic a 2D piano. Horizontal levels are mapped to the same pitch, vertical levels correspond to increasing pitch
  4. */
  5.  
  6.  
  7.  
  8. import ddf.minim.*;
  9. import processing.video.*;
  10. MovieMaker mm;
  11. AudioSample S;
  12. Minim minim;
  13. int counter = 0;
  14. int c=0;
  15. int life = 60;
  16. // int beat = 100;
  17. // float step = beat/7;
  18. int unit = 60;
  19. int wWidth = unit*15;
  20. int wHeight = unit*12;
  21. String note;
  22. boolean SoundOn = true;
  23. boolean makeMovie;
  24.  
  25. //Location arrays:
  26. Hold [][] HoldArray = new Hold [wWidth][wHeight];
  27. Coord [] Buffer = new Coord [32];
  28. Coord topLeft, topRight, bottomLeft, bottomRight;
  29. String[] noteMatrix;
  30. String[] row;
  31.  
  32. void setup () {
  33. size(wWidth, wHeight);
  34.  
  35. frameRate(10);
  36.  
  37. if(makeMovie)
  38. mm = new MovieMaker(this, width, height, "tone_wall_vertical.mov", 10, MovieMaker.ANIMATION, MovieMaker.HIGH);
  39. background(255);
  40. noteMatrix = loadStrings("vertical_mp3.txt");
  41.  
  42.  
  43. // PImage r = loadImage("red.png");
  44. // PImage y = loadImage("yellow.png");
  45. // PImage yr= loadImage("yellow_roll.png");
  46.  
  47. PImage pressed = loadImage("pressed_60.png");
  48. PImage gray = loadImage("gray_60.png");
  49. //PImage yr= loadImage("gray_60.png");
  50.  
  51. int w = gray.width;
  52. int h = gray.height;
  53. minim = new Minim(this);
  54. for (int j=0; j<wHeight/unit;j++)
  55. {
  56. row = split(noteMatrix[j], '\t');
  57. for (int i=0; i<wWidth/unit;i++)
  58. {
  59.  
  60. note = row[i];
  61. HoldArray[i][j] = new Hold(i*unit,j*unit,i,j,w,h,gray,pressed,note);
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  
  71. void draw (){
  72.  
  73.  
  74.  
  75. for (int i=0; i<wWidth/unit;i++)
  76. {
  77. for (int j=0;j<wHeight/unit;j++)
  78. {
  79.  
  80. //background(255);
  81. HoldArray[i][j].update(); // the function which determines whether the mouse is on the hold or not, and loads the according image
  82. HoldArray[i][j].display(); // then the loaded image is displayed on screen
  83.  
  84.  
  85. }
  86. }
  87. fill(182,37,227,100);
  88. if(c==3)
  89. {
  90. triangle(Buffer[0].x+unit/2,Buffer[0].y+unit/2,Buffer[1].x+unit/2,Buffer[1].y+unit/2,Buffer[2].x+unit/2,Buffer[2].y+unit/2);
  91. }
  92. else if (c==4)
  93. {
  94. // make quad here
  95. int indBot1=0, indBot2=0, indTop1=0, indTop2=0;
  96. int maxY;
  97.  
  98. maxY=0;
  99. for(int k=0;k<4;k++)
  100. {
  101. if(maxY<Buffer[k].y)
  102. {
  103. maxY=Buffer[k].y;
  104. indBot1=k;
  105. }
  106. }
  107. maxY=0;
  108. for(int k=0;k<4;k++)
  109. {
  110. if(maxY<Buffer[k].y && k !=indBot1)
  111. {
  112. maxY=Buffer[k].y;
  113. indBot2=k;
  114. }
  115. }
  116. for(int k=0;k<4;k++)
  117. {
  118. if(k!=indBot1 && k!=indBot2)
  119. indTop1=k;
  120. }
  121. for(int k=0;k<4;k++)
  122. {
  123. if(k!=indBot1 && k!=indBot2 && k!=indTop1)
  124. indTop2=k;
  125. }
  126. if (Buffer[indBot1].x < Buffer[indBot2].x)
  127. {
  128. bottomLeft = Buffer[indBot1];
  129. bottomRight= Buffer[indBot2];
  130. }
  131. else
  132. {
  133. bottomLeft = Buffer[indBot2];
  134. bottomRight= Buffer[indBot1];
  135. }
  136. if (Buffer[indTop1].x < Buffer[indTop2].x)
  137. {
  138. topLeft = Buffer[indTop1];
  139. topRight= Buffer[indTop2];
  140. }
  141. else
  142. {
  143. topLeft = Buffer[indTop2];
  144. topRight= Buffer[indTop1];
  145. }
  146.  
  147. quad(topLeft.x+unit/2,topLeft.y+unit/2,topRight.x+unit/2,topRight.y+unit/2,bottomRight.x+unit/2,bottomRight.y+unit/2,bottomLeft.x+unit/2,bottomLeft.y+unit/2);
  148.  
  149.  
  150.  
  151. }
  152. if(makeMovie)
  153.  
  154. mm.addFrame();
  155.  
  156. }
  157.  
  158.  
  159.  
  160. void stop() {
  161. // always close Minim audio classes when you are done with them
  162. //S.close();
  163. if(makeMovie)
  164. mm.finish();
  165. minim.stop();
  166. super.stop();
  167.  
  168. }
  169.  
  170.  
  171.  
  172.  
  173. class Coord
  174. {
  175. int x;
  176. int y;
  177. Coord (int ii, int ij) {
  178. x = ii;
  179. y = ij;
  180. }
  181. }
  182.  
  183.  
  184.  
  185. boolean mReleased = false;
  186.  
  187. void mouseReleased()
  188. {
  189. mReleased = true;
  190. }
  191.  
  192.  
  193. class Hold
  194. {
  195.  
  196. PImage base; // image on screen
  197.  
  198. PImage down; // image when clicked
  199. PImage currentimage;
  200. int x, y; // coordinates of the holds
  201. int i,j; // place in the array, like HoldArray [i][j]
  202. int w, h; // width and height of the image
  203. int age = 0; // "age" of the hold. when clicked, it's born, and after 4 seconds, its "born" again, creating a sound
  204. String note; //name of the tone of each hold
  205. float [] rgb = new float [3];
  206. //AudioSample sound;
  207.  
  208. boolean played = false;
  209. boolean over = false; // bool if mouse over
  210. boolean pressed = false; // bool if mouse clicked
  211. boolean activated = false; // bool if sound of the hold is activated
  212.  
  213. Hold(int ix, int iy, int ii, int ij, int iw, int ih, PImage ibase, PImage idown, String iNote)
  214. {
  215. x = ix;
  216. y = iy;
  217. i=ii;
  218. j=ij;
  219. w = iw;
  220. h = ih;
  221. base = ibase;
  222.  
  223. down = idown;
  224. currentimage = base;
  225. note = iNote;
  226. for (int c=0;c<2;c++)
  227. rgb[c] = int(random(0,255));
  228. }
  229.  
  230. void pressed()
  231. {
  232.  
  233. if(over && mReleased && !activated)
  234. {
  235. pressed = !pressed;
  236. activated = !activated;
  237. if(SoundOn)
  238. {
  239.  
  240. S = minim.loadSample(note, 2048);
  241. S.trigger();
  242. }
  243. Buffer[c]= new Coord(x,y);
  244. c++;
  245. age = 0;
  246. mReleased = false;
  247. }
  248. else if (over && mReleased && activated)
  249. {
  250. pressed = !pressed;
  251. activated = !activated;
  252. mReleased = false;
  253. played = false;
  254. age=0;
  255. if(c==4)
  256. for(int t=0;t<4;t++)
  257. {
  258. if (Buffer[t].x==x && Buffer[t].y == y)
  259. {
  260. int v=t;
  261. while((v+1)!=4)
  262. {
  263. Buffer[v]=Buffer[v+1];
  264. v++;
  265.  
  266. }
  267. }
  268. }
  269. c--;
  270. }
  271. }
  272.  
  273. void over()
  274. {
  275. if (mouseX >= x && mouseX <= x+w &&
  276. mouseY >= y && mouseY <= y+h)
  277. {
  278. over = true;
  279. }
  280. else
  281. {
  282. over = false;
  283. }
  284. }
  285.  
  286. void update()
  287. {
  288. over();
  289. pressed();
  290. if(pressed)
  291. {
  292. currentimage = down;
  293. }
  294.  
  295. else
  296. {
  297. currentimage = base;
  298. }
  299. }
  300.  
  301.  
  302.  
  303. void display()
  304. {
  305.  
  306.  
  307. fill(255,255,255);
  308. noStroke();
  309. rect(x,y,unit,unit);
  310. image(currentimage, x, y);
  311.  
  312. if (!played && activated)
  313. age=age+3;
  314.  
  315. if (age == life)
  316. {
  317.  
  318. age=0;
  319. played=true;
  320.  
  321. }
  322.  
  323.  
  324. fill(rgb[0],rgb[1],rgb[2],100);
  325.  
  326. ellipse(x+(w/2),y+(h/2),int((float(age)/float(life))*float(w)),int((float(age)/float(life))*float(w)));
  327. }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement