Advertisement
tameraslan

Jukerock - RhythmLoop

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